@alcalzone/jsonl-db
Advanced tools
Comparing version
@@ -82,3 +82,3 @@ import * as fs from "fs-extra"; | ||
private _dumpBacklog; | ||
private compressInterval; | ||
private _compressInterval; | ||
private _openPromise; | ||
@@ -85,0 +85,0 @@ open(): Promise<void>; |
@@ -18,3 +18,2 @@ "use strict"; | ||
constructor(filename, options = {}) { | ||
var _a; | ||
this._db = new Map(); | ||
@@ -37,11 +36,2 @@ this._uncompressedSize = Number.NaN; | ||
this[Symbol.iterator] = this._db[Symbol.iterator].bind(this._db); | ||
// Start regular auto-compression | ||
const { intervalMs, intervalMinChanges = 1 } = (_a = options.autoCompress) !== null && _a !== void 0 ? _a : {}; | ||
if (intervalMs) { | ||
this.compressInterval = setInterval(() => { | ||
if (this._changesSinceLastCompress >= intervalMinChanges) { | ||
void this.compress(); | ||
} | ||
}, intervalMs); | ||
} | ||
} | ||
@@ -132,2 +122,7 @@ validateOptions(options) { | ||
} | ||
const { onOpen, intervalMs, intervalMinChanges = 1 } = (_a = this.options.autoCompress) !== null && _a !== void 0 ? _a : {}; | ||
// If the DB should be compressed while opening, do it before starting the write thread | ||
if (onOpen) { | ||
await this.compressInternal(); | ||
} | ||
// Start the write thread | ||
@@ -138,4 +133,9 @@ this._openPromise = deferred_promise_1.createDeferredPromise(); | ||
this._isOpen = true; | ||
if ((_a = this.options.autoCompress) === null || _a === void 0 ? void 0 : _a.onOpen) { | ||
await this.compress(); | ||
// Start regular auto-compression | ||
if (intervalMs) { | ||
this._compressInterval = setInterval(() => { | ||
if (this._changesSinceLastCompress >= intervalMinChanges) { | ||
void this.compress(); | ||
} | ||
}, intervalMs); | ||
} | ||
@@ -248,12 +248,7 @@ } | ||
autoCork() { | ||
var _a; | ||
var _a, _b, _c; | ||
if (!((_a = this.options.throttleFS) === null || _a === void 0 ? void 0 : _a.intervalMs)) | ||
return; | ||
const schedule = () => { | ||
if (this._writeCorkTimeout) { | ||
clearTimeout(this._writeCorkTimeout); | ||
} | ||
this._writeCorkTimeout = setTimeout(() => maybeUncork.bind(this)(), this.options.throttleFS.intervalMs); | ||
}; | ||
function maybeUncork() { | ||
const maybeUncork = () => { | ||
var _a; | ||
if (this._writeBacklog && this._writeBacklog.writableLength > 0) { | ||
@@ -266,8 +261,8 @@ // This gets the stream flowing again. The write thread will call | ||
// Nothing to uncork, schedule the next timeout | ||
schedule(); | ||
(_a = this._writeCorkTimeout) === null || _a === void 0 ? void 0 : _a.refresh(); | ||
} | ||
} | ||
}; | ||
// Cork once and schedule the uncork | ||
this.cork(); | ||
schedule(); | ||
this._writeCorkTimeout = (_c = (_b = this._writeCorkTimeout) === null || _b === void 0 ? void 0 : _b.refresh()) !== null && _c !== void 0 ? _c : setTimeout(maybeUncork, this.options.throttleFS.intervalMs); | ||
} | ||
@@ -370,3 +365,3 @@ /** | ||
// When this is a throttled stream, auto-cork it when it was drained | ||
if (this._writeBacklog.readableLength === 0) { | ||
if (this._writeBacklog.readableLength === 0 && this._isOpen) { | ||
this.autoCork(); | ||
@@ -390,3 +385,3 @@ } | ||
async compressInternal() { | ||
if (!this._writeBacklog || this.compressPromise) | ||
if (this.compressPromise) | ||
return; | ||
@@ -404,5 +399,7 @@ this.compressPromise = deferred_promise_1.createDeferredPromise(); | ||
this.uncork(); | ||
this._writeBacklog.end(); | ||
await this._writePromise; | ||
this._writeBacklog = undefined; | ||
if (this._writeBacklog) { | ||
this._writeBacklog.end(); | ||
await this._writePromise; | ||
this._writeBacklog = undefined; | ||
} | ||
// Replace the aof file | ||
@@ -412,6 +409,8 @@ await fs.move(this.filename, this.filename + ".bak"); | ||
await fs.unlink(this.filename + ".bak"); | ||
// Start the write thread again | ||
this._openPromise = deferred_promise_1.createDeferredPromise(); | ||
void this.writeThread(); | ||
await this._openPromise; | ||
if (this._isOpen) { | ||
// Start the write thread again | ||
this._openPromise = deferred_promise_1.createDeferredPromise(); | ||
void this.writeThread(); | ||
await this._openPromise; | ||
} | ||
// In case there is any data in the backlog stream, persist that too | ||
@@ -439,4 +438,4 @@ let line; | ||
this._isOpen = false; | ||
if (this.compressInterval) | ||
clearInterval(this.compressInterval); | ||
if (this._compressInterval) | ||
clearInterval(this._compressInterval); | ||
if (this._writeCorkTimeout) | ||
@@ -443,0 +442,0 @@ clearTimeout(this._writeCorkTimeout); |
{ | ||
"name": "@alcalzone/jsonl-db", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Simple JSONL-based key-value store", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -133,2 +133,5 @@ # jsonl-db | ||
### 1.1.2 (2020-05-11) | ||
Fixed a timeout leak that would prevent Node.js from exiting | ||
### 1.1.1 (2020-05-07) | ||
@@ -135,0 +138,0 @@ Leading directories are now created if they don't exist |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50695
0.39%176
1.73%588
-0.17%