forked from booo/node-bitcoin-p2p
-
Notifications
You must be signed in to change notification settings - Fork 10
/
db-test.js
77 lines (70 loc) · 1.95 KB
/
db-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var leveldown = require('leveldown'); // database
var Settings = require('./lib/settings').Settings;
if (process.argv.length <= 2) {
console.log('No key given');
process.exit(1);
}
var rawKey = process.argv[2];
cfg = new Settings();
var dataDir = cfg.getDataDir();
storageUri = dataDir + '/leveldb/';
hMain = leveldown(storageUri+'main.db');
var defaultCreateOpts = {
createIfMissing: true,
cacheSize: 100 * 1024 * 1024,
keyEncoding: 'json',
valueEncoding: 'json'
};
var dataLoop = function dataLoop(i, limit) {
//console.log('loop', limit);
if (limit > 10000) return false; // runaway process trap
i.next(function(err, key, value) {
if (err !== null) {
console.log('err:', err);
return false;
}
console.log('key:', key, 'value:', value);
if (key == 'undefined') return false;
dataLoop(i, limit+1);
});
}
// From lib/storage.js
var formatHeightKey = function formatHeightKey(height) {
var tempHeightBuffer = new Buffer(4);
height = Math.floor(+height);
tempHeightBuffer[0] = height >> 24 & 0xff;
tempHeightBuffer[1] = height >> 16 & 0xff;
tempHeightBuffer[2] = height >> 8 & 0xff;
tempHeightBuffer[3] = height & 0xff;
return tempHeightBuffer;
};
hMain.open(defaultCreateOpts, function (err, value) {
if (err !== undefined) {
console.log('err:', err);
process.exit(1);
}
if (rawKey.length > 20) {
// Full hash
var key = new Buffer(rawKey, 'ascii').fromHex().reverse();
console.log('key:', key);
hMain.get(key, function (err, value) {
console.log('err:', JSON.stringify(err));
console.log('value:', JSON.stringify(value));
});
} else if (parseInt(rawKey) > 0) {
// Block height
var i = hMain.iterator({
'limit': 50,
'start': formatHeightKey(rawKey)
});
dataLoop(i, 0);
} else {
// String key
var key = rawKey;
console.log('key:', key);
hMain.get(key, function (err, value) {
console.log('err:', JSON.stringify(err));
console.log('value:', JSON.stringify(value));
});
}
});