Skip to content

Commit

Permalink
Made subspacing methods consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
keijokapp committed Jul 8, 2024
1 parent 168a848 commit cd6e044
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
44 changes: 26 additions & 18 deletions lib/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fdb from './native'
import Transaction, { RangeOptions, Watch } from './transaction'
import {Transformer, defaultTransformer} from './transformer'
import {Transformer} from './transformer'
import {NativeValue} from './native'
import {KeySelector} from './keySelector'
import Subspace, { root, GetSubspace, isGetSubspace } from './subspace'
Expand Down Expand Up @@ -43,40 +43,48 @@ export default class Database<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
// convenience and backwards compatibility.
/** Create a shallow reference to the database at a specified subspace */
at<CKI, CKO, CVI, CVO>(hasSubspace: GetSubspace<CKI, CKO, CVI, CVO>): Database<CKI, CKO, CVI, CVO>
at(prefix: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Database<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Database<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Database<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: undefined):
at(prefix?: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Database<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Database<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Database<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO>(prefix: KeyIn | null | undefined, keyXf?: Transformer<CKI, CKO>, valueXf?: undefined):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
at<CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, CVI, CVO>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Database<CKI, CKO, ValIn, ValOut>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix?: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, ValIn, ValOut>
| Database<CKI, CKO, ValIn, ValOut>
| Database<KeyIn, KeyOut, CVI, CVO>
| Database<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefixOrSubspace: GetSubspace<CKI, CKO, CVI, CVO> | KeyIn | null, keyXf?: Transformer<unknown, unknown>, valueXf?: Transformer<unknown, unknown>) {
at<CKI, CKO, CVI, CVO>(prefixOrSubspace?: GetSubspace<CKI, CKO, CVI, CVO> | KeyIn | null, keyXf?: Transformer<unknown, unknown>, valueXf?: Transformer<unknown, unknown>) {
if (isGetSubspace(prefixOrSubspace)) return new Database(this._db, prefixOrSubspace.getSubspace())
else return new Database(this._db, this.subspace.at(prefixOrSubspace, keyXf, valueXf))
}

withKeyEncoding<ChildKeyIn, ChildKeyOut>(keyXf: Transformer<ChildKeyIn, ChildKeyOut>): Database<ChildKeyIn, ChildKeyOut, ValIn, ValOut>
withKeyEncoding<NativeValue, Buffer>(): Database<NativeValue, Buffer, ValIn, ValOut>
withKeyEncoding<ChildKeyIn, ChildKeyOut>(keyXf: Transformer<any, any> = defaultTransformer): Database<ChildKeyIn, ChildKeyOut, ValIn, ValOut> {
return new Database(this._db, this.subspace.at(null, keyXf))
withKeyEncoding(keyXf?: undefined): Database<NativeValue, Buffer, ValIn, ValOut>
withKeyEncoding<CKI, CKO>(keyXf: Transformer<CKI, CKO>): Database<CKI, CKO, ValIn, ValOut>
withKeyEncoding<CKI, CKO>(keyXf?: Transformer<CKI, CKO>):
| Database<NativeValue, Buffer, ValIn, ValOut>
| Database<CKI, CKO, ValIn, ValOut>
withKeyEncoding(keyXf?: Transformer<unknown, unknown>) {
return new Database(this._db, this.subspace.withKeyEncoding(keyXf))
}

withValueEncoding<ChildValIn, ChildValOut>(valXf: Transformer<ChildValIn, ChildValOut>): Database<KeyIn, KeyOut, ChildValIn, ChildValOut> {
return new Database(this._db, this.subspace.at(null, undefined /* inherit */, valXf))

withValueEncoding(valueXf?: undefined): Database<KeyIn, KeyOut, NativeValue, Buffer>
withValueEncoding<CVI, CVO>(valueXf: Transformer<CVI, CVO>): Database<KeyIn, KeyOut, CVI, CVO>
withValueEncoding<CVI, CVO>(valueXf?: Transformer<CVI, CVO>):
| Database<KeyIn, KeyOut, NativeValue, Buffer>
| Database<KeyIn, KeyOut, CVI, CVO>
withValueEncoding(valueXf?: Transformer<unknown, unknown>) {
return new Database(this._db, this.subspace.withValueEncoding(valueXf))
}

// This is the API you want to use for non-trivial transactions.
Expand Down
39 changes: 24 additions & 15 deletions lib/subspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
// legit all the variants. Typescript can probably infer using less than this,
// but I honestly don't trust it not to land with any or unknown or something
// in some of the derived types
at(prefix: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Subspace<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Subspace<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Subspace<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: undefined):
at(prefix?: KeyIn | null, keyXf?: undefined, valueXf?: undefined): Subspace<KeyIn, KeyOut, ValIn, ValOut>;
at<CKI, CKO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf?: undefined): Subspace<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: undefined, valueXf: Transformer<CVI, CVO>): Subspace<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf: Transformer<CVI, CVO>): Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO>(prefix: KeyIn | null | undefined, keyXf?: Transformer<CKI, CKO>, valueXf?: undefined):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<CKI, CKO, ValIn, ValOut>;
at<CVI, CVO>(prefix: KeyIn | null, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
at<CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: undefined, valueXf?: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<KeyIn, KeyOut, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO> | undefined, valueXf: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, CVI, CVO>
| Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null | undefined, keyXf: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Subspace<CKI, CKO, ValIn, ValOut>
| Subspace<CKI, CKO, CVI, CVO>;
at<CKI, CKO, CVI, CVO>(prefix: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
at<CKI, CKO, CVI, CVO>(prefix?: KeyIn | null, keyXf?: Transformer<CKI, CKO>, valueXf?: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, ValIn, ValOut>
| Subspace<CKI, CKO, ValIn, ValOut>
| Subspace<KeyIn, KeyOut, CVI, CVO>
| Subspace<CKI, CKO, CVI, CVO>;
at(prefix: KeyIn | null, keyXf: Transformer<unknown, unknown> = this.keyXf, valueXf: Transformer<unknown, unknown> = this.valueXf) {
at(prefix?: KeyIn | null, keyXf: Transformer<unknown, unknown> = this.keyXf, valueXf: Transformer<unknown, unknown> = this.valueXf) {
const _prefix = prefix == null ? null : this.keyXf.pack(prefix)
return new Subspace(concatPrefix(this.prefix, _prefix), keyXf, valueXf)
}
Expand All @@ -72,13 +72,22 @@ export default class Subspace<KeyIn = NativeValue, KeyOut = Buffer, ValIn = Nati
return new Subspace(concatPrefix(this.prefix, prefix), this.keyXf, this.valueXf)
}


withKeyEncoding<CKI, CKO>(keyXf: Transformer<CKI, CKO>): Subspace<CKI, CKO, ValIn, ValOut> {
withKeyEncoding(keyXf?: undefined): Subspace<NativeValue, Buffer, ValIn, ValOut>
withKeyEncoding<CKI, CKO>(keyXf: Transformer<CKI, CKO>): Subspace<CKI, CKO, ValIn, ValOut>
withKeyEncoding<CKI, CKO>(keyXf?: Transformer<CKI, CKO>):
| Subspace<NativeValue, Buffer, ValIn, ValOut>
| Subspace<CKI, CKO, ValIn, ValOut>
withKeyEncoding(keyXf?: Transformer<unknown, unknown>) {
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)

withValueEncoding(valueXf?: undefined): Subspace<KeyIn, KeyOut, NativeValue, Buffer>
withValueEncoding<CVI, CVO>(valueXf: Transformer<CVI, CVO>): Subspace<KeyIn, KeyOut, CVI, CVO>
withValueEncoding<CVI, CVO>(valueXf?: Transformer<CVI, CVO>):
| Subspace<KeyIn, KeyOut, NativeValue, Buffer>
| Subspace<KeyIn, KeyOut, CVI, CVO>
withValueEncoding(valueXf?: Transformer<unknown, unknown>) {
return new Subspace(this.prefix, this.keyXf, valueXf)
}

// GetSubspace implementation
Expand Down

0 comments on commit cd6e044

Please sign in to comment.