Skip to content

Commit

Permalink
Implement synchronous indexOf.
Browse files Browse the repository at this point in the history
Closes #432.
  • Loading branch information
flatheadmill committed Feb 5, 2015
1 parent fe043ae commit c52fad3
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 130 deletions.
42 changes: 12 additions & 30 deletions strata.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,34 +201,18 @@ prototype(Cursor, 'next', cadence(function (async) {
}))

// to user land
prototype(Cursor, 'indexOf', cadence(function (async, key) {
async(function () {
return this._sheaf.find(this._page, key, this._page.ghosts)
}, function (index) {
var unambiguous
unambiguous = -1 < index
|| ~ index < this._page.items.length
|| ! this._page.right.address
|| this._searchKey.length && this._sheaf.comparator(this._searchKey[0], key) == 0
if (!unambiguous) async(function () {
if (!this._rightLeafKey) async(function () {
this._locker.lock(this._page.right.address, false, async())
}, function (rightLeafPage) {
async([function () {
this._locker.unlock(rightLeafPage)
}], function (entry) {
this._rightLeafKey = rightLeafPage.items[0].key
})
})
}, function () {
if (this._sheaf.comparator(key, this._rightLeafKey) >= 0) {
return [ ~(this._page.items.length + 1) ]
} else {
return index
}
})
})
}))
Cursor.prototype._indexOf = function (key) {
var page = this._page
var index = this._sheaf.find(page, key, page.ghosts)
var unambiguous
unambiguous = -1 < index // <- todo: ?
|| ~ index < this._page.items.length
|| page.right.address == 0
if (!unambiguous && this._sheaf.comparator(key, page.right.key) >= 0) {
return [ ~(this._page.items.length + 1) ]
}
return index
}

// todo: pass an integer as the first argument to force the arity of the
// return.
Expand Down Expand Up @@ -1171,7 +1155,6 @@ prototype(Sheaf, 'deleteGhost', cadence(function (async, key) {

prototype(Sheaf, 'referring', cadence(function (async, leftKey, descents, pivot, pages) {
var referring
console.log(leftKey)
if (leftKey != null && pages.referring == null) {
descents.push(referring = pages.referring = pivot.fork())
async(function () {
Expand All @@ -1182,7 +1165,6 @@ prototype(Sheaf, 'referring', cadence(function (async, leftKey, descents, pivot,
}
}, function () {
var key = referring.page.items[referring.index].key
console.log([ leftKey, key, this.comparator(leftKey, key) ])
ok(this.comparator(leftKey, key) === 0, 'cannot find left key')
referring.index--
referring.descend(referring.right, referring.leaf, async())
Expand Down
4 changes: 1 addition & 3 deletions t/basics/delete.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ function prove (async, assert) {
strata.mutator('a', async())
}, function (cursor) {
async(function () {
cursor.indexOf('c', async())
}, function (i) {
cursor.remove(i, async())
cursor.remove(cursor._indexOf('c'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
8 changes: 2 additions & 6 deletions t/coverage/split-race.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ function prove (async, assert) {
async(function () {
cursor.remove(cursor.index, async())
}, function () {
cursor.indexOf('c', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('c'), async())
}, function () {
cursor.indexOf('d', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('d'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
4 changes: 1 addition & 3 deletions t/delete/ghost.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ function prove (async, assert) {
strata.mutator('c', async())
}, function (cursor) {
async(function() {
cursor.indexOf('c', async())
}, function (i) {
cursor.remove(i, async())
cursor.remove(cursor._indexOf('c'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down
3 changes: 1 addition & 2 deletions t/ghost/reinsert.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ function prove (async, assert) {
async(function () {
cursor.remove(cursor.index, async())
}, function () {
cursor.indexOf('d', async())
}, function (index) {
cursor.insert('d', 'd', ~index, async())
cursor.insert('d', 'd', ~cursor._indexOf('d'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down
6 changes: 2 additions & 4 deletions t/ghost/split.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ function prove (async, assert) {
async(function () {
cursor.remove(cursor.index, async())
}, function () {
cursor.indexOf('g', async())
}, function (index) {
cursor.insert('g', 'g', ~index, async())
cursor.insert('g', 'g', ~cursor._indexOf('g'), async())
}, function () {
cursor.indexOf('h', async())
}, function (index) {
cursor.insert('h', 'h', ~index, async())
cursor.insert('h', 'h', ~cursor._indexOf('h'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down
4 changes: 1 addition & 3 deletions t/ghost/unsplittable.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ function prove (async, assert) {
async(function () {
cursor.insert('g', 'g', ~cursor.index, async())
}, function () {
cursor.indexOf('d', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('d'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down
3 changes: 1 addition & 2 deletions t/insert/ambiguous-exact-ghosts.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ function prove (async, assert) {
strata.mutator('j', async())
}, function (cursor) {
async(function () {
cursor.indexOf('j', async())
}, function (index) {
var index = cursor._indexOf('j')
assert(~index <= cursor.length, 'unambiguous')
cursor.insert('j', 'j', ~cursor.index, async())
}, function () {
Expand Down
3 changes: 1 addition & 2 deletions t/insert/ambiguous-exact.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function prove (async, assert) {
strata.mutator('c', async())
}, function (cursor) {
async(function () {
cursor.indexOf('c', async())
}, function (index) {
var index = cursor._indexOf('c')
assert(~index <= cursor.length, 'unambiguous')
cursor.insert('c', 'c', ~cursor.index, async())
}, function () {
Expand Down
3 changes: 1 addition & 2 deletions t/insert/ambiguous-last-page.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function prove (async, assert) {
strata.mutator('l', async())
}, function (cursor) {
async(function () {
cursor.indexOf('z', async())
}, function (index) {
var index = cursor._indexOf('z')
assert(~index <= cursor.length, 'unambiguous')
cursor.insert('z', 'z', ~index, async())
}, function () {
Expand Down
3 changes: 1 addition & 2 deletions t/insert/ambiguous-less-than-length.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function prove (async, assert) {
strata.mutator('d', async())
}, function (cursor) {
async(function () {
cursor.indexOf('e', async())
}, function (index) {
var index = cursor._indexOf('e')
assert(index <= cursor.length, 'unambiguous')
cursor.insert('e', 'e', ~index, async())
}, function () {
Expand Down
6 changes: 2 additions & 4 deletions t/insert/ambiguous-peek.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ function prove (async, assert) {
strata.mutator('a', async())
}, function (cursor) {
async(function () {
cursor.indexOf('b', async())
}, function (index) {
var index = cursor._indexOf('b')
assert(~index <= cursor.length, 'unambiguous')
cursor.insert('b', 'b', ~index, async())
}, function () {
cursor.indexOf('c', async())
}, function (index) {
var index = cursor._indexOf('c')
assert(~index <= cursor.length, 'unambiguous cached')
cursor.insert('c', 'c', ~index, async())
}, function (unambiguous) {
Expand Down
3 changes: 1 addition & 2 deletions t/insert/ambiguous-too-early.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function prove (async, assert) {
strata.mutator('a', async())
}, function (cursor) {
var page = async(function () {
cursor.indexOf('z', async())
}, function (index) {
var index = cursor._indexOf('z')
ambiguity.unshift(cursor.length < ~index)
if (ambiguity[0]) {
async(function () {
Expand Down
16 changes: 4 additions & 12 deletions t/lock/purge.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,19 @@ function prove (async, assert) {
strata.mutator('h', async())
}, function (cursor) {
async(function () {
cursor.indexOf('h', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('h'), async())
}, function () {
cursor.indexOf('i', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('i'), async())
}, function () {
cursor.unlock(async())
})
}, function () {
strata.mutator('e', async())
}, function (cursor) {
async(function () {
cursor.indexOf('e', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('e'), async())
}, function () {
cursor.indexOf('g', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('g'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
8 changes: 2 additions & 6 deletions t/merge/branch.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ function prove (async, assert) {
strata.mutator('h', async())
}, function (cursor) {
async(function () {
cursor.indexOf('h', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('h'), async())
}, function () {
cursor.indexOf('i', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('i'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
4 changes: 1 addition & 3 deletions t/merge/leaf-less-than-max.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ function prove (async, assert) {
}, function () {
cursor.next(async())
}, function () {
cursor.indexOf('d', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('d'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
7 changes: 2 additions & 5 deletions t/merge/left-empty.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ function prove (async, assert) {
strata.mutator('a', async())
}, function (cursor) {
async(function () {
cursor.indexOf('a', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('a'), async())
}, function () {
cursor.indexOf('b', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('b'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
12 changes: 3 additions & 9 deletions t/merge/left-singles.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ function prove (async, assert) {
strata.mutator('bt', async())
}, function (cursor) {
async(function () {
cursor.indexOf('bt', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('bt'), async())
}, function () {
cursor.indexOf('bu', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('bu'), async())
}, function () {
cursor.unlock(async())
})
}, function () {
strata.mutator('bw', async())
}, function (cursor) {
async(function () {
cursor.indexOf('bw', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('bw'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
8 changes: 2 additions & 6 deletions t/merge/right-empty.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ function prove (async, assert) {
strata.mutator('c', async())
}, function (cursor) {
async(function () {
cursor.indexOf('c', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('c'), async())
}, function () {
cursor.indexOf('d', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('d'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
24 changes: 6 additions & 18 deletions t/merge/root-fill.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,29 @@ function prove (async, assert) {
strata.mutator('h', async())
}, function (cursor) {
async(function () {
cursor.indexOf('h', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('h'), async())
}, function () {
cursor.indexOf('i', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('i'), async())
}, function () {
cursor.unlock(async())
})
}, function () {
strata.mutator('e', async())
}, function (cursor) {
async(function () {
cursor.indexOf('e', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('e'), async())
}, function () {
cursor.indexOf('g', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('g'), async())
}, function () {
cursor.unlock(async())
})
}, function () {
strata.mutator('m', async())
}, function (cursor) {
async(function () {
cursor.indexOf('m', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('m'), async())
}, function () {
cursor.indexOf('n', async())
}, function (index) {
cursor.remove(index, async())
cursor.remove(cursor._indexOf('n'), async())
}, function () {
cursor.unlock(async())
})
Expand Down
4 changes: 1 addition & 3 deletions t/write/leaf.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function prove (async, assert) {
async(function () {
cursor.next(async())
}, function () {
cursor.indexOf('e', async())
}, function (index) {
cursor.insert('e', 'e', ~ index, async())
cursor.insert('e', 'e', ~cursor._indexOf('e'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down
4 changes: 1 addition & 3 deletions t/write/tree.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function prove (async, assert) {
async(function () {
cursor.next(async())
}, function () {
cursor.indexOf('e', async())
}, function (index) {
cursor.insert('e', 'e', ~ index, async())
cursor.insert('e', 'e', ~cursor._indexOf('e'), async())
}, function () {
cursor.unlock(async())
}, function () {
Expand Down

0 comments on commit c52fad3

Please sign in to comment.