-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
194 lines (190 loc) · 5.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
var Promise = require('lie');
var RTree = require('async-rtree');
var calculatebounds = require('geojson-bounding-volume');
var createView = require('./create-view');
var Store = require('./store');
var upsert = require('./upsert');
exports.spatial = spatial;
function spatial(fun, bbox, opts, cb, /*only needed if people use 2 bboxen-->*/cb2) {
if (bbox.length === 4) {
bbox = [[bbox[0], bbox[1]], [bbox[2], bbox[3]]];
}
if (Array.isArray(opts)) {
bbox = [bbox, opts];
opts = cb;
cb = cb2;
}
var db = this;
var viewName, temporary;
if (!opts || typeof opts !== 'object') {
cb = opts;
opts = {};
}
var store, rawStore;
var viewID;
return makeFunc(db, fun).then(function (func) {
if (typeof fun === 'function') {
viewName = 'temporary';
temporary = true;
} else {
viewName = func;
viewID = '_design/' + fun.split('/')[0];
}
var view = createView(db, viewName, temporary, fun);
var updated = view.then(updateIndex(func));
if (opts.stale !== true) {
return updated;
} else {
return view;
}
}).then(queryIndex).then(function (resp) {
if (cb) {
return cb(null, resp);
} else {
return resp;
}
}, function (err) {
if (cb) {
return cb(err);
} else {
throw err;
}
});
function updateIndex(func) {
return function (viewDB) {
//console.log(viewDB);
viewDB = viewDB.db;
if (temporary) {
store = new RTree();
} else if (viewDB._rStore) {
store = viewDB._rStore;
} else {
store = viewDB._rStore = new RTree(new Store(viewDB));
}
function addDoc(doc) {
var id = doc._id;
var emited = [];
var i = 0;
function emit(doc) {
if (i++) {
emited.push(store.append(id, calculatebounds(doc)));
} else {
emited.push(store.insert(id, calculatebounds(doc)));
}
}
function fixMulti (doc) {
var type = doc.type;
switch (type) {
case 'MultiPoint':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Point',
coordinates: coord
});
});
case 'MultiLineString':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'LineString',
coordinates: coord
});
});
case 'MultiPolygon':
return doc.coordinates.forEach(function (coord) {
emit({
type: 'Polygon',
coordinates: coord
});
});
case 'GeometryCollection':
return doc.geometries.forEach(fixMulti);
default:
return emit(doc);
}
}
func(doc, fixMulti);
return Promise.all(emited);
}
var lastSeq;
return viewDB.get('_local/gclastSeq').catch(function () {
return {_id: '_local/gclastSeq', last_seq: 0};
}).then(function (doc) {
lastSeq = doc;
return db.changes({
include_docs: true,
since: doc.last_seq
});
}).then(function (res) {
if (!res.results) {
return;
}
return Promise.all(res.results.filter(function (doc) {
if (doc.id.indexOf('_design/') !== 0) {
return true;
}// } else if (doc.id === viewID) {
// return true;
// }
}).map(function (doc) {
if (doc.deleted) {
return store.remove(doc.id).catch(function () {
// might not be in there
});
}
return addDoc(doc.doc);
})).then(function () {
lastSeq.last_seq = res.last_seq;
if (temporary) {
return;
}
return upsert(viewDB, '_local/gclastSeq', function (doc) {
if (!doc.last_seq) {
return lastSeq;
} else {
doc.last_seq = Math.max(doc.last_seq, lastSeq.last_seq);
return doc;
}
});
});
});
};
}
function queryIndex() {
return new Promise(function (resolve, reject) {
var out = {};
var promises = [];
store.query(bbox).on('data', function (d) {
if (d.id in out) {
out[d.id].bboxen.push(d.bbox);
} else {
if (opts.include_docs) {
promises.push(db.get(d.id).then(function (doc) {
out[d.id].doc = doc;
}));
}
out[d.id] = {
id: d.id,
bboxen: [d.bbox]
};
}
}).on('error', reject).on('end', function () {
resolve(Promise.all(promises).then(function () {
return Object.keys(out).map(function (id) {
return out[id];
});
}));
});
});
}
}
function makeFunc (db, fun) {
return new Promise (function (resolve, reject) {
if (typeof fun === 'function') {
return resolve(new Function ('doc', 'emit', 'var func = (' + fun.toString().replace(/;\s*$/,'') + ');func(doc);'));
}
var parts = fun.split('/');
resolve(db.get('_design/' + parts[0]).then(function (doc) {
var fun = doc.spatial[parts[1]];
return new Function ('doc', 'emit', 'var func = (' + fun.replace(/;\s*$/,'') + ');func(doc);');
}));
});
}