|
|
@ -21,6 +21,7 @@ class Persistence { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
constructor (options) { |
|
|
|
constructor (options) { |
|
|
|
this.db = options.db |
|
|
|
this.db = options.db |
|
|
|
|
|
|
|
this.storage = options.storage || storage |
|
|
|
this.inMemoryOnly = this.db.inMemoryOnly |
|
|
|
this.inMemoryOnly = this.db.inMemoryOnly |
|
|
|
this.filename = this.db.filename |
|
|
|
this.filename = this.db.filename |
|
|
|
this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1 |
|
|
|
this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1 |
|
|
@ -92,7 +93,7 @@ class Persistence { |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
storage.crashSafeWriteFileLines(this.filename, lines, err => { |
|
|
|
this.storage.crashSafeWriteFileLines(this.filename, lines, err => { |
|
|
|
if (err) return callback(err) |
|
|
|
if (err) return callback(err) |
|
|
|
this.db.emit('compaction.done') |
|
|
|
this.db.emit('compaction.done') |
|
|
|
return callback(null) |
|
|
|
return callback(null) |
|
|
@ -146,7 +147,7 @@ class Persistence { |
|
|
|
|
|
|
|
|
|
|
|
if (toPersist.length === 0) return callback(null) |
|
|
|
if (toPersist.length === 0) return callback(null) |
|
|
|
|
|
|
|
|
|
|
|
storage.appendFile(this.filename, toPersist, 'utf8', err => callback(err)) |
|
|
|
this.storage.appendFile(this.filename, toPersist, 'utf8', err => callback(err)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -254,7 +255,7 @@ class Persistence { |
|
|
|
Persistence.ensureDirectoryExists(path.dirname(this.filename), err => { |
|
|
|
Persistence.ensureDirectoryExists(path.dirname(this.filename), err => { |
|
|
|
// TODO: handle error
|
|
|
|
// TODO: handle error
|
|
|
|
// eslint-disable-next-line node/handle-callback-err
|
|
|
|
// eslint-disable-next-line node/handle-callback-err
|
|
|
|
storage.ensureDatafileIntegrity(this.filename, err => { |
|
|
|
this.storage.ensureDatafileIntegrity(this.filename, err => { |
|
|
|
// TODO: handle error
|
|
|
|
// TODO: handle error
|
|
|
|
const treatedDataCallback = (err, treatedData) => { |
|
|
|
const treatedDataCallback = (err, treatedData) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
@ -275,15 +276,15 @@ class Persistence { |
|
|
|
this.db.persistence.persistCachedDatabase(cb) |
|
|
|
this.db.persistence.persistCachedDatabase(cb) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (storage.readFileStream) { |
|
|
|
if (this.storage.readFileStream) { |
|
|
|
// Server side
|
|
|
|
// Server side
|
|
|
|
const fileStream = storage.readFileStream(this.filename, { encoding: 'utf8' }) |
|
|
|
const fileStream = this.storage.readFileStream(this.filename, { encoding: 'utf8' }) |
|
|
|
this.treatRawStream(fileStream, treatedDataCallback) |
|
|
|
this.treatRawStream(fileStream, treatedDataCallback) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Browser
|
|
|
|
// Browser
|
|
|
|
storage.readFile(this.filename, 'utf8', (err, rawData) => { |
|
|
|
this.storage.readFile(this.filename, 'utf8', (err, rawData) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
@ -309,7 +310,7 @@ class Persistence { |
|
|
|
* cb is optional, signature: err |
|
|
|
* cb is optional, signature: err |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static ensureDirectoryExists (dir, callback = () => {}) { |
|
|
|
static ensureDirectoryExists (dir, callback = () => {}) { |
|
|
|
storage.mkdir(dir, { recursive: true }, err => { callback(err) }) |
|
|
|
this.storage.mkdir(dir, { recursive: true }, err => { callback(err) }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|