lowlevelup: automatic dummies.

This commit is contained in:
Christopher Jeffrey 2017-07-12 23:18:38 -07:00
parent 37da047a34
commit cedc54436a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 34 additions and 28 deletions

View File

@ -27,7 +27,6 @@ const ChainEntry = require('./chainentry');
const TXMeta = require('../primitives/txmeta'); const TXMeta = require('../primitives/txmeta');
const U8 = encoding.U8; const U8 = encoding.U8;
const U32 = encoding.U32; const U32 = encoding.U32;
const DUMMY = Buffer.from([0]);
/** /**
* The database backend for the {@link Chain} object. * The database backend for the {@link Chain} object.
@ -1318,7 +1317,7 @@ ChainDB.prototype._save = async function save(entry, block, view) {
// Tip index. // Tip index.
this.del(layout.p(entry.prevBlock)); this.del(layout.p(entry.prevBlock));
this.put(layout.p(hash), DUMMY); this.put(layout.p(hash), null);
// Update state caches. // Update state caches.
this.saveUpdates(); this.saveUpdates();
@ -1520,7 +1519,7 @@ ChainDB.prototype.reset = async function reset(block) {
// Revert the tip index. // Revert the tip index.
this.del(layout.p(tip.hash)); this.del(layout.p(tip.hash));
this.put(layout.p(tip.prevBlock), DUMMY); this.put(layout.p(tip.prevBlock), null);
// Remove all records (including // Remove all records (including
// main-chain-only records). // main-chain-only records).
@ -1859,7 +1858,7 @@ ChainDB.prototype.indexTX = function indexTX(tx, view, entry, index) {
if (this.options.indexAddress) { if (this.options.indexAddress) {
let hashes = tx.getHashes(view); let hashes = tx.getHashes(view);
for (let addr of hashes) for (let addr of hashes)
this.put(layout.T(addr, hash), DUMMY); this.put(layout.T(addr, hash), null);
} }
} }
@ -1885,7 +1884,7 @@ ChainDB.prototype.indexTX = function indexTX(tx, view, entry, index) {
if (!addr) if (!addr)
continue; continue;
this.put(layout.C(addr, hash, i), DUMMY); this.put(layout.C(addr, hash, i), null);
} }
}; };
@ -1919,7 +1918,7 @@ ChainDB.prototype.unindexTX = function unindexTX(tx, view) {
if (!addr) if (!addr)
continue; continue;
this.put(layout.C(addr, prevout.hash, prevout.index), DUMMY); this.put(layout.C(addr, prevout.hash, prevout.index), null);
} }
} }

View File

@ -10,12 +10,14 @@
const assert = require('assert'); const assert = require('assert');
const Lock = require('../utils/lock'); const Lock = require('../utils/lock');
const co = require('../utils/co'); const co = require('../utils/co');
const LOW = Buffer.from([0x00]);
const HIGH = Buffer.from([0xff]);
let VERSION_ERROR; let VERSION_ERROR;
/** /**
* Extremely low-level version of levelup. * Extremely low-level version of levelup.
* The only levelup feature it provides is
* error-wrapping.
* *
* This avoids pulling in extra deps and * This avoids pulling in extra deps and
* lowers memory usage. * lowers memory usage.
@ -288,6 +290,9 @@ LowlevelUp.prototype.get = function get(key) {
*/ */
LowlevelUp.prototype.put = function put(key, value) { LowlevelUp.prototype.put = function put(key, value) {
if (!value)
value = LOW;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.loaded) { if (!this.loaded) {
reject(new Error('Database is closed.')); reject(new Error('Database is closed.'));
@ -396,10 +401,10 @@ LowlevelUp.prototype.approximateSize = function approximateSize(start, end) {
LowlevelUp.prototype.compactRange = function compactRange(start, end) { LowlevelUp.prototype.compactRange = function compactRange(start, end) {
if (!start) if (!start)
start = Buffer.from([0x00]); start = LOW;
if (!end) if (!end)
end = Buffer.from([0xff]); end = HIGH;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.loaded) { if (!this.loaded) {
@ -564,8 +569,8 @@ LowlevelUp.prototype.dump = async function dump() {
let records = {}; let records = {};
let items = await this.range({ let items = await this.range({
gte: Buffer.from([0x00]), gte: LOW,
lte: Buffer.from([0xff]) lte: HIGH
}); });
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
@ -678,7 +683,11 @@ function Batch(db) {
*/ */
Batch.prototype.put = function put(key, value) { Batch.prototype.put = function put(key, value) {
if (!value)
value = LOW;
this.batch.put(key, value); this.batch.put(key, value);
return this; return this;
}; };

View File

@ -24,7 +24,6 @@ const Script = require('../script/script');
const BlockMapRecord = records.BlockMapRecord; const BlockMapRecord = records.BlockMapRecord;
const OutpointMapRecord = records.OutpointMapRecord; const OutpointMapRecord = records.OutpointMapRecord;
const TXRecord = records.TXRecord; const TXRecord = records.TXRecord;
const DUMMY = Buffer.from([0]);
/** /**
* TXDB * TXDB
@ -299,7 +298,7 @@ TXDB.prototype.saveCredit = async function saveCredit(credit, path) {
await this.addOutpointMap(coin.hash, coin.index); await this.addOutpointMap(coin.hash, coin.index);
this.put(layout.c(coin.hash, coin.index), raw); this.put(layout.c(coin.hash, coin.index), raw);
this.put(layout.C(path.account, coin.hash, coin.index), DUMMY); this.put(layout.C(path.account, coin.hash, coin.index), null);
this.coinCache.push(key, raw); this.coinCache.push(key, raw);
}; };
@ -763,7 +762,7 @@ TXDB.prototype._add = async function add(tx, block) {
// We need to index every spender // We need to index every spender
// hash to detect "passive" // hash to detect "passive"
// replace-by-fee. // replace-by-fee.
this.put(layout.r(hash), DUMMY); this.put(layout.r(hash), null);
return; return;
} }
@ -919,24 +918,24 @@ TXDB.prototype.insert = async function insert(wtx, block) {
// Save and index the transaction record. // Save and index the transaction record.
this.put(layout.t(hash), wtx.toRaw()); this.put(layout.t(hash), wtx.toRaw());
this.put(layout.m(wtx.ps, hash), DUMMY); this.put(layout.m(wtx.ps, hash), null);
if (!block) if (!block)
this.put(layout.p(hash), DUMMY); this.put(layout.p(hash), null);
else else
this.put(layout.h(height, hash), DUMMY); this.put(layout.h(height, hash), null);
// Do some secondary indexing for account-based // Do some secondary indexing for account-based
// queries. This saves us a lot of time for // queries. This saves us a lot of time for
// queries later. // queries later.
for (let account of details.accounts) { for (let account of details.accounts) {
this.put(layout.T(account, hash), DUMMY); this.put(layout.T(account, hash), null);
this.put(layout.M(account, wtx.ps, hash), DUMMY); this.put(layout.M(account, wtx.ps, hash), null);
if (!block) if (!block)
this.put(layout.P(account, hash), DUMMY); this.put(layout.P(account, hash), null);
else else
this.put(layout.H(account, height, hash), DUMMY); this.put(layout.H(account, height, hash), null);
} }
// Update block records. // Update block records.
@ -1105,12 +1104,12 @@ TXDB.prototype._confirm = async function confirm(wtx, block) {
// updated. Also reindex for height. // updated. Also reindex for height.
this.put(layout.t(hash), wtx.toRaw()); this.put(layout.t(hash), wtx.toRaw());
this.del(layout.p(hash)); this.del(layout.p(hash));
this.put(layout.h(height, hash), DUMMY); this.put(layout.h(height, hash), null);
// Secondary indexing also needs to change. // Secondary indexing also needs to change.
for (let account of details.accounts) { for (let account of details.accounts) {
this.del(layout.P(account, hash)); this.del(layout.P(account, hash));
this.put(layout.H(account, height, hash), DUMMY); this.put(layout.H(account, height, hash), null);
} }
if (block) { if (block) {
@ -1431,12 +1430,12 @@ TXDB.prototype.disconnect = async function disconnect(wtx, block) {
// block properties and reindex due // block properties and reindex due
// to the height change. // to the height change.
this.put(layout.t(hash), wtx.toRaw()); this.put(layout.t(hash), wtx.toRaw());
this.put(layout.p(hash), DUMMY); this.put(layout.p(hash), null);
this.del(layout.h(height, hash)); this.del(layout.h(height, hash));
// Secondary indexing also needs to change. // Secondary indexing also needs to change.
for (let account of details.accounts) { for (let account of details.accounts) {
this.put(layout.P(account, hash), DUMMY); this.put(layout.P(account, hash), null);
this.del(layout.H(account, height, hash)); this.del(layout.H(account, height, hash));
} }

View File

@ -38,7 +38,6 @@ const PathMapRecord = records.PathMapRecord;
const OutpointMapRecord = records.OutpointMapRecord; const OutpointMapRecord = records.OutpointMapRecord;
const TXRecord = records.TXRecord; const TXRecord = records.TXRecord;
const U32 = encoding.U32; const U32 = encoding.U32;
const DUMMY = Buffer.from([0]);
/** /**
* WalletDB * WalletDB
@ -1256,7 +1255,7 @@ WalletDB.prototype.savePath = async function savePath(wallet, path) {
batch.put(layout.P(wid, hash), path.toRaw()); batch.put(layout.P(wid, hash), path.toRaw());
// Wallet ID + Account Index + Address Hash -> Dummy // Wallet ID + Account Index + Address Hash -> Dummy
batch.put(layout.r(wid, path.account, hash), DUMMY); batch.put(layout.r(wid, path.account, hash), null);
}; };
/** /**