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
142 lines (118 loc) · 3.48 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
'use strict'
const { Buffer } = require('buffer')
const { Adapter, Key } = 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)
/**
* 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 {
constructor (store, shard) {
super()
this.child = new KeytransformStore(store, {
convert: this._convertKey.bind(this),
invert: this._invertKey.bind(this)
})
this.shard = shard
}
open () {
return this.child.open()
}
_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)
}
_invertKey (key) {
const s = key.toString()
if (s === shardKey.toString() || s === shardReadmeKey.toString()) {
return key
}
return Key.withNamespaces(key.list().slice(1))
}
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)
}
static async open (store) {
const shard = await sh.readShardFun('/', store)
return new ShardingDatastore(store, shard)
}
static async create (store, shard) {
const exists = await store.has(shardKey)
if (!exists) {
const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store)
return Promise.all([put(shardKey, Buffer.from(shard.toString() + '\n')),
put(shardReadmeKey, Buffer.from(sh.readme))])
}
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}`)
throw new Error('datastore exists')
}
put (key, val, options) {
return this.child.put(key, val, options)
}
get (key, options) {
return this.child.get(key, options)
}
has (key, options) {
return this.child.has(key, options)
}
delete (key, options) {
return this.child.delete(key, options)
}
batch () {
return this.child.batch()
}
query (q, options) {
const tq = {
keysOnly: q.keysOnly,
offset: q.offset,
limit: q.limit,
filters: [
e => e.key.toString() !== shardKey.toString(),
e => e.key.toString() !== shardReadmeKey.toString()
]
}
if (q.prefix != null) {
tq.filters.push((e) => {
return this._invertKey(e.key).toString().startsWith(q.prefix)
})
}
if (q.filters != null) {
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