Skip to content

Commit

Permalink
[RN Support] Use local tree implementation + Remove util Dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 21, 2016
1 parent 2cff3f6 commit 1049fc7
Show file tree
Hide file tree
Showing 11 changed files with 1,060 additions and 44 deletions.
22 changes: 0 additions & 22 deletions lib/customUtils.js

This file was deleted.

15 changes: 8 additions & 7 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var customUtils = require('./custom-utils')
, async = require('async')
, Executor = require('./executor')
, Index = require('./indexes')
, util = require('util')
, _ = require('underscore')
, Persistence = require('./persistence')
, Cursor = require('./cursor')
, is = require('is')
;


Expand Down Expand Up @@ -83,7 +83,8 @@ function Datastore (options) {
}

try {
util.inherits(Datastore, require('events').EventEmitter);
// require('util').inherits(Datastore, require('events').EventEmitter);
Datastore.prototype = Object.assign(Datastore.prototype, require('events').EventEmitter.prototype);
} catch (e){
// TODO
// console.log('[WARN] [NeDB] EventEmitter Not Available!');
Expand Down Expand Up @@ -317,7 +318,7 @@ Datastore.prototype.getCandidates = function (query, dontExpireStaleDocs, callba
// For a basic match
usableQueryKeys = [];
Object.keys(query).forEach(function (k) {
if (typeof query[k] === 'string' || typeof query[k] === 'number' || typeof query[k] === 'boolean' || util.isDate(query[k]) || query[k] === null) {
if (typeof query[k] === 'string' || typeof query[k] === 'number' || typeof query[k] === 'boolean' || is.date(query[k]) || query[k] === null) {
usableQueryKeys.push(k);
}
});
Expand Down Expand Up @@ -362,7 +363,7 @@ Datastore.prototype.getCandidates = function (query, dontExpireStaleDocs, callba
docs.forEach(function (doc) {
var valid = true;
ttlIndexesFieldNames.forEach(function (i) {
if (doc[i] !== undefined && util.isDate(doc[i]) && Date.now() > doc[i].getTime() + self.ttlIndexes[i] * 1000) {
if (doc[i] !== undefined && is.date(doc[i]) && Date.now() > doc[i].getTime() + self.ttlIndexes[i] * 1000) {
valid = false;
}
});
Expand Down Expand Up @@ -399,7 +400,7 @@ Datastore.prototype._insert = function (newDoc, cb) {
return callback(e);
}

this.persistence.persistNewState(util.isArray(preparedDoc) ? preparedDoc : [preparedDoc], function (err) {
this.persistence.persistNewState(is.array(preparedDoc) ? preparedDoc : [preparedDoc], function (err) {
if (err) { return callback(err); }
return callback(null, model.deepCopy(preparedDoc));
});
Expand Down Expand Up @@ -437,7 +438,7 @@ Datastore.prototype.createNewId = function () {
Datastore.prototype.prepareDocumentForInsertion = function (newDoc) {
var preparedDoc, self = this;

if (util.isArray(newDoc)) {
if (is.array(newDoc)) {
preparedDoc = [];
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); });
} else {
Expand All @@ -457,7 +458,7 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) {
* @api private
*/
Datastore.prototype._insertInCache = function (preparedDoc) {
if (util.isArray(preparedDoc)) {
if (is.array(preparedDoc)) {
this._insertMultipleDocsInCache(preparedDoc);
} else {
this.addToIndexes(preparedDoc);
Expand Down
20 changes: 10 additions & 10 deletions lib/indexes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var BinarySearchTree = require('binary-search-tree').AVLTree
var BinarySearchTree = require('./tree/avl')
, model = require('./model')
, _ = require('underscore')
, util = require('util')
, is = require('is')
;

/**
Expand All @@ -19,7 +19,7 @@ function projectForUnique (elt) {
if (typeof elt === 'string') { return '$string' + elt; }
if (typeof elt === 'boolean') { return '$boolean' + elt; }
if (typeof elt === 'number') { return '$number' + elt; }
if (util.isArray(elt)) { return '$date' + elt.getTime(); }
if (is.array(elt)) { return '$date' + elt.getTime(); }

return elt; // Arrays and objects, will check for pointer equality
}
Expand Down Expand Up @@ -66,14 +66,14 @@ Index.prototype.insert = function (doc) {
, keys, i, failingI, error
;

if (util.isArray(doc)) { this.insertMultipleDocs(doc); return; }
if (is.array(doc)) { this.insertMultipleDocs(doc); return; }

key = model.getDotValue(doc, this.fieldName);

// We don't index documents that don't contain the field if the index is sparse
if (key === undefined && this.sparse) { return; }

if (!util.isArray(key)) {
if (!is.array(key)) {
this.tree.insert(key, doc);
} else {
// If an insert fails due to a unique constraint, roll back all inserts before it
Expand Down Expand Up @@ -138,13 +138,13 @@ Index.prototype.insertMultipleDocs = function (docs) {
Index.prototype.remove = function (doc) {
var key, self = this;

if (util.isArray(doc)) { doc.forEach(function (d) { self.remove(d); }); return; }
if (is.array(doc)) { doc.forEach(function (d) { self.remove(d); }); return; }

key = model.getDotValue(doc, this.fieldName);

if (key === undefined && this.sparse) { return; }

if (!util.isArray(key)) {
if (!is.array(key)) {
this.tree.delete(key, doc);
} else {
_.uniq(key, projectForUnique).forEach(function (_key) {
Expand All @@ -160,7 +160,7 @@ Index.prototype.remove = function (doc) {
* Naive implementation, still in O(log(n))
*/
Index.prototype.update = function (oldDoc, newDoc) {
if (util.isArray(oldDoc)) { this.updateMultipleDocs(oldDoc); return; }
if (is.array(oldDoc)) { this.updateMultipleDocs(oldDoc); return; }

this.remove(oldDoc);

Expand Down Expand Up @@ -219,7 +219,7 @@ Index.prototype.updateMultipleDocs = function (pairs) {
Index.prototype.revertUpdate = function (oldDoc, newDoc) {
var revert = [];

if (!util.isArray(oldDoc)) {
if (!is.array(oldDoc)) {
this.update(newDoc, oldDoc);
} else {
oldDoc.forEach(function (pair) {
Expand All @@ -238,7 +238,7 @@ Index.prototype.revertUpdate = function (oldDoc, newDoc) {
Index.prototype.getMatching = function (value) {
var self = this;

if (!util.isArray(value)) {
if (!is.array(value)) {
return self.tree.search(value);
} else {
var _res = {}, res = [];
Expand Down
Loading

0 comments on commit 1049fc7

Please sign in to comment.