Skip to content
Open
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
14 changes: 7 additions & 7 deletions lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class Database<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
return this.doOneshot(tn => tn.clear(key))
}

clearRange(start: KeyIn, end?: KeyIn) {
clearRange(start?: KeyIn, end?: KeyIn) {
return this.doOneshot(tn => tn.clearRange(start, end))
}

Expand Down Expand Up @@ -144,21 +144,21 @@ export default class Database<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
}

getRangeAll(
start: KeyIn | KeySelector<KeyIn>,
end?: KeyIn | KeySelector<KeyIn>,
start?: KeyIn | KeySelector<undefined | KeyIn>,
end?: KeyIn | KeySelector<undefined | KeyIn>,
opts?: RangeOptions) {
return this.doTransaction(async tn => tn.snapshot().getRangeAll(start, end, opts))
return this.doTransaction(tn => tn.snapshot().getRangeAll(start, end, opts))
}

getRangeAllStartsWith(prefix: KeyIn | KeySelector<KeyIn>, opts?: RangeOptions) {
return this.getRangeAll(prefix, undefined, opts)
return this.doTransaction(tn => tn.snapshot().getRangeAllStartsWith(prefix, opts))
}

getEstimatedRangeSizeBytes(start: KeyIn, end: KeyIn): Promise<number> {
getEstimatedRangeSizeBytes(start?: KeyIn, end?: KeyIn): Promise<number> {
return this.doTransaction(tn => tn.getEstimatedRangeSizeBytes(start, end))
}

getRangeSplitPoints(start: KeyIn, end: KeyIn, chunkSize: number): Promise<KeyOut[]> {
getRangeSplitPoints(start: KeyIn | undefined, end: KeyIn | undefined, chunkSize: number): Promise<KeyOut[]> {
return this.doTransaction(tn => tn.getRangeSplitPoints(start, end, chunkSize))
}

Expand Down
2 changes: 1 addition & 1 deletion lib/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ export class DirectoryLayer {

private async* _subdirNamesAndNodes(txn: TxnAny, node: NodeSubspace) {
// TODO: This could work using async iterators to improve performance of searches on very large directories.
for await (const [key, prefix] of txn.at(node).getRange(SUBDIRS_KEY)) {
for await (const [key, prefix] of txn.at(node).getRangeStartsWith(SUBDIRS_KEY)) {
yield [key[1], this._nodeWithPrefix(prefix)] as [Buffer, NodeSubspace]
}
}
Expand Down
47 changes: 42 additions & 5 deletions lib/subspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import { Transformer, prefixTransformer, defaultTransformer, defaultGetRange } from "./transformer"
import { NativeValue } from "./native"
import { asBuf, concat2, startsWith } from "./util"
import {
asBuf, concat2, startsWith, strInc
} from "./util"
import { UnboundStamp } from './versionstamp.js'

const EMPTY_BUF = Buffer.alloc(0)

Expand Down Expand Up @@ -75,17 +78,51 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
unpackKey(key: Buffer): KeyOut {
return this._bakedKeyXf.unpack(key)
}
packKeyUnboundVersionstamp(key: KeyIn): UnboundStamp {
if (!this._bakedKeyXf.packUnboundVersionstamp) {
throw TypeError('Value encoding does not support unbound versionstamps. Use setVersionstampPrefixedValue instead')
}

return this._bakedKeyXf.packUnboundVersionstamp(key)
}
packValue(val: ValIn): NativeValue {
return this.valueXf.pack(val)
}
unpackValue(val: Buffer): ValOut {
return this.valueXf.unpack(val)
}
packValueUnboundVersionstamp(value: ValIn): UnboundStamp {
if (!this.valueXf.packUnboundVersionstamp) {
throw TypeError('Value encoding does not support unbound versionstamps. Use setVersionstampPrefixedValue instead')
}

return this.valueXf.packUnboundVersionstamp(value)
}

/**
* Encodes a range specified by `start`/`end` pair using configured key encoder.
*
* @param start Start of the key range. If undefined, the start of the subspace is assumed.
* @param end End of the key range. If undefined, the end of the subspace is assumed.
* @returns Encoded range as a `{ begin, end }` record.
*/
packRange(start?: KeyIn, end?: KeyIn): {begin: NativeValue, end: NativeValue} {
return {
begin: start !== undefined ? this._bakedKeyXf.pack(start) : this.prefix,
end: end !== undefined ? this._bakedKeyXf.pack(end) : strInc(this.prefix)
}
}

/**
* Encodes a range specified by the prefix using configured key encoder.
*
* @param prefix Start of the key key range. If undefined, the start of the subspace is assumed.
* @returns Encoded range as a `{ begin, end }` record.
*/
packRangeStartsWith(prefix: KeyIn): {begin: NativeValue, end: NativeValue} {
const encodePrefix = this._bakedKeyXf.range ?? defaultGetRange

packRange(prefix: KeyIn): {begin: NativeValue, end: NativeValue} {
// if (this._bakedKeyXf.range) return this._bakedKeyXf.range(prefix)
// else return defaultGetRange(prefix, this._bakedKeyXf)
return (this._bakedKeyXf.range || defaultGetRange)(prefix, this._bakedKeyXf)
return encodePrefix(prefix, this._bakedKeyXf)
}

contains(key: NativeValue) {
Expand Down
Loading