-
Notifications
You must be signed in to change notification settings - Fork 33
/
interfaceEmulated.js
349 lines (279 loc) · 9.04 KB
/
interfaceEmulated.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
var geohash = require('ngeohash');
var geolib = require('geolib');
var helper = require('./util/helper');
var query = require('./query');
var range = require('./range');
var convertUnitsFromMeters = helper.convertUnitsFromMeters;
var convertUnitsToMeters = helper.convertUnitsToMeters;
var buildObjectFromKeyedArray = helper.buildObjectFromKeyedArray;
var buildObjectFromStringArray = helper.buildObjectFromStringArray;
/*
* Emulated interface constructor
* This interface emulates the native redis geo commands
*/
function EmulatedInterface(client) {
this.client = client;
}
// Emulated Geo Commands
EmulatedInterface.prototype.geoadd = function(locationName, point, zSetName, callBack) {
this.client.zadd(zSetName, geohash.encode_int(point.latitude, point.longitude, 52), locationName, callBack);
};
EmulatedInterface.prototype.geoadd_multi = function(locationSet, zSetName, callBack) {
var argArray = [zSetName];
var locationName;
var location;
for (locationName in locationSet) {
location = locationSet[locationName];
argArray.push(geohash.encode_int(location.latitude, location.longitude, 52), locationName);
}
this.client.zadd(argArray, callBack);
};
EmulatedInterface.prototype.geodist = function(locationA, locationB, units, zSetName, callBack) {
var client = this.client;
var multi = client.multi();
var pointA;
var pointB;
var distance;
multi.zscore(zSetName, locationA);
multi.zscore(zSetName, locationB);
multi.exec(function(err, replies) {
if (err) {
callBack(err, null);
} else {
replies = helper.normalizeMultiResponse(client, replies);
pointA = geohashDecode(replies[0]);
pointB = geohashDecode(replies[1]);
distance = geolib.getDistance(pointA, pointB, 1);
distance = convertUnitsFromMeters(units, distance);
callBack(null, distance);
}
});
};
EmulatedInterface.prototype.geohash = function(member, zSetName, callBack) {
var point;
var hash;
this.client.zscore(zSetName, member, function(err, result) {
if (err) {
callBack(err, null);
} else {
if (result !== null) {
point = geohashDecode(result);
hash = geohash.encode(point.latitude, point.longitude);
} else {
hash = null;
}
callBack(null, hash);
}
});
};
EmulatedInterface.prototype.geohashes = function(members, zSetName, callBack) {
var client = this.client;
var multi = client.multi();
var locationSet = {};
var point;
var i;
for (i = 0; i < members.length; i++) {
multi.zscore(zSetName, members[i]);
}
multi.exec(function(err, replies) {
if (err) {
callBack(err, null);
} else {
replies = helper.normalizeMultiResponse(client, replies);
for (i = 0; i < replies.length; i++) {
if (replies[i] !== null) {
point = geohashDecode(replies[i]);
locationSet[members[i]] = geohash.encode(point.latitude, point.longitude);
} else {
locationSet[members[i]] = null;
}
}
callBack(null, locationSet);
}
});
};
EmulatedInterface.prototype.geopos = function(member, zSetName, callBack) {
var location;
this.client.zscore(zSetName, member, function(err, result) {
if (err) {
callBack(err, null);
} else {
if (result !== null) {
location = geohashDecode(result);
} else {
location = null;
}
callBack(null, location);
}
});
};
EmulatedInterface.prototype.geopos_multi = function(members, zSetName, callBack) {
var client = this.client;
var multi = client.multi();
var locationSet = {};
var i;
for (i = 0; i < members.length; i++) {
multi.zscore(zSetName, members[i]);
}
multi.exec(function(err, replies) {
if (err) {
callBack(err, null);
} else {
replies = helper.normalizeMultiResponse(client, replies);
for (i = 0; i < replies.length; i++) {
if (replies[i] !== null) {
locationSet[members[i]] = geohashDecode(replies[i]);
} else {
locationSet[members[i]] = null;
}
}
callBack(null, locationSet);
}
});
};
EmulatedInterface.prototype.georadius = function(point, radius, options, zSetName, callBack) {
options.accurate = true;
this.nearby(point, radius, options, zSetName, callBack);
};
EmulatedInterface.prototype.georadiusbymember = function(member, radius, options, zSetName, callBack) {
options.accurate = true;
this.nearbymember(member, radius, options, zSetName, callBack);
};
EmulatedInterface.prototype.nearby = function(point, distance, options, zSetName, callBack) {
var ranges;
var count = options.count;
var units = options.units || 'm';
var accurate = options.accurate;
var withValues = (accurate || options.withDistances || options.withCoordinates || options.withHashes || options.order);
distance = convertUnitsToMeters(units, distance);
ranges = range(point.latitude, point.longitude, distance, accurate);
query(this.client, zSetName, ranges, withValues, function(err, locations) {
if (!err) {
if (withValues === true) {
locations = processLocations(locations, point, distance, options, callBack);
} else {
if (typeof count === 'number') {
locations = locations.slice(0, count);
}
Object.defineProperty(locations, 'locationSet', {
get: function() {
return buildObjectFromStringArray(this);
}
});
}
callBack(null, locations);
} else {
callBack(err, null);
}
});
};
EmulatedInterface.prototype.nearbymember = function(locationName, distance, options, zSetName, callBack) {
var self = this;
this.geopos([locationName], zSetName, function(err, location) {
if (!err) {
if (location === null) {
callBack(new Error('ERR could not decode requested zset member'), null);
} else {
self.nearby(location, distance, options, zSetName, callBack);
}
} else {
callBack(err, null);
}
});
};
// Native Ordered Set Commands
EmulatedInterface.prototype.del = function(zSetName, callBack) {
this.client.del(zSetName, callBack);
};
EmulatedInterface.prototype.zrem = function(locations, zSetName, callBack) {
var argsArray = [zSetName].concat(locations);
this.client.zrem(argsArray, callBack);
};
function processLocations(locationSetOriginal, point, queryDistance, options, callBack) {
var locations = [];
var units = options.units || 'm';
var withDistances = options.withDistances;
var withCoordinates = options.withCoordinates;
var withHashes = options.withHashes;
var order = options.order;
var count = options.count;
var accurate = options.accurate;
var locationName;
var location;
var distance;
if (order) {
locations = orderResults(locationSetOriginal, point, queryDistance, withDistances, withCoordinates, withHashes, order, accurate, units);
} else {
for (locationName in locationSetOriginal) {
location = locationSetOriginal[locationName];
if (accurate === true) {
distance = geolib.getDistance(point, location, 1);
if (distance > queryDistance) {
continue;
}
location.distance = convertUnitsFromMeters(units, distance);
} else if (withDistances === true) {
distance = geolib.getDistance(point, location, 1);
location.distance = convertUnitsFromMeters(units, distance);
}
if (withHashes === true) {
location.hash = geohash.encode_int(location.latitude, location.longitude, 52);
}
location.key = locationName;
locations.push(location);
}
}
if (typeof count === 'number') {
locations = locations.slice(0, count);
}
Object.defineProperty(locations, 'locationSet', {
get: function() {
return buildObjectFromKeyedArray(this);
}
});
return locations;
}
function orderResults(locationSetOriginal, point, queryDistance, withDistances, withCoordinates, withHashes, order, accurate, units) {
var orderedLocations = geolib.orderByDistance(point, locationSetOriginal);
var location;
var i;
if (accurate) {
for (i = orderedLocations.length - 1; i > -1; i--) {
if (orderedLocations[i].distance > queryDistance) {
orderedLocations = orderedLocations.slice(0, i);
break;
}
}
}
if (withHashes) {
for (i = 0; i < orderedLocations.length; i++) {
location = orderedLocations[i];
location.hash = geohash.encode_int(location.latitude, location.longitude, 52);
}
}
if (units !== 'm') {
for (i = 0; i < orderedLocations.length; i++) {
location = orderedLocations[i];
location.distance = convertUnitsFromMeters(units, location.distance);
}
}
if (order === 'DESC') {
orderedLocations = orderedLocations.reverse();
}
return orderedLocations;
}
function geohashDecode(hash) {
var decoded;
var latlon;
if (typeof hash === 'string' || typeof hash === 'number') {
latlon = geohash.decode_int(hash, 52);
decoded = {
latitude: latlon.latitude,
longitude: latlon.longitude
};
} else {
decoded = null;
}
return decoded;
}
module.exports = EmulatedInterface;