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

Updating keyPathToKey to escape key parts #22

Closed
wants to merge 4 commits into from
Closed
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
19 changes: 12 additions & 7 deletions src/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class DB {
* @internal
*/
public async _del(keyPath: KeyPath): Promise<void> {
console.log('DEL', keyPath);
return this._db.del(utils.keyPathToKey(keyPath));
}

Expand Down Expand Up @@ -458,15 +459,15 @@ class DB {
if (options.gt != null) {
options.gt = Buffer.concat([
levelKeyStart,
typeof options.gt === 'string' ? Buffer.from(options.gt) : options.gt,
utils.escapePart(typeof options.gt === 'string' ? Buffer.from(options.gt) : options.gt),
]);
}
if (options.gte != null) {
options.gte = Buffer.concat([
levelKeyStart,
typeof options.gte === 'string'
utils.escapePart(typeof options.gte === 'string'
? Buffer.from(options.gte)
: options.gte,
: options.gte),
]);
}
if (options.gt == null && options.gte == null) {
Expand All @@ -475,15 +476,15 @@ class DB {
if (options?.lt != null) {
options.lt = Buffer.concat([
levelKeyStart,
typeof options.lt === 'string' ? Buffer.from(options.lt) : options.lt,
utils.escapePart(typeof options.lt === 'string' ? Buffer.from(options.lt) : options.lt),
]);
}
if (options?.lte != null) {
options.lte = Buffer.concat([
levelKeyStart,
typeof options.lte === 'string'
utils.escapePart(typeof options.lte === 'string'
? Buffer.from(options.lte)
: options.lte,
: options.lte),
]);
}
if (options.lt == null && options.lte == null) {
Expand All @@ -503,11 +504,15 @@ class DB {
const kv = await next();
// If kv is undefined, we have reached the end of iteration
if (kv != null) {

console.log('K', kv[0]);
// Handle keys: false
if (kv[0] != null) {
// Truncate level path so the returned key is relative to the level path
const keyPath = utils.parseKey(kv[0]).slice(levelPath.length);
console.log('KEYPATH REMAINING', keyPath);
kv[0] = utils.keyPathToKey(keyPath);
console.log('KEYPATH NEW', kv[0]);
}
// Handle values: false
if (kv[1] != null) {
Expand Down Expand Up @@ -535,7 +540,7 @@ class DB {
*/
public async _clear(levelPath: LevelPath = []): Promise<void> {
for await (const [k] of this._iterator({ values: false }, levelPath)) {
await this._del([...levelPath, k]);
await this._del([...levelPath, ...utils.parseKey(k)]);
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const esc = Buffer.from([92]);
* Converts KeyPath to key buffer
* e.g. ['A', 'B'] => !A!B (where ! is the sep)
* An empty key path is converted to `['']`
* Level parts must not contain the separator
* Key actual part is allowed to contain the separator
* Level and key parts must not contain the separator
*/
function keyPathToKey(keyPath: KeyPath): Buffer {
if (keyPath.length < 1) {
Expand All @@ -29,7 +28,9 @@ function keyPathToKey(keyPath: KeyPath): Buffer {
const levelPath = keyPath.slice(0, -1);
return Buffer.concat([
levelPathToKey(levelPath),
typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart,
escapePart(
typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart,
),
]);
}

Expand All @@ -42,16 +43,16 @@ function levelPathToKey(levelPath: LevelPath): Buffer {
return Buffer.concat(
levelPath.map((p) => {
p = typeof p === 'string' ? Buffer.from(p, 'utf-8') : p;
p = escapeLevel(p);
p = escapePart(p);
return Buffer.concat([sep, p, sep]);
}),
);
}

/**
* Escapes the level part for escape and separator
* Escapes level and key parts for escape and separator
*/
function escapeLevel(buf: Buffer): Buffer {
function escapePart(buf: Buffer): Buffer {
const bytes: Array<number> = [];
for (let i = 0; i < buf.byteLength; i++) {
const b = buf[i];
Expand All @@ -67,9 +68,9 @@ function escapeLevel(buf: Buffer): Buffer {
}

/**
* Unescapes the level part of escape and separator
* Unescapes level and key parts of escape and separator
*/
function unescapeLevel(buf: Buffer): Buffer {
function unescapePart(buf: Buffer): Buffer {
const bytes: Array<number> = [];
for (let i = 0; i < buf.byteLength; i++) {
const b = buf[i];
Expand Down Expand Up @@ -108,8 +109,8 @@ function parseKey(key: Buffer): KeyPath {
if (bufs.length < 1) {
throw new TypeError('Buffer is not a key');
}
for (let i = 0; i < bufs.length - 1; i++) {
bufs[i] = unescapeLevel(bufs[i]);
for (let i = 0; i < bufs.length; i++) {
bufs[i] = unescapePart(bufs[i]);
}
return bufs;
}
Expand Down Expand Up @@ -250,8 +251,8 @@ function fromArrayBuffer(
export {
sep,
esc,
escapeLevel,
unescapeLevel,
escapePart,
unescapePart,
keyPathToKey,
levelPathToKey,
parseKey,
Expand Down
50 changes: 50 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,65 @@ describe('utils', () => {
[Buffer.concat([utils.sep, Buffer.from('foobar')])],
[Buffer.concat([Buffer.from('foobar'), utils.sep])],
[Buffer.concat([utils.sep, Buffer.from('foobar'), utils.sep])],
[
Buffer.concat([
utils.sep,
Buffer.from('foobar'),
utils.sep,
Buffer.from('foobar'),
]),
],
[
Buffer.concat([
Buffer.from('foobar'),
utils.sep,
Buffer.from('foobar'),
utils.sep,
]),
],
// Escape can be used in key part
[utils.esc],
[Buffer.concat([utils.esc, Buffer.from('foobar')])],
[Buffer.concat([Buffer.from('foobar'), utils.esc])],
[Buffer.concat([utils.esc, Buffer.from('foobar'), utils.esc])],
[
Buffer.concat([
utils.esc,
Buffer.from('foobar'),
utils.esc,
Buffer.from('foobar'),
]),
],
[
Buffer.concat([
Buffer.from('foobar'),
utils.esc,
Buffer.from('foobar'),
utils.esc,
]),
],
// Separator can be used in level parts
[Buffer.concat([utils.sep, Buffer.from('foobar')]), 'key'],
[Buffer.concat([Buffer.from('foobar'), utils.sep]), 'key'],
[Buffer.concat([utils.sep, Buffer.from('foobar'), utils.sep]), 'key'],
[
Buffer.concat([
utils.sep,
Buffer.from('foobar'),
utils.sep,
Buffer.from('foobar'),
]),
'key',
],
[
Buffer.concat([
Buffer.from('foobar'),
utils.sep,
Buffer.from('foobar'),
utils.sep,
]),
'key',
],
// Escape can be used in level parts
[Buffer.concat([utils.sep, utils.esc, utils.sep]), 'key'],
[Buffer.concat([utils.esc, utils.esc, utils.esc]), 'key'],
Expand Down