@alcalzone/jsonl-db
Advanced tools
Comparing version
@@ -20,3 +20,3 @@ export declare class DB<V extends unknown = unknown> { | ||
private readLines; | ||
/** Opens the database file or creates it if it doesn't exist */ | ||
private _openPromise; | ||
open(): Promise<void>; | ||
@@ -23,0 +23,0 @@ /** Parses a line and updates the internal DB correspondingly */ |
@@ -37,2 +37,3 @@ "use strict"; | ||
fd: this._fd, | ||
autoClose: false, | ||
}); | ||
@@ -48,3 +49,3 @@ const rl = readline.createInterface(readStream); | ||
} | ||
/** Opens the database file or creates it if it doesn't exist */ | ||
// /** Opens the database file or creates it if it doesn't exist */ | ||
async open() { | ||
@@ -67,4 +68,9 @@ var e_1, _a; | ||
} | ||
// Close the file again to avoid EBADF | ||
await fs.close(this._fd); | ||
this._fd = undefined; | ||
// Start the write thread | ||
this._openPromise = deferred_promise_1.createDeferredPromise(); | ||
void this.writeThread(); | ||
await this._openPromise; | ||
} | ||
@@ -141,11 +147,15 @@ /** Parses a line and updates the internal DB correspondingly */ | ||
} | ||
// TODO: use cork() and uncork() to throttle filesystem accesses | ||
/** Asynchronously performs all write actions */ | ||
async writeThread() { | ||
var e_2, _a; | ||
var _b; | ||
// TODO: use cork() and uncork() | ||
var _b, _c; | ||
// This must be called before any awaits | ||
this._writeBacklog = new stream.PassThrough({ objectMode: true }); | ||
// Open the file for appending and reading | ||
this._fd = await fs.open(this.filename, "a+"); | ||
(_b = this._openPromise) === null || _b === void 0 ? void 0 : _b.resolve(); | ||
try { | ||
for (var _c = __asyncValues(this._writeBacklog), _d; _d = await _c.next(), !_d.done;) { | ||
const action = _d.value; | ||
for (var _d = __asyncValues(this._writeBacklog), _e; _e = await _d.next(), !_e.done;) { | ||
const action = _e.value; | ||
if (action === "") { | ||
@@ -165,3 +175,3 @@ // Since we opened the file in append mode, we cannot truncate | ||
try { | ||
if (_d && !_d.done && (_a = _c.return)) await _a.call(_c); | ||
if (_e && !_e.done && (_a = _d.return)) await _a.call(_d); | ||
} | ||
@@ -173,3 +183,3 @@ finally { if (e_2) throw e_2.error; } | ||
await fs.close(this._fd); | ||
(_b = this._closeDBPromise) === null || _b === void 0 ? void 0 : _b.resolve(); | ||
(_c = this._closeDBPromise) === null || _c === void 0 ? void 0 : _c.resolve(); | ||
} | ||
@@ -179,5 +189,10 @@ /** Compresses the db by dumping it and overwriting the aof file. */ | ||
await this.dump(); | ||
// After dumping, cork the write backlog, so nothing gets written | ||
this._writeBacklog.cork(); | ||
await fs.close(this._fd); | ||
// After dumping, restart the write thread so no duplicate entries get written | ||
if (this._writeBacklog) { | ||
this._closeDBPromise = deferred_promise_1.createDeferredPromise(); | ||
// Disable writing into the backlog stream | ||
this._writeBacklog.end(); | ||
this._writeBacklog = undefined; | ||
await this._closeDBPromise; | ||
} | ||
// Replace the aof file | ||
@@ -187,6 +202,6 @@ await fs.move(this.filename, this.filename + ".bak"); | ||
await fs.unlink(this.filename + ".bak"); | ||
// Re-open the file for appending | ||
this._fd = await fs.open(this.filename, "a+"); | ||
// and allow writing again | ||
this._writeBacklog.uncork(); | ||
// Start the write thread | ||
this._openPromise = deferred_promise_1.createDeferredPromise(); | ||
void this.writeThread(); | ||
await this._openPromise; | ||
} | ||
@@ -193,0 +208,0 @@ /** Closes the DB and waits for all data to be written */ |
{ | ||
"name": "@alcalzone/jsonl-db", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Simple JSONL-based key-value store", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -5,2 +5,8 @@ # jsonl-db | ||
 | ||
[](https://coveralls.io/github/AlCalzone/jsonl-db) | ||
[](https://lgtm.com/projects/g/AlCalzone/jsonl-db/context:javascript) | ||
[ ](https://www.npmjs.com/package/@alcalzone/jsonl-db) | ||
## Usage | ||
@@ -50,4 +56,7 @@ | ||
### 0.1.1 (2020-04-25) | ||
* Fixed some race conditions | ||
### 0.1.0 (2020-04-25) | ||
First official release |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24884
7.75%269
5.91%61
17.31%