localstorage-down
Advanced tools
Comparing version 0.6.2 to 0.6.3
122
index.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
var LocalStorageCore = require('./localstorage-core'); | ||
var utils = require('./utils'); | ||
@@ -35,28 +36,4 @@ // see http://stackoverflow.com/a/15349865/680742 | ||
LDIterator.prototype._init = function (callback) { | ||
var self = this; | ||
self.db.container.length(function (err, len) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (self._startkey) { | ||
self.db.container.binarySearch(self._startkey, function (err, res) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self._pos = res.index; | ||
var startkey = res.key; | ||
if (self._reverse) { | ||
if (self._exclusiveStart || startkey !== self._startkey) { | ||
self._pos--; | ||
} | ||
} else if (self._exclusiveStart && startkey === self._startkey) { | ||
self._pos++; | ||
} | ||
callback(); | ||
}); | ||
} else { | ||
self._pos = self._reverse ? len - 1 : 0; | ||
callback(); | ||
} | ||
nextTick(function () { | ||
callback(); | ||
}); | ||
@@ -69,38 +46,34 @@ }; | ||
function onInitComplete() { | ||
self.db.container.getKeyAt(self._pos, function (err, key) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (self._pos === self._keys.length || self._pos < 0) { // done reading | ||
return callback(); | ||
} | ||
if (typeof key === 'undefined') { // done reading | ||
return callback(); | ||
} | ||
var key = self._keys[self._pos]; | ||
if (!!self._endkey && (self._reverse ? key < self._endkey : key > self._endkey)) { | ||
return callback(); | ||
} | ||
if (!!self._endkey && (self._reverse ? key < self._endkey : key > self._endkey)) { | ||
return callback(); | ||
} | ||
if (!!self._limit && self._limit > 0 && self._count++ >= self._limit) { | ||
return callback(); | ||
} | ||
if (!!self._limit && self._limit > 0 && self._count++ >= self._limit) { | ||
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(); | ||
} | ||
if ((self._lt && key >= self._lt) || | ||
(self._lte && key > self._lte) || | ||
(self._gt && key <= self._gt) || | ||
(self._gte && key < self._gte)) { | ||
return callback(); | ||
} | ||
self.db.container.getItem(key, function (err, value) { | ||
if (err) { | ||
if (err.message === 'NotFound') { | ||
return nextTick(function () { | ||
self._next(callback); | ||
}); | ||
} | ||
return callback(err); | ||
self._pos += self._reverse ? -1 : 1; | ||
self.db.container.getItem(key, function (err, value) { | ||
if (err) { | ||
if (err.message === 'NotFound') { | ||
return nextTick(function () { | ||
self._next(callback); | ||
}); | ||
} | ||
self._pos += self._reverse ? -1 : 1; | ||
callback(null, key, value); | ||
}); | ||
return callback(err); | ||
} | ||
callback(null, key, value); | ||
}); | ||
@@ -114,14 +87,33 @@ } | ||
} | ||
onInitComplete(); | ||
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) { | ||
nextTick(self.onInitCompleteListeners[i]); | ||
} | ||
self.initCompleted = true; | ||
var i = -1; | ||
while (++i < self.onInitCompleteListeners) { | ||
nextTick(self.onInitCompleteListeners[i]); | ||
} | ||
}); | ||
}); | ||
} else if (!self.initCompleted) { | ||
self.onInitCompleteListeners.push(function () { | ||
onInitComplete(); | ||
}); | ||
self.onInitCompleteListeners.push(onInitComplete); | ||
} else { | ||
@@ -128,0 +120,0 @@ onInitComplete(); |
@@ -41,24 +41,9 @@ 'use strict'; | ||
// returns the key index if found, else the index where | ||
// the key should be inserted. also returns the key | ||
// at that index | ||
LocalStorage.prototype.binarySearch = function (key, callback) { | ||
LocalStorage.prototype.keys = function (callback) { | ||
var self = this; | ||
self.sequentialize(callback, function (callback) { | ||
var index = utils.sortedIndexOf(self._keys, key); | ||
var foundKey = (index >= self._keys.length || index < 0) ? | ||
undefined : self._keys[index]; | ||
callback(null, {index: index, key: foundKey}); | ||
callback(null, self._keys.slice()); | ||
}); | ||
}; | ||
LocalStorage.prototype.getKeyAt = function (index, callback) { | ||
var self = this; | ||
self.sequentialize(callback, function (callback) { | ||
var foundKey = (index >= self._keys.length || index < 0) ? | ||
undefined : self._keys[index]; | ||
callback(null, foundKey); | ||
}); | ||
}; | ||
//setItem: Saves and item at the key provided. | ||
@@ -65,0 +50,0 @@ LocalStorage.prototype.setItem = function (key, value, callback) { |
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"main": "index.js", | ||
@@ -18,0 +18,0 @@ "dependencies": { |
@@ -90,4 +90,4 @@ 'use strict'; | ||
t.notOk(err, 'no error'); | ||
t.equals(key, 'c'); | ||
t.equal(value, 'C'); | ||
t.ok(key, 'key exists'); | ||
t.ok(value, 'value exists'); | ||
t.end(); | ||
@@ -99,2 +99,36 @@ }); | ||
tape('add many while iterating', function (t) { | ||
var db = leveldown(testCommon.location()); | ||
var noerr = function (err) { | ||
t.error(err, 'opens crrectly'); | ||
}; | ||
var noop = function () {}; | ||
var iterator; | ||
db.open(noerr); | ||
db.put('c', 'C', noop); | ||
db.put('d', 'D', noop); | ||
db.put('e', 'E', noop); | ||
iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: 'c' }); | ||
iterator.next(function (err, key, value) { | ||
t.equal(key, 'c'); | ||
t.equal(value, 'C'); | ||
db.del('c', function (err) { | ||
t.notOk(err, 'no error'); | ||
db.put('a', 'A', function (err) { | ||
t.notOk(err, 'no error'); | ||
db.put('b', 'B', function (err) { | ||
t.notOk(err, 'no error'); | ||
iterator.next(function (err, key, value) { | ||
t.notOk(err, 'no error'); | ||
t.ok(key, 'key exists'); | ||
t.ok(value, 'value exists'); | ||
t.ok(key >= 'c', 'key "' + key + '" should be greater than c'); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
tape('concurrent batch delete while iterating', function (t) { | ||
@@ -101,0 +135,0 @@ var db = leveldown(testCommon.location()); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
759
68969