This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsharding.js
214 lines (188 loc) · 5.03 KB
/
sharding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict'
const { Adapter, Key, utils: { utf8Encoder } } = require('interface-datastore')
const sh = require('./shard')
const KeytransformStore = require('./keytransform')
const shardKey = new Key(sh.SHARDING_FN)
const shardReadmeKey = new Key(sh.README_FN)
/**
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Options} Options
* @typedef {import('interface-datastore').Batch} Batch
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').Pair} Pair
* @typedef {import('./types').Shard} Shard
*
*/
/**
* @template TValue
* @typedef {import('./types').Await<TValue> } Await
*/
/**
* Backend independent abstraction of go-ds-flatfs.
*
* Wraps another datastore such that all values are stored
* sharded according to the given sharding function.
*/
class ShardingDatastore extends Adapter {
/**
* @param {Datastore} store
* @param {Shard} shard
*/
constructor (store, shard) {
super()
this.child = new KeytransformStore(store, {
convert: this._convertKey.bind(this),
invert: this._invertKey.bind(this)
})
this.shard = shard
}
async open () {
await this.child.open()
this.shard = await ShardingDatastore.create(this.child, this.shard)
}
/**
* @param {Key} key
*/
_convertKey (key) {
const s = key.toString()
if (s === shardKey.toString() || s === shardReadmeKey.toString()) {
return key
}
const parent = new Key(this.shard.fun(s))
return parent.child(key)
}
/**
* @param {Key} key
*/
_invertKey (key) {
const s = key.toString()
if (s === shardKey.toString() || s === shardReadmeKey.toString()) {
return key
}
return Key.withNamespaces(key.list().slice(1))
}
/**
* @deprecated
* @param {Datastore} store
* @param {Shard} shard
*/
static async createOrOpen (store, shard) {
try {
await ShardingDatastore.create(store, shard)
} catch (err) {
if (err && err.message !== 'datastore exists') throw err
}
return ShardingDatastore.open(store)
}
/**
* @deprecated
* @param {Datastore} store
*/
static async open (store) {
const shard = await sh.readShardFun('/', store)
return new ShardingDatastore(store, shard)
}
/**
* @param {Datastore} store
* @param {Shard} shard
*/
static async create (store, shard) {
const hasShard = await store.has(shardKey)
if (!hasShard) {
// @ts-ignore i have no idea what putRaw is or saw any implementation
const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store)
await Promise.all([
put(shardKey, utf8Encoder.encode(shard.toString() + '\n')),
put(shardReadmeKey, utf8Encoder.encode(sh.readme))
])
return shard
}
// test shards
const diskShard = await sh.readShardFun('/', store)
const a = (diskShard || '').toString()
const b = shard.toString()
if (a !== b) {
throw new Error(`specified fun ${b} does not match repo shard fun ${a}`)
}
return diskShard
}
/**
* @param {Key} key
* @param {Uint8Array} val
* @param {Options} [options]
*/
put (key, val, options) {
return this.child.put(key, val, options)
}
/**
* @param {Key} key
* @param {Options} [options]
*/
get (key, options) {
return this.child.get(key, options)
}
/**
* @param {Key} key
* @param {Options} [options]
*/
has (key, options) {
return this.child.has(key, options)
}
/**
* @param {Key} key
* @param {Options} [options]
*/
delete (key, options) {
return this.child.delete(key, options)
}
batch () {
return this.child.batch()
}
/**
* @param {Query} q
* @param {Options} [options]
*/
query (q, options) {
const tq = {
keysOnly: q.keysOnly,
offset: q.offset,
limit: q.limit,
/** @type Array<(items: Pair[]) => Await<Pair[]>> */
orders: [],
filters: [
/** @type {(item: Pair) => boolean} */
e => e.key.toString() !== shardKey.toString(),
/** @type {(item: Pair) => boolean} */
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) {
// @ts-ignore - can't find a way to easily type this
const filters = q.filters.map((f) => (e) => {
return f(Object.assign({}, e, {
key: this._invertKey(e.key)
}))
})
tq.filters = tq.filters.concat(filters)
}
if (q.orders != null) {
tq.orders = q.orders.map((o) => async (res) => {
res.forEach((e) => { e.key = this._invertKey(e.key) })
const ordered = await o(res)
ordered.forEach((e) => { e.key = this._convertKey(e.key) })
return ordered
})
}
return this.child.query(tq, options)
}
close () {
return this.child.close()
}
}
module.exports = ShardingDatastore