Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-lmdb' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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);
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 = {
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();
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
});
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)
BucketStore.prototype._getCursor = function (txn) {
return new lmdb.Cursor(txn, this._databaseInstance);
};
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');
}
};
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
}
});
}
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
});
}
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
});
}