-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtable.js
420 lines (352 loc) · 9.85 KB
/
memtable.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
var lodash = require('lodash')
var assert = require('assert')
var highland = require('highland')
//secondary:[] secondary keys
//primary:'id' id prop
//resume:[] data to resume
//filterable:[] keys to store in memory to filter on
module.exports = function(props){
var state = {}
var propsToKeep = []
var propsRequired = []
var uniqueProps = []
var primaryProps = []
function getTable(index){
assert(index === 0 || index,'requires table index')
index = makeKey(index)
var table = state[index]
assert(table,'unable to find table with index ' + index)
return table
}
//joins array of properties together to make a key
function makeKey(keys){
if(lodash.isArray(keys)){
return lodash.join(keys,props.delimiter)
}
//order is not guaranteed here so this is a problem
if(lodash.isObject(keys)){
return lodash(keys).keys().join(props.delimiter)
}
return keys
}
//check for unique props by looking at all secondary ids
//then comparing to make sure values primary ids match or dont exist
function collides(value){
var id = getPrimaryID(value)
return lodash.some(props.secondary,function(index){
var val = null
try{
val = getBy(index,compositeIndex(index,value))
}catch(e){
return false
}
return getPrimaryID(val) != id
})
}
function touchesPrimary(prop){
assert(prop,'requires prop')
prop = lodash.toPath(prop)
prop = prop[0]
return lodash.some(primaryProps,function(p){
return p == prop
})
}
function getPrimaryID(value){
return compositeIndex(props.primary,value)
}
//gets unique key from object and returns array
function compositeIndex(composite,value){
if(!lodash.isArray(composite)){
return lodash.get(value,composite)
}
var hasAllProps = lodash.every(composite,function(prop){
return lodash.has(value,prop)
})
assert(hasAllProps,'Object Missing required composite properties: ' + makeKey(composite))
return makeKey(lodash.reduce(composite,function(result,prop){
result.push(lodash.get(value,prop))
return result
},[]))
}
function setBy(index,value){
var table = getTable(index)
index = compositeIndex(index,value)
table[index] = value
return value
}
function getBy(index,id){
assert(id === 0 || id,'requires id')
var table = getTable(index)
id = makeKey(id)
var result = table[id]
assert(result,'unable to find id: ' + id + ' on index: ' + index)
return result
}
function removeBy(index,value){
var table = getTable(index)
if(lodash.isArray(index)){
index = compositeIndex(index,value)
}else{
index = value[index]
}
delete table[index]
}
function hasBy(index,id){
try{
getBy(index,id)
return true
}catch(e){
if(props.warn) console.log(e)
return false
}
}
function primary(){
return getTable(props.primary)
}
function strip(value){
return lodash.pick(value,propsToKeep)
}
//takes an object, and returns whats stored in table
//based on input object propsk
function get(value){
assert(value,'requires value with primary id')
var index = null
if(lodash.isArray(props.primary)){
index = compositeIndex(props.primary,value)
}else{
index = value[index]
}
return getBy(props.primary,index)
}
function updateSecondaryIDs(next,prev){
lodash.each(props.secondary,function(index){
var a, b = null
if(lodash.isArray(index)){
try{
a = compositeIndex(index,prev)
b = compositeIndex(index,next)
}catch(e){
if(props.warn) console.log(e)
}
}else{
a = lodash.get(prev,index)
b = lodash.get(next,index)
}
if(a==b) return
removeBy(index,prev)
})
return next
}
function removeSecondaryIndices(value){
lodash.each(props.secondary,function(index){
try{
removeBy(index,value)
}catch(e){
if(props.warn) console.log(e)
}
})
}
function remove(value){
assert(value,'requires value with id')
removeBy(props.primary,value)
removeSecondaryIndices(value)
return value
}
//dumb set
function set(value){
assert(value,'requires value with id prop')
var tosave = props.saveAll ? value : strip(value)
lodash.each(propsRequired,function(prop){
assert(lodash.has(tosave,prop),'required property ' + prop + ' not found')
})
lodash.each(props.secondary,function(index){
try{
setBy(index,tosave)
}catch(e){
if(props.warn) console.log(e)
}
})
setBy(props.primary,tosave)
return value
}
function isMatch(objectToSearch,query,propsToMatch,insensitive){
return lodash.find(propsToMatch,function(prop){
var value = insensitive ? lodash.toUpper(lodash.get(objectToSearch,prop)) : lodash.get(objectToSearch,prop)
return lodash.includes(value,query)
})
}
var methods = {}
methods.getBy = function(prop,key){
return getBy(prop,key)
}
methods.get = function(id){
return methods.getBy(props.primary,id)
}
methods.getAll = function(ids){
return lodash.map(ids,function(id){
return methods.get(id)
})
}
methods.getAllBy = function(prop,keys){
return lodash.map(keys,function(key){
return methods.getBy(prop,key)
})
}
methods.hasBy = function(prop,id){
return hasBy(prop,id)
}
methods.has = function(id){
return methods.hasBy(props.primary,id)
}
methods.hasAll = function(ids){
return lodash.map(ids,function(id){
return methods.has(id)
})
}
methods.hasAllBy = function(prop,ids){
return lodash.map(ids,function(id){
return methods.hasBy(prop,id)
})
}
methods.set = function(value){
assert(!collides(value),'Trying to set a record which collides with unique value of another record')
var id = getPrimaryID(value)
if(methods.has(id)){
//value exists at this id, delete all secondary references
var prev = getBy(props.primary,id)
updateSecondaryIDs(value,prev)
}
var result = set(value)
props.onChange(result,id)
return value
}
methods.setAll = function(values){
return lodash.map(values,methods.set)
}
//index is the table index name
//id is the unique id of the item
//prop is the property on the item to update
//value is the value on the item property to update
methods.updateBy = function(index,id,kv){
assert(lodash.isPlainObject(kv),'requires an object with key value')
var item = methods.getBy(index,id)
//if all props are the same do nothing and return item
if(lodash.every(kv,function(value,key){
return lodash.isEqual(value,item[key])
})){
return item
}
lodash.each(function(value,key){
assert(!touchesPrimary(key),'you cannot update primary id, use set instead')
})
//we need to remove all references to secondary ids which may have changed
return methods.set(lodash.assign({},item,kv))
// return methods.set(lodash.assign(lodash.cloneDeep(item),kv))
}
//update record by merging in kv
methods.update = function(id,kv){
return methods.updateBy(props.primary,id,kv)
}
methods.search = function(query,insensitive){
query = insensitive ? lodash.toUpper(query) : query
return lodash.filter(primary(),function(value,key){
return isMatch(value,query,props.searchable,insensitive)
})
}
methods.filter = function(filter){
return lodash.filter(primary(),filter)
}
methods.reduce = function(reduce,start){
return lodash.reduce(primary(),reduce,start)
}
methods.map = function(map){
return lodash.map(primary(),map)
}
methods.each = function(each){
return lodash.each(primary(),each)
}
//syncronous data iteration
methods.lodash = function(includeKeys){
if(includeKeys) return lodash(primary())
return lodash(primary()).values()
}
//async stream data
methods.highland = function(includeKeys){
if(includeKeys) return highland.pairs(primary())
return highland.values(primary())
}
methods.list = function(){
return lodash.values(primary())
}
methods.sort = function(props,direction){
return lodash.orderBy(primary(),props,direction)
}
methods.removeBy = function(prop,id){
var value = remove(methods.getBy(prop,id))
props.onRemove(value,getPrimaryID(value))
return value
}
methods.remove = function(id){
return methods.removeBy(props.primary,id)
}
methods.removeAll = function(ids){
return lodash.map(ids,methods.remove)
}
methods.removeAllBy = function(prop,ids){
return lodash.map(ids,function(id){
return methods.removeBy(prop,id)
})
}
methods.drop = function(){
lodash.each(state,function(value,key){
state[key] = {}
})
}
methods.state = function(){
return state
}
methods.getPrimaryID = getPrimaryID
function init(p){
props = lodash.defaults(p,{
primary:'id',
secondary:[],
searchable:[],
required:[],
save:[],
resume:[],
saveAll:false,
delimiter:'.',
onChange:function(x){return x},
onRemove:function(x){return x},
})
propsToKeep = lodash([props.primary])
.concat(
props.searchable,
props.secondary,
props.save,
props.required
)
.flattenDeep()
.uniq()
.value()
propsRequired = lodash([props.primary])
.concat(props.required)
.flattenDeep()
.uniq()
.value()
uniqueProps = lodash([props.primary])
.concat(props.secondary)
.flattenDeep()
.uniq()
.value()
primaryProps = lodash.flatten([props.primary])
state[makeKey(props.primary)] = {}
lodash.each(props.secondary,function(index){
index = makeKey(index)
state[index] = {}
})
lodash.each(props.resume,set)
return methods
}
return init(props)
}