Example #1
0
test('replicate 3', function (t) {

  var db1 = sublevel(level('replicate3_1'))
  var db2 = sublevel(level('replicate3_2'))
  var db3 = sublevel(level('replicate3_3'))

  var m1 = master(db1, 'master', "M1")
  var m2 = master(db2, 'master', "M2")
  var m3 = master(db3, 'master', "M3")

  series (
    para (
      put (db1, 'foo', new Date()),
      put (db2, 'bar', new Date()),
      put (db3, 'baz', new Date())
    ),
    para (replicate(m1, m2), replicate(m2, m1)),
    para (replicate(m2, m3), replicate(m3, m2)),
    para (replicate(m1, m3), replicate(m3, m1)),
    para (all(db1), all (db2), all(db3))
  ) (function (err, all) {
      t.notOk(err)
      //assert that both databases are equal!
      console.log(all)
      t.deepEqual(all[0], all[1])
      t.deepEqual(all[0], all[2])
      t.end()
    })

})
Example #2
0
function WorkList(opts) {
  var self = this
  if (!(self instanceof WorkList)) return new WorkList(opts)
  if (!opts) opts = {}
  self.name = process.env.HOME + '/worklist/worklist.db'
  var m = moment()
  if (opts.workDate) {
    m = moment(opts.workDate)
  }
  if (opts.weekly) {
    m = m.startOf('week').add('days', 1)
  }
  self.workDate = m.format('YYYY-MM-DD')
  
  if (opts.dbname) {
    if (opts.dbname === 'memory') {
      self.db = sublevel(levelup({ db: require('memdown') }))
    } else {
      self.name = opts.dbname
      self.db = sublevel(levelup(self.name, {db: require('leveldown-prebuilt')}))
    }
  } else {  
    self.db = sublevel(levelup(self.name, {db: require('leveldown-prebuilt')}))
  }

  self.list = self.db.sublevel('list')

  self.current = []
}
Example #3
0
module.exports = function(socket, opts) {
  var opts = opts || {}
  var db = sublevel(opts.db) || sublevel(levelup('data.db'))
  var replicator = opts.replicator || replicate(db, 'master', "MASTER-1")
  
  var stream = websocket(socket)
  stream.pipe(replicator.createStream({tail: true})).pipe(stream)
  stream.on('data', function(c) {
    console.log(c)
  })
}
Example #4
0
sum(dir, function (err, db) {
  if(err)
    throw err
  SubLevel(db)

  var mapper = MapReduce(db, 'sum',
    function (key, value, emit) {
      value = JSON.parse(value)
      emit(value % 2 ? 'odd' : 'even', value || '0')
    },
    function (big, little) {
      return JSON.stringify(JSON.parse(big || 0) + JSON.parse(little))
    }).start()

  mapper.on('reduce',
    mac(function (key, sum) {
    console.log('reduce', key, sum)
      if(key.length == 0) {
        assert.equal(JSON.parse(sum), ( 1000 * 1001 ) / 2)
    }
  }).times(3))

//*/
  /*
  db.mapReduce.view('sum', [])
    .on('data', mac(function (data) {
      console.log('LIVE', data.key, ''+data.value)
    }).atLeast(1))
  */
})
Example #5
0
test('load', function (t) {
  t.plan(4);
  var db = tree(sublevel(level('./test_db',  {valueEncoding:'json', db: require('memdown') })));
  db.put('1', schools.features[0], function (err) {
    t.error(err);
    db.del('1', function (err) {
      t.error(err);
      db.batch([{
        key: '1',
        value: schools.features[0]
      },{
        key: '2',
        value: schools.features[1]
      },
      {
        key: 'not geojson',
        value: {sandwich: 'ham'}
      }], function (err) {
        t.error(err);
        db.once('uptodate', function () {
          db.treeQuery([-180, 0, 0, 90], function (err, resp) {
            t.equals(resp.length, 2, 'got both');
          });
        });
      });
    });
  });
});
Example #6
0
    getDb(function (db) {
      sublevel(db)
      var foo = db.sublevel('foo')

      //add inc to the manifest
      foo.methods['inc'] = {type: 'async'}

      //implement it
      foo.inc = function (f, cb) {
        if(!cb) {
          cb = f
          foo.get('inc', function (err, val) {
            cb(err, val)
          })
        } else {
          foo.get('inc', function (err, val) {
            val = Number(val || 0) + Number(f)
            foo.put('inc', val, function (err) {
              if(err) cb(err)
              else    cb(null, val)
            })
          })
        }
      }

    },
test('\nwhen storing existing user and repos were not modified', function (t) {
  var db = sublevel(level(null, { valueEncoding: 'json' }))
  
  
  store(db, JSON.parse(jsonWithRepos), 'isaacs', function () {
    store(db, JSON.parse(jsonWithoutRepos), 'isaacs', function (err, res) {

        
        var users = []
        dump(
          sublevels(db).github.users
          , [].push.bind(users)
          , function () {
              var fst = users[0];

              t.equal(fst.key, 'isaacs', 'stores the user')
              t.equal(users.length, 1, 'stores no other user')
              t.equal(fst.value.followersCount, 5, 'correctly calculates and stores followers count')
              t.equal(fst.value.id, 9287, 'preserves user\'s id')

              t.end()
            }
        )
    })
  })
})
Example #8
0
function Backer(dir) {
  if (!(this instanceof Backer)) return new Backer(dir);
  EventEmitter.call(this);

  this.dir = dir;
  this.db = sub(level(join(dir, '/.backer')));
}
module.exports = (serviceLocator) => {
  const dbName = serviceLocator.get('dbName');

  return sublevel(
    level(dbName, {valueEncoding: 'json'})
  );
}
Example #10
0
function LevelMicroBlog(dbPath) {
  if (!(this instanceof LevelMicroBlog)) return new LevelMicroBlog(dbPath);
  this.db = sublevel(level(dbPath, { keyEncoding: bytewise, valueEncoding: 'json' }));
  this.Users = new Users(this);
  this.Messages = new Messages(this);
  this.Feed = new Feed(this);
}
Example #11
0
test('simple', function (t) {

  var db = sublevel(level('window2', {encoding: 'json'}))

  var i = 0
  var windowDb = Window(db, 'window',
    window(function (data, cb) {
      if(i++ % 5) return
      var sum = 0, count = 0
      return function (end, data) {
        if(end) return
        sum += +data.value
        if(++count >= 20)
          cb(null, {sum:sum, count: count, avg: sum/count})
      }
    }))

  pull.count(100)
    .pipe(pull.map(function (e) {
      return {key: timestamp(), value: Math.random()}
    }))
    .pipe(pl.write(db, function (err) {
      t.notOk(err)

      pl.read(windowDb)
        .pipe(pull.collect(function (err, arr) {
          console.log(arr)
          
          t.equal(arr.length, 14)
          t.end()
        }))
    }))
  })
Example #12
0
tape('remote open, local open', function (t) {
  var path = 'test-scuttlebutt-remote3'
  t.plan(2)

  var db = SubLevel(level(path))
  var schema = {test: Model}
  LevelScuttlebutt(db, 'test', schema)
  var local  = db.scuttlebutt
  var remote = Client(schema, 'test-client')

  remote.open('test1', mac(function remoteOpen (err, a) {
    if(err) t.fail(err)
     a.set('x', Math.random())
    a.set('y', Math.random())
    a.set('z', Math.random())

    local.open('test1', mac(function localOpen (err, b) {
      t.notStrictEqual(a, b)
      console.log('A', a.history())
      console.log('B', b.history())
      t.deepEqual(b.history(), a.history())
      t.end()
    }).once())

  }).once())
  var ls = local.createRemoteStream()
  var rs = remote.createStream()

  ls.pipe(rs).pipe(ls)
})
Example #13
0
  level(dbLocation, function (err, db) {
    console.log('New LevelUP instance', dbLocation)
    if (err) return callback(err)

    db.on('error', function (err) {
      console.error('LEVELDB ERROR: [' + err + ']')
      console.error(err.stack)
      process.exit(1)
    })

    db = sublevel(db)
    db = ttl(db, { checkFrequency: 1000 })

    var dbs = {
        root : db
      , log  : db.sublevel('log')
    }

    liveStream(dbs.log, { old: false }).on('data', function (data) {
      var d = JSON.parse(data.value)
      console.log(d.time, d.url, d.remoteHost, d.headers.referer || '-')
    })

    return callback(null, dbs)
  })
Example #14
0
function testOrgReport () {
  var db = sublevel(levelup(path.join(__dirname, 'db2'), {valueEncoding: 'json'}))
  
  db.get('meta', function (e, info) {
    // getRepos('nodejs', function (e, repos) {
      // if (e) throw e
      var count = 0
      repos.forEach(function (repo) {
        var sub = db.sublevel('nodejs/'+repo, {valueEncoding: 'json'})
        sub.createValueStream().on('data', v => {
          report(v, [], info.starttime, info.endtime)
         }).on('end', function () {
          count += 1
          if (count === repos.length) {
            var totals = thisMonth.total.activity
            for (var key in thisMonth) {
              var totals = thisMonth[key].activity
              var tops = top(totals).slice(0, 10)
              var line = tops.map(person => `${person} (**${totals[person]}**)`).join('\n * ')
              console.log(`* Top ${key}: \n * ${line}`)
            }
          }
        })
      })
    // }) 
  })
}
Example #15
0
function Queue(db, worker, options) {
  var q = this;
  EventEmitter.call(this);

  if (typeof options == 'number') options = { maxConcurrency: options };
  options = xtend(defaultOptions, options);

  this._options        = options;
  this._db             = db = Sublevel(db);
  this._work           = db.sublevel('work');
  this._pending        = db.sublevel('pending');
  this._worker         = worker;
  this._concurrency    = 0;

  // flags
  this._starting   = true;
  this._flushing   = false;
  this._peeking    = false;
  this._needsFlush = false;
  this._needsDrain = true;

  // hooks
  Hooks(this._work);
  this._work.hooks.post(function() {
    maybeFlush(q)
  });


  start(this);
}
Example #16
0
  , setup = function (db, options) {
      if (db._ttl)
        return

      options = xtend({
          methodPrefix   : ''
        , sublevel       : 'ttl'
        , checkFrequency : DEFAULT_FREQUENCY
      }, options)

      db = sublevel(db)

      db._ttl = {
          put   : db.put
        , del   : db.del
        , batch : db.batch
        , close : db.close
        , sub   : db.sublevel(options.sublevel)
      }

      db[options.methodPrefix + 'put']   = put.bind(null, db)
      db[options.methodPrefix + 'del']   = del.bind(null, db)
      db[options.methodPrefix + 'batch'] = batch.bind(null, db)
      // we must intercept close()
      db['close']                        = close.bind(null, db)

      startTtl(db, options.checkFrequency)

      return db
    }
test('multilevel should not blow up when using live stream overlapping a sublevel',function(t){

  var db = sublevel(level('./_testdb'));
  var subdb = db.sublevel('s');

  livestream.install(db);

  db.pre(function(ch,add){
    if(ch.type == "put") add({type:"put",key:ch.key,value:ch.value,prefix:subdb});
  });

  multilevel.writeManifest(db, __dirname + '/manifest.json');

  var manifest = require('./manifest.json');
  var client = multilevel.client(manifest);
  var server = multilevel.server(db);

  server.pipe(client.createRpcStream()).pipe(server);

  client.liveStream().on('data',function(change){

    t.ok(change,'should not have crashed');

    t.end();
    
  });

  db.put('z',1);

});
function setup (cb) {
  // contains metadata of these users
  var users = require('./fixtures/npm-users-tj-thlorenz-substack-dominictarr-raynos.json')

  // contains byOwner indexes for all batched packages
    , owners = require('./fixtures/byOwner-tj-thlorenz-substack-dominictarr-raynos.json')
    , usersBatch = users.map(toPut)
    , byOwnerBatch = owners.map(toPut).map(function (x) { return xtend(x, { valueEncoding: 'utf8' }) })
    , packagesBatch = getPackagesBatch()

  var db = sublevel(level({ mem: true })(null, { valueEncoding: 'json' }))

  var subnpm      =  sublevels(db).npm
    , npmPackages =  subnpm.packages
    , npmUsers    =  subnpm.users
    , npmByOwner  =  subnpm.byOwner


  npmPackages.batch(packagesBatch, function (err) {
    if (err) return cb(err);
    npmByOwner.batch(byOwnerBatch, function (err) {
      if (err) return cb(err);
      npmUsers.batch(usersBatch, function (err) {
        if (err) return cb(err);
        cb(null, db);
      })
    })
  })
}
Example #19
0
// put fake data in the database!
function initializeDb(db) {
    var pets = []

    pets.push({ name: "Tobi", id: "0" })
    pets.push({ name: "Loki", id: "1" })
    pets.push({ name: "Jane", id: "2" })
    pets.push({ name: "Raul", id: "3" })

    var users = []

    users.push({ name: "TJ", pets: [pets[0], pets[1], pets[2]], id: "0"  })
    users.push({ name: "Guillermo", pets: [pets[3]], id: "1" })
    users.push({ name: "Nathan", pets: [], id: "2" })

    db = SubLevel(db)

    var usersDb = db.sublevel("users")
    var petsDb = db.sublevel("pets")

    petsDb.put(pets[0].id, pets[0])
    petsDb.put(pets[1].id, pets[1])
    petsDb.put(pets[2].id, pets[2])
    petsDb.put(pets[3].id, pets[3])

    usersDb.put(users[0].id, users[0])
    usersDb.put(users[1].id, users[1])
    usersDb.put(users[2].id, users[2])
}
Example #20
0
 levelup(config.leveldb, (err, db) => {
     self.db = sublevel(db);
     self.dbMarkets = self.db.sublevel('markets');
     self.populateMarkets(self.dbMarkets, (err) => {
         if (err) return callback(err);
         return callback(null);
     });
 });
Example #21
0
VhostChecker.prototype.initCache = function () {
  if (this.options.noCache) return;
  var options = this.options;
  var self = this;
  this.cache = sublevel(level(options.cachePath, {
    valueEncoding: 'json'
  })).sublevel(this.vhost);
};
Example #22
0
 it('supports sublevel', function(done) {
   client = Client('tcp+msgpack://localhost:8060', {
     skiff: {dbPath: dbPath},
     port: 7010
   });
   client = sublevel(client);
   client = client.sublevel('level1');
   done();
 });
Example #23
0
var Brain = module.exports = function Brain(opts)
{
	assert(opts && _.isObject(opts), 'you must pass an options object');
	assert(opts.dbpath && _.isString(opts.dbpath), 'you must pass a `dbpath` option');

	this.db = sublevel(level(opts.dbpath, { valueEncoding: 'json' }));
	this.plugindbs = {};
	this.opts = opts;
};
Example #24
0
levelup('PIT', {db: memDown, valueEncoding: 'json'}, function (err, db) {

  // set up our LevelUP instance
  db = ttl(sublevel(db))
  PIT.db = db


  // ... use the database
})
	function getBjorling(done) {
		db = sub(level(dbPath))
		__bjorling = db.sublevel('__bjorling')
		__bjorling.get('aProjection', function(err, result) {
			if(err) return done(err)
			bjorlingEntry = result
			done()
		})
	}
test('\nunfiltered - empty db', function (t) {
  var db = sublevel(level({ mem: true })(null, { valueEncoding: 'json' }))

  getLogins(db, function (err, logins) {
    if (err) return console.error(err);
    t.deepEqual(logins.byOwner, {}, 'empty byOwner')
    t.deepEqual(logins.all, [], 'empty all')
    t.end()
  });
})
test('\nempty db - filtered', function (t) {
  var db = sublevel(level({ mem: true })(null, { valueEncoding: 'json' }))

  getLogins(db, [ 'tjholowaychuk', 'thlorenz', 'substack' ], function (err, logins) {
    if (err) return console.error(err);
    t.deepEqual(logins.byOwner, {}, 'empty byOwner')
    t.deepEqual(logins.all, [], 'empty all')
    t.end()
  });
})
Example #28
0
  beforeEach(function(done) {
    //rimraf.sync(dbPath);
    var dbPath = [
      path.join(__dirname, '..', 'data', 'test-db1'),
      path.join(__dirname, '..', 'data', 'test-db2')
    ];

    db1 = sub(level(dbPath[0], { valueEncoding: 'json' }));
    db2 = level(dbPath[1], { valueEncoding: 'json' }, done);
  });
Example #29
0
  beforeEach(function()  {

    var mockSubDb = this.sinon.mock(SubDb);
    var mockedLevelUp = this.sinon.spy(levelup.exports, 'LevelUP');
    var db = sublevel(mockedLevelUp);
    this.sinon.stub(db, 'sublevel', function(){
      return mockSubDb;
    });
        
  })
 self.onmessage = function(event) {
   var data = event.data
   if (data.dbName) {
     level = voxelLevel(sublevel(levelup(data.dbName, {
       db: leveljs
     })))
     return self.postMessage({ready: true})
   }
   loadChunk(data.worldID, data.position, data.gameChunkSize, data.seed)
 }