Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-byte keys. #25

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ class Batch {

/**
* Get bucket.
* @param {Buffer} prefix
* @returns {Batch}
*/

Expand Down Expand Up @@ -1907,6 +1908,12 @@ function wrap(resolve, reject) {
};
}

/**
* @param {Buffer|null} prefix
* @param {Buffer|null} key
* @returns {Buffer?}
*/

function slice(prefix, key) {
if (key == null)
return key;
Expand All @@ -1925,6 +1932,12 @@ function slice(prefix, key) {
return key.slice(prefix.length);
}

/**
* @param {Buffer} prefix
* @param {Buffer} key
* @returns {Buffer}
*/

function concat(prefix, key) {
assert(Buffer.isBuffer(key), 'Key must be a buffer.');

Expand Down
185 changes: 165 additions & 20 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ const types = {

/**
* BaseKey
* @ignore
*/

class BaseKey {
Expand All @@ -323,6 +322,11 @@ class BaseKey {
this.init(ops);
}

/**
* @param {String[]} [ops]
* @returns {BaseKey}
*/

static create(ops) {
const hash = ops ? ops.join(':') : '';
const cache = keyCache[hash];
Expand All @@ -337,6 +341,11 @@ class BaseKey {
return key;
}

/**
* @param {String[]} ops
* @returns {void}
*/

init(ops) {
for (let i = 0; i < ops.length; i++) {
const name = ops[i];
Expand All @@ -360,10 +369,16 @@ class BaseKey {
}
}

getSize(args) {
/**
* @param {Buffer} id
* @param {Array} args
* @returns {Number}
*/

getSize(id, args) {
assert(args.length === this.ops.length);

let size = 1 + this.size;
let size = id.length + this.size;

if (this.index === -1)
return size;
Expand All @@ -379,18 +394,24 @@ class BaseKey {
return size;
}

/**
* @param {Buffer} id
* @param {Array} args
* @returns {Buffer}
*/

encode(id, args) {
assert(Array.isArray(args));

if (args.length !== this.ops.length)
throw new Error('Wrong number of arguments passed to key.');

const size = this.getSize(args);
const size = this.getSize(id, args);
const key = Buffer.allocUnsafe(size);

key[0] = id;
id.copy(key);

let offset = 1;
let offset = id.length;

for (let i = 0; i < this.ops.length; i++) {
const op = this.ops[i];
Expand All @@ -402,18 +423,24 @@ class BaseKey {
return key;
}

/**
* @param {Buffer} id
* @param {Buffer} key
* @returns {Array}
*/

decode(id, key) {
assert(Buffer.isBuffer(key));

if (this.ops.length === 0)
return key;
return [];

if (key.length === 0 || key[0] !== id)
if (!prefixMatches(id, key))
throw new Error('Key prefix mismatch.');

const args = [];

let offset = 1;
let offset = id.length;

for (const op of this.ops) {
const arg = op.read(key, offset);
Expand All @@ -426,6 +453,12 @@ class BaseKey {
return args;
}

/**
* @param {Buffer} id
* @param {Array} args
* @returns {Buffer}
*/

min(id, args) {
for (let i = args.length; i < this.ops.length; i++) {
const op = this.ops[i];
Expand All @@ -436,6 +469,12 @@ class BaseKey {
return this.encode(id, args);
}

/**
* @param {Buffer} id
* @param {Array} args
* @returns {Buffer}
*/

max(id, args) {
for (let i = args.length; i < this.ops.length; i++) {
const op = this.ops[i];
Expand All @@ -446,11 +485,14 @@ class BaseKey {
return this.encode(id, args);
}

root(id) {
const key = Buffer.allocUnsafe(1);

key[0] = id;
/**
* @param {Buffer} id
* @returns {Buffer}
*/

root(id) {
const key = Buffer.allocUnsafe(id.length);
id.copy(key);
return key;
}
}
Expand All @@ -471,26 +513,53 @@ class Key {
constructor(id, ops = []) {
assert(Array.isArray(ops));

/** @type {Buffer} */
this.id = makeID(id);

/** @type {BaseKey} */
this.base = BaseKey.create(ops);
}

/**
* @param {Array} args
* @returns {Buffer}
*/

encode(...args) {
return this.base.encode(this.id, args);
}

/**
* @param {Buffer} key
* @returns {Array}
*/

decode(key) {
return this.base.decode(this.id, key);
}

/**
* @param {Array} args
* @returns {Buffer}
*/

min(...args) {
return this.base.min(this.id, args);
}

/**
* @param {Array} args
* @returns {Buffer}
*/

max(...args) {
return this.base.max(this.id, args);
}

/**
* @returns {Buffer}
*/

root() {
return this.base.root(this.id);
}
Expand All @@ -500,30 +569,76 @@ class Key {
* Helpers
*/

/**
* @param {Number|String|Buffer} id
* @returns {Buffer}
*/

function makeID(id) {
if (typeof id === 'string') {
assert(id.length === 1);
id = id.charCodeAt(0);
}
if (typeof id === 'string')
id = Buffer.from(id, 'ascii');

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

id = Buffer.from([id]);
}

assert(Buffer.isBuffer(id));
return id;
}

/**
* @param {Buffer} id
* @param {Buffer} key
* @returns {Boolean}
*/

function prefixMatches(id, key) {
if (key.length === 0)
return false;

if (key.length < id.length)
return false;

return key.slice(0, id.length).equals(id);
}

/**
* @param {String} v
* @param {BufferEncoding} [enc]
* @returns {Number}
*/

function sizeString(v, enc) {
assertType(typeof v === 'string');
return 1 + Buffer.byteLength(v, enc);
}

/**
* @param {Buffer} k
* @param {Number} o
* @param {BufferEncoding} [enc]
* @returns {String}
*/

function readString(k, o, enc) {
assertLen(o + 1 <= k.length);
assertLen(o + 1 + k[o] <= k.length);

return k.toString(enc, o + 1, o + 1 + k[o]);
}

/**
* @param {Buffer} k
* @param {String} v
* @param {Number} o
* @param {BufferEncoding} [enc]
* @returns {Number}
*/

function writeString(k, v, o, enc) {
assertType(typeof v === 'string');

Expand All @@ -540,19 +655,37 @@ function writeString(k, v, o, enc) {
return 1 + size;
}

/**
* @param {Buffer} v
* @returns {Number}
*/

function sizeBuffer(v) {
assertType(Buffer.isBuffer(v));
return 1 + v.length;
}

/**
* @param {Buffer} k
* @param {Number} o
* @returns {Buffer}
*/

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) {
/**
* @param {Buffer} k
* @param {Buffer} v
* @param {Number} o
* @returns {Number}
*/

function writeBuffer(k, v, o) {
assertType(Buffer.isBuffer(v));
assertLen(v.length <= 255);
assertLen(o + 1 <= k.length);
Expand All @@ -564,6 +697,11 @@ function writeBuffer(k, v, o, enc) {
return 1 + v.length;
}

/**
* @param {Buffer|String} data
* @returns {Number}
*/

function sizeHex(data) {
if (Buffer.isBuffer(data))
return data.length;
Expand All @@ -573,6 +711,13 @@ function sizeHex(data) {
return data.length >>> 1;
}

/**
* @param {Buffer} data
* @param {String} str
* @param {Number} off
* @returns {Number}
*/

function writeHex(data, str, off) {
if (Buffer.isBuffer(str))
return str.copy(data, off);
Expand Down
Loading