-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (58 loc) · 1.42 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
var BKD = require('bkd-tree')
var once = require('once')
module.exports = MBKD
function MBKD (opts) {
if (!(this instanceof MBKD)) return new MBKD(opts)
this.storage = opts.storage
this._getPoint = opts.getPoint
this._isLinked = opts.isLinked
this.bkd = new BKD(this.storage, {
type: opts.type,
branchFactor: opts.branchFactor,
levels: opts.levels,
compare: opts.compare
})
}
MBKD.prototype.batch = function (rows, cb) {
var self = this
cb = once(cb || noop)
var ops = []
var pending = 1
rows.forEach(function (row) {
var links = row.links || []
links.forEach(function (link) {
pending++
self._getPoint(link, function (err, pt) {
if (err) return cb(err)
if (pt) {
ops.push({
type: 'delete',
point: pt.point,
value: pt.value
})
}
if (--pending === 0) done()
})
})
pending++
self._isLinked(row.id, function (err, ex) {
if (err) return cb(err)
if (!ex) {
ops.push({
type: row.type === 'delete' ? 'delete' : 'insert',
point: row.point,
value: Array.isArray(row.id) ? row.id : [row.id]
})
}
if (--pending === 0) done()
})
})
if (--pending === 0) done()
function done () {
self.bkd.batch(ops, cb)
}
}
MBKD.prototype.query = function (bbox, cb) {
return this.bkd.query(bbox, cb)
}
function noop () {}