Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`lte` key limits in LevelDB and Unicode
// Generated by CoffeeScript 1.8.0
(function() {
var $, D, db, db_route, feed, fs, gte, keys, log, new_db, read, rmrf, rpr, through2;
fs = require('fs');
log = console.log;
rpr = (require('util')).inspect;
new_db = require('level');
through2 = require('through2');
rmrf = require('rimraf');
D = require('pipedreams2');
$ = D.remit.bind(D);
feed = function(db, keys, handler) {
var input, key, output, _i, _len;
output = db.createWriteStream();
input = through2.obj();
input.pipe($(function(key, send) {
return send({
key: key,
value: 1
});
})).pipe(output).on('close', function() {
return handler();
});
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
input.write(key);
}
return input.end();
};
read = function(db, lte, gte, handler) {
var input;
log("listing keys up to LTE: " + (rpr(lte)) + " " + (rpr(new Buffer(lte))));
input = db.createKeyStream({
gte: gte,
lte: lte
});
return input.pipe($(function(key, send, end) {
log(rpr(key));
if (end != null) {
end();
return handler();
}
}));
};
if (module.parent == null) {
keys = ['a', 'abcdef', 'aäöü', 'aΔ', 'a中', 'a𠁩', '~', 'äöü', 'ÿ', 'Δ', '中', '𠁩', '\uffff', '\xff'];
db_route = '/tmp/level';
if (fs.existsSync(db_route)) {
rmrf.sync(db_route);
}
db = new_db(db_route);
gte = 'a';
feed(db, keys, function() {
var lte;
log('--------');
lte = 'a~';
return read(db, lte, gte, function() {
log('--------');
lte = 'a\xff';
return read(db, lte, gte, function() {
log('--------');
lte = 'a\uffff';
return read(db, lte, gte, function() {
log('--------');
lte = new Buffer([0x61, 0xff]);
return read(db, lte, gte, function() {
return log('--------');
});
});
});
});
});
}
}).call(this);
############################################################################################################
fs = require 'fs'
log = console.log
rpr = ( require 'util' ).inspect
new_db = require 'level'
through2 = require 'through2'
rmrf = require 'rimraf'
D = require 'pipedreams2'
$ = D.remit.bind D
#-----------------------------------------------------------------------------------------------------------
feed = ( db, keys, handler ) ->
output = db.createWriteStream()
input = through2.obj()
#.........................................................................................................
input
.pipe $ ( key, send ) -> send { key, value: 1, }
.pipe output
.on 'close', -> handler()
#.........................................................................................................
input.write key for key in keys
input.end()
#-----------------------------------------------------------------------------------------------------------
read = ( db, lte, gte, handler ) ->
log "listing keys up to LTE: #{rpr lte} #{rpr new Buffer lte}"
input = db.createKeyStream { gte, lte, }
input
.pipe $ ( key, send, end ) ->
log rpr key
if end?
end()
handler()
############################################################################################################
unless module.parent?
keys = [
'a'
'abcdef'
'aäöü'
''
'a中'
'a𠁩'
'~'
'äöü'
'ÿ'
'Δ'
''
'𠁩'
'\uffff'
'\xff'
]
#---------------------------------------------------------------------------------------------------------
db_route = '/tmp/level'
rmrf.sync db_route if fs.existsSync db_route
db = new_db db_route
gte = 'a'
#---------------------------------------------------------------------------------------------------------
feed db, keys, ->
log '--------'
lte = 'a~'
read db, lte, gte, ->
log '--------'
lte = 'a\xff'
read db, lte, gte, ->
log '--------'
lte = 'a\uffff'
read db, lte, gte, ->
log '--------'
lte = new Buffer [ 0x61, 0xff, ]
read db, lte, gte, ->
log '--------'
--------
listing keys up to LTE: 'a~' <Buffer 61 7e>
'a'
'abcdef'
--------
listing keys up to LTE: 'aÿ' <Buffer 61 c3 bf>
'a'
'abcdef'
'aäöü'
--------
listing keys up to LTE: 'a￿' <Buffer 61 ef bf bf>
'a'
'abcdef'
'aäöü'
'aΔ'
'a中'
--------
listing keys up to LTE: <Buffer 61 ff> <Buffer 61 ff>
'a'
'abcdef'
'aäöü'
'aΔ'
'a中'
'a𠁩'
--------
@loveencounterflow
Copy link
Author

loveencounterflow commented Jan 12, 2015

quick demo to demonstrate that you should not use any of ~, ÿ, or even \uFFFF in your LevelDB when you want 'all possible keys with this prefix'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment