Skip to content

Commit

Permalink
More graceful close
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 22, 2024
1 parent 93821c5 commit 49d16ae
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.2

- Be more graceful when closing

## 0.13.1

- Allow unlocking the database while closing it.
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.13.1",
"version": "0.13.2",
"exports": {
".": "./mod.ts"
},
Expand Down
17 changes: 1 addition & 16 deletions src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ export class KV extends EventEmitter {

// If there is an existing ledger, close it and clear the index
if (this.ledger) {
this.ledger?.close();
this.index.clear();
}

Expand Down Expand Up @@ -246,14 +245,6 @@ export class KV extends EventEmitter {
// Throw if database isn't open
if (force) this.ensureOpen();

// Ensure ledger is open and instance is not closed
if (this.ledger?.isClosing() || this.aborted) {
const error = new Error(
this.aborted ? "Store is closed" : "Ledger is not open",
);
return { result: "noop", error }; // Emit noop since no sync was performed
}

if (this.blockSync && !force) {
const error = new Error("Store synchronization is blocked");
// @ts-ignore .emit exists
Expand Down Expand Up @@ -390,11 +381,7 @@ export class KV extends EventEmitter {
* @returns True if open, false if closed.
*/
public isOpen(): boolean {
if (!this.ledger || this.ledger.isClosing() || this.aborted) {
return false;
} else {
return true;
}
return !!this.ledger && this.ledger.isOpen();
}

/**
Expand Down Expand Up @@ -714,7 +701,5 @@ export class KV extends EventEmitter {

// @ts-ignore emit exists
this.emit("closing");

this.ledger?.close();
}
}
17 changes: 7 additions & 10 deletions src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface KVLedgerResult {
}

export class KVLedger {
private aborted: boolean = false;
private opened: boolean = false;
private dataPath: string;
public header: KVLedgerHeader = {
fileId: LEDGER_FILE_ID,
Expand Down Expand Up @@ -85,10 +85,8 @@ export class KVLedger {
} else {
throw new Error("Database not found.");
}
}

close() {
this.aborted = true;
this.opened = true;
}

/**
Expand Down Expand Up @@ -400,7 +398,6 @@ export class KVLedger {
await tempLedger.add([transaction.transaction.toUint8Array()]);
}
this.header.currentOffset = tempLedger.header.currentOffset;
tempLedger.close();

// 5. Replace Original File
await unlink(this.dataPath);
Expand All @@ -414,12 +411,8 @@ export class KVLedger {
}
}

public isClosing() {
return this.aborted;
}

private ensureOpen(): void {
if (this.aborted) throw new Error("Ledger is closed.");
if (!this.open) throw new Error("Ledger is not opened yet.");
}

public async lock(): Promise<void> {
Expand Down Expand Up @@ -487,4 +480,8 @@ export class KVLedger {
if (fd) fd.close();
}
}

public isOpen(): boolean {
return this.opened;
}
}
8 changes: 0 additions & 8 deletions test/ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ test("KVLedger: readHeader - valid header", async () => {
"Created timestamp should be valid",
);
assertEquals(ledger.header.currentOffset, LEDGER_BASE_OFFSET);

ledger.close();
});

test("KVLedger: readHeader - invalid file ID", async () => {
Expand All @@ -43,8 +41,6 @@ test("KVLedger: readHeader - invalid file ID", async () => {
() => ledger.readHeader(),
"Invalid database file format",
);

ledger.close();
});

test("KVLedger: readHeader - invalid version", async () => {
Expand All @@ -58,8 +54,6 @@ test("KVLedger: readHeader - invalid version", async () => {

// Act & Assert: Expect an InvalidLedgerError
await assertRejects(() => ledger.readHeader(), "Invalid version");

ledger.close();
});

test("KVLedger: writeHeader", async () => {
Expand All @@ -77,6 +71,4 @@ test("KVLedger: writeHeader", async () => {
await ledger.readHeader(); // Re-read from disk
assertEquals(ledger.header.created, 1234567890);
assertEquals(ledger.header.currentOffset, 2048);

ledger.close();
});

0 comments on commit 49d16ae

Please sign in to comment.