walletdb: varint serialization.
This commit is contained in:
parent
e07f9b737c
commit
827ab4b539
@ -244,14 +244,12 @@ function BlockMapRecord(height) {
|
|||||||
|
|
||||||
BlockMapRecord.prototype.fromRaw = function fromRaw(data) {
|
BlockMapRecord.prototype.fromRaw = function fromRaw(data) {
|
||||||
var p = new BufferReader(data);
|
var p = new BufferReader(data);
|
||||||
var i, hash, tx, count;
|
var count = p.readVarint();
|
||||||
|
var i, hash, tx;
|
||||||
|
|
||||||
while (p.left()) {
|
for (i = 0; i < count; i++) {
|
||||||
hash = p.readHash('hex');
|
hash = p.readHash('hex');
|
||||||
tx = new TXMapRecord(hash);
|
tx = TXMapRecord.fromRaw(hash, p);
|
||||||
count = p.readVarint();
|
|
||||||
for (i = 0; i < count; i++)
|
|
||||||
tx.wids.push(p.readU32());
|
|
||||||
this.txs.push(tx);
|
this.txs.push(tx);
|
||||||
this.index[tx.hash] = tx;
|
this.index[tx.hash] = tx;
|
||||||
}
|
}
|
||||||
@ -278,14 +276,14 @@ BlockMapRecord.fromRaw = function fromRaw(height, data) {
|
|||||||
|
|
||||||
BlockMapRecord.prototype.toRaw = function toRaw(writer) {
|
BlockMapRecord.prototype.toRaw = function toRaw(writer) {
|
||||||
var p = new BufferWriter(writer);
|
var p = new BufferWriter(writer);
|
||||||
var i, j, tx;
|
var i, tx;
|
||||||
|
|
||||||
|
p.writeVarint(this.txs.length);
|
||||||
|
|
||||||
for (i = 0; i < this.txs.length; i++) {
|
for (i = 0; i < this.txs.length; i++) {
|
||||||
tx = this.txs[i];
|
tx = this.txs[i];
|
||||||
p.writeHash(tx.hash);
|
p.writeHash(tx.hash);
|
||||||
p.writeVarint(tx.wids.length);
|
tx.toRaw(p);
|
||||||
for (j = 0; j < tx.wids.length; j++)
|
|
||||||
p.writeU32(tx.wids[j]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!writer)
|
if (!writer)
|
||||||
@ -362,6 +360,19 @@ TXMapRecord.prototype.remove = function remove(wid) {
|
|||||||
return utils.binaryRemove(this.wids, wid, cmp);
|
return utils.binaryRemove(this.wids, wid, cmp);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TXMapRecord.prototype.toRaw = function toRaw(writer) {
|
||||||
|
return serializeWallets(this.wids, writer);
|
||||||
|
};
|
||||||
|
|
||||||
|
TXMapRecord.prototype.fromRaw = function fromRaw(data) {
|
||||||
|
this.wids = parseWallets(data);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
TXMapRecord.fromRaw = function fromRaw(hash, data) {
|
||||||
|
return new TXMapRecord(hash).fromRaw(data);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outpoint Map
|
* Outpoint Map
|
||||||
* @constructor
|
* @constructor
|
||||||
@ -381,8 +392,8 @@ OutpointMapRecord.prototype.remove = function remove(wid) {
|
|||||||
return utils.binaryRemove(this.wids, wid, cmp);
|
return utils.binaryRemove(this.wids, wid, cmp);
|
||||||
};
|
};
|
||||||
|
|
||||||
OutpointMapRecord.prototype.toRaw = function toRaw() {
|
OutpointMapRecord.prototype.toRaw = function toRaw(writer) {
|
||||||
return serializeWallets(this.wids);
|
return serializeWallets(this.wids, writer);
|
||||||
};
|
};
|
||||||
|
|
||||||
OutpointMapRecord.prototype.fromRaw = function fromRaw(data) {
|
OutpointMapRecord.prototype.fromRaw = function fromRaw(data) {
|
||||||
@ -412,8 +423,8 @@ PathMapRecord.prototype.remove = function remove(wid) {
|
|||||||
return utils.binaryRemove(this.wids, wid, cmp);
|
return utils.binaryRemove(this.wids, wid, cmp);
|
||||||
};
|
};
|
||||||
|
|
||||||
PathMapRecord.prototype.toRaw = function toRaw() {
|
PathMapRecord.prototype.toRaw = function toRaw(writer) {
|
||||||
return serializeWallets(this.wids);
|
return serializeWallets(this.wids, writer);
|
||||||
};
|
};
|
||||||
|
|
||||||
PathMapRecord.prototype.fromRaw = function fromRaw(data) {
|
PathMapRecord.prototype.fromRaw = function fromRaw(data) {
|
||||||
@ -439,24 +450,31 @@ function cmpid(a, b) {
|
|||||||
|
|
||||||
function parseWallets(data) {
|
function parseWallets(data) {
|
||||||
var p = new BufferReader(data);
|
var p = new BufferReader(data);
|
||||||
|
var count = p.readVarint();
|
||||||
var wids = [];
|
var wids = [];
|
||||||
|
var i;
|
||||||
|
|
||||||
while (p.left())
|
for (i = 0; i < count; i++)
|
||||||
wids.push(p.readU32());
|
wids.push(p.readU32());
|
||||||
|
|
||||||
return wids;
|
return wids;
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeWallets(wids) {
|
function serializeWallets(wids, writer) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter(writer);
|
||||||
var i, wid;
|
var i, wid;
|
||||||
|
|
||||||
|
p.writeVarint(wids.length);
|
||||||
|
|
||||||
for (i = 0; i < wids.length; i++) {
|
for (i = 0; i < wids.length; i++) {
|
||||||
wid = wids[i];
|
wid = wids[i];
|
||||||
p.writeU32(wid);
|
p.writeU32(wid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.render();
|
if (!writer)
|
||||||
|
p = p.render();
|
||||||
|
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -3436,13 +3436,15 @@ BlockRecord.prototype.remove = function remove(hash) {
|
|||||||
|
|
||||||
BlockRecord.prototype.fromRaw = function fromRaw(data) {
|
BlockRecord.prototype.fromRaw = function fromRaw(data) {
|
||||||
var p = new BufferReader(data);
|
var p = new BufferReader(data);
|
||||||
var hash;
|
var i, hash, count;
|
||||||
|
|
||||||
this.hash = p.readHash('hex');
|
this.hash = p.readHash('hex');
|
||||||
this.height = p.readU32();
|
this.height = p.readU32();
|
||||||
this.ts = p.readU32();
|
this.ts = p.readU32();
|
||||||
|
|
||||||
while (p.left()) {
|
count = p.readVarint();
|
||||||
|
|
||||||
|
for (i = 0; i < count; i++) {
|
||||||
hash = p.readHash('hex');
|
hash = p.readHash('hex');
|
||||||
this.index[hash] = true;
|
this.index[hash] = true;
|
||||||
this.hashes.push(hash);
|
this.hashes.push(hash);
|
||||||
@ -3475,6 +3477,8 @@ BlockRecord.prototype.toRaw = function toRaw(writer) {
|
|||||||
p.writeU32(this.height);
|
p.writeU32(this.height);
|
||||||
p.writeU32(this.ts);
|
p.writeU32(this.ts);
|
||||||
|
|
||||||
|
p.writeVarint(this.hashes.length);
|
||||||
|
|
||||||
for (i = 0; i < this.hashes.length; i++)
|
for (i = 0; i < this.hashes.length; i++)
|
||||||
p.writeHash(this.hashes[i]);
|
p.writeHash(this.hashes[i]);
|
||||||
|
|
||||||
|
|||||||
@ -112,6 +112,50 @@ var indexPaths = co(function* indexPaths() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var patchPathMaps = co(function* patchPathMaps() {
|
||||||
|
var i, items, item, hash, wids;
|
||||||
|
|
||||||
|
items = yield db.range({
|
||||||
|
gte: new Buffer('70' + constants.NULL_HASH, 'hex'), // p
|
||||||
|
lte: new Buffer('70' + constants.HIGH_HASH, 'hex') // p
|
||||||
|
});
|
||||||
|
|
||||||
|
for (i = 0; i < items.length; i++) {
|
||||||
|
item = items[i];
|
||||||
|
hash = item.key.toString('hex', 1);
|
||||||
|
wids = parseWallets(item.value);
|
||||||
|
console.log('p[%s] -> varint(%d)', hash, wids.length);
|
||||||
|
batch.put(item.key, serializeWallets(wids));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function parseWallets(data) {
|
||||||
|
var p = new BufferReader(data);
|
||||||
|
var wids = [];
|
||||||
|
|
||||||
|
while (p.left())
|
||||||
|
wids.push(p.readU32());
|
||||||
|
|
||||||
|
return wids;
|
||||||
|
}
|
||||||
|
|
||||||
|
function serializeWallets(wids, writer) {
|
||||||
|
var p = new BufferWriter(writer);
|
||||||
|
var i, wid;
|
||||||
|
|
||||||
|
p.writeVarint(wids.length);
|
||||||
|
|
||||||
|
for (i = 0; i < wids.length; i++) {
|
||||||
|
wid = wids[i];
|
||||||
|
p.writeU32(wid);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!writer)
|
||||||
|
p = p.render();
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
function accountToRaw(account) {
|
function accountToRaw(account) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
var i, key;
|
var i, key;
|
||||||
@ -192,6 +236,7 @@ co.spawn(function* () {
|
|||||||
yield wipeTXDB();
|
yield wipeTXDB();
|
||||||
yield patchAccounts();
|
yield patchAccounts();
|
||||||
yield indexPaths();
|
yield indexPaths();
|
||||||
|
yield patchPathMaps();
|
||||||
yield batch.write();
|
yield batch.write();
|
||||||
yield db.close();
|
yield db.close();
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user