|
|
|
@ -4,6 +4,7 @@ var fs = require('fs') |
|
|
|
|
, model = require('./model') |
|
|
|
|
, async = require('async') |
|
|
|
|
, Executor = require('./executor') |
|
|
|
|
, Index = require('./indexes') |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -18,9 +19,33 @@ function Datastore (filename) { |
|
|
|
|
// We keep internally the number of lines in the datafile
|
|
|
|
|
// This will be used when/if I implement autocompacting when the datafile grows too big
|
|
|
|
|
this.datafileSize = 0; |
|
|
|
|
|
|
|
|
|
// Indexed by field name, dot notation can be used
|
|
|
|
|
this.indexes = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Ensure an index is kept for this field. Same parameters as lib/indexes |
|
|
|
|
* For now this function is synchronous, we need to test how much time it takes |
|
|
|
|
* @param {String} options.fieldName |
|
|
|
|
* @param {Boolean} options.unique |
|
|
|
|
* @param {Boolean} options.sparse |
|
|
|
|
* @return {Boolean} true if index was created or already exists, false otherwise |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype.ensureIndex = function (options) { |
|
|
|
|
options = options || {}; |
|
|
|
|
|
|
|
|
|
if (!options.fieldName) { return false; } |
|
|
|
|
if (this.indexes[options.fieldName]) { return true; } |
|
|
|
|
|
|
|
|
|
options.datastore = this; |
|
|
|
|
this.indexes[options.fieldName] = new Index(options); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Load the database |
|
|
|
|
* This means pulling data out of the data file or creating it if it doesn't exist |
|
|
|
|