Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable storage engine #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Datastore extends EventEmitter {
* @param {Function} [options.afterSerialization] Optional, serialization hooks
* @param {Number} [options.corruptAlertThreshold] Optional, threshold after which an alert is thrown if too much data is corrupt
* @param {Function} [options.compareStrings] Optional, string comparison function that overrides default for sorting
* @param {Object} [options.storage] Optional, custom storage engine
*
* Event Emitter - Events
* * compaction.done - Fired whenever a compaction operation was finished
Expand Down Expand Up @@ -58,7 +59,8 @@ class Datastore extends EventEmitter {
nodeWebkitAppName: options.nodeWebkitAppName,
afterSerialization: options.afterSerialization,
beforeDeserialization: options.beforeDeserialization,
corruptAlertThreshold: options.corruptAlertThreshold
corruptAlertThreshold: options.corruptAlertThreshold,
storage: options.storage
})

// This new executor is ready if we don't use persistence
Expand Down
15 changes: 8 additions & 7 deletions lib/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Persistence {
*/
constructor (options) {
this.db = options.db
this.storage = options.storage || storage
this.inMemoryOnly = this.db.inMemoryOnly
this.filename = this.db.filename
this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1
Expand Down Expand Up @@ -92,7 +93,7 @@ class Persistence {
}
})

storage.crashSafeWriteFileLines(this.filename, lines, err => {
this.storage.crashSafeWriteFileLines(this.filename, lines, err => {
if (err) return callback(err)
this.db.emit('compaction.done')
return callback(null)
Expand Down Expand Up @@ -146,7 +147,7 @@ class Persistence {

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))
}

/**
Expand Down Expand Up @@ -254,7 +255,7 @@ class Persistence {
Persistence.ensureDirectoryExists(path.dirname(this.filename), err => {
// TODO: handle error
// eslint-disable-next-line node/handle-callback-err
storage.ensureDatafileIntegrity(this.filename, err => {
this.storage.ensureDatafileIntegrity(this.filename, err => {
// TODO: handle error
const treatedDataCallback = (err, treatedData) => {
if (err) return cb(err)
Expand All @@ -275,15 +276,15 @@ class Persistence {
this.db.persistence.persistCachedDatabase(cb)
}

if (storage.readFileStream) {
if (this.storage.readFileStream) {
// Server side
const fileStream = storage.readFileStream(this.filename, { encoding: 'utf8' })
const fileStream = this.storage.readFileStream(this.filename, { encoding: 'utf8' })
this.treatRawStream(fileStream, treatedDataCallback)
return
}

// Browser
storage.readFile(this.filename, 'utf8', (err, rawData) => {
this.storage.readFile(this.filename, 'utf8', (err, rawData) => {
if (err) return cb(err)

try {
Expand All @@ -309,7 +310,7 @@ class Persistence {
* cb is optional, signature: err
*/
static ensureDirectoryExists (dir, callback = () => {}) {
storage.mkdir(dir, { recursive: true }, err => { callback(err) })
this.storage.mkdir(dir, { recursive: true }, err => { callback(err) })
}

/**
Expand Down