Skip to content

Instantly share code, notes, and snippets.

@loveencounterflow
Last active August 29, 2015 14:13
Show Gist options
  • Save loveencounterflow/cb0b76c9d5d0b64137b0 to your computer and use it in GitHub Desktop.
Save loveencounterflow/cb0b76c9d5d0b64137b0 to your computer and use it in GitHub Desktop.

Revisions

  1. loveencounterflow revised this gist Jan 12, 2015. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    --------
    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𠁩'
    --------
  2. loveencounterflow revised this gist Jan 12, 2015. 2 changed files with 85 additions and 37 deletions.
    59 changes: 44 additions & 15 deletions fill-and-read-db.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,15 @@
    // Generated by CoffeeScript 1.8.0
    (function() {
    var $, D, keys, log, new_db, read, rmrf, rpr, through2, write;
    var $, D, db, db_route, feed, fs, gte, keys, log, new_db, read, rmrf, rpr, through2;

    new_db = require('level');
    fs = require('fs');

    log = console.log;

    rpr = (require('util')).inspect;

    new_db = require('level');

    through2 = require('through2');

    rmrf = require('rimraf');
    @@ -16,11 +18,8 @@

    $ = D.remit.bind(D);

    write = function(keys) {
    var db, db_route, input, key, output, _i, _len;
    db_route = '/tmp/level';
    rmrf.sync(db_route);
    db = new_db(db_route);
    feed = function(db, keys, handler) {
    var input, key, output, _i, _len;
    output = db.createWriteStream();
    input = through2.obj();
    input.pipe($(function(key, send) {
    @@ -29,7 +28,7 @@
    value: 1
    });
    })).pipe(output).on('close', function() {
    return read(db);
    return handler();
    });
    for (_i = 0, _len = keys.length; _i < _len; _i++) {
    key = keys[_i];
    @@ -38,20 +37,50 @@
    return input.end();
    };

    read = function(db) {
    read = function(db, lte, gte, handler) {
    var input;
    log("listing keys up to LTE: " + (rpr(lte)) + " " + (rpr(new Buffer(lte))));
    input = db.createKeyStream({
    gte: '',
    lte: 'xxx'
    gte: gte,
    lte: lte
    });
    return input.pipe($(function(key, send) {
    return log(rpr(key));
    return input.pipe($(function(key, send, end) {
    log(rpr(key));
    if (end != null) {
    end();
    return handler();
    }
    }));
    };

    if (module.parent == null) {
    keys = ['abcdef', 'aäöü', 'a~', 'aΔ', 'äöü', '中中', '中', '𠁩', '中\xff', '𠁩\xff', '中\uffff', '中𠁩\uffff', '𠁩\uffff', 'a', 'a\xff', 'a\uffff'];
    write(keys);
    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);
    63 changes: 41 additions & 22 deletions fille-and-read-db.coffee
    Original file line number Diff line number Diff line change
    @@ -1,54 +1,73 @@

    ############################################################################################################
    new_db = require 'level'
    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

    #-----------------------------------------------------------------------------------------------------------
    write = ( keys ) ->
    db_route = '/tmp/level'
    rmrf.sync db_route
    db = new_db db_route
    feed = ( db, keys, handler ) ->
    output = db.createWriteStream()
    input = through2.obj()
    #.........................................................................................................
    input
    .pipe $ ( key, send ) -> send { key, value: 1, }
    .pipe output
    .on 'close', -> read db
    .on 'close', -> handler()
    #.........................................................................................................
    input.write key for key in keys
    input.end()

    #-----------------------------------------------------------------------------------------------------------
    read = ( db ) ->
    input = db.createKeyStream { gte: '', lte: 'xxx', }
    input.pipe $ ( key, send ) -> log rpr key

    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中'
    'a𠁩'
    '~'
    'äöü'
    '中中'
    'ÿ'
    'Δ'
    ''
    '𠁩'
    '\xff'
    '𠁩\xff'
    '\uffff'
    '中𠁩\uffff'
    '𠁩\uffff'
    'a'
    'a\xff'
    'a\uffff'
    '\uffff'
    '\xff'
    ]
    write keys

    #---------------------------------------------------------------------------------------------------------
    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 '--------'
  3. loveencounterflow revised this gist Jan 12, 2015. 1 changed file with 57 additions and 0 deletions.
    57 changes: 57 additions & 0 deletions fill-and-read-db.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    // Generated by CoffeeScript 1.8.0
    (function() {
    var $, D, keys, log, new_db, read, rmrf, rpr, through2, write;

    new_db = require('level');

    log = console.log;

    rpr = (require('util')).inspect;

    through2 = require('through2');

    rmrf = require('rimraf');

    D = require('pipedreams2');

    $ = D.remit.bind(D);

    write = function(keys) {
    var db, db_route, input, key, output, _i, _len;
    db_route = '/tmp/level';
    rmrf.sync(db_route);
    db = new_db(db_route);
    output = db.createWriteStream();
    input = through2.obj();
    input.pipe($(function(key, send) {
    return send({
    key: key,
    value: 1
    });
    })).pipe(output).on('close', function() {
    return read(db);
    });
    for (_i = 0, _len = keys.length; _i < _len; _i++) {
    key = keys[_i];
    input.write(key);
    }
    return input.end();
    };

    read = function(db) {
    var input;
    input = db.createKeyStream({
    gte: '',
    lte: 'xxx'
    });
    return input.pipe($(function(key, send) {
    return log(rpr(key));
    }));
    };

    if (module.parent == null) {
    keys = ['abcdef', 'aäöü', 'a~', 'aΔ', 'äöü', '中中', '中', '𠁩', '中\xff', '𠁩\xff', '中\uffff', '中𠁩\uffff', '𠁩\uffff', 'a', 'a\xff', 'a\uffff'];
    write(keys);
    }

    }).call(this);
  4. loveencounterflow created this gist Jan 12, 2015.
    54 changes: 54 additions & 0 deletions fille-and-read-db.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@

    ############################################################################################################
    new_db = require 'level'
    log = console.log
    rpr = ( require 'util' ).inspect
    through2 = require 'through2'
    rmrf = require 'rimraf'
    D = require 'pipedreams2'
    $ = D.remit.bind D

    #-----------------------------------------------------------------------------------------------------------
    write = ( keys ) ->
    db_route = '/tmp/level'
    rmrf.sync db_route
    db = new_db db_route
    output = db.createWriteStream()
    input = through2.obj()
    #.........................................................................................................
    input
    .pipe $ ( key, send ) -> send { key, value: 1, }
    .pipe output
    .on 'close', -> read db
    #.........................................................................................................
    input.write key for key in keys
    input.end()

    #-----------------------------------------------------------------------------------------------------------
    read = ( db ) ->
    input = db.createKeyStream { gte: '', lte: 'xxx', }
    input.pipe $ ( key, send ) -> log rpr key


    ############################################################################################################
    unless module.parent?
    keys = [
    'abcdef'
    'aäöü'
    'a~'
    ''
    'äöü'
    '中中'
    ''
    '𠁩'
    '\xff'
    '𠁩\xff'
    '\uffff'
    '中𠁩\uffff'
    '𠁩\uffff'
    'a'
    'a\xff'
    'a\uffff'
    ]
    write keys