-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.js
More file actions
369 lines (339 loc) · 11 KB
/
Copy pathadapter.js
File metadata and controls
369 lines (339 loc) · 11 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { crocks, HyperErr, R } from './deps.js'
import { handleHyperErr } from './utils.js'
const { Async } = crocks
const { always, append, identity, ifElse, isNil, not } = R
/**
* Create a hyper store cache key, with the given prefix.
* By default, the prefix is hashtagged to ensure all keys "contained" in the prefix
* are stored within the same hash slot. This is useful when performing multi-key operations
* on a Redis cluster. See https://github.com/hyper63/hyper-adapter-redis/issues/17
*
* See: https://redis.io/docs/reference/cluster-spec/#hash-tags
*
* @param {string} prefix - the prefix for the key, typically the hyper cache store name
* @param {string} key - the key
* @param {boolean} [hashSlot] - whether to hashtag the prefix, which will cause all keys
* "within" this prefix to be mapped to the same hash slot. This is useful for multi-key operations
* performed on a Redis cluster. Defaults to false, which is to say "hashtag the prefix"
* @returns
*/
const createKey = (prefix, key, hashSlot = true) =>
`${hashSlot ? `{${prefix}}` : `${prefix}`}_${key}`
/**
* Pages sizes above ~2500 will cause maximum callstack exceeded errors
* when using Async.all() which uses recursion internally.
*
* But we only use Async.all() when hashSlots are disabled, to sideskirt CROSSSLOT
* errors: see https://github.com/hyper63/hyper-adapter-redis/issues/17
*
* So if hashSlots are enabled, then we simply return the provided count, nooping. Otherwise,
* we return the minimum between count, and our limit of 2500 we have tested to work despite
* Async.all()'s implementation.
*
* This is purely an implementation detail, when performing multi key operations
* and so does not impact the consumer:
* - listDocs
* - deleting all keys in hyper Cache store, as part of destroyStore()
*/
const maxPageSize = (count, hashSlot) => hashSlot ? count : Math.min(count, 2500)
const mapTtl = (ttl) =>
ifElse(
() => not(isNil(ttl)),
/**
* If ttl is <0, then it should be expired
* immediately.
*
* Setting the ttl to 1 effectively does that.
*/
append({ px: Math.max(Number(ttl), 1) }),
/**
* No ttl, ergo live forever
*/
identity,
)
/**
* @typedef Options
* @property {number} scanCount
* @property {any} client
*
* @param {Options} options
*/
export default function ({ redis, scanCount, hashSlot = true }) {
// redis commands
// key: Promise<string>
const get = Async.fromPromise(redis.get.bind(redis))
// key, value, { px, ex }: Promise<string>
const set = Async.fromPromise(redis.set.bind(redis))
// key, key, key: Promise<string[]>
const del = Async.fromPromise(redis.del.bind(redis))
// cursor, { type, pattern }: Promise<[string, string[]]>
const scan = Async.fromPromise(redis.scan.bind(redis))
// key, key, key: Promise<any[]>
const mget = Async.fromPromise(redis.mget.bind(redis))
const index = () => {
return Promise.resolve(HyperErr({ status: 501, msg: 'Not Implemented' }))
}
const checkIfStoreExists = (store) => (key) =>
get(createKey('store', store, false))
.chain(
(_) =>
_ ? Async.Resolved(key) : Async.Rejected(HyperErr({
status: 400,
msg: 'Store does not exist',
})),
)
const checkForConflict = ([id]) =>
get(id)
.chain(
(_) =>
_
? Async.Rejected(HyperErr({
status: 409,
msg: 'Document Conflict',
}))
: Async.Resolved([id]),
)
/**
* TODO: should this return error if store already exists?
*
* @param {string} name
* @returns {Promise<object>}
*/
const createStore = (name) =>
Async.of([])
.map(append(createKey('store', name, false)))
.map(append('active'))
.chain((args) => set(...args))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
)
.toPromise()
/**
* TODO: should this return error if store doesn't exist?
*
* @param {string} name
* @returns {Promise<object>}
*/
const destroyStore = (name) =>
Async.of(createKey(name, '*', hashSlot))
// grab all keys belonging to this store
.chain((matcher) => getKeys(scan, matcher, scanCount))
.chain(
ifElse(
(keys) => keys.length > 0,
deleteKeys(del, scanCount, hashSlot),
(keys) => Async.of(keys),
),
)
// Delete the key that tracks the store's existence
.chain(() => del(createKey('store', name, false)))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
)
.toPromise()
/**
* @param {CacheDoc}
* @returns {Promise<object>}
*/
const createDoc = ({ store, key, value, ttl }) =>
Async.of([createKey(store, key, hashSlot)])
.chain(checkIfStoreExists(store))
// don't allow over-writting of existing keys
.chain(checkForConflict)
.map(append(JSON.stringify(value)))
.map(mapTtl(ttl))
.chain((args) => set(...args))
.bichain(
handleHyperErr,
always(Async.Resolved({
ok: true,
doc: value,
})),
)
.toPromise()
/**
* @param {CacheDoc}
* @returns {Promise<object>}
*/
const getDoc = ({ store, key }) =>
Async.of(createKey(store, key, hashSlot))
.chain(checkIfStoreExists(store))
.chain(get)
.chain((v) => {
if (!v) {
return Async.Rejected(HyperErr({
status: 404,
msg: 'document not found',
}))
}
return Async.Resolved(JSON.parse(v))
})
.bichain(
handleHyperErr,
(v) => Async.Resolved(v),
)
.toPromise()
/**
* @param {CacheDoc}
* @returns {Promise<object>}
*/
const updateDoc = ({ store, key, value, ttl }) =>
Async.of([])
.chain(checkIfStoreExists(store))
.map(append(createKey(store, key, hashSlot)))
.map(append(JSON.stringify(value)))
.map(mapTtl(ttl))
.chain((args) => set(...args))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
)
.toPromise()
/**
* @param {CacheDoc}
* @returns {Promise<object>}
*/
const deleteDoc = ({ store, key }) =>
checkIfStoreExists(store)()
.chain(() => del(createKey(store, key, hashSlot)))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
)
.toPromise()
/**
* @param {CacheQuery}
* @returns {Promise<object>}
*/
const listDocs = ({ store, pattern = '*' }) => {
return getKeys(scan, createKey(store, pattern, hashSlot), scanCount)
.chain(getValues({ get, mget }, store, scanCount, hashSlot))
.bichain(
handleHyperErr,
(docs) => Async.Resolved({ ok: true, docs }),
)
.toPromise()
}
return Object.freeze({
index,
createStore,
destroyStore,
createDoc,
getDoc,
updateDoc,
deleteDoc,
listDocs,
})
}
function getKeys(scan, matcher, count) {
function page(cursor, keys) {
if (cursor === '0') return Async.of(keys).toPromise()
return scan(cursor, { pattern: matcher, count })
.map(([nCursor, nKeys]) => {
keys = keys.concat(nKeys)
/**
* Return a thunk that continues the next iteration, thus ensuring the callstack
* is only ever one call deep.
*
* This is continuation passing style, to be leverage by our trampoline
*/
return () => page(nCursor, keys)
}).toPromise()
}
/**
* Our initial thunk that performs the first scan
*/
return Async.fromPromise(page)(0, [])
.chain(Async.fromPromise(trampoline))
}
function getValues({ get, mget }, store, count, hashSlot) {
const prefix = `${createKey(store, '', hashSlot).split('_').shift()}_`
count = maxPageSize(count, hashSlot)
return function (keys) {
function page(keys, values) {
if (!keys.length) return Async.of(values).toPromise()
const nKeys = keys.splice(0, count)
return Async.of()
/**
* If the adapter is NOT configured to use hash slot for the store,
* then we must get the keys individually see https://github.com/hyper63/hyper-adapter-redis/issues/17
*
* Otherwise, we are using hash slots, so all keys for a store will be mapped
* to a single hash slot and so can be retrieved as a set.
*
* Regardless, we still break up the operations into "pages" according
* to the provided count, so as to not block the Redis thread for too long
* on any given operation
*/
.chain(() => hashSlot ? mget(...nKeys) : Async.all(nKeys.map((key) => get(key))))
.map((nValues) => {
values = values.concat(
nValues.map((v, i) => ({
key: nKeys[i].replace(prefix, ''),
value: JSON.parse(v),
})),
)
/**
* Return a thunk that continues the next iteration, thus ensuring the callstack
* is only ever one call deep.
*
* This is continuation passing style, to be leverage by our trampoline
*/
return () => page(keys, values)
}).toPromise()
}
/**
* Our initial thunk that performs the first scan
*/
return Async.fromPromise(page)(keys, [])
.chain(Async.fromPromise(trampoline))
}
}
function deleteKeys(del, count, hashSlot) {
return function (keys) {
count = maxPageSize(count, hashSlot)
function page(keys) {
if (!keys.length) return Async.of([]).toPromise()
const nKeys = keys.splice(0, count)
return Async.of()
/**
* If the adapter is NOT configured to use hash slot for the store,
* then we must del the keys individually see https://github.com/hyper63/hyper-adapter-redis/issues/17
*
* Otherwise, we are using hash slots, so all keys for a store will be mapped
* to a single hash slot and so can be deleted as a set.
*
* Regardless, we still break up the operations into "pages" according
* to the provided count, so as to not block the Redis thread for too long
* on any given operation
*/
.chain(() => hashSlot ? del(...nKeys) : Async.all(nKeys.map((key) => del(key))))
/**
* Return a thunk that continues the next iteration, thus ensuring the callstack
* is only ever one call deep.
*
* This is continuation passing style, to be leverage by our trampoline
*/
.map(() => () => page(keys)).toPromise()
}
/**
* Our initial thunk that performs the first scan
*/
return Async.of(keys)
.chain(Async.fromPromise(page))
.chain(Async.fromPromise(trampoline))
}
}
async function trampoline(init) {
let result = init
/**
* Call toPromise() on the Async, to unwrap each iteration
* instead of chaining them in a single Async.
*
* This prevents overflowing the callback, and gives us our trampoline
*/
while (typeof result === 'function') result = await result()
return result
}