forked from saidabou/carmen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
300 lines (269 loc) · 10 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
var path = require('path'),
EventEmitter = require('events').EventEmitter,
queue = require('queue-async');
var dawgcache = require('./lib/util/dawg');
var Cache = require('./lib/util/cxxcache'),
getContext = require('./lib/context'),
loader = require('./lib/loader'),
geocode = require('./lib/geocode'),
analyze = require('./lib/analyze'),
loadall = require('./lib/loadall'),
termops = require('./lib/util/termops'),
token = require('./lib/util/token'),
copy = require('./lib/copy'),
index = require('./lib/index');
require('util').inherits(Geocoder, EventEmitter);
module.exports = Geocoder;
// Initialize and load Geocoder, with a selection of indexes.
function Geocoder(indexes, options) {
if (!indexes) throw new Error('Geocoder indexes required.');
options = options || {};
var q = queue(10);
this.indexes = indexes;
this.replacer = token.createReplacer(options.tokens || {});
this.byname = {};
this.bytype = {};
this.bystack = {};
this.byidx = [];
this.names = [];
for (var k in indexes) {
indexes[k] = clone(indexes[k]);
q.defer(loadIndex, k, indexes[k]);
}
q.awaitAll(function(err, results) {
var names = [];
var types = [];
var stacks = [];
if (results) results.forEach(function(data, i) {
var id = data.id;
var info = data.info;
var dictcache = data.dictcache;
var source = indexes[id];
var name = info.geocoder_name || id;
var type = info.geocoder_type||info.geocoder_name||id;
var stack = info.geocoder_stack || false;
if (names.indexOf(name) === -1) {
names.push(name);
this.byname[name] = [];
}
if (types.indexOf(type) === -1) {
types.push(type);
this.bytype[type] = [];
}
if (typeof stack === 'string') stack = [stack];
if (stack) {
for (var j = 0; j < stack.length; j++) {
if (stacks.indexOf(stack[j]) === -1) {
stacks.push(stack[j]);
this.bystack[stack[j]] = [];
}
}
}
source._geocoder = source._original._geocoder || new Cache(name, info.geocoder_cachesize);
source._dictcache = source._original._dictcache || dictcache;
// Set references to _geocoder, _dictcache on original source to
// avoid duplication if it's loaded again.
source._original._geocoder = source._geocoder;
source._original._dictcache = source._dictcache;
if (info.geocoder_address) {
source.geocoder_address = info.geocoder_address;
} else {
source.geocoder_address = false;
}
if (info.geocoder_version) {
source.version = parseInt(info.geocoder_version, 10);
if (source.version !== 6) {
err = new Error('geocoder version is not 6, index: ' + id);
return;
}
} else {
source.version = 0;
source.shardlevel = info.geocoder_shardlevel || 0;
}
var keys = Object.keys(info);
for (var ix = 0; ix < keys.length; ix ++) {
if (/geocoder_format_/.test(keys[ix])) source[keys[ix]] = info[keys[ix]]||false;
}
source.geocoder_format = info.geocoder_format||false;
source.geocoder_layer = (info.geocoder_layer||'').split('.').shift();
source.geocoder_tokens = info.geocoder_tokens||{};
source.token_replacer = token.createReplacer(info.geocoder_tokens||{});
source.maxzoom = info.maxzoom;
source.stack = stack;
source.zoom = info.maxzoom + parseInt(info.geocoder_resolution||0,10);
source.type = type;
source.name = name;
source.id = id;
source.idx = i;
source.ndx = names.indexOf(name);
source.bounds = info.bounds || [ -180, -85, 180, 85 ];
// add index idx => name idx lookup
this.names[i] = names.indexOf(name);
// add byname index lookup
this.byname[name].push(source);
// add bytype index lookup
this.bytype[type].push(source);
// add bystack index lookup
for (var j = 0; j < stack.length; j++) {
this.bystack[stack[j]].push(source);
}
// add byidx index lookup
this.byidx[i] = source;
}.bind(this));
// Second pass -- generate bmask (geocoder_stack) per index.
// The bmask of an index represents a mask of all indexes that their
// geocoder_stacks do not intersect with -- ie. a spatialmatch with any of
// these indexes should not be attempted as it will fail anyway.
for (var i = 0; i < this.byidx.length; i++) {
var bmask = [];
var a = this.byidx[i];
for (var j = 0; j < this.byidx.length; j++) {
var b = this.byidx[j];
var a_it = a.stack.length;
while (a_it--) {
var b_it = b.stack.length;
while (b_it--) {
if (a.stack[a_it] === b.stack[b_it]) {
bmask[j] = 0;
} else if (bmask[j] !== 0) {
bmask[j] = 1;
}
}
}
}
this.byidx[i].bmask = bmask;
}
this._error = err;
this._opened = true;
this.emit('open', err);
}.bind(this));
function loadIndex(id, source, callback) {
source.open(function opened(err) {
if (err) return callback(err);
var q = queue();
q.defer(function(done) { source.getInfo(done); });
q.defer(function(done) {
if (source._original._dictcache || !source.getGeocoderData) {
done();
} else {
source.getGeocoderData('stat', 0, done);
}
});
q.awaitAll(function(err, loaded) {
if (err) return callback(err);
// if dictcache is already initialized don't recreate
if (source._original._dictcache) {
callback(null, {
id: id,
info: loaded[0]
});
// create dictcache at load time to allow incremental gc
} else {
callback(null, {
id: id,
info: loaded[0],
dictcache: new dawgcache(loaded[1])
});
}
});
});
}
}
function clone(source) {
var cloned = {};
cloned.getInfo = source.getInfo.bind(source);
cloned.getTile = source.getTile.bind(source);
cloned.open = function(callback) {
if (source.open === true) return callback();
if (typeof source.open === 'function') return source.open(callback);
return source.once('open', callback);
};
// Optional methods
[
'_commit',
'putInfo',
'putTile',
'getGeocoderData',
'putGeocoderData',
'startWriting',
'stopWriting',
'getIndexableDocs',
'serialize'
].forEach(function(method) {
if (typeof source[method] === 'function') {
cloned[method] = source[method].bind(source);
}
});
// Include reference to original
cloned._original = source;
return cloned;
}
function boundsIntersect(a, b) {
if (a[2] < b[0]) return false; // a is left of b
if (a[0] > b[2]) return false; // a is right of b
if (a[3] < b[1]) return false; // a is below b
if (a[1] > b[3]) return false; // a is above b
return true;
}
// Ensure that all carmen sources are opened.
Geocoder.prototype._open = function(callback) {
return this._opened ? callback(this._error) : this.once('open', callback);
};
// Main geocoding API entry point.
// Returns results across all indexes for a given query.
//
// Actual searches are delegated to `Geocoder.prototype.search` over each
// enabled backend.
//
// `query` is a string of text, like "Chester, NJ"
// `options` is an object with additional parameters
// `callback` is called with (error, results)
Geocoder.prototype.geocode = function(query, options, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
geocode(self, query, options, callback);
});
};
// Index docs from one source to another.
Geocoder.prototype.index = function(from, to, pointer, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
index(self, from, to, pointer, callback);
});
};
// Analyze a source's index.
Geocoder.prototype.analyze = function(source, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
analyze(source, callback);
});
};
// Load all shards for a source.
Geocoder.prototype.loadall = function(source, type, concurrency, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
loadall.loadall(source, type, concurrency, callback);
});
};
Geocoder.prototype.unloadall = function(source, type, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
loadall.unloadall(source, type, callback);
});
};
// Copy a source's index to another.
Geocoder.prototype.copy = function(from, to, callback) {
var self = this;
this._open(function(err) {
if (err) return callback(err);
copy(from, to, callback);
});
};
Geocoder.auto = loader.auto;
Geocoder.autodir = loader.autodir;
Geocoder.setVtCacheSize = getContext.getTile.setVtCacheSize;