Skip to content

Commit

Permalink
Removed compatibility with defalting to prefix operation
Browse files Browse the repository at this point in the history
  • Loading branch information
keijokapp committed Jul 1, 2024
1 parent a69630f commit bf6f952
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 57 deletions.
9 changes: 0 additions & 9 deletions lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ export default class Database<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
this.subspace = subspace//new Subspace<KeyIn, KeyOut, ValIn, ValOut>(prefix, keyXf, valueXf)
}

/**
* Switch to a new mode of handling ranges.
*
* @see Subspace.noDefaultPrefix
*/
noDefaultPrefix() {
return new Database(this._db, this.subspace.noDefaultPrefix())
}

setNativeOptions(opts: DatabaseOptions) {
eachOption(databaseOptionData, opts, (code, val) => this._db.setOption(code, val))
}
Expand Down
52 changes: 8 additions & 44 deletions lib/subspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati

_bakedKeyXf: Transformer<KeyIn, KeyOut> // This is cached from _prefix + keyXf.

_noDefaultPrefix: boolean

constructor(
rawPrefix: string | Buffer | null,
keyXf?: Transformer<KeyIn, KeyOut>,
valueXf?: Transformer<ValIn, ValOut>,
noDefaultPrefix: boolean = false
) {
constructor(rawPrefix: string | Buffer | null, keyXf?: Transformer<KeyIn, KeyOut>, valueXf?: Transformer<ValIn, ValOut>) {
this.prefix = rawPrefix != null ? Buffer.from(rawPrefix) : EMPTY_BUF

// Ugh typing this is a mess. Usually this will be fine since if you say new
Expand All @@ -45,24 +38,6 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
this.valueXf = valueXf || (defaultTransformer as Transformer<any, any>)

this._bakedKeyXf = rawPrefix ? prefixTransformer(rawPrefix, this.keyXf) : this.keyXf

this._noDefaultPrefix = noDefaultPrefix
}

/**
* Switch to a new mode of handling ranges. By default, the range operations (`getRange` family
* and `clearRange`) treat calls with missing end key as operations on prefix ranges. That means
* that a call like `tn.at('a').getRange('x')` acts on prefix `ax`, ie key range `[ax, ay)`. In
* the new mode, the missing end key defaults to a subspace end (inclusive), ie that call would
* act on a range `[ax, b)`. This enabled specifying key ranges not possible before.
*
* To specifiy range as a prefix, use `StartsWith` version of those methods (eg
* `getRangeAllStartsWith`).
*
* @see Subspace.packRange
*/
noDefaultPrefix() {
return new Subspace(this.prefix, this.keyXf, this.valueXf, true)
}

// All these template parameters make me question my life choices, but this is
Expand All @@ -76,21 +51,21 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
// ***
at(prefix: KeyIn | null, keyXf: Transformer<any, any> = this.keyXf, valueXf: Transformer<any, any> = this.valueXf) {
const _prefix = prefix == null ? null : this.keyXf.pack(prefix)
return new Subspace(concatPrefix(this.prefix, _prefix), keyXf, valueXf, this._noDefaultPrefix)
return new Subspace(concatPrefix(this.prefix, _prefix), keyXf, valueXf)
}

/** At a child prefix thats specified without reference to the key transformer */
atRaw(prefix: Buffer) {
return new Subspace(concatPrefix(this.prefix, prefix), this.keyXf, this.valueXf, this._noDefaultPrefix)
return new Subspace(concatPrefix(this.prefix, prefix), this.keyXf, this.valueXf)
}


withKeyEncoding<CKI, CKO>(keyXf: Transformer<CKI, CKO>): Subspace<CKI, CKO, ValIn, ValOut> {
return new Subspace(this.prefix, keyXf, this.valueXf, this._noDefaultPrefix)
return new Subspace(this.prefix, keyXf, this.valueXf)
}

withValueEncoding<CVI, CVO>(valXf: Transformer<CVI, CVO>): Subspace<KeyIn, KeyOut, CVI, CVO> {
return new Subspace(this.prefix, this.keyXf, valXf, this._noDefaultPrefix)
return new Subspace(this.prefix, this.keyXf, valXf)
}

// GetSubspace implementation
Expand Down Expand Up @@ -128,21 +103,10 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
* 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 Start of the key range. If undefined, the end of the subspace is assumed, unless
* `noDefaultPrefix` flag is set or enabled for this subspace, in which case, start key is treated
* as a prefix.
* @param noDefaultPrefix Disable treating start key as a prefix if end key is not specified.
* @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,
noDefaultPrefix: boolean = false
): {begin: NativeValue, end: NativeValue} {
if (start !== undefined && end === undefined && !this._noDefaultPrefix && !noDefaultPrefix) {
return this.packRangeStartsWith(start)
}

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)
Expand All @@ -152,7 +116,7 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
/**
* Encodes a range specified by the prefix using configured key encoder.
*
* @param start Start of the key key range. If undefined, the start of the subspace is assumed.
* @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} {
Expand Down
8 changes: 4 additions & 4 deletions lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ export default class Transaction<KeyIn = NativeValue, KeyOut = Buffer, ValIn = N
}

getEstimatedRangeSizeBytes(start?: KeyIn, end?: KeyIn): Promise<number> {
const range = this.subspace.packRange(start, end, true)
const range = this.subspace.packRange(start, end)

return this._tn.getEstimatedRangeSizeBytes(range.begin, range.end)
}

getRangeSplitPoints(start: KeyIn | undefined, end: KeyIn | undefined, chunkSize: number): Promise<KeyOut[]> {
const range = this.subspace.packRange(start, end, true)
const range = this.subspace.packRange(start, end)

return this._tn.getRangeSplitPoints(range.begin, range.end, chunkSize).then(results => (
results.map(r => this.subspace.unpackKey(r))
Expand Down Expand Up @@ -595,7 +595,7 @@ export default class Transaction<KeyIn = NativeValue, KeyOut = Buffer, ValIn = N
}

addReadConflictRange(start?: KeyIn, end?: KeyIn) {
const range = this.subspace.packRange(start, end, true)
const range = this.subspace.packRange(start, end)
this._tn.addReadConflictRange(range.begin, range.end)
}
addReadConflictRangeStartsWith(prefix: KeyIn) {
Expand All @@ -608,7 +608,7 @@ export default class Transaction<KeyIn = NativeValue, KeyOut = Buffer, ValIn = N
}

addWriteConflictRange(start?: KeyIn, end?: KeyIn) {
const range = this.subspace.packRange(start, end, true)
const range = this.subspace.packRange(start, end)
this._tn.addWriteConflictRange(range.begin, range.end)
}
addWriteConflictRangeStartsWith(prefix: KeyIn) {
Expand Down

0 comments on commit bf6f952

Please sign in to comment.