-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.js
196 lines (140 loc) · 5.24 KB
/
main.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
var NativeInterface = require('./lib/interfaceNative');
var EmulatedInterface = require('./lib/interfaceEmulated');
var QueuedInterface = require('./lib/interfaceQueued');
var randomId = require('./lib/util/helper').randomId;
var nativeCommands = ['info', 'geoadd', 'geohash', 'geopos', 'geodist', 'georadius', 'georadiusbymember'];
// main constructor
function GeoSet(options) {
options = options || {};
this.zset = options.zset || 'geo:locations';
this.parentGeoSet = options.parentGeoSet || null;
}
GeoSet.prototype.getClientInterface = function() {
return this.parentGeoSet ? this.parentGeoSet.clientInterface : this.clientInterface;
};
// initialization
GeoSet.prototype.initialize = function(client, options) {
options = options || {};
this.zset = options.zset ? options.zset : 'geo:locations';
setInterface(this, client, options.nativeGeo);
return this;
};
// managing sets
GeoSet.prototype.addSet = function(setName) {
return new GeoSet({
zset: this.zset + ':' + (setName || 'subset_' + randomId()),
parentGeoSet: this.parentGeoSet || this
});
};
GeoSet.prototype.deleteSet = function(setName, callBack) {
this.getClientInterface().del(this.zset + ':' + setName, callBack);
};
GeoSet.prototype.delete = function(callBack) {
this.getClientInterface().del(this.zset, callBack);
};
// adding locations
GeoSet.prototype.addLocation = function(locationName, point, callBack) {
this.getClientInterface().geoadd(locationName, point, this.zset, callBack);
};
GeoSet.prototype.addLocations = function(locationSet, callBack) {
this.getClientInterface().geoadd_multi(locationSet, this.zset, callBack);
};
// Calculations
GeoSet.prototype.distance = function(locationNameA, locationNameB, options, callBack) {
if (typeof options === 'function') {
callBack = options;
options = {};
} else {
options = options || {};
}
this.getClientInterface().geodist(locationNameA, locationNameB, options.units, this.zset, callBack);
};
// updating locations (same methods as add, existing locations get updated)
GeoSet.prototype.updateLocation = GeoSet.prototype.addLocation;
GeoSet.prototype.updateLocations = GeoSet.prototype.addLocations;
// removing locations
GeoSet.prototype.removeLocation = function(locationName, callBack) {
this.getClientInterface().zrem([locationName], this.zset, callBack);
};
GeoSet.prototype.removeLocations = function(locationNameArray, callBack) {
this.getClientInterface().zrem(locationNameArray, this.zset, callBack);
};
// querying location positions
GeoSet.prototype.location = function(locationName, callBack) {
this.getClientInterface().geopos([locationName], this.zset, callBack);
};
GeoSet.prototype.locations = function(locationNameArray, callBack) {
this.getClientInterface().geopos_multi(locationNameArray, this.zset, callBack);
};
// querying location geohashes
GeoSet.prototype.getGeohash = function(locationName, callBack) {
this.getClientInterface().geohash([locationName], this.zset, callBack);
};
GeoSet.prototype.getGeohashes = function(locationNameArray, callBack) {
this.getClientInterface().geohashes(locationNameArray, this.zset, callBack);
};
// querying nearby locations
GeoSet.prototype.radius = function(location, radius, options, callBack) {
if (typeof options === 'function') {
callBack = options;
options = {};
} else {
options = options || {};
}
if (typeof location === 'string') {
this.getClientInterface().georadiusbymember(location, radius, options, this.zset, callBack);
} else {
this.getClientInterface().georadius(location, radius, options, this.zset, callBack);
}
};
GeoSet.prototype.nearby = function(location, distance, options, callBack) {
if (typeof options === 'function') {
callBack = options;
options = {};
} else {
options = options || {};
}
if (typeof location === 'string') {
this.getClientInterface().nearbymember(location, distance, options, this.zset, callBack);
} else {
this.getClientInterface().nearby(location, distance, options, this.zset, callBack);
}
};
// interface config
function setInterface(geoSet, client, nativeGeo) {
var queuedInterface = new QueuedInterface(client);
if (nativeGeo === true) {
geoSet.clientInterface = new NativeInterface(client);
return;
} else if (nativeGeo === false) {
geoSet.clientInterface = new EmulatedInterface(client);
return;
}
geoSet.clientInterface = queuedInterface;
checkNativeInterface(queuedInterface, geoSet, client);
}
function checkNativeInterface(queuedInterface, geoSet, client) {
try {
client.send_command('command', nativeCommands, function(err, response) {
if (!err && hasNativeCommands(response)) {
geoSet.clientInterface = queuedInterface.drain(new NativeInterface(client));
} else {
geoSet.clientInterface = queuedInterface.drain(new EmulatedInterface(client));
}
});
} catch (err) {
geoSet.clientInterface = queuedInterface.drain(new EmulatedInterface(client));
}
}
function hasNativeCommands(response) {
if (Array.isArray(response) && response.length === nativeCommands.length - 1) {
for (var i = 0; i < response.length; i++) {
if (!response[i]) {
return false;
}
}
return true;
}
return false;
}
module.exports = exports = new GeoSet();