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

fix: stop namespaced datastore throwing when queried #296

Merged
merged 4 commits into from
Feb 28, 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
80 changes: 78 additions & 2 deletions packages/datastore-core/src/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Key } from 'interface-datastore'
import { Key, type Datastore, type Query, type Pair, type KeyQuery } from 'interface-datastore'
import map from 'it-map'
import { KeyTransformDatastore } from './keytransform.js'
import type { Datastore } from 'interface-datastore'
import type { AbortOptions } from 'interface-store'

/**
* Wraps a given datastore into a keytransform which
Expand All @@ -11,6 +12,9 @@ import type { Datastore } from 'interface-datastore'
* `/hello/world`.
*/
export class NamespaceDatastore extends KeyTransformDatastore {
private readonly iChild: Datastore
private readonly iKey: Key

constructor (child: Datastore, prefix: Key) {
super(child, {
convert (key) {
Expand All @@ -28,5 +32,77 @@ export class NamespaceDatastore extends KeyTransformDatastore {
return new Key(key.toString().slice(prefix.toString().length), false)
}
})

this.iChild = child
this.iKey = prefix
}

query (q: Query, options?: AbortOptions): AsyncIterable<Pair> {
const query: Query = {
...q
}

query.filters = (query.filters ?? []).map(filter => {
return ({ key, value }) => filter({ key: this.transform.invert(key), value })
})

const { prefix } = q
if (prefix != null && prefix !== '/') {
delete query.prefix
query.filters.push(({ key }) => {
return this.transform.invert(key).toString().startsWith(prefix)
})
}

if (query.orders != null) {
query.orders = query.orders.map(order => {
return (a, b) => order(
{ key: this.transform.invert(a.key), value: a.value },
{ key: this.transform.invert(b.key), value: b.value }
)
})
}

query.filters.unshift(({ key }) => this.iKey.isAncestorOf(key))

return map(this.iChild.query(query, options), ({ key, value }) => {
return {
key: this.transform.invert(key),
value
}
})
}

queryKeys (q: KeyQuery, options?: AbortOptions): AsyncIterable<Key> {
const query = {
...q
}

query.filters = (query.filters ?? []).map(filter => {
return (key) => filter(this.transform.invert(key))
})

const { prefix } = q
if (prefix != null && prefix !== '/') {
delete query.prefix
query.filters.push((key) => {
return this.transform.invert(key).toString().startsWith(prefix)
})
}

if (query.orders != null) {
query.orders = query.orders.map(order => {
return (a, b) => order(
this.transform.invert(a),
this.transform.invert(b)
)
})
}

query.filters.unshift(key => this.iKey.isAncestorOf(key))

return map(this.iChild.queryKeys(query, options), key => {
return this.transform.invert(key)
})
}
}
93 changes: 84 additions & 9 deletions packages/datastore-core/test/namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ describe('NamespaceDatastore', () => {
'abc',
''
]

const keys = [
'foo',
'foo/bar',
'foo/bar/baz',
'foo/barb',
'foo/bar/bazb',
'foo/bar/baz/barb'
].map((s) => new Key(s))

prefixes.forEach((prefix) => it(`basic '${prefix}'`, async () => {
const mStore = new MemoryDatastore()
const store = new NamespaceDatastore(mStore, new Key(prefix))

const keys = [
'foo',
'foo/bar',
'foo/bar/baz',
'foo/barb',
'foo/bar/bazb',
'foo/bar/baz/barb'
].map((s) => new Key(s))

await Promise.all(keys.map(async key => { await store.put(key, uint8ArrayFromString(key.toString())) }))
const nResults = Promise.all(keys.map(async (key) => store.get(key)))
const mResults = Promise.all(keys.map(async (key) => mStore.get(new Key(prefix).child(key))))
Expand All @@ -45,6 +46,80 @@ describe('NamespaceDatastore', () => {
expect(results[0]).to.eql(results[1])
}))

const setupStores = async (keys: Key[]): Promise<NamespaceDatastore[]> => {
const prefixes = ['abc', '123', 'sub/prefix']
const mStore = new MemoryDatastore()

return Promise.all(prefixes.map(async prefix => {
const store = new NamespaceDatastore(mStore, new Key(prefix))

await Promise.all(keys.map(async key => { await store.put(key, uint8ArrayFromString(key.toString())) }))

return store
}))
}

const setupNestedStores = async (keys: Key[]): Promise<NamespaceDatastore[]> => {
const prefixes = ['abc', '123', 'sub/prefix']
const mStore = new MemoryDatastore()

return (await Promise.all(prefixes.map(async prefix => {
const store = new NamespaceDatastore(mStore, new Key(prefix))

return Promise.all(prefixes.map(async prefix => {
const child = new NamespaceDatastore(store, new Key(prefix))

await Promise.all(keys.map(async key => { await child.put(key, uint8ArrayFromString(key.toString())) }))

return child
}))
}))).reduce((a, c) => [...a, ...c], [])
}

it('queries keys under each prefix', async () => {
const stores = await setupStores(keys)

for (const store of stores) {
const nRes = await all(store.queryKeys({}))

expect(nRes).deep.equal(keys)
}
})

it('queries values under each prefix', async () => {
const stores = await setupStores(keys)

for (const store of stores) {
const nRes = await all(store.query({}))
const values = keys.map(key => uint8ArrayFromString(key.toString()))

expect(nRes.map(p => p.key)).deep.equal(keys)
expect(nRes.map(p => p.value)).deep.equal(values)
}
})

it('queries keys under each prefix in nested stores', async () => {
const stores = await setupNestedStores(keys)

for (const store of stores) {
const nRes = await all(store.queryKeys({}))

expect(nRes).deep.equal(keys)
}
})

it('queries values under each prefix in nested stores', async () => {
const stores = await setupNestedStores(keys)

for (const store of stores) {
const nRes = await all(store.query({}))
const values = keys.map(key => uint8ArrayFromString(key.toString()))

expect(nRes.map(p => p.key)).deep.equal(keys)
expect(nRes.map(p => p.value)).deep.equal(values)
}
})

prefixes.forEach((prefix) => {
describe(`interface-datastore: '${prefix}'`, () => {
interfaceDatastoreTests({
Expand Down
Loading