forked from digidem/osm-p2p-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
356 lines (336 loc) · 9.35 KB
/
index.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
var hyperkv = require('hyperkv')
var hyperkdb = require('hyperlog-kdb-index')
var kdbtree = require('kdb-tree-store')
var sub = require('subleveldown')
var randomBytes = require('randombytes')
var has = require('has')
var once = require('once')
var through = require('through2')
var readonly = require('read-only-stream')
var xtend = require('xtend')
var join = require('hyperlog-join')
var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter
var hex2dec = require('./lib/hex2dec.js')
var lock = require('mutexify')
var defined = require('defined')
module.exports = DB
inherits(DB, EventEmitter)
function DB (opts) {
var self = this
if (!(self instanceof DB)) return new DB(opts)
self.log = opts.log
self.db = opts.db
self.kv = hyperkv({
log: self.log,
db: sub(self.db, 'kv')
})
self.kv.on('error', function (err) { self.emit('error', err) })
self.lock = lock()
self.kdb = hyperkdb({
log: self.log,
store: opts.store,
db: sub(self.db, 'kdb'),
kdbtree: kdbtree,
types: [ 'float', 'float' ],
map: function (row) {
if (!row.value) return null
var v = row.value.v, d = row.value.d
if (v && v.lat !== undefined && v.lon !== undefined) {
return { type: 'put', point: ptf(v) }
} else if (d && Array.isArray(row.value.points)) {
return { type: 'del', points: row.value.points.map(ptf) }
}
function ptf (x) { return [ x.lat, x.lon ] }
}
})
self.kdb.on('error', function (err) { self.emit('error', err) })
self.refs = join({
log: self.log,
db: sub(self.db, 'r'),
map: function (row) {
if (!row.value) return
var k = row.value.k, v = row.value.v || {}
var ops = []
var refs = v.refs || row.value.refs || []
refs.forEach(function (ref) {
row.links.forEach(function (link) {
ops.push({ type: 'del', key: ref, rowKey: link })
})
if (k) ops.push({ type: 'put', key: ref, value: k })
})
var members = v.members || row.value.members || []
members.forEach(function (member) {
if (typeof member === 'string') member = { ref: member }
if (typeof member.ref !== 'string') return
row.links.forEach(function (link) {
ops.push({ type: 'del', key: member.ref, rowKey: link })
})
if (k) ops.push({ type: 'put', key: member.ref, value: k })
})
return ops
}
})
self.refs.on('error', function (err) { self.emit('error', err) })
self.changeset = join({
log: self.log,
db: sub(self.db, 'c'),
map: function (row) {
if (!row.value) return
var v = row.value.v
if (!v || !v.changeset) return
return { type: 'put', key: v.changeset, value: 0 }
}
})
self.changeset.on('error', function (err) { self.emit('error', err) })
}
DB.prototype._links = function (link, cb) {
var self = this
self.log.get(link, function (err, doc) {
if (err) return cb(err)
self.refs.list(doc.value.k, function (err, rows) {
if (err) cb(err)
else cb(null, rows.map(keyf))
})
})
function keyf (row) { return row.key }
}
DB.prototype.ready = function (cb) {
var self = this
var pending = 2
self.refs.dex.ready(ready)
self.kdb.ready(ready)
function ready () { if (--pending === 0) cb() }
}
DB.prototype.create = function (value, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
if (!cb) cb = noop
var key = hex2dec(randomBytes(8).toString('hex'))
self.put(key, value, opts, function (err, node) {
cb(err, key, node)
})
}
DB.prototype.put = function (key, value, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
if (!cb) cb = noop
self.lock(function (release) {
self.kv.put(key, value, opts, function (err, node) {
release(cb, err, node)
})
})
}
DB.prototype.del = function (key, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
cb = once(cb || noop)
self._del(key, opts, function (err, rows) {
if (err) return cb(err)
self.batch(rows, opts, function (err, nodes) {
if (err) cb(err)
else cb(null, nodes[0])
})
})
}
DB.prototype._del = function (key, opts, cb) {
var self = this
self.kv.get(key, function (err, values) {
if (err) return cb(err)
var pending = 1
var fields = {}
var links = opts.keys || Object.keys(values)
links.forEach(function (ln) {
var v = values[ln] || {}
if (v.lat !== undefined && v.lon !== undefined) {
if (!fields.points) fields.points = []
fields.points.push({ lat: v.lat, lon: v.lon })
}
if (Array.isArray(v.refs)) {
if (!fields.refs) fields.refs = []
fields.refs.push.apply(fields.refs, v.refs)
}
})
cb(null, [ { type: 'del', key: key, links: links, fields: fields } ])
})
}
DB.prototype.batch = function (rows, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
cb = once(cb || noop)
var batch = []
self.lock(function (release) {
var pending = 1 + rows.length
rows.forEach(function (row) {
var key = defined(row.key, row.id)
if (row.type === 'put') {
batch.push(row)
if (--pending === 0) done()
} else if (row.type === 'del') {
self._del(key, xtend(opts, row), function (err, xrows) {
if (err) return release(cb, err)
batch.push.apply(batch, xrows)
if (--pending === 0) done()
})
} else {
var err = new Error('unexpected row type: ' + row.type)
process.nextTick(function () { release(cb, err) })
}
})
if (--pending === 0) done()
function done () {
self.kv.batch(rows, opts, function (err, nodes) {
release(cb, err, nodes)
})
}
})
}
DB.prototype.get = function (key, opts, cb) {
this.kv.get(key, opts, function (err, doc) {
if (err) return cb(err)
else if (doc.type === 'changeset') {
//...
} else cb(null, doc)
})
}
DB.prototype.query = function (q, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}
cb = once(cb || noop)
self.ready(function () {
self.kdb.query(q, opts, onquery)
})
function onquery (err, pts) {
if (err) return cb(err)
var pending = 1, res = [], seen = {}
pts.forEach(function (pt) {
pending++
self._onpt(pt, seen, function (err, r) {
if (r) res.push.apply(res, r)
if (--pending === 0) cb(null, res)
})
})
if (--pending === 0) cb(null, res)
}
}
DB.prototype._onpt = function (pt, seen, cb) {
var self = this
var link = pt.value.toString('hex')
if (has(seen, link)) return cb(null, [])
seen[link] = true
var res = [], pending = 2
self.log.get(link, function (err, doc) {
if (doc && doc.value && doc.value.k && doc.value.v) {
res.push(xtend(doc.value.v, {
id: doc.value.k,
version: doc.key
}))
}
if (--pending === 0) cb(null, res)
})
self._links(link, function onlinks (err, links) {
if (!links) links = []
links.forEach(function (link) {
if (has(seen, link)) return
seen[link] = true
pending++
self.log.get(link, function (err, doc) {
if (doc && doc.value && doc.value.k && doc.value.v) {
res.push(xtend(doc.value.v, {
id: doc.value.k,
version: doc.key
}))
}
if (doc && doc.value && doc.value.k && doc.value.v
&& doc.value.v.type === 'way' && Array.isArray(doc.value.v.refs)) {
addWayNodes(doc.value.v.refs)
} else if (--pending === 0) cb(null, res)
})
pending++
self._links(link, function (err, links) {
onlinks(err, links)
})
})
if (--pending === 0) cb(null, res)
})
function addWayNodes (refs) {
refs.forEach(function (ref) {
if (has(seen, ref)) return
seen[ref] = true
pending++
self.get(ref, function (err, docs) {
Object.keys(docs || {}).forEach(function (key) {
if (has(seen, key)) return
seen[key] = true
res.push(xtend(docs[key], {
id: ref,
version: key
}))
})
if (--pending === 0) cb(null, res)
})
})
if (--pending === 0) cb(null, res)
}
}
DB.prototype.queryStream = function (q, opts) {
var self = this
var stream = through.obj(write)
var seen = {}
self.ready(function () {
var r = self.kdb.queryStream(q, opts)
r.on('error', stream.emit.bind(stream, 'error'))
r.pipe(stream)
})
return readonly(stream)
function write (row, enc, next) {
next = once(next)
var tr = this
self._onpt(row, seen, function (err, res) {
if (res) res.forEach(function (r) { tr.push(r) })
next()
})
}
}
DB.prototype.getChanges = function (key, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
var r = this.changeset.list(key, opts)
var rows = cb ? [] : null
if (cb) cb = once(cb)
var stream = r.pipe(through.obj(write, end))
if (cb) r.once('error', cb)
return readonly(stream)
function write (row, enc, next) {
if (rows) rows.push(row.key)
this.push(row.key)
next()
}
function end (next) {
if (cb) cb(null, rows)
next()
}
}
function noop () {}