Skip to content

Commit a9263a9

Browse files
authored
feat: support opt-out of updating timestamp for individual set calls (#415)
1 parent ade9dcf commit a9263a9

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ The following options exist (all optional) and can be combined:
128128

129129
### Keeping track of timestamps
130130

131-
The DB can automatically keep track of the last time a key was accessed or modified. To do so, set the `enableTimestamps` option to `true`. Every `set` call will update the timestamp of a value, which can be retrieved using `getTimestamp(key)`.
131+
The DB can automatically keep track of the last time a key was written. To do so, set the `enableTimestamps` option to `true`. Every `set` call will then update the timestamp of a value, which can be retrieved using `getTimestamp(key)`. To opt out of this behavior for individual writes, pass `false` as the 3rd argument to `set`.
132+
133+
```ts
134+
db.set("key", "value", false); // Don't update the timestamp
135+
```
136+
137+
> **Note**
138+
> Recording timestamps decreases the DB throughput by ~10...30% depending on how it is used.
132139
133140
### Import / Export
134141

src/lib/db.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ describe("lib/db", () => {
15341534
expect(actual).toBe(content);
15351535
}
15361536

1537-
it("should be recorded and read", async () => {
1537+
it("should be recorded and can be read back", async () => {
15381538
mockFs({
15391539
[testFilename]: ``,
15401540
});
@@ -1556,6 +1556,27 @@ describe("lib/db", () => {
15561556
expect(db.getTimestamp("1")).toBe(1000);
15571557
});
15581558

1559+
it("should not be updated when the 3rd argument to `set()` is false", async () => {
1560+
mockFs({
1561+
[testFilename]: ``,
1562+
});
1563+
1564+
db = new JsonlDB(testFilename, {
1565+
enableTimestamps: true,
1566+
});
1567+
await db.open();
1568+
1569+
vi.useFakeTimers({
1570+
now: 0,
1571+
});
1572+
db.set("0", "0");
1573+
vi.advanceTimersByTime(1000);
1574+
1575+
db.set("0", "1", false);
1576+
1577+
expect(db.getTimestamp("0")).toBe(0);
1578+
});
1579+
15591580
it("should be stored in the db file", async () => {
15601581
mockFs({
15611582
[testFilename]: ``,

src/lib/db.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,20 @@ export class JsonlDB<V = unknown> {
579579
return ret;
580580
}
581581

582-
public set(key: string, value: V): this {
582+
public set(key: string, value: V, updateTimestamp: boolean = true): this {
583583
if (!this._isOpen) {
584584
throw new Error("The database is not open!");
585585
}
586586
this._db.set(key, value);
587587
if (this.options.enableTimestamps) {
588-
const ts = Date.now();
589-
this._timestamps.set(key, ts);
588+
// If the timestamp should updated, use the current time, otherwise try to preserve the old one
589+
let ts: number | undefined;
590+
if (updateTimestamp) {
591+
ts = Date.now();
592+
this._timestamps.set(key, ts);
593+
} else {
594+
ts = this._timestamps.get(key);
595+
}
590596
this._journal.push(this.makeLazyWrite(key, value, ts));
591597
} else {
592598
this._journal.push(this.makeLazyWrite(key, value));

0 commit comments

Comments
 (0)