@alcalzone/jsonl-db
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -536,12 +536,15 @@ "use strict"; | ||
// And persist them | ||
let serialized = ""; | ||
for (const [key, value] of entries) { | ||
await fs.appendFile(this._dumpFd, | ||
// No need to serialize lazily here | ||
this.entryToLine(key, value) + "\n"); | ||
serialized += this.entryToLine(key, value) + "\n"; | ||
} | ||
await fs.appendFile(this._dumpFd, serialized); | ||
// In case there is any data in the backlog stream, persist that too | ||
let lazy; | ||
serialized = ""; | ||
while (null !== (lazy = this._dumpBacklog.read())) { | ||
await fs.appendFile(this._dumpFd, lazy.serialize() + "\n"); | ||
serialized += lazy.serialize() + "\n"; | ||
} | ||
await fs.appendFile(this._dumpFd, serialized); | ||
this._dumpBacklog.destroy(); | ||
@@ -570,2 +573,4 @@ this._dumpBacklog = undefined; | ||
const chunk = new Map(); | ||
let serialized = ""; | ||
let truncate = false; | ||
for await (const action of this | ||
@@ -575,3 +580,3 @@ ._writeBacklog) { | ||
chunk.clear(); | ||
chunk.set("", action); | ||
truncate = true; | ||
} | ||
@@ -584,13 +589,18 @@ else { | ||
if (this._writeBacklog.readableLength === 0) { | ||
if (truncate) { | ||
// Since we opened the file in append mode, we cannot truncate | ||
// therefore close and open in write mode again | ||
await fs.close(this._fd); | ||
this._fd = await fs.open(this.filename, "w+"); | ||
truncate = false; | ||
} | ||
// Collect all changes | ||
for (const entry of chunk.values()) { | ||
if (entry.op === Operation.Clear) { | ||
// Since we opened the file in append mode, we cannot truncate | ||
// therefore close and open in write mode again | ||
await fs.close(this._fd); | ||
this._fd = await fs.open(this.filename, "w+"); | ||
if (entry.op !== Operation.Clear) { | ||
serialized += entry.serialize() + "\n"; | ||
} | ||
else { | ||
await fs.appendFile(this._fd, entry.serialize() + "\n"); | ||
} | ||
} | ||
// and write once | ||
await fs.appendFile(this._fd, serialized); | ||
serialized = ""; | ||
chunk.clear(); | ||
@@ -695,4 +705,5 @@ } | ||
this._dumpBacklog.end(); | ||
} | ||
if (this._dumpPromise) | ||
await this._dumpPromise; | ||
} | ||
// Reset all variables | ||
@@ -699,0 +710,0 @@ this._writePromise = undefined; |
{ | ||
"name": "@alcalzone/jsonl-db", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"description": "Simple JSONL-based key-value store", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -145,2 +145,5 @@ # jsonl-db | ||
--> | ||
### 2.4.1 (2021-12-30) | ||
* Individual writes are now collected in a string and written at once, increasing throughput for larger entries by ~10x. | ||
### 2.4.0 (2021-12-27) | ||
@@ -147,0 +150,0 @@ * Stringifying the individual lines now happens lazily and only when actually necessary, increasing the throughput by 30...50x. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70459
882
227