-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom separator #48
Open
jmatsushita
wants to merge
3
commits into
hypercore-protocol:master
Choose a base branch
from
jmatsushita:custom-sep
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Custom separator #48
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
const tape = require('tape') | ||
const create = require('./helpers/create') | ||
|
||
const sep = Buffer.alloc(1) | ||
|
||
tape('basic iteration', function (t) { | ||
const db = create(null, {sep}) | ||
const vals = ['a', 'b', 'c'] | ||
const expected = toMap(vals) | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator(), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('iterate a big db', function (t) { | ||
const db = create(null, {sep}) | ||
const vals = range(1000, '#') | ||
const expected = toMap(vals) | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator(), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('prefix basic iteration', function (t) { | ||
const db = create(null, {sep}) | ||
var vals = ['foo' + sep + 'a', 'foo' + sep + 'b', 'foo' + sep + 'c'] | ||
const expected = toMap(vals) | ||
|
||
vals = vals.concat(['a', 'b', 'c']) | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator('foo'), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('empty prefix iteration', function (t) { | ||
const db = create(null, {sep}) | ||
const vals = ['foo/a', 'foo/b', 'foo/c'] | ||
const expected = {} | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator('bar'), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('prefix iterate a big db', function (t) { | ||
var vals = range(1000, 'foo' + sep + '#') | ||
const db = create(null, {sep}) | ||
const expected = toMap(vals) | ||
|
||
vals = vals.concat(range(1000, '#')) | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator('foo'), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('non recursive iteration', function (t) { | ||
const db = create(null, {sep}) | ||
const vals = [ | ||
'a', | ||
'a' + sep + 'b' + sep + 'c' + sep + 'd', | ||
'a' + sep + 'b', | ||
'b', | ||
'b' + sep + 'b' + sep + 'c', | ||
'c' + sep + 'a', | ||
'c' | ||
] | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator({recursive: false}), function (err, map) { | ||
t.error(err, 'no error') | ||
// console.log('map', map) | ||
const keys = Object.keys(map).map(k => k.split(sep)[0]) | ||
t.same(keys.sort(), ['a', 'b', 'c'], 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('mixed nested and non nexted iteration', function (t) { | ||
const db = create(null, {sep}) | ||
const vals = ['a', 'a' + sep + 'a', 'a' + sep + 'b', 'a' + sep + 'c', 'a' + sep + 'a' + sep + 'a', 'a' + sep + 'a' + sep + 'b', 'a' + sep + 'a' + sep + 'c'] | ||
const expected = toMap(vals) | ||
|
||
put(db, vals, function (err) { | ||
t.error(err, 'no error') | ||
all(db.iterator(), function (err, map) { | ||
t.error(err, 'no error') | ||
t.same(map, expected, 'iterated all values') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
tape('list buffers an iterator', function (t) { | ||
const db = create(null, {sep}) | ||
|
||
put(db, ['a', 'b', 'b' + sep + 'c'], function (err) { | ||
t.error(err, 'no error') | ||
db.list(function (err, all) { | ||
t.error(err, 'no error') | ||
t.same(all.map(v => v.key).sort(), ['a', 'b', 'b' + sep + 'c']) | ||
db.list('b', {gt: true}, function (err, all) { | ||
t.error(err, 'no error') | ||
t.same(all.length, 1) | ||
t.same(all[0].key, 'b' + sep + 'c') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function range (n, v) { | ||
// #0, #1, #2, ... | ||
return new Array(n).join('.').split('.').map((a, i) => v + i) | ||
} | ||
|
||
function toMap (list) { | ||
const map = {} | ||
for (var i = 0; i < list.length; i++) { | ||
map[list[i]] = list[i] | ||
} | ||
return map | ||
} | ||
|
||
function all (ite, cb) { | ||
const vals = {} | ||
|
||
ite.next(function loop (err, node) { | ||
if (err) return cb(err) | ||
if (!node) return cb(null, vals) | ||
const key = Array.isArray(node) ? node[0].key : node.key | ||
// console.log('node', node) | ||
if (vals[key]) return cb(new Error('duplicate node for ' + key)) | ||
vals[key] = Array.isArray(node) ? node.map(n => n.value).sort() : node.value | ||
ite.next(loop) | ||
}) | ||
} | ||
|
||
function put (db, vals, cb) { | ||
db.batch(vals.map(v => ({key: v, value: v})), cb) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldnt it be easier just to load this from the db instance? It's not really configurable per node/iterator anyway, but more of a db wide thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we don't need all the options updates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sounds good! I'll work on this later.