How to use node-lmdb - 10 common examples

To help you get started, we’ve selected a few node-lmdb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bitpay / bwdb / lib / writer-worker.js View on Github external
WriterWorker.prototype._pruneWalletBlocks = function(callback) {
  /* jshint maxstatements: 30 */
  var txn = this.db.env.beginTxn();

  var self = this;
  var cursor = new lmdb.Cursor(txn, this.db.blocks);
  var found = cursor.goToLast();
  if (found === null) {
    return abort();
  }

  var currentHeight;
  var pruneHeight = 0;
  cursor.getCurrentBinary(function(key, value) {
    var walletBlock = models.WalletBlock.fromBuffer(key, value);
    pruneHeight = Math.max(0, walletBlock.height - self.pruneDepth);
    log.info('Pruning wallet blocks from height', pruneHeight);
  });

  var pruneKey = models.WalletBlock.getKey(pruneHeight);
  found = cursor.goToKey(pruneKey);
github bitpay / bwdb / lib / web-workers.js View on Github external
WebWorker.prototype.getWalletUTXOs = function(walletId, options, callback) {
  assert(Buffer.isBuffer(walletId), '"walletId" is expected to be a buffer');
  var self = this;
  var txn = this.db.env.beginTxn({readOnly: true});

  var utxos = [];

  var cursor = new lmdb.Cursor(txn, this.db.utxos);

  var start = models.WalletUTXO.getKey(walletId, NULL_TXID, 0);
  var found = cursor.goToRange(start);

  function iterator() {
    cursor.getCurrentBinary(function(key, value) {
      var utxo = models.WalletUTXO.fromBuffer(key, value, self.network);
      utxos.push(utxo);

      var nextFound = cursor.goToNext();
      if (nextFound && utxos.length < options.limit) {
        // TODO make sure maximum call stack is not reached
        iterator();
      } else {
        cursor.close();
        var result = {
github bitpay / bwdb / lib / writer-worker.js View on Github external
WriterWorker.prototype._loadLatestWalletBlock = function(callback) {
  var self = this;
  var txn = this.db.env.beginTxn({readOnly: true});
  var cursor = new lmdb.Cursor(txn, this.db.blocks);
  var found = cursor.goToLast();

  if (found === null) {
    // we will create the wallet later
    callback();
  } else {
    cursor.getCurrentBinary(function(key, value) {
      self.walletBlock = models.WalletBlock.fromBuffer(key, value);

      self.blockFilter = new BlockFilter({
        network: self.network,
        addressFilter: self.walletBlock.addressFilter
      });

      cursor.close();
      txn.abort();
github nimiq / jungle-db / src / main / backend / lmdb / JungleDB.js View on Github external
connect() {
        if (this._db) return Promise.resolve(this._db);

        // Ensure existence of directory.
        if (!fs.existsSync(this._databaseDir)){
            fs.mkdirSync(this._databaseDir);
        }

        let numDbs = 1;
        for (const { /** @type {LMDBBackend} */ backend, upgradeCondition } of this._objectStoreBackends) {
            numDbs += 1 + backend.indices.size;
        }

        this._db = new lmdb.Env();
        this._db.open({
            path: this._databaseDir,
            mapSize: this._options.maxDbSize || (1024*1024*5), // 5MB default
            maxDbs: (this._options.maxDbs + 1) || numDbs, // default, always add 1 for the internal db
            useWritemap: this._options.useWritemap || false,
        });

        // Check if resize is needed.
        if (this.autoResize && this.needsResize()) {
            this.doResize();
        }

        this._mainDb = this._db.openDbi({
            name: null,
            create: true
        });
github josephg / sephsplace / server.js View on Github external
const url = require('url')
const WSS = require('ws').Server;
const express = require('express')
const fs = require('fs')

const kclient = new kafka.Client()
const app = express()
const server = require('http').createServer(app)
const wss = new WSS({server, perfMessageDeflate: false})

app.use('/sp', express.static(__dirname + '/public'))

// This is important so we can hot-resume when the server starts without
// needing to read the entire kafka log. A file would almost be good enough,
// but we need to atomically write to it. So, this is easier.
const dbenv = new lmdb.Env()

if (!fs.existsSync('snapshot')) fs.mkdirSync('snapshot')
dbenv.open({ path: 'snapshot', mapSize: 100*1024*1024 })
const snapshotdb = dbenv.openDbi({create: true})


const randInt = max => (Math.random() * max) | 0

const loadSnapshot = () => {
  // Read a snapshot from the database if we can.
  const txn = dbenv.beginTxn({readOnly: true})

  const _version = txn.getNumber(snapshotdb, 'version')
  if (_version != null) {
    const data = txn.getBinary(snapshotdb, 'current')
    assert(data)
github peeriodproject / core / src / core / topology / BucketStore.js View on Github external
BucketStore.prototype._getCursor = function (txn) {
        return new lmdb.Cursor(txn, this._databaseInstance);
    };
github bitpay / bwdb / lib / web-workers.js View on Github external
WebWorker.prototype._updateLatestTip = function() {
  var self = this;
  var txn = this.db.env.beginTxn({readOnly: true});

  var cursor = new lmdb.Cursor(txn, this.db.blocks);
  var found = cursor.goToLast();
  if (found !== null) {
    cursor.getCurrentBinary(function(key, value) {
      var walletBlock = models.WalletBlock.fromBuffer(key, value);
      self.bitcoinHeight = walletBlock.height;
      self.bitcoinHash = walletBlock.blockHash.toString('hex');
      cursor.close();
      txn.abort();
    });
  } else {
    cursor.close();
    txn.abort();
    log.info('Active syncing is idle, there are currently no wallets');
  }
};
github bitpay / bwdb / lib / web-workers.js View on Github external
WebWorker.prototype._getLatestTxids = function(txn, walletId, options, callback) {
  assert(Buffer.isBuffer(walletId), '"walletId" is expected to be a buffer');
  var txids = [];

  try {
    options = validators.sanitizeRangeOptions(options);
  } catch(err) {
    return callback(err);
  }

  var cursor = new lmdb.Cursor(txn, this.db.txids);

  var start = models.WalletTxid.create(walletId, options.height, options.index);
  var found = cursor.goToRange(start.getKey());

  if (found) {
    iterate();
  } else {
    cursor.close();
    callback(null, {
      txids: txids,
      start: {
        height: options.height,
        index: options.index
      }
    });
  }
github tinkerhub / tinkerhub / lib / storage / index.js View on Github external
constructor(path) {
		debug('Opening storage at ' + path);

		mkdirp.sync(path);

		this._env = new lmdb.Env();
		this._env.open({
			path: path,
		});

		this._db = this._env.openDbi({
			name: 'shared',
			create: true
		});
	}
github polkadot-js / common / packages / db / src / engines / LmDb.ts View on Github external
constructor (base: string, name: string, options?: BaseDbOptions) {
    this._env = new lmdb.Env();
    this._path = path.join(base, name);
    this._rtxn = null;
    this._wtxn = null;

    mkdirp.sync(this._path);

    const dbsize = this.size(true);
    const mapSize = Math.ceil(1 + (dbsize / GB)) * GB;

    l.debug(() => `Current mapsize set to ${(mapSize / GB).toFixed(1)}GB`);

    this._env.open({
      path: this._path,
      mapSize
    });
  }

node-lmdb

Node binding for LMDB, the Lightning Memory-Mapped Database

MIT
Latest version published 8 months ago

Package Health Score

62 / 100
Full package analysis

Popular node-lmdb functions