Skip to content

Commit

Permalink
Merge 36566e6 into 805a22a
Browse files Browse the repository at this point in the history
  • Loading branch information
plainweaver committed Nov 22, 2018
2 parents 805a22a + 36566e6 commit 5f3c9f8
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 526 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ results
npm-debug.log
node_modules
coverage
package-lock.json
159 changes: 67 additions & 92 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,129 +7,123 @@ var AbstractIterator = require('abstract-leveldown').AbstractIterator;

var LocalStorage = require('./localstorage').LocalStorage;
var LocalStorageCore = require('./localstorage-core');
var utils = require('./utils');
var ltgt = require('ltgt');

// see http://stackoverflow.com/a/15349865/680742
var nextTick = global.setImmediate || process.nextTick;

function LDIterator(db, options) {

AbstractIterator.call(this, db);

this._reverse = !!options.reverse;
this._endkey = options.end;
this._startkey = options.start;
this._gt = options.gt;
this._gte = options.gte;
this._lt = options.lt;
this._lte = options.lte;
this._exclusiveStart = options.exclusiveStart;
this.range = ltgt.toLtgt(options);

this._keysOnly = options.values === false;
this._limit = options.limit;

this.keyAsBuffer = options.keyAsBuffer !== false;
this.valueAsBuffer = options.valueAsBuffer !== false;

this._pos = 0;
this._count = 0;
this._nextQueue = [];

this.onInitCompleteListeners = [];
this._init();
}

inherits(LDIterator, AbstractIterator);

LDIterator.prototype._init = function (callback) {
var self = this;
self.initStarted = true;

nextTick(function () {
callback();
self.initStarted = true;
self.db.container.keys(function (err, keys) {
self._keys = keys;

if (self.range.reverse) {
self._pos = keys.length - 1;
}

if (callback) { callback(); }

self.initCompleted = true;
for (var i = 0; i < self._nextQueue.length; i++) {
self._nextQueue[i]();
}
});
});
};

LDIterator.prototype._next = function (callback) {
var self = this;

function onInitComplete() {
if (self._pos === self._keys.length || self._pos < 0) { // done reading
return callback();
}
if (!self.initStarted) {
return self._init(onInitComplete);
}

var key = self._keys[self._pos];
if (!self.initCompleted) {
return self._nextQueue.push(onInitComplete);
}

nextTick(onInitComplete);

if (!!self._endkey && (self._reverse ? key < self._endkey : key > self._endkey)) {
function onInitComplete () {
if (self._pos >= self._keys.length || self._pos < 0) {
return callback();
}

if (!!self._limit && self._limit > 0 && self._count++ >= self._limit) {
if (self._limit >= 0 && self._limit <= self._count) {
return callback();
}

if ((self._lt && key >= self._lt) ||
(self._lte && key > self._lte) ||
(self._gt && key <= self._gt) ||
(self._gte && key < self._gte)) {
return callback();
var key = self._keys[self._pos];
self._pos += self.range.reverse ? -1 : 1;

if (!ltgt.contains(self.range, key)) {
return self._next(callback);
}

if (self.keyAsBuffer) {
key = bufferFrom(String(key));
}

self._pos += self._reverse ? -1 : 1;
if (self._keysOnly) {
self._count++;
return callback(null, key);
}

self.db.container.getItem(key, function (err, value) {
if (err) {
if (err.message === 'NotFound') {
return nextTick(function () {
self._next(callback);
});
return self._next(callback);
}
return callback(err);
}
callback(null, key, value);
});
}
if (!self.initStarted) {
process.nextTick(function () {
self.initStarted = true;
self._init(function (err) {
if (err) {
return callback(err);
}
self.db.container.keys(function (err, keys) {
if (err) {
return callback(err);
}
self._keys = keys;
if (self._startkey) {
var index = utils.sortedIndexOf(self._keys, self._startkey);
var startkey = (index >= self._keys.length || index < 0) ?
undefined : self._keys[index];
self._pos = index;
if (self._reverse) {
if (self._exclusiveStart || startkey !== self._startkey) {
self._pos--;
}
} else if (self._exclusiveStart && startkey === self._startkey) {
self._pos++;
}
} else {
self._pos = self._reverse ? self._keys.length - 1 : 0;
}
onInitComplete();

self.initCompleted = true;
var i = -1;
while (++i < self.onInitCompleteListeners.length) {
nextTick(self.onInitCompleteListeners[i]);
}
});
});
if (self.valueAsBuffer) {
value = bufferFrom(String(value));
}

self._count++;
callback(null, key, value);
});
} else if (!self.initCompleted) {
self.onInitCompleteListeners.push(onInitComplete);
} else {
process.nextTick(onInitComplete);
}
};


function LD(location) {
if (!(this instanceof LD)) {
return new LD(location);
}
AbstractLevelDOWN.call(this, location);

if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument');
}

AbstractLevelDOWN.call(this);

this.location = location;
this.container = new LocalStorage(location);
}

Expand Down Expand Up @@ -157,13 +151,6 @@ LD.prototype._put = function (key, value, options, callback) {
});
}

if (typeof value === 'object' && !Buffer.isBuffer(value) && value.buffer === undefined) {
var obj = {};
obj.storetype = "json";
obj.data = value;
value = JSON.stringify(obj);
}

this.container.setItem(key, value, callback);
};

Expand Down Expand Up @@ -210,9 +197,6 @@ LD.prototype._del = function (key, options, callback) {
callback(err);
});
}
if (!Buffer.isBuffer(key)) {
key = String(key);
}

this.container.removeItem(key, callback);
};
Expand Down Expand Up @@ -267,8 +251,8 @@ LD.prototype._iterator = function (options) {
return new LDIterator(this, options);
};

LD.destroy = function (name, callback) {
LocalStorageCore.destroy(name, callback);
LD.prototype.destroy = function (callback) {
LocalStorageCore.destroy(this.location, callback);
};

function checkKeyValue(obj, type) {
Expand All @@ -280,7 +264,6 @@ function checkKeyValue(obj, type) {
}

if (type === 'key') {

if (obj instanceof Boolean) {
return new Error(type + ' cannot be `null` or `undefined`');
}
Expand All @@ -293,14 +276,6 @@ function checkKeyValue(obj, type) {
return new Error(type + ' cannot be an empty Buffer');
}
}

if (Buffer.isBuffer(obj)) {
if (obj.length === 0) {
return new Error(type + ' cannot be an empty Buffer');
}
} else if (String(obj) === '') {
return new Error(type + ' cannot be an empty String');
}
}

module.exports = LD;
2 changes: 1 addition & 1 deletion lib/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ LocalStorage.prototype.removeItem = function (key, callback) {
var self = this;
self.sequentialize(callback, function (callback) {
var idx = utils.sortedIndexOf(self._keys, key);
if (self._keys[idx] === key) {
if (self._keys[idx] === key) {
self._keys.splice(idx, 1);
self._store.remove(key, function (err) {
if (err) {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ exports.sortedIndexOf = function(arr, item) {
}
return low;
};

exports.test = function(value) {

};
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
"version": "0.6.7",
"main": "lib/index.js",
"dependencies": {
"abstract-leveldown": "0.12.3",
"abstract-leveldown": "^6.0.0",
"argsarray": "0.0.1",
"buffer-from": "^0.1.1",
"buffer-from": "^1.1.1",
"d64": "^1.0.0",
"humble-localstorage": "^1.4.2",
"inherits": "^2.0.1",
"tiny-queue": "0.2.0"
"humble-localstorage": "^2.0.0",
"inherits": "^2.0.3",
"ltgt": "^2.2.1",
"tiny-queue": "0.2.1"
},
"devDependencies": {
"browserify": "^4.1.2",
"es5-shim": "^4.3.1",
"istanbul": "^0.4.1",
"browserify": "^16.2.3",
"es5-shim": "^4.5.12",
"istanbul": "^0.4.5",
"istanbul-coveralls": "^1.0.3",
"jshint": "2.8.0",
"levelup": "^0.18.2",
"tape": "^2.12.3",
"zuul": "^3.10.1",
"jshint": "^2.9.6",
"levelup": "^3.1.1",
"tape": "^4.9.1",
"zuul": "^3.12.0",
"zuul-ngrok": "nolanlawson/zuul-ngrok#patch-1"
},
"repository": {
Expand All @@ -42,7 +43,7 @@
"bindings": false
},
"scripts": {
"test": "npm run jshint && tape tests/*.js",
"test": "npm run jshint && tape tests/index.js",
"test-browser": "npm run jshint && zuul --no-coverage tests/*.js",
"test-zuul-local": "npm run jshint && zuul --no-coverage --local 9000 tests/*.js",
"jshint": "jshint -c .jshintrc lib/*.js tests/*.js",
Expand Down
Loading

0 comments on commit 5f3c9f8

Please sign in to comment.