-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
702 lines (651 loc) · 20.2 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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
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 lock = require('mutexify')
var mapLimit = require('map-limit')
var geojsonCoords = require('@mapbox/geojson-coords')
module.exports = DB
inherits(DB, EventEmitter)
/**
* Create a new geojson-p2p database
* @param {Object} opts
* @param {string} opts.log a hyperlog with a valueEncoding of `json`
* @param {string} opts.db a levelup instace to store index data
* @param {string} opts.store an abstract-chunk-store instance
* @param {string} [opts.kv='kv'] an optional hyperkv instance,
* if not specified, one will be created
* @example
* var hyperlog = require('hyperlog')
* var level = require('level')
* var fdstore = require('fd-chunk-store')
* var gjdb = require('geojson-p2p-db')
*
* var gj = gjdb({
* log: hyperlog(level('log'), { valueEncoding: 'json' }),
* db: level('index'),
* store: fdstore(4096, '/tmp/geojson-p2p/kdb')
* })
*/
function DB (opts) {
var self = this
if (!(self instanceof DB)) return new DB(opts)
self.log = opts.log
self.db = opts.db
self.kv = opts.kv
? 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.kdb = hyperkdb({
log: self.log,
store: opts.store,
db: sub(self.db, 'kdb'),
kdbtree: kdbtree,
types: [ 'float64', 'float64' ],
map: mapKdb
})
self.kdb.on('error', function (err) {
self.emit('error', err)
})
self.refs = function () {
self.emit('error', 'Not implemented')
}
self.changeset = join({
log: self.log,
db: sub(self.db, 'c'),
map: function (row, cb) {
if (!row.value) cb()
var v = row.value.v
if (v && v.changeset) {
cb(null, { type: 'put', key: v.changeset, value: 0 })
} else cb()
}
})
self.changeset.on('error', function (err) { self.emit('error', err) })
}
DB.prototype._getReferers = function (version, cb) {
cb()
}
DB.prototype.ready = function (cb) {
cb = once(cb || noop)
var pending = 2
this.kdb.ready(ready)
this.changeset.dex.ready(ready)
function ready () { if (--pending <= 0) cb() }
}
/**
* Store a new geojson feature or changeset from `value`. `cb(err, id, node)`
* is returned with the generated `id` and the `node` from the underlying
* hyperlog.
* @param {Object} value A GeoJSON Feature object containing `geometry`,
* `properties` and `changeset` properties _or_ a changeset object with a
* `type='changeset'` and `tags` properties. `tags.comment` is recommended for
* storing text describing the changeset. *Note:* The GeoJSON specification
* allows an `id` property on Feature objects. This property will be added or
* destructively overwritten in the geojson-p2p-db to ensure uniqueness.
* @param {Object} [opts={}] Options to pass to hyperkv.
* @param {Function} cb A callback function with the parameters `err`, `id`,
* `node` where `id` is the generated id and the `node` from the underlying
* hyperlog.
* @example
* gj.create({ type: 'changeset', tags: { comment: 'This is a new changeset' }},
* function (err, id, node) {
* if (err) throw err
* var feat = {
* type: "Feature",
* properties: {
* beep: 'boop'
* },
* geometry: {
* type: 'Point',
* coordinates: [-123.027648, 48.695492]
* },
* changeset: id
* }
* gj.create(feat, function (err, id, node) {
* if (err) console.error(err)
* console.log('Id', id)
* console.log('Node', node)
* })
* })
*/
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 = randomBytes(8).toString('hex')
var feat = xtend(value, { id: key })
self.put(key, feat, opts, function (err, node) {
cb(err, key, node)
})
}
/**
* Replace a document `key` from `value`. The document will be created if it does
* not exist. `cb(err, node)` is returned with the `node` from the underlying
* hyperlog.
* @param {string} key Id of a document to replace with `value`.
* @param {Object} value A GeoJSON Feature object containing `geometry`,
* `properties` and `changeset` properties _or_ a changeset object with a
* `type='changeset'` and `tags` properties. `tags.comment` is recommended for
* storing text describing the changeset. *Note:* The GeoJSON specification
* allows an `id` property on Feature objects. This property will be added or
* destructively overwritten in the geojson-p2p-db to ensure uniqueness.
* @param {Object} [opts={}] Options to pass to hyperkv.
* @param {Function} cb A callback function with the parameters `err`, `node`
* with the `node` from the underlying hyperlog.
* @example
* gj.create({ type: 'changeset', tags: { comment: 'This is a new changeset' }},
* function (err, id, node) {
* if (err) throw err
* var feat = {
* type: "Feature",
* properties: {
* beep: 'boop'
* },
* geometry: {
* type: 'Point',
* coordinates: [-123.027648, 48.695492]
* },
* changeset: id
* }
* gj.create(feat, function (err, id, node) {
* if (err) console.error(err)
* console.log('Id', id)
* console.log('Node', node)
* feat.properties = {
* boop: 'beep'
* }
* feat.changeset = id
* gj.put(id, feat, function (err, node) {
* console.log('New node', 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)
})
})
}
/**
* Mark the document at `key` as deleted. `cb(err, node)` is returned with the
* `node` from the underlying hyperlog.
* @param {string} key Id of the document to mark as deleted.
* @param {Object} [opts={}] Options to pass to hyperkv.
* @param {Function} cb A callback function with the parameters `err`, `node`
* with the `node` from the underlying hyperlog.
* @example
* gj.create({ type: 'changeset', tags: { comment: 'This is a new changeset' }},
* function (err, id, node) {
* if (err) throw err
* var feat = {
* type: "Feature",
* properties: {
* beep: 'boop'
* },
* geometry: {
* type: 'Point',
* coordinates: [-123.027648, 48.695492]
* },
* changeset: id
* }
* gj.create(feat, function (err, id, node) {
* if (err) console.error(err)
* console.log('Id', id)
* console.log('Node', node)
* gj.del(id, function (err, node) {
* if (err) throw err
* gj.get(id, function (err, node) {
* if (err) throw err
* console.log('Deleted', 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])
})
}
DB.prototype._getDocumentDeletionBatchOps = function (id, opts, cb) {
var self = this
if (!opts || !opts.links) {
self.kv.get(id, function (err, docs) {
if (err) 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) 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) 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 (!fields.points) fields.points = []
fields.points = fields.points.concat(geojsonCoords(v))
})
cb(null, [ { type: 'del', key: id, links: links, fields: fields } ])
}
}
/**
* Atomically put or delete an array of documents as `rows`
* @param {Object[]} rows Array of `row` to put or delete.
* @param {string} rows[].type Type of transaction. Either `'put'` or `'del'`.
* @param {string} rows[].key The id of the document to transact.
* @param {string[]} rows[].links An array of links to ancestor ids.
* @param {Object} rows[].value For `put`, the document to store.
* @param {Object} [opts={}] Options to pass to hyperkv
* @param {Function} cb A callback function with the parameters `err`, `nodes`
* with the `nodes` from the underlying hyperlog.
* @example
* // With existing changeset id of 'A'
* var rows = [
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.85, 48.52] }, changeset: 'A' } },
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.90, 48.60] }, changeset: 'A' } }
* ]
* gj.batch(rows, function (err, nodes) {
* if (err) throw err
* console.log(nodes)
* })
*/
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 = row.key
? row.key
: row.id
if (!key) {
key = row.key = randomBytes(8).toString('hex')
}
if (row.links && !Array.isArray(row.links)) {
return cb(new Error('row has a "links" field that isn\'t 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) 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()
})
}
/**
* Get the documents with the id `key`.
* @param {string} key The id of the documents to retrieve.
* @param {Object} [opts={}] Options to pass to hyperkv.
* @param {Function} cb A callback function with the parameters `err`, `docs`
* where `docs` is an object mapping hyperlog hashes to current document values.
* If a document has been deleted, it will only have the properties
* `{ id: <id>, version: <version>, deleted: true }`.
* @example
* gj.create({ type: 'changeset', tags: { comment: 'This is a new changeset' } },
* function (err, id, node) {
* if (err) throw err
* var feat = {
* type: "Feature",
* properties: {
* beep: 'boop'
* },
* geometry: {
* type: 'Point',
* coordinates: [-123.027648, 48.695492]
* },
* changeset: id
* }
* gj.create(feat, function (err, id, node) {
* if (err) console.error(err)
* console.log('Id', id)
* gj.get(id, function (err, nodes) {
* if (err) throw err
* console.log('Nodes', nodes)
* })
* })
* })
*/
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 xtend(doc.value, { id: key })
}
})
cb(null, docs)
})
}
/**
* Query the database using latitude/longitude bounds.
* @param {Array<Array<Number>>} q An array of `[[ minLat, maxLat], [minLng, maxLng]]`
* coordinate pairs to specify a bounding box.
* @param {Object} [opts={}] Options to pass to kdb-tree-store query.
* @param {Function} cb A callback function with the parameters `err, res`
* where `res` is an array of documents each containing an `id` property and a
* `version` property which is the hash key from the underlying hyperlog.
* Deleted documents will only have the properties
* `{ id: <id>, version: <version>, deleted: true }`.
* @example
* // With existing changeset id of 'A'
* var rows = [
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.85, 48.52] }, changeset: 'A' } },
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.90, 48.70] }, changeset: 'A' } }
* ]
*
* gj.batch(rows, function (err, nodes) {
* if (err) throw err
* console.log('Created', nodes)
* gj.query([ [ 48.50, 48.60 ], [ -122.89, -122.80 ] ], function (err, res) {
* if (err) throw err
* console.log('Results', res)
* })
* })
*/
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 () {
cb(null, res)
})
self.ready(function () {
self.kdb.query(q, opts, onquery)
})
function onquery (err, pts) {
if (err) cb(err)
var pending = 1
var seen = {}
for (var i = 0; i < pts.length; i++) {
var pt = pts[i]
pending++
self._collectFeatures(kdbPointToVersion(pt), seen, function (err, r) {
if (err) cb(err)
if (r) res = res.concat(r)
if (--pending <= 0) done()
})
}
if (--pending <= 0) done()
}
}
DB.prototype._collectFeatures = function (version, seenAccum, cb) {
cb = once(cb || noop)
var self = this
if (seenAccum[version]) cb(null)
var res = []
var added = {}
var pending = 1
var selfNode
self.log.get(version, function (err, node) {
if (err) cb(err)
else {
selfNode = node
}
self._getReferers(version, onLinks)
})
function onLinks (err, links) {
// TODO Handle error
if (!links) links = []
if (links.length === 0 && selfNode && !seenAccum[selfNode.key]) {
addDocFromNode(selfNode)
seenAccum[selfNode.key] = true
}
selfNode = null
links.forEach(function (link) {
if (seenAccum[link]) return
seenAccum[link] = true
pending++
self.log.get(link, function (err, node) {
if (err) self.emit('error', err)
if (!err) {
addDocFromNode(node)
if (node && node.value && node.value.k && node.value.v) {
pending++
self.get(node.value.k, function (err, docs) {
if (err) 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++
})
if (--pending <= 0) cb(null, res)
}
function addDocFromNode (node) {
if (node && node.value && node.value.k && node.value.v) {
return addDoc(node.value.k, node.key, node.value.v)
} else if (node && node.value && node.value.d) {
return addDoc(node.value.d, node.key, { deleted: true })
}
}
function addDoc (id, version, doc) {
if (!added[version]) {
doc = xtend(doc, {
id: id,
version: version
})
res.push(doc)
added[version] = true
}
return doc
}
}
/**
* Return a readable object stream of query results contained in the
* query `q`.
* @param {Array<Array<Number>>} q An array of `[[ minLat, maxLat], [minLng, maxLng]]`
* coordinate pairs to specify a bounding box.
* @param {Object} [opts={}] Options to pass to kdb-tree-store query.
*/
DB.prototype.queryStream = function (q, opts) {
var self = this
if (!opts) opts = {}
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._collectFeatures(kdbPointToVersion(row), seen, function (err, res) {
if (err) next()
if (res) tr.push(res)
next()
})
}
}
/**
* Get a list of document version ids in a changeset by the changeset id `key`
* @param {string} key Id of changeset.
* @param {Function} [cb] An optional callback function with the parameters
* `err`, `versions` where `versions` is an array of changeset ids. If no
* callback is specified, a readable object stream is returned.
* @example
* gj.create({ type: 'changeset', tags: { comment: 'This is a new changeset' }},
* function (err, changesetId, node) {
* if (err) throw err
* var rows = [
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.85, 48.52] }, changeset: changesetId } },
* { type: 'put', value: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-122.90, 48.60] }, changeset: changesetId } }
* ]
* gj.batch(rows, function (err, nodes) {
* if (err) throw err
* gj.getChanges(changesetId, function (err, nodes) {
* if (err) throw err
* console.log(nodes)
* })
* })
* })
*/
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()
}
}
function mapKdb (row, next) {
if (!row.value) return null
var v = row.value.v
var d = row.value.d
if (v && v.type === 'Feature' && v.geometry && v.geometry.coordinates) {
next(null, { type: 'put', points: geojsonCoords(v).map(ptf) })
} else if (d && Array.isArray(row.value.points)) {
var pts = row.value.points.map(ptf)
next(null, { type: 'put', points: pts })
} else next()
}
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) }
}
function mapObj (obj, fn) {
Object.keys(obj).forEach(function (key) {
obj[key] = fn(key, obj[key])
})
return obj
}
function kdbPointToVersion (pt) {
return pt.value.toString('hex')
}
function ptf (v) {
return [v[1], v[0]]
}