Skip to content

Commit

Permalink
Do not assume that the ledger is open for the full duration of each m…
Browse files Browse the repository at this point in the history
…ethod

  call.
  • Loading branch information
Hexagon committed Jun 7, 2024
1 parent 4aeb442 commit 86b5b25
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Unrelesed

## 0.15.10

- Do not assume that the ledger is open for the full duration of each method
call.

## 0.15.9

- Fix `isOpen´ to return false while closing the database
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.15.9",
"version": "0.15.10",
"exports": {
".": "./mod.ts",
"./cli": "./src/cli/mod.ts"
Expand Down
28 changes: 19 additions & 9 deletions src/lib/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,14 @@ export class KV extends EventEmitter {
fetchData: boolean = true,
): AsyncGenerator<KVTransactionResult<T>> {
this.ensureOpen();
for await (const result of this.ledger!.scan(query, recursive, fetchData)) {
if (result?.transaction) { // Null check to ensure safety
const processedResult = result.transaction.asResult<T>();
yield processedResult;
if (this.ledger) {
for await (
const result of this.ledger?.scan(query, recursive, fetchData)
) {
if (result?.transaction) { // Null check to ensure safety
const processedResult = result.transaction.asResult<T>();
yield processedResult;
}
}
}
}
Expand Down Expand Up @@ -664,7 +668,7 @@ export class KV extends EventEmitter {
currentOffset += transactionData.length;
}

await this.ledger!.lock();
await this.ledger?.lock();
let unlocked = false;
try {
// Sync before writing the transactions
Expand All @@ -674,10 +678,16 @@ export class KV extends EventEmitter {
}

// Write all buffered transactions at once and get the base offset
const baseOffset = await this.ledger!.add(bufferedTransactions);
const baseOffset = await this.ledger?.add(bufferedTransactions);

if (baseOffset === undefined) {
throw new Error(
"Database closed during transaction, data could possibly be lost.",
);
}

// Unlock early if everying successed
await this.ledger!.unlock();
await this.ledger?.unlock();
unlocked = true;

// Update the index and check for errors
Expand All @@ -687,7 +697,7 @@ export class KV extends EventEmitter {
) {
try {
// Add to ledger cache
this.ledger!.cache.cacheTransactionData(
this.ledger?.cache.cacheTransactionData(
baseOffset + relativeOffset,
{
offset: baseOffset + relativeOffset,
Expand All @@ -710,7 +720,7 @@ export class KV extends EventEmitter {
}
} finally {
// Back-up unlock
if (!unlocked) await this.ledger!.unlock();
if (!unlocked) await this.ledger?.unlock();
this.pendingTransactions = []; // Clear pending transactions
this.isInTransaction = false;
}
Expand Down

0 comments on commit 86b5b25

Please sign in to comment.