Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

fix: do not strip prefixes for MountDatastores #82

Merged
merged 1 commit into from
Jan 19, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- Keytransform: [`src/keytransform`](src/keytransform.js)
- Sharding: [`src/sharding`](src/sharding.js)
- Tiered: [`src/tiered`](src/tirered.js)
- Namespace: [`src/tiered`](src/namespace.js)
- Namespace: [`src/namespace`](src/namespace.js)

## Install

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
"devDependencies": {
"@types/debug": "^4.1.5",
"aegir": "^35.0.2",
"aegir": "^36.1.3",
"interface-datastore-tests": "^2.0.3",
"it-all": "^1.0.4",
"rimraf": "^3.0.2",
Expand Down
55 changes: 53 additions & 2 deletions src/keytransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,33 @@ export class KeyTransformDatastore extends BaseDatastore {
* @param {Options} [options]
*/
query (q, options) {
return map(this.child.query(q, options), ({ key, value }) => {
/** @type {Query} */
const query = {
...q
}

query.filters = (query.filters || []).map(filter => {
return ({ key, value }) => filter({ key: this.transform.convert(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) {
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 }
)
})
}

return map(this.child.query(query, options), ({ key, value }) => {
return {
key: this.transform.invert(key),
value
Expand All @@ -182,7 +208,32 @@ export class KeyTransformDatastore extends BaseDatastore {
* @param {Options} [options]
*/
queryKeys (q, options) {
return map(this.child.queryKeys(q, options), key => {
const query = {
...q
}

query.filters = (query.filters || []).map(filter => {
return (key) => filter(this.transform.convert(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) {
query.orders = query.orders.map(order => {
return (a, b) => order(
this.transform.invert(a),
this.transform.invert(b)
)
})
}

return map(this.child.queryKeys(query, options), key => {
return this.transform.invert(key)
})
}
Expand Down
63 changes: 15 additions & 48 deletions src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import filter from 'it-filter'
import take from 'it-take'
import merge from 'it-merge'
import { BaseDatastore } from './base.js'
import { KeyTransformDatastore } from './keytransform.js'
import * as Errors from './errors.js'
import {
sortAll,
replaceStartWith
sortAll
} from './utils.js'
import { Key } from 'interface-datastore/key'

/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Key} Key
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
Expand Down Expand Up @@ -44,16 +42,14 @@ export class MountDatastore extends BaseDatastore {
*
* @private
* @param {Key} key
* @returns {{datastore: Datastore, mountpoint: Key, rest: Key} | undefined}
* @returns {{datastore: Datastore, mountpoint: Key} | undefined}
*/
_lookup (key) {
for (const mount of this.mounts) {
if (mount.prefix.toString() === key.toString() || mount.prefix.isAncestorOf(key)) {
const s = replaceStartWith(key.toString(), mount.prefix.toString())
return {
datastore: mount.datastore,
mountpoint: mount.prefix,
rest: new Key(s)
mountpoint: mount.prefix
}
}
}
Expand All @@ -70,7 +66,7 @@ export class MountDatastore extends BaseDatastore {
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
}

return match.datastore.put(match.rest, value, options)
return match.datastore.put(key, value, options)
}

/**
Expand All @@ -82,7 +78,7 @@ export class MountDatastore extends BaseDatastore {
if (match == null) {
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
}
return match.datastore.get(match.rest, options)
return match.datastore.get(key, options)
}

/**
Expand All @@ -94,7 +90,7 @@ export class MountDatastore extends BaseDatastore {
if (match == null) {
return Promise.resolve(false)
}
return match.datastore.has(match.rest, options)
return match.datastore.has(key, options)
}

/**
Expand All @@ -107,7 +103,7 @@ export class MountDatastore extends BaseDatastore {
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
}

return match.datastore.delete(match.rest, options)
return match.datastore.delete(key, options)
}

async close () {
Expand Down Expand Up @@ -137,19 +133,18 @@ export class MountDatastore extends BaseDatastore {
}

return {
batch: batchMounts[m],
rest: match.rest
batch: batchMounts[m]
}
}

return {
put: (key, value) => {
const match = lookup(key)
match.batch.put(match.rest, value)
match.batch.put(key, value)
},
delete: (key) => {
const match = lookup(key)
match.batch.delete(match.rest)
match.batch.delete(key)
},
commit: async (options) => {
await Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit(options)))
Expand All @@ -163,22 +158,8 @@ export class MountDatastore extends BaseDatastore {
*/
query (q, options) {
const qs = this.mounts.map(m => {
const ks = new KeyTransformDatastore(m.datastore, {
convert: (key) => {
throw new Error('should never be called')
},
invert: (key) => {
return m.prefix.child(key)
}
})

let prefix
if (q.prefix != null) {
prefix = replaceStartWith(q.prefix, m.prefix.toString())
}

return ks.query({
prefix: prefix,
return m.datastore.query({
prefix: q.prefix,
filters: q.filters
}, options)
})
Expand All @@ -201,22 +182,8 @@ export class MountDatastore extends BaseDatastore {
*/
queryKeys (q, options) {
const qs = this.mounts.map(m => {
const ks = new KeyTransformDatastore(m.datastore, {
convert: (key) => {
throw new Error('should never be called')
},
invert: (key) => {
return m.prefix.child(key)
}
})

let prefix
if (q.prefix != null) {
prefix = replaceStartWith(q.prefix, m.prefix.toString())
}

return ks.queryKeys({
prefix: prefix,
return m.datastore.queryKeys({
prefix: q.prefix,
filters: q.filters
}, options)
})
Expand Down
29 changes: 0 additions & 29 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { KeyTransformDatastore } from './keytransform.js'
* For example, if the prefix is `new Key(/hello)` a call
* to `store.put(new Key('/world'), mydata)` would store the data under
* `/hello/world`.
*
*/
export class NamespaceDatastore extends KeyTransformDatastore {
/**
Expand All @@ -40,33 +39,5 @@ export class NamespaceDatastore extends KeyTransformDatastore {
return new Key(key.toString().slice(prefix.toString().length), false)
}
})

this.prefix = prefix
}

/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {
if (q.prefix && this.prefix.toString() !== '/') {
return super.query(Object.assign({}, q, {
prefix: this.prefix.child(new Key(q.prefix)).toString()
}))
}
return super.query(q, options)
}

/**
* @param {KeyQuery} q
* @param {Options} [options]
*/
queryKeys (q, options) {
if (q.prefix && this.prefix.toString() !== '/') {
return super.queryKeys(Object.assign({}, q, {
prefix: this.prefix.child(new Key(q.prefix)).toString()
}))
}
return super.queryKeys(q, options)
}
}
89 changes: 8 additions & 81 deletions src/sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,57 +208,15 @@ export class ShardingDatastore extends BaseDatastore {
* @param {Options} [options]
*/
query (q, options) {
/** @type {Query} */
const tq = {
offset: q.offset,
limit: q.limit,
/** @type {QueryOrder[]} */
orders: [],
/** @type {QueryFilter[]} */
...q,
filters: [
/** @type {QueryFilter} */
e => e.key.toString() !== shardKey.toString(),
({ key }) => key.toString() !== shardKey.toString(),
/** @type {QueryFilter} */
e => e.key.toString() !== shardReadmeKey.toString()
]
}

const { prefix } = q
if (prefix != null) {
tq.filters.push((e) => {
return this._invertKey(e.key).toString().startsWith(prefix)
})
}

if (q.filters != null) {
const filters = q.filters.map(f => {
/** @type {QueryFilter} */
const filter = ({ key, value }) => {
return f({
key: this._invertKey(key),
value
})
}

return filter
})
tq.filters = tq.filters.concat(filters)
}

if (q.orders != null) {
tq.orders = q.orders.map(o => {
/** @type {QueryOrder} */
const order = (a, b) => {
return o({
key: this._invertKey(a.key),
value: a.value
}, {
key: this._invertKey(b.key),
value: b.value
})
}

return order
})
({ key }) => key.toString() !== shardReadmeKey.toString()
].concat(q.filters || [])
}

return this.child.query(tq, options)
Expand All @@ -269,46 +227,15 @@ export class ShardingDatastore extends BaseDatastore {
* @param {Options} [options]
*/
queryKeys (q, options) {
/** @type {KeyQuery} */
const tq = {
offset: q.offset,
limit: q.limit,
/** @type {KeyQueryOrder[]} */
orders: [],
/** @type {KeyQueryFilter[]} */
...q,
filters: [
/** @type {KeyQueryFilter} */
key => key.toString() !== shardKey.toString(),
/** @type {KeyQueryFilter} */
key => key.toString() !== shardReadmeKey.toString()
]
}

const { prefix } = q
if (prefix != null) {
tq.filters.push((key) => {
return this._invertKey(key).toString().startsWith(prefix)
})
}

if (q.filters != null) {
const filters = q.filters.map(f => {
/** @type {KeyQueryFilter} */
const filter = (key) => {
return f(this._invertKey(key))
}

return filter
})
tq.filters = tq.filters.concat(filters)
}

if (q.orders != null) {
tq.orders = q.orders.map(o => {
/** @type {KeyQueryOrder} */
const order = (a, b) => o(this._invertKey(a), this._invertKey(b))

return order
})
].concat(q.filters || [])
}

return this.child.queryKeys(tq, options)
Expand Down
Loading