|
|
@ -73,26 +73,27 @@ class Persistence { |
|
|
|
* @param {Function} callback Optional callback, signature: err |
|
|
|
* @param {Function} callback Optional callback, signature: err |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
persistCachedDatabase (callback = () => {}) { |
|
|
|
persistCachedDatabase (callback = () => {}) { |
|
|
|
let toPersist = '' |
|
|
|
|
|
|
|
|
|
|
|
const lines = [] |
|
|
|
|
|
|
|
|
|
|
|
if (this.inMemoryOnly) return callback(null) |
|
|
|
if (this.inMemoryOnly) return callback(null) |
|
|
|
|
|
|
|
|
|
|
|
this.db.getAllData().forEach(doc => { |
|
|
|
this.db.getAllData().forEach(doc => { |
|
|
|
toPersist += this.afterSerialization(model.serialize(doc)) + '\n' |
|
|
|
lines.push(this.afterSerialization(model.serialize(doc))) |
|
|
|
}) |
|
|
|
}) |
|
|
|
Object.keys(this.db.indexes).forEach(fieldName => { |
|
|
|
Object.keys(this.db.indexes).forEach(fieldName => { |
|
|
|
if (fieldName !== '_id') { // The special _id index is managed by datastore.js, the others need to be persisted
|
|
|
|
if (fieldName !== '_id') { // The special _id index is managed by datastore.js, the others need to be persisted
|
|
|
|
toPersist += this.afterSerialization(model.serialize({ |
|
|
|
lines.push(this.afterSerialization(model.serialize({ |
|
|
|
$$indexCreated: { |
|
|
|
$$indexCreated: { |
|
|
|
fieldName: fieldName, |
|
|
|
fieldName: fieldName, |
|
|
|
unique: this.db.indexes[fieldName].unique, |
|
|
|
unique: this.db.indexes[fieldName].unique, |
|
|
|
sparse: this.db.indexes[fieldName].sparse |
|
|
|
sparse: this.db.indexes[fieldName].sparse |
|
|
|
} |
|
|
|
} |
|
|
|
})) + '\n' |
|
|
|
}))) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
storage.crashSafeWriteFile(this.filename, toPersist, err => { |
|
|
|
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) |
|
|
|