Skip to content

Commit 3b10d18

Browse files
committed
bdb-key: add multi-byte strings/buffers as keys.
1 parent 8d96c56 commit 3b10d18

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/db.js

+13
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ class Batch {
852852

853853
/**
854854
* Get bucket.
855+
* @param {Buffer} prefix
855856
* @returns {Batch}
856857
*/
857858

@@ -1907,6 +1908,12 @@ function wrap(resolve, reject) {
19071908
};
19081909
}
19091910

1911+
/**
1912+
* @param {Buffer|null} prefix
1913+
* @param {Buffer|null} key
1914+
* @returns {Buffer?}
1915+
*/
1916+
19101917
function slice(prefix, key) {
19111918
if (key == null)
19121919
return key;
@@ -1925,6 +1932,12 @@ function slice(prefix, key) {
19251932
return key.slice(prefix.length);
19261933
}
19271934

1935+
/**
1936+
* @param {Buffer} prefix
1937+
* @param {Buffer} key
1938+
* @returns {Buffer}
1939+
*/
1940+
19281941
function concat(prefix, key) {
19291942
assert(Buffer.isBuffer(key), 'Key must be a buffer.');
19301943

lib/key.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,10 @@ class Key {
575575
*/
576576

577577
function makeID(id) {
578-
if (typeof id === 'string') {
579-
assert(id.length === 1);
578+
if (typeof id === 'string')
580579
id = Buffer.from(id, 'ascii');
581-
}
582580

581+
// Number is not supported for multi-byte ids.
583582
if (typeof id === 'number') {
584583
assert((id & 0xff) === id);
585584
assert(id !== 0xff);

test/key-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ const KEY_IDS = [{
2121
num: 0x7a, // 'z'
2222
buf: Buffer.from('z', 'ascii'),
2323
expected: Buffer.from('z', 'ascii')
24+
}, {
25+
str: 'aa',
26+
num: null, // does not support.
27+
buf: Buffer.from('aa', 'ascii'),
28+
expected: Buffer.from('aa', 'ascii')
29+
}, {
30+
str: 'abcd',
31+
num: null, // does not support.
32+
buf: Buffer.from('abcd', 'ascii'),
33+
expected: Buffer.from('abcd', 'ascii')
2434
}];
2535

2636
const KEY_OPS = [{
@@ -69,6 +79,11 @@ describe('Key', function() {
6979
});
7080

7181
it(`should create key for ${KEY_ID.str} (num)`, () => {
82+
if (KEY_ID.num == null) {
83+
this.skip();
84+
return;
85+
}
86+
7287
const key = bdb.key(KEY_ID.num);
7388

7489
assert.bufferEqual(key.encode(), KEY_ID.expected);

0 commit comments

Comments
 (0)