Skip to content

Commit

Permalink
1.0.0 - passes all leveldown tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed May 3, 2013
1 parent 4820a39 commit 8911826
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Level.prototype._put = function (key, value, options, callback) {

Level.prototype.iterator = function (options) {
if (typeof options !== 'object') options = {}
return new Iterator(this.idb)
return new Iterator(this.idb, options)
}

Level.prototype.batch = function (array, options, callback) {
Expand Down
54 changes: 46 additions & 8 deletions iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,46 @@ function Iterator (db, options) {
AbstractIterator.call(this, db)
this._order = !!options.reverse ? 'DESC': 'ASC'
this._start = options.start
this._limit = options.limit
if (this._limit) this._count = 0
this._end = options.end
this._done = false
}

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.createIterator = function() {
if (typeof this._start !== 'undefined' || typeof this._end !== 'undefined') {
var lower, upper
var onlyStart = typeof this._start !== 'undefined' && typeof this._end === 'undefined'
var onlyEnd = typeof this._start === 'undefined' && typeof this._end !== 'undefined'
var startAndEnd = typeof this._start !== 'undefined' && typeof this._end !== 'undefined'
if (onlyStart) {
var index = this._start
if (this._order === 'ASC') {
lower = index
} else {
upper = index
}
} else if (onlyEnd) {
var index = this._end
if (this._order === 'DESC') {
lower = index
} else {
upper = index
}
} else if (startAndEnd) {
lower = this._start
upper = this._end
if (this._start > this._end) {
lower = this._end
upper = this._start
}
}
if (lower || upper) {
this._keyRange = this.options.keyRange || this.db.makeKeyRange({
lower: this._start,
upper: this._end
// todo excludeUpper/excludeLower
lower: lower,
upper: upper
// todo expose excludeUpper/excludeLower
})
}
this.iterator = this.db.iterate(this.onItem.bind(this), {
Expand All @@ -30,11 +58,21 @@ Iterator.prototype.createIterator = function() {
})
}

// TODO the limit implementation here just ignores all reads after limit has been reached
// it should cancel the iterator instead but I don't know how
Iterator.prototype.onItem = function (cursor, cursorTransaction) {
if (!cursor && this.callback) return this.callback()
if (this.callback) this.callback(false, cursor.key, cursor.value)
this.callback = false
cursor.continue()
if (!cursor && this.callback) {
this.callback()
this.callback = false
return
}
if (this._limit) {
if (this._limit > this._count) this.callback(false, cursor.key, cursor.value)
} else {
this.callback(false, cursor.key, cursor.value)
}
if (this._limit) this._count++
if (cursor) cursor.continue()
}

Iterator.prototype._next = function (callback) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "level-js",
"version": "0.0.0",
"version": "1.0.0",
"description": "leveldown/leveldb library for browsers",
"main": "index.js",
"scripts": {
Expand All @@ -17,12 +17,12 @@
"author": "max ogden",
"license": "BSD",
"devDependencies": {
"abstract-leveldown": "~0.1.0",
"abstract-leveldown": "git://github.com/rvagg/node-abstract-leveldown.git",
"tape": "~0.3.3",
"beefy": "0.1.1",
"browserify": "~2.13.2"
},
"dependencies": {
"idb-wrapper": "~1.1.0"
"idb-wrapper": "git://github.com/maxogden/IDBWrapper.git#autoContinueOption"
}
}

0 comments on commit 8911826

Please sign in to comment.