@@ -7,42 +7,28 @@ const pullAwaitable = require('pull-awaitable')
7
7
const cat = require ( 'pull-cat' )
8
8
const { safeFilename } = require ( './files' )
9
9
10
- function query ( ...cbs ) {
11
- let res = cbs [ 0 ]
12
- for ( let i = 1 , n = cbs . length ; i < n ; i ++ ) if ( cbs [ i ] ) res = cbs [ i ] ( res )
13
- return res
14
- }
10
+ //#region Helper functions and util operators
15
11
16
- function fromDB ( db ) {
17
- return {
18
- meta : { db } ,
12
+ function copyMeta ( orig , dest ) {
13
+ if ( orig . meta ) {
14
+ dest . meta = orig . meta
19
15
}
20
16
}
21
17
22
- function toBufferOrFalsy ( value ) {
23
- if ( ! value ) return value
24
- return Buffer . isBuffer ( value ) ? value : Buffer . from ( value )
25
- }
26
-
27
- function seqs ( values ) {
28
- return {
29
- type : 'SEQS' ,
30
- seqs : values ,
31
- }
18
+ function updateMeta ( orig , key , value ) {
19
+ const res = Object . assign ( { } , orig )
20
+ res . meta [ key ] = value
21
+ return res
32
22
}
33
23
34
- function liveSeqs ( pullStream ) {
35
- return {
36
- type : 'LIVESEQS' ,
37
- stream : pullStream ,
38
- }
24
+ function extractMeta ( orig ) {
25
+ const meta = orig . meta
26
+ return meta
39
27
}
40
28
41
- function offsets ( values ) {
42
- return {
43
- type : 'OFFSETS' ,
44
- offsets : values ,
45
- }
29
+ function toBufferOrFalsy ( value ) {
30
+ if ( ! value ) return value
31
+ return Buffer . isBuffer ( value ) ? value : Buffer . from ( value )
46
32
}
47
33
48
34
function seekFromDesc ( desc ) {
@@ -58,6 +44,40 @@ function seekFromDesc(desc) {
58
44
}
59
45
}
60
46
47
+ function query ( ...cbs ) {
48
+ let res = cbs [ 0 ]
49
+ for ( let i = 1 , n = cbs . length ; i < n ; i ++ ) if ( cbs [ i ] ) res = cbs [ i ] ( res )
50
+ return res
51
+ }
52
+
53
+ function debug ( ) {
54
+ return ( ops ) => {
55
+ const meta = JSON . stringify ( ops . meta , ( key , val ) =>
56
+ key === 'db' ? void 0 : val
57
+ )
58
+ console . log (
59
+ 'debug' ,
60
+ JSON . stringify (
61
+ ops ,
62
+ ( key , val ) => {
63
+ if ( key === 'meta' ) return void 0
64
+ else if ( key === 'task' && typeof val === 'function' )
65
+ return '[Function]'
66
+ else if ( key === 'value' && val . type === 'Buffer' )
67
+ return Buffer . from ( val . data ) . toString ( )
68
+ else return val
69
+ } ,
70
+ 2
71
+ ) ,
72
+ meta === '{}' ? '' : 'meta: ' + meta
73
+ )
74
+ return ops
75
+ }
76
+ }
77
+
78
+ //#endregion
79
+ //#region "Unit operators": they create objects that JITDB interprets
80
+
61
81
function slowEqual ( seekDesc , target , opts ) {
62
82
opts = opts || { }
63
83
const seek = seekFromDesc ( seekDesc )
@@ -150,13 +170,6 @@ function includes(seek, target, opts) {
150
170
}
151
171
}
152
172
153
- function not ( ops ) {
154
- return {
155
- type : 'NOT' ,
156
- data : [ ops ] ,
157
- }
158
- }
159
-
160
173
function gt ( value , indexName ) {
161
174
if ( typeof value !== 'number' ) throw new Error ( 'gt() needs a number arg' )
162
175
return {
@@ -201,62 +214,51 @@ function lte(value, indexName) {
201
214
}
202
215
}
203
216
204
- function deferred ( task ) {
217
+ function seqs ( values ) {
205
218
return {
206
- type : 'DEFERRED ' ,
207
- task ,
219
+ type : 'SEQS ' ,
220
+ seqs : values ,
208
221
}
209
222
}
210
223
211
- function debug ( ) {
212
- return ( ops ) => {
213
- const meta = JSON . stringify ( ops . meta , ( key , val ) =>
214
- key === 'db' ? void 0 : val
215
- )
216
- console . log (
217
- 'debug' ,
218
- JSON . stringify (
219
- ops ,
220
- ( key , val ) => {
221
- if ( key === 'meta' ) return void 0
222
- else if ( key === 'task' && typeof val === 'function' )
223
- return '[Function]'
224
- else if ( key === 'value' && val . type === 'Buffer' )
225
- return Buffer . from ( val . data ) . toString ( )
226
- else return val
227
- } ,
228
- 2
229
- ) ,
230
- meta === '{}' ? '' : 'meta: ' + meta
231
- )
232
- return ops
224
+ function liveSeqs ( pullStream ) {
225
+ return {
226
+ type : 'LIVESEQS' ,
227
+ stream : pullStream ,
233
228
}
234
229
}
235
230
236
- function copyMeta ( orig , dest ) {
237
- if ( orig . meta ) {
238
- dest . meta = orig . meta
231
+ function offsets ( values ) {
232
+ return {
233
+ type : 'OFFSETS' ,
234
+ offsets : values ,
239
235
}
240
236
}
241
237
242
- function updateMeta ( orig , key , value ) {
243
- const res = Object . assign ( { } , orig )
244
- res . meta [ key ] = value
245
- return res
238
+ function deferred ( task ) {
239
+ return {
240
+ type : 'DEFERRED' ,
241
+ task,
242
+ }
246
243
}
247
244
248
- function extractMeta ( orig ) {
249
- const meta = orig . meta
250
- return meta
245
+ //#endregion
246
+ //#region "Combinator operators": they build composite operations
247
+
248
+ function not ( ops ) {
249
+ return {
250
+ type : 'NOT' ,
251
+ data : [ ops ] ,
252
+ }
251
253
}
252
254
253
255
function and ( ...args ) {
254
- const rhs = args
255
- . map ( ( arg ) => ( typeof arg === 'function' ? arg ( ) : arg ) )
256
- . filter ( ( arg ) => ! ! arg )
257
- return ( ops ) => {
256
+ return ( ops , isSpecialOps ) => {
257
+ const rhs = args
258
+ . map ( ( arg ) => ( typeof arg === 'function' ? arg ( ops , true ) : arg ) )
259
+ . filter ( ( arg ) => ! ! arg )
258
260
const res =
259
- ops && ops . type
261
+ ops && ops . type && ! isSpecialOps
260
262
? {
261
263
type : 'AND' ,
262
264
data : [ ops , ...rhs ] ,
@@ -273,12 +275,12 @@ function and(...args) {
273
275
}
274
276
275
277
function or ( ...args ) {
276
- const rhs = args
277
- . map ( ( arg ) => ( typeof arg === 'function' ? arg ( ) : arg ) )
278
- . filter ( ( arg ) => ! ! arg )
279
- return ( ops ) => {
278
+ return ( ops , isSpecialOps ) => {
279
+ const rhs = args
280
+ . map ( ( arg ) => ( typeof arg === 'function' ? arg ( ops , true ) : arg ) )
281
+ . filter ( ( arg ) => ! ! arg )
280
282
const res =
281
- ops && ops . type
283
+ ops && ops . type && ! isSpecialOps
282
284
? {
283
285
type : 'OR' ,
284
286
data : [ ops , ...rhs ] ,
@@ -294,6 +296,15 @@ function or(...args) {
294
296
}
295
297
}
296
298
299
+ //#endregion
300
+ //#region "Special operators": they only update meta
301
+
302
+ function fromDB ( db ) {
303
+ return {
304
+ meta : { db } ,
305
+ }
306
+ }
307
+
297
308
function live ( opts ) {
298
309
if ( opts && opts . old ) return ( ops ) => updateMeta ( ops , 'live' , 'liveAndOld' )
299
310
else return ( ops ) => updateMeta ( ops , 'live' , 'liveOnly' )
@@ -311,6 +322,9 @@ function paginate(pageSize) {
311
322
return ( ops ) => updateMeta ( ops , 'pageSize' , pageSize )
312
323
}
313
324
325
+ //#endregion
326
+ //#region "Consumer operators": they execute the query tree
327
+
314
328
async function executeDeferredOps ( ops , meta ) {
315
329
// Collect all deferred tasks and their object-traversal paths
316
330
const allDeferred = [ ]
@@ -420,6 +434,8 @@ function toAsyncIter() {
420
434
}
421
435
}
422
436
437
+ //#endregion
438
+
423
439
module . exports = {
424
440
fromDB,
425
441
query,
0 commit comments