forked from saidabou/carmen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (157 loc) · 5.69 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
var path = require('path'),
EventEmitter = require('events').EventEmitter,
queue = require('queue-async');
var Cache = require('./lib/util/cxxcache'),
getContext = require('./lib/context'),
loader = require('./lib/loader'),
geocode = require('./lib/geocode'),
analyze = require('./lib/analyze'),
verify = require('./lib/verify'),
loadall = require('./lib/loadall'),
termops = require('./lib/util/termops'),
wipe = require('./lib/wipe'),
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(options) {
if (!options) throw new Error('Geocoder options required.');
var q = queue(),
indexes = pairs(options);
this.indexes = indexes.reduce(toObject, {});
this.order = {};
indexes.forEach(function(index) {
q.defer(loadIndex, index);
});
q.awaitAll(function(err, results) {
this._error = err;
this._opened = true;
var names = [];
results.forEach(function(info, i) {
var id = indexes[i][0];
var source = indexes[i][1];
var name = info.geocoder_name || id;
if (names.indexOf(name) === -1) names.push(name);
source._geocoder = source._geocoder || new Cache(name, +info.geocoder_shardlevel || 0);
source._geocoder.geocoder_address = !!parseInt(info.geocoder_address||0,10);
source._geocoder.geocoder_layer = (info.geocoder_layer||'').split('.').shift();
source._geocoder.geocoder_tokens = termops.tokenizeMapping(info.geocoder_tokens||{});
source._geocoder.maxzoom = info.maxzoom;
source._geocoder.zoom = info.maxzoom + parseInt(info.geocoder_resolution||0,10);
source._geocoder.format = info.format || '';
source._geocoder.group = info.geocoder_group || '';
source._geocoder.name = name;
source._geocoder.id = id;
source._geocoder.bounds = info.bounds || [ -180, -85, 180, 85 ];
// map id => idx
this.order[id] = names.indexOf(name);
}.bind(this));
this.emit('open', err);
}.bind(this));
function loadIndex(sourceindex, callback) {
var source = sourceindex[1],
key = sourceindex[0];
source = source.source ? source.source : source;
if (source.open === true) return source.getInfo(callback);
if (typeof source.open === 'function') return source.open(opened);
return source.once('open', opened);
function opened(err) {
if (err) return callback(err);
source.getInfo(callback);
}
}
}
function pairs(o) {
var a = [];
for (var k in o) a.push([k, o[k]]);
return a;
}
function toObject(mem, s) {
mem[s[0]] = s[1].source ? s[1].source : s[1];
return mem;
}
// 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) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
geocode(this, query, options, callback);
}.bind(this));
}
return geocode(this, query, options, callback);
};
// Index docs from one source to another.
Geocoder.prototype.index = function(from, to, pointer, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
index(this, from, to, pointer, callback);
}.bind(this));
}
return index(this, from, to, pointer, callback);
};
// Verify the integrity of a source's index.
Geocoder.prototype.verify = function(source, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
verify(source, callback);
}.bind(this));
}
return verify(source, callback);
};
// Analyze a source's index.
Geocoder.prototype.analyze = function(source, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
analyze(source, callback);
}.bind(this));
}
return analyze(source, callback);
};
// Wipe a source's index.
Geocoder.prototype.wipe = function(source, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
wipe(source, callback);
}.bind(this));
}
return wipe(source, callback);
};
// Load all shards for a source.
Geocoder.prototype.loadall = function(source, concurrency, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
loadall(source, concurrency, callback);
}.bind(this));
}
return loadall(source, concurrency, callback);
};
// Copy a source's index to another.
Geocoder.prototype.copy = function(from, to, callback) {
if (!this._opened) {
return this._open(function(err) {
if (err) return callback(err);
copy(from, to, callback);
}.bind(this));
}
return copy(from, to, callback);
};
Geocoder.auto = loader.auto;
Geocoder.autodir = loader.autodir;