-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
666 lines (602 loc) · 16.9 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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 once = require('once')
var through = require('through2')
var to = require('to2')
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')
var after = require('after-all')
var mapLimit = require('async/mapLimit')
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.store = opts.store
self.kv = defined(opts.kv, hyperkv({
log: self.log,
db: sub(self.db, 'kv')
}))
self.kv.on('error', function (err) { self.emit('error', err) })
self.lock = lock()
self._restartIndexes()
}
DB.prototype._restartIndexes = function () {
var self = this
if (self.kdb) self.kdb.dex.pause()
if (self.refs) self.refs.dex.pause()
if (self.changeset) self.changeset.dex.pause()
self.kdb = hyperkdb({
log: self.log,
store: self.store,
db: sub(self.db, 'kdb'),
kdbtree: kdbtree,
types: [ 'float64', 'float64' ],
map: function (row, next) {
if (!row.value) return null
var v = row.value.v
var d = row.value.d
if (v && v.lat !== undefined && v.lon !== undefined) {
next(null, { type: 'put', point: ptf(v) })
} else if (d && Array.isArray(row.value.points)) {
var pts = row.value.points.map(ptf)
next(null, { type: 'put', points: pts })
} else next()
}
})
self.kdb.on('error', function (err) { self.emit('error', err) })
self.refs = join({
log: self.log,
db: sub(self.db, 'r'),
map: function (row, cb) {
if (!row.value) return
var k = row.value.k
var v = row.value.v || {}
var d = row.value.d
var ops = []
var next = after(function (err) {
cb(err, ops)
})
// Delete the old refs for this osm document ID
var refs = v.refs || row.value.refs || []
var members = v.members || row.value.members || []
row.links.forEach(function (link) {
var done = next()
self.log.get(link, function (err, node) {
if (err) return done(err)
var refs = (node.value.v ? node.value.v.refs : node.value.refs)
var members = (node.value.v ? node.value.v.members : node.value.members)
if (refs) {
for (var i = 0; i < refs.length; i++) {
var ref = refs[i]
ops.push({ type: 'del', key: ref, rowKey: link })
if (d) ops.push({ type: 'put', key: ref, value: d })
}
}
if (members) {
for (var i = 0; i < members.length; i++) {
var member = members[i]
if (typeof member === 'string') member = { ref: member }
if (typeof member.ref !== 'string') return
ops.push({ type: 'del', key: member.ref, rowKey: link })
if (d) ops.push({ type: 'put', key: member.ref, value: d })
}
}
done()
})
})
// Write the new ref entries for this new osm document
if (k) {
for (var i = 0; i < refs.length; i++) {
ops.push({ type: 'put', key: refs[i], value: k })
}
for (i = 0; i < members.length; i++) {
ops.push({ type: 'put', key: members[i].ref || members[i], value: k })
}
}
}
})
self.refs.on('error', function (err) { self.emit('error', err) })
self.changeset = join({
log: self.log,
db: sub(self.db, 'c'),
map: function (row, cb) {
if (!row.value) return cb()
var v = row.value.v
if (!v || !v.changeset) return cb()
return cb(null, { type: 'put', key: v.changeset, value: 0 })
}
})
self.changeset.on('error', function (err) { self.emit('error', err) })
}
// Given the OsmVersion of a document, returns the OsmVersions of all documents
// that reference it (non-recursively).
// OsmVersion -> [OsmVersion]
DB.prototype._getReferers = function (version, cb) {
var self = this
self.log.get(version, function (err, doc) {
if (err) return cb(err)
self.refs.list(doc.value.k || doc.value.d, function (err, rows) {
if (err) cb(err)
else cb(null, rows.map(keyf))
})
})
function keyf (row) { return row.key }
}
DB.prototype.ready = function (cb) {
cb = once(cb || noop)
var pending = 3
this.refs.dex.ready(ready)
this.kdb.ready(ready)
this.changeset.dex.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)
var rows = [
{
type: 'del',
key: key,
links: opts.links
}
]
self.batch(rows, opts, function (err, nodes) {
if (err) cb(err)
else cb(null, nodes[0])
})
}
// OsmId, Opts -> [OsmBatchOp]
DB.prototype._getDocumentDeletionBatchOps = function (id, opts, cb) {
var self = this
if (!opts || !opts.links) {
// Fetch all versions of the document ID
self.kv.get(id, function (err, docs) {
if (err) return cb(err)
docs = mapObj(docs, function (version, doc) {
if (doc.deleted) {
return {
id: id,
version: version,
deleted: true
}
} else {
return doc.value
}
})
handleLinks(docs)
})
} else {
// Fetch all versions of documents that match 'opts.links`.
mapLimit(opts.links, 10, linkToDocument, function (err, docList) {
if (err) return cb(err)
var docs = {}
docList.forEach(function (doc) {
docs[doc.version] = doc
})
handleLinks(docs)
})
}
function linkToDocument (link, done) {
self.log.get(link, function (err, node) {
if (err) return done(err)
done(null, node.value.d ? {
id: node.value.d,
version: node.key,
deleted: true
} : xtend(node.value.v, {
id: node.value.k,
version: node.key
}))
})
}
function handleLinks (docs) {
var fields = {}
var links = Object.keys(docs)
links.forEach(function (ln) {
var v = docs[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)
}
if (Array.isArray(v.members)) {
if (!fields.members) fields.members = []
fields.members.push.apply(fields.members, v.members)
}
})
cb(null, [ { type: 'del', key: id, 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 done = once(function () {
self.kv.batch(batch, opts, function (err, nodes) {
release(cb, err, nodes)
})
})
var pending = 1 + rows.length
rows.forEach(function (row) {
var key = defined(row.key, row.id)
if (!key) {
key = row.key = hex2dec(randomBytes(8).toString('hex'))
}
if (row.links && !Array.isArray(row.links)) {
return cb(new Error('row has a "links" field that isnt an array'))
} else if (!row.links && row.links !== undefined) {
return cb(new Error('row has a "links" field that is non-truthy but not undefined'))
}
if (row.type === 'put') {
batch.push(row)
if (--pending <= 0) done()
} else if (row.type === 'del') {
var xrow = xtend(opts, row)
self._getDocumentDeletionBatchOps(key, xrow, 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()
})
}
DB.prototype.get = function (key, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
this.kv.get(key, function (err, docs) {
if (err) return cb(err)
docs = mapObj(docs, function (version, doc) {
if (doc.deleted) {
return {
id: key,
version: version,
deleted: true
}
} else {
return doc.value
}
})
cb(null, docs)
})
}
DB.prototype.getByVersion = function (version, cb) {
this.log.get(version, function (err, node) {
if (err) return cb(err)
var doc
if (node.value.d) {
doc = {
id: node.value.d,
version: version,
deleted: true
}
} else {
doc = Object.assign(node.value.v, {
id: node.value.k,
version: version
})
}
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)
var res = []
var done = once(function () {
if (opts.order === 'type') {
res.sort(cmpType)
}
cb(null, res)
})
self.ready(function () {
self.kdb.query(q, opts, onquery)
})
function onquery (err, pts) {
if (err) return cb(err)
var pending = 1
var seen = {}
for (var i = 0; i < pts.length; i++) {
var pt = pts[i]
pending++
self._collectNodeAndReferers(kdbPointToVersion(pt), seen, function (err, r) {
if (err) return cb(err)
if (r) res = res.concat(r)
if (--pending <= 0) done()
})
}
if (--pending <= 0) done()
}
}
var typeOrder = { node: 0, way: 1, relation: 2 }
function cmpType (a, b) {
return typeOrder[a.type] - typeOrder[b.type]
}
// Given a node by its version, this collects the node itself, and also
// recursively climbs all ways and relations that the node (or its referers)
// are referred to by.
// OsmVersion, { OsmVersion: Boolean } -> [OsmDocument]
DB.prototype._collectNodeAndReferers = function (version, seenAccum, cb) {
cb = once(cb || noop)
var self = this
if (seenAccum['deep'+version]) return cb(null, [])
var res = []
var added = {}
var pending = 1
// Track the original node that came from the kdb query that brought us here,
// but don't add it yet. There are certain conditions (e.g. there is only one
// way containing us, and it's deleted) where the node would not be returned.
var selfNode
var originalNode
self.log.get(version, function (err, node) {
// TODO: handle error
if (!err) {
selfNode = node
originalNode = node
}
self._getReferers(version, onLinks)
})
function onLinks (_, links) {
if (!links) links = []
// The original node has nothing referring to it, so it's a standalone node
// on the map: add it.
if (links.length === 0 && selfNode && !seenAccum[selfNode.key]) {
addDocFromNode(selfNode)
seenAccum['deep'+selfNode.key] = true
}
selfNode = null
links.forEach(function (link) {
if (seenAccum[link]) return
seenAccum[link] = true
pending++
self.log.get(link, function (err, node) {
// TODO: handle error
if (!err) {
addDocFromNode(node)
if (node && node.value && node.value.k && node.value.v) {
// Add the original node if a referer is a relation.
if (originalNode && !seenAccum[originalNode.key] && node.value.v.type === 'relation') {
addDocFromNode(originalNode)
seenAccum[originalNode.key] = true
originalNode = null
}
pending++
self.get(node.value.k, function (err, docs) {
if (err) return cb(err)
Object.keys(docs).forEach(function (key) {
addDoc(node.value.k, key, docs[key])
})
if (--pending <= 0) cb(null, res)
})
}
}
if (--pending <= 0) cb(null, res)
})
pending++
self._getReferers(link, function (err, links2) {
// TODO: handle error
if (!err) {
originalNode = null
onLinks(err, links2)
}
})
})
if (--pending <= 0) cb(null, res)
}
function addDocFromNode (node) {
if (node && node.value && node.value.k && node.value.v) {
addDoc(node.value.k, node.key, node.value.v)
} else if (node && node.value && node.value.d) {
addDoc(node.value.d, node.key, {deleted: true})
}
}
function addDoc (id, version, doc) {
if (!added[version] && !seenAccum['deep'+version]) {
doc = xtend(doc, {
id: id,
version: version
})
res.push(doc)
added[version] = true
}
if (doc && Array.isArray(doc.refs || doc.nodes)) {
addWayNodes(doc.refs || doc.nodes)
}
}
function addWayNodes (refs) {
refs.forEach(function (ref) {
if (seenAccum[ref]) return
seenAccum[ref] = true
pending++
self.get(ref, function (err, docs) {
// TODO: handle error
if (!err) {
Object.keys(docs || {}).forEach(function (key) {
if (seenAccum[key]) return
seenAccum[key] = true
addDoc(ref, key, docs[key])
})
}
if (--pending <= 0) cb(null, res)
})
})
}
}
DB.prototype.queryStream = function (q, opts) {
var self = this
if (!opts) opts = {}
var stream = opts.order === 'type'
? through.obj(writeType, endType)
: through.obj(write)
var seen = {}
var queue = []
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._collectNodeAndReferers(kdbPointToVersion(row), seen, function (err, res) {
if (err) return next()
if (res) {
res.forEach(function (r) {
tr.push(r)
})
}
next()
})
}
function writeType (row, enc, next) {
next = once(next)
var tr = this
self._collectNodeAndReferers(kdbPointToVersion(row), seen, function (err, res) {
if (err) return next()
if (res) {
res.forEach(function (r) {
if (r.type === 'node') tr.push(r)
else queue.push(r)
})
}
next()
})
}
function endType (next) {
var tr = this
queue.sort(cmpType).forEach(function (q) { tr.push(q) })
next()
}
}
DB.prototype.getChanges = function (key, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
var r = this.changeset.list(key, opts)
var stream = r.pipe(through.obj(write))
if (cb) collectObj(stream, cb)
return readonly(stream)
function write (row, enc, next) {
this.push(row.key)
next()
}
}
DB.prototype.close = function (cb) {
this.db.close(onDone)
this.log.db.close(onDone)
this.store.close(onDone) // TODO: investigate fd-chunk-store (or deferred-chunk-store) not calling its cb on 'close'
var pending = 3
function onDone (err) {
if (err) {
pending = Infinity
cb(err)
} else if (--pending === 0) {
cb()
}
}
}
DB.prototype.getReferrers = function (id, cb) {
this.refs.list(id, function (err, rows) {
if (err) cb(err)
else cb(null, rows.map(keyf))
})
function keyf (row) {
return {
id: row.value,
version: row.key
}
}
}
function noop () {}
function collectObj (stream, cb) {
cb = once(cb)
var rows = []
stream.on('error', cb)
stream.pipe(to.obj(write, end))
function write (x, enc, next) {
rows.push(x)
next()
}
function end () { cb(null, rows) }
}
// Object, (k, v -> v) -> Object
function mapObj (obj, fn) {
Object.keys(obj).forEach(function (key) {
obj[key] = fn(key, obj[key])
})
return obj
}
// KdbPoint -> OsmVersion
function kdbPointToVersion (pt) {
return pt.value.toString('hex')
}
// {lat: Number, lon: Number} -> [Number, Number]
function ptf (x) {
return [ Number(x.lat), Number(x.lon) ]
}