Comparing version 0.2.3 to 1.0.0
@@ -9,2 +9,3 @@ /** | ||
const assert = require('bsert'); | ||
const DB = require('./db'); | ||
@@ -19,4 +20,12 @@ const Key = require('./key'); | ||
exports.create = (options) => { | ||
const {memory, location} = options || {}; | ||
if (options == null) | ||
options = {}; | ||
if (typeof options === 'string') | ||
options = { location: options }; | ||
assert(options && typeof options === 'object'); | ||
const {memory, location} = options; | ||
if (memory) | ||
@@ -23,0 +32,0 @@ return new DB(MemDB, 'memory', options); |
@@ -56,10 +56,9 @@ /*! | ||
const Backend = this.backend; | ||
const db = new Backend(this.location); | ||
// A lower-level binding. | ||
if (db.binding && typeof db.binding.approximateSize === 'function') { | ||
this.binding = db.binding; | ||
if (Backend.leveldown) { | ||
this.binding = new Backend(this.location); | ||
this.leveldown = true; | ||
} else { | ||
this.binding = db; | ||
this.binding = new Backend(this.location); | ||
} | ||
@@ -1267,6 +1266,2 @@ } | ||
this.memory = false; | ||
this.sync = false; | ||
this.mapSize = 256 * (1024 << 20); | ||
this.writeMap = false; | ||
this.noSubdir = true; | ||
@@ -1339,27 +1334,2 @@ if (options) | ||
if (options.sync != null) { | ||
assert(typeof options.sync === 'boolean', | ||
'`sync` must be a boolean.'); | ||
this.sync = options.sync; | ||
} | ||
if (options.mapSize != null) { | ||
assert(typeof options.mapSize === 'number', | ||
'`mapSize` must be a number.'); | ||
assert(options.mapSize >= 0); | ||
this.mapSize = options.mapSize; | ||
} | ||
if (options.writeMap != null) { | ||
assert(typeof options.writeMap === 'boolean', | ||
'`writeMap` must be a boolean.'); | ||
this.writeMap = options.writeMap; | ||
} | ||
if (options.noSubdir != null) { | ||
assert(typeof options.noSubdir === 'boolean', | ||
'`noSubdir` must be a boolean.'); | ||
this.noSubdir = options.noSubdir; | ||
} | ||
return this; | ||
@@ -1366,0 +1336,0 @@ } |
157
lib/key.js
@@ -103,4 +103,3 @@ /** | ||
size(v) { | ||
assertType(Buffer.isBuffer(v)); | ||
return sizeString(v, null); | ||
return sizeBuffer(v); | ||
}, | ||
@@ -111,9 +110,8 @@ read(k, o) { | ||
write(k, v, o) { | ||
assertType(Buffer.isBuffer(v)); | ||
return writeString(k, v, o, null); | ||
return writeBuffer(k, v, o); | ||
} | ||
}, | ||
hex: { | ||
min: BUFFER_MIN, | ||
max: BUFFER_MAX, | ||
min: BUFFER_MIN.toString('hex'), | ||
max: BUFFER_MAX.toString('hex'), | ||
dynamic: true, | ||
@@ -131,18 +129,18 @@ size(v) { | ||
ascii: { | ||
min: BUFFER_MIN, | ||
max: BUFFER_MAX, | ||
min: BUFFER_MIN.toString('binary'), | ||
max: BUFFER_MAX.toString('binary'), | ||
dynamic: true, | ||
size(v) { | ||
return sizeString(v, 'ascii'); | ||
return sizeString(v, 'binary'); | ||
}, | ||
read(k, o) { | ||
return readString(k, o, 'ascii'); | ||
return readString(k, o, 'binary'); | ||
}, | ||
write(k, v, o) { | ||
return writeString(k, v, o, 'ascii'); | ||
return writeString(k, v, o, 'binary'); | ||
} | ||
}, | ||
utf8: { | ||
min: BUFFER_MIN, | ||
max: BUFFER_MAX, | ||
min: BUFFER_MIN.toString('utf8'), | ||
max: BUFFER_MAX.toString('utf8'), | ||
dynamic: true, | ||
@@ -168,60 +166,2 @@ size(v) { | ||
assertLen(o + 20 <= k.length); | ||
return k.toString('hex', o, o + 20); | ||
}, | ||
write(k, v, o) { | ||
assertType(writeHex(k, v, o) === 20); | ||
return 20; | ||
} | ||
}, | ||
hash256: { | ||
min: Buffer.alloc(32, 0x00), | ||
max: Buffer.alloc(32, 0xff), | ||
dynamic: false, | ||
size(v) { | ||
return 32; | ||
}, | ||
read(k, o) { | ||
assertLen(o + 32 <= k.length); | ||
return k.toString('hex', o, o + 32); | ||
}, | ||
write(k, v, o) { | ||
assertType(writeHex(k, v, o) === 32); | ||
return 32; | ||
} | ||
}, | ||
hash: { | ||
min: Buffer.alloc(1, 0x00), | ||
max: Buffer.alloc(64, 0xff), | ||
dynamic: true, | ||
size(v) { | ||
return 1 + sizeHex(v); | ||
}, | ||
read(k, o) { | ||
assertLen(o + 1 <= k.length); | ||
assertLen(k[o] >= 1 && k[o] <= 64); | ||
assertLen(o + 1 + k[o] <= k.length); | ||
return k.toString('hex', o + 1, o + 1 + k[o]); | ||
}, | ||
write(k, v, o) { | ||
const size = sizeHex(v); | ||
assertType(size >= 1 && size <= 64); | ||
assertLen(o + 1 <= k.length); | ||
k[o] = size; | ||
assertType(writeHex(k, v, o + 1) === size); | ||
return 1 + size; | ||
} | ||
}, | ||
bhash160: { | ||
min: Buffer.alloc(20, 0x00), | ||
max: Buffer.alloc(20, 0xff), | ||
dynamic: false, | ||
size(v) { | ||
return 20; | ||
}, | ||
read(k, o) { | ||
assertLen(o + 20 <= k.length); | ||
return k.slice(o, o + 20); | ||
@@ -235,3 +175,3 @@ }, | ||
}, | ||
bhash256: { | ||
hash256: { | ||
min: Buffer.alloc(32, 0x00), | ||
@@ -253,3 +193,3 @@ max: Buffer.alloc(32, 0xff), | ||
}, | ||
bhash: { | ||
hash: { | ||
min: Buffer.alloc(1, 0x00), | ||
@@ -357,3 +297,3 @@ max: Buffer.alloc(64, 0xff), | ||
build(id, args) { | ||
encode(id, args) { | ||
assert(Array.isArray(args)); | ||
@@ -380,3 +320,3 @@ | ||
parse(id, key) { | ||
decode(id, key) { | ||
assert(Buffer.isBuffer(key)); | ||
@@ -400,5 +340,2 @@ | ||
if (args.length === 1) | ||
return args[0]; | ||
return args; | ||
@@ -412,3 +349,3 @@ } | ||
} | ||
return this.build(id, args); | ||
return this.encode(id, args); | ||
} | ||
@@ -421,3 +358,3 @@ | ||
} | ||
return this.build(id, args); | ||
return this.encode(id, args); | ||
} | ||
@@ -452,8 +389,8 @@ | ||
build(...args) { | ||
return this.base.build(this.id, args); | ||
encode(...args) { | ||
return this.base.encode(this.id, args); | ||
} | ||
parse(key) { | ||
return this.base.parse(this.id, key); | ||
decode(key) { | ||
return this.base.decode(this.id, key); | ||
} | ||
@@ -490,19 +427,3 @@ | ||
function sizeHex(data) { | ||
if (Buffer.isBuffer(data)) | ||
return data.length; | ||
assertType(typeof data === 'string'); | ||
return data.length >>> 1; | ||
} | ||
function writeHex(data, str, off) { | ||
if (Buffer.isBuffer(str)) | ||
return str.copy(data, off); | ||
assertType(typeof str === 'string'); | ||
return data.write(str, off, 'hex'); | ||
} | ||
function sizeString(v, enc) { | ||
if (Buffer.isBuffer(v)) | ||
return 1 + v.length; | ||
assertType(typeof v === 'string'); | ||
@@ -518,17 +439,3 @@ return 1 + Buffer.byteLength(v, enc); | ||
function readBuffer(k, o) { | ||
assertLen(o + 1 <= k.length); | ||
assertLen(o + 1 + k[o] <= k.length); | ||
return k.slice(o + 1, o + 1 + k[o]); | ||
} | ||
function writeString(k, v, o, enc) { | ||
if (Buffer.isBuffer(v)) { | ||
assertLen(v.length <= 255); | ||
assertLen(o + 1 <= k.length); | ||
k[o] = v.length; | ||
assertLen(v.copy(k, o + 1) === v.length); | ||
return 1 + v.length; | ||
} | ||
assertType(typeof v === 'string'); | ||
@@ -549,5 +456,25 @@ | ||
function sizeBuffer(v) { | ||
assertType(Buffer.isBuffer(v)); | ||
return 1 + v.length; | ||
} | ||
function readBuffer(k, o) { | ||
assertLen(o + 1 <= k.length); | ||
assertLen(o + 1 + k[o] <= k.length); | ||
return k.slice(o + 1, o + 1 + k[o]); | ||
} | ||
function writeBuffer(k, v, o, enc) { | ||
assertType(Buffer.isBuffer(v)); | ||
assertLen(v.length <= 255); | ||
assertLen(o + 1 <= k.length); | ||
k[o] = v.length; | ||
assertLen(v.copy(k, o + 1) === v.length); | ||
return 1 + v.length; | ||
} | ||
function assertLen(ok) { | ||
if (!ok) { | ||
const err = new Error('Invalid length for database key.'); | ||
const err = new RangeError('Invalid length for database key.'); | ||
if (Error.captureStackTrace) | ||
@@ -554,0 +481,0 @@ Error.captureStackTrace(err, assertLen); |
@@ -9,2 +9,5 @@ /** | ||
module.exports = require('leveldown'); | ||
const binding = require('bindings')('leveldown').leveldown; | ||
binding.leveldown = true; | ||
module.exports = binding; |
{ | ||
"name": "bdb", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "Database for bcoin", | ||
@@ -8,5 +8,3 @@ "keywords": [ | ||
"db", | ||
"leveldb", | ||
"leveldown", | ||
"levelup" | ||
"leveldb" | ||
], | ||
@@ -22,2 +20,3 @@ "license": "MIT", | ||
"scripts": { | ||
"install": "node-gyp rebuild", | ||
"lint": "eslint lib/ || exit 0", | ||
@@ -27,4 +26,5 @@ "test": "mocha --reporter spec test/*-test.js" | ||
"dependencies": { | ||
"bsert": "~0.0.3", | ||
"leveldown": "4.0.1" | ||
"bindings": "~1.3.0", | ||
"bsert": "~0.0.4", | ||
"nan": "~2.10.0" | ||
}, | ||
@@ -38,2 +38,3 @@ "devDependencies": { | ||
}, | ||
"gypfile": true, | ||
"browser": { | ||
@@ -40,0 +41,0 @@ "./lib/level": "./lib/level-browser.js" |
# bdb | ||
Database for bcoin. | ||
Database for bcoin (leveldown backend). | ||
@@ -9,13 +9,10 @@ ## Usage | ||
const bdb = require('bdb'); | ||
const db = bdb.create('/path/to/my.db'); | ||
const db = bdb.create({ | ||
location: './mydb' | ||
}); | ||
await db.open(); | ||
const root = bdb.key('r'); | ||
const rec = bdb.key('t', ['hash160', 'uint32']); | ||
const myPrefix = bdb.key('r'); | ||
const myKey = bdb.key('t', ['hash160', 'uint32']); | ||
const bucket = db.bucket(root.build()); | ||
const bucket = db.bucket(myPrefix.encode()); | ||
const batch = bucket.batch(); | ||
@@ -26,3 +23,3 @@ | ||
// Write `foo` to `rt[1111111111111111111111111111111111111111][00000000]`. | ||
batch.put(rec.build(hash, 0), Buffer.from('foo')); | ||
batch.put(myKey.encode(hash, 0), Buffer.from('foo')); | ||
@@ -35,4 +32,4 @@ await batch.write(); | ||
const iter = bucket.iterator({ | ||
gte: rec.min(), | ||
lte: rec.max(), | ||
gte: myKey.min(), | ||
lte: myKey.max(), | ||
values: true | ||
@@ -43,10 +40,9 @@ }); | ||
// Parse each key. | ||
const [hash, uint] = rec.parse(key); | ||
const [hash, index] = myKey.decode(key); | ||
console.log('Hash: %s', hash); | ||
console.log('Uint: %d', uint); | ||
console.log('Value: %s', value.toString('ascii')); | ||
console.log('Index: %d', index); | ||
console.log('Value: %s', value.toString()); | ||
}); | ||
await db.close(); | ||
``` | ||
@@ -64,2 +60,6 @@ | ||
Parts of this software are based on leveldown: | ||
- Copyright (c) 2017, Rod Vagg (MIT License). | ||
See LICENSE for more info. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2913847
223
0
3
5
70
3640
1
+ Addedbindings@~1.3.0
+ Addednan@~2.10.0
- Removedleveldown@4.0.1
- Removedabstract-leveldown@5.0.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedaproba@1.2.0(transitive)
- Removedare-we-there-yet@1.1.7(transitive)
- Removedbl@1.2.3(transitive)
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removedchownr@1.1.4(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedconsole-control-strings@1.1.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddelegates@1.0.0(transitive)
- Removeddetect-libc@1.0.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexpand-template@1.1.1(transitive)
- Removedfast-future@1.0.2(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedgauge@2.7.4(transitive)
- Removedgithub-from-package@0.0.0(transitive)
- Removedhas-unicode@2.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedleveldown@4.0.1(transitive)
- Removedmimic-response@1.0.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednode-abi@2.30.1(transitive)
- Removednoop-logger@0.1.1(transitive)
- Removednpmlog@4.1.2(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedprebuild-install@4.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpump@1.0.32.0.1(transitive)
- Removedrc@1.2.8(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedset-blocking@2.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedsimple-concat@1.0.1(transitive)
- Removedsimple-get@2.8.2(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedtar-fs@1.16.3(transitive)
- Removedtar-stream@1.6.2(transitive)
- Removedto-buffer@1.1.1(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwhich-pm-runs@1.1.0(transitive)
- Removedwide-align@1.1.5(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
Updatedbsert@~0.0.4