git ssb

0+

Matt McKegg / ssb-backlinks



Tree: b1068aef134784c8c327b16d432ecaf218ea86de

Files: b1068aef134784c8c327b16d432ecaf218ea86de / lib / flumeview-links-raw.js

2813 bytesRaw
1// FORKED TO REMOVE FILTERING APPLIED TO INDEXED DATA RESULT
2// AND ADD unbox OPTION
3
4'use strict'
5var pull = require('pull-stream')
6var query = require('flumeview-query/query')
7var select = require('flumeview-query/select')
8var mfr = require('map-filter-reduce')
9var u = require('flumeview-query/util')
10var Flatmap = require('pull-flatmap')
11var FlumeViewLevel = require('flumeview-level')
12
13var isArray = Array.isArray
14
15module.exports = function (indexes, links, version, unbox) {
16 if(!links)
17 links = function (data, emit) { emit(data) }
18
19 function getIndexes (data, seq) {
20 var A = []
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)
29 A.push(a)
30 })
31 return A
32 }
33
34 var create = FlumeViewLevel(version || 1, function (msg, seq) {
35 var result = []
36 if (typeof msg.value.content === 'string' && unbox) {
37 msg = unbox(msg)
38 }
39 links(msg, function (value) {
40 result = result.concat(getIndexes(value, seq))
41 })
42 return result
43 })
44
45 return function (log, name) {
46 var index = create(log, name)
47 var read = index.read
48
49 index.read = function (opts) {
50
51 opts = opts || {}
52 var _opts = {}
53 var q, k
54
55 if(isArray(opts.query)) {
56 q = opts.query[0].$filter || {}
57 }
58 else if(opts.query) {
59 q = opts.query
60 }
61 else
62 q = {}
63
64 var index = select(indexes, q)
65
66 // HACK: allow manual selection of indexes
67 if (opts.index) {
68 index = indexes.find(x => x.key === opts.index)
69 }
70
71 if(!index)
72 return pull(
73 log.stream({
74 values: true, seqs: false, live: opts.live, limit: opts.limit, reverse: opts.reverse
75 }),
76 Flatmap(function (data) {
77 var emit = []
78 links(data, function (a) {
79 emit.push(a)
80 })
81 return emit
82 })
83 )
84 var _opts = query(index, q)
85
86 _opts.values = true
87 _opts.keys = true
88
89 _opts.reverse = !!opts.reverse
90 _opts.live = opts.live
91 _opts.old = opts.old
92 _opts.sync = opts.sync
93 _opts.limit = opts.limit
94
95 return pull(
96 read(_opts),
97 pull.map(function (data) {
98 if (data.sync) return data
99 var msg = data.value
100 if (typeof msg.value.content === 'string' && unbox) {
101 msg = unbox(msg)
102 }
103 for (var i = 0; i < index.value.length; i++) {
104 u.set(index.value[i], data.key[i + 1], msg)
105 }
106 return msg
107 }),
108 isArray(opts.query) ? mfr(opts.query) : pull.through()
109 )
110 }
111 return index
112 }
113}
114

Built with git-ssb-web