git ssb

0+

Dominic / flumeview-query



Commit 0dbc8d028f30f0dc315e970c0cc8c7792e4b5b44

split query and links into two parts

Dominic Tarr committed on 11/22/2016, 8:52:49 AM
Parent: 5866169f0371367f273daf5dd1f21f38b5a0e236

Files changed

index.jschanged
links.jsadded
index.jsView
@@ -12,51 +12,38 @@
1212 var isArray = Array.isArray
1313
1414 //sorted index.
1515
16 +//split this into TWO modules. flumeview-links and flumeview-query
17 +module.exports = function (indexes, version) {
1618
17-module.exports = function (indexes, links, version) {
18-
19- if(!links)
20- links = function (data, emit) { emit(data) }
21-
2219 var create = FlumeViewLevel(version || 1, function (data, seq) {
23-
2420 var A = []
25-
26- links(data, function (link) {
27- indexes.forEach(function (index) {
28- var a = [index.key]
29- for(var i = 0; i < index.value.length; i++) {
30- var key = index.value[i]
31- if(!u.has(key, link)) return
32- a.push(u.get(key, link))
33- }
34- a.push(seq)
35- A.push(a)
36- })
21 + indexes.forEach(function (index) {
22 + var a = [index.key]
23 + for(var i = 0; i < index.value.length; i++) {
24 + var key = index.value[i]
25 + if(!u.has(key, data)) return []
26 + a.push(u.get(key, data))
27 + }
28 + a.push(seq); A.push(a)
3729 })
38-
3930 return A
40-
4131 })
4232
4333 return function (log, name) {
4434
4535 var index = create(log, name)
46-
4736 var read = index.read
4837
4938 index.read = function (opts) {
5039
51- var lookup
5240 opts = opts || {}
5341 var _opts = {}
5442 var q, k
5543
5644 if(isArray(opts.query)) {
5745 q = opts.query[0].$filter || {}
58-// k = keys(opts.query)
5946 }
6047 else if(opts.query) {
6148 q = opts.query
6249 }
@@ -64,56 +51,30 @@
6451 q = {}
6552
6653 var index = select(indexes, q)
6754
68- //just a hack. consider this disabled...
69-// if(index.key === 'log') {
70-// //TODO: handle log properly.
71-// //insert log
72-// return pull(
73-// log.stream(opts),
74-// isArray(opts.query) ? mfr(opts.query) : pull.through()
75-// )
76-// }
55 + if(!index)
56 + return log.stream({
57 + values: true, seqs: false, live: opts.live, limit: opts.limit, reverse: opts.reverse
58 + })
7759
78-
7960 var _opts = query(index, q)
8061
8162 _opts.values = false
8263 _opts.keys = true
83-// _opts.keyEncoding = codec
8464
8565 _opts.reverse = !!opts.reverse
8666 _opts.live = opts.live
8767 _opts.old = opts.old
8868 _opts.sync = opts.sync
89-// _opts.limit = opts.limit || -1
9069
9170 return pull(
92- read(_opts),
93- //rehydrate the index to resemble the original object.
94- pull.map(function (data) {
95- console.log(data)
96- if(data.sync) return data
97- var o = {}
98- for(var i = 0; i < index.value.length; i++)
99- u.set(index.value[i], data.key[i+1], o)
100- return o
101- }),
102- lookup,
103- isArray(opts.query) ? mfr(opts.query) : pull.through(),
104- opts.limit > 0 ? pull.take(opts.limit) : pull.through()
71 + read(_opts), pull.map('value'),
72 + isArray(opts.query) ? mfr(opts.query) : pull.through()
10573 )
10674
10775 }
10876
10977 return index
11078 }
111-
11279 }
11380
114-
115-
116-
117-
118-
119-
links.jsView
@@ -1,0 +1,106 @@
1 +'use strict'
2 +var pull = require('pull-stream')
3 +var query = require('./query')
4 +var select = require('./select')
5 +var mfr = require('map-filter-reduce')
6 +var keys = require('map-filter-reduce/keys')
7 +var explain = require('explain-error')
8 +var u = require('./util')
9 +var Flatmap = require('pull-flatmap')
10 +var FlumeViewLevel = require('flumeview-level')
11 +
12 +var isArray = Array.isArray
13 +
14 +//sorted index.
15 +
16 +//split this into TWO modules. flumeview-links and flumeview-query
17 +module.exports = function (indexes, links, version) {
18 +
19 + if(!links)
20 + links = function (data, emit) { emit(data) }
21 +
22 + function getIndexes (data, seq) {
23 + var A = []
24 + indexes.forEach(function (index) {
25 + var a = [index.key]
26 + for(var i = 0; i < index.value.length; i++) {
27 + var key = index.value[i]
28 + if(!u.has(key, data)) return
29 + a.push(u.get(key, data))
30 + }
31 + a.push(seq)
32 + A.push(a)
33 + })
34 + return A
35 + }
36 +
37 + var create = FlumeViewLevel(version || 1, function (value, seq) {
38 + var A = []
39 + links(value, function (value) {
40 + A = A.concat(getIndexes(value, seq))
41 + })
42 + return A
43 + })
44 +
45 + return function (log, name) {
46 +
47 + var index = create(log, name)
48 + var read = index.read
49 +
50 + index.read = function (opts) {
51 +
52 + opts = opts || {}
53 + var _opts = {}
54 + var q, k
55 +
56 + if(isArray(opts.query)) {
57 + q = opts.query[0].$filter || {}
58 + }
59 + else if(opts.query) {
60 + q = opts.query
61 + }
62 + else
63 + q = {}
64 +
65 + var index = select(indexes, q)
66 +
67 + if(!index)
68 + return pull(
69 + log.stream({
70 + values: true, seqs: false, live: opts.live, limit: opts.limit, reverse: opts.reverse
71 + }),
72 + Flatmap(function (data) {
73 + var emit = []
74 + links(data, function (a) {
75 + emit.push(a)
76 + })
77 + return emit
78 + })
79 + )
80 + var _opts = query(index, q)
81 +
82 +
83 + _opts.values = false
84 + _opts.keys = true
85 +
86 + _opts.reverse = !!opts.reverse
87 + _opts.live = opts.live
88 + _opts.old = opts.old
89 + _opts.sync = opts.sync
90 +
91 + return pull(
92 + read(_opts),
93 + pull.map(function (data) {
94 + if(data.sync) return data
95 + var o = {}
96 + for(var i = 0; i < index.value.length; i++)
97 + u.set(index.value[i], data.key[i+1], o)
98 + return o
99 + }),
100 + isArray(opts.query) ? mfr(opts.query) : pull.through()
101 + )
102 + }
103 + return index
104 + }
105 +}
106 +

Built with git-ssb-web