-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
450 lines (412 loc) · 12.1 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright 2016: Neil Jagdish Patel <njpatel@gmail.com>
*/
'use strict';
var events = require('events');
var grpc = require('grpc');
var defaultMembUnique = 1000000;
var defaultMembErrorRate = 0.01;
var defaultFreqUnique = 100000;
var defaultFreqErrorRate = 0.01;
var defaultRankSize = 100;
/**
* Enum for Sketch type values.
* @readonly
* @enum {number}
*/
var sketchType = {
MEMBERSHIP : 1,
FREQUENCY : 2,
RANKINGS : 3,
CARDINALITY : 4
};
/**
* Enum for Snapshot state values.
* @readonly
* @enum {number}
*/
var snapshotState = {
PENDING : 1,
IN_PROGRESS : 2,
SUCCESSFUL : 3,
FAILED : 4
};
/**
* Represents a connection to the Skizze database.
* @constructor
*/
function SkizzeClient(address, options) {
events.EventEmitter.call(this);
this.skizzeProto = grpc.load(__dirname +"/skizze.proto").protobuf;
var clientOpts = options.insecure ? grpc.credentials.createInsecure() : null;
this.client = new this.skizzeProto.Skizze(address, clientOpts);
}
/**
* Request a snapshot to be taken.
* @param {function(Error)} callback - A callback to call when a reply is received.
*/
SkizzeClient.prototype.createSnapshot = function(callback) {
this.client.createSnapshot({}, function(err) {
callback(err);
});
}
/**
* Get details of the current or last snapshot.
* @param {function(Error)} callback - A callback to call with the details of the snapshot.
*/
SkizzeClient.prototype.getSnapshot = function(callback) {
this.client.getSnapshot({}, callback);
}
/**
* List Sketches of a specific type.
* @param {number} type - The type of sketch to list.
* @param {function(Error,Array)} callback - A callback to call with the list of Sketches.
*/
SkizzeClient.prototype.listSketches = function(type, callback) {
this.client.list({ type: type }, callback);
}
/**
* List all domains.
* @param {function(Error,Array)} callback - A callback to call with the list of Domains.
*/
SkizzeClient.prototype.listDomains = function(callback) {
this.client.listDomains({}, callback);
}
/**
* List all Sketches.
* @param {function(Error,Array)} callback - A callback to call with the list of Sketches.
*/
SkizzeClient.prototype.listAll = function(callback) {
this.client.listAll({}, callback);
}
/**
* Create a new domain with default settings for it's Sketches.
* @param {string} name - The name of the new domain.
* @param {function(Error)} callback - A callback to call with the newly created domain.
*/
SkizzeClient.prototype.createDomain = function(name, callback) {
var args = {
name: name,
sketches: [
{
name: name,
type: sketchType.MEMBERSHIP,
properties: {
maxUniqueItems: defaultMembUnique,
errorRate: defaultMembErrorRate
}
},
{
name: name,
type: sketchType.FREQUENCY,
properties: {
maxUniqueItems: defaultFreqUnique,
errorRate: defaultFreqErrorRate
}
},
{
name: name,
type: sketchType.RANKINGS,
properties: {
size: defaultRankSize
}
},
{
name: name,
type: sketchType.CARDINALITY
}
]
};
this.client.createDomain(args, callback)
}
/**
* Create a new domain with customized settings.
* @param {string} name - The name of the new domain.
* @param {Object} sketches - Configuration for each sketch type.
* @param {function(Error)} callback - A callback to call with the newly created domain.
*/
SkizzeClient.prototype.createDomainWithProperties = function(name, sketches, callback) {
var args = {
name: name,
sketches: [sketches.membership, sketches.frequency, sketches.rankings, sketches.cardinality]
};
this.client.createDomain(args, callback)
}
/**
* Delete a domain.
* @param {string} name - The name of the domain to delete.
* @param {function(Error)} callback - A callback to call when the operation is complete.
*/
SkizzeClient.prototype.deleteDomain = function(name, callback) {
this.client.deleteDomain({ name: name }, function(err) {
callback(err);
});
}
/**
* Get details of an existing domain.
* @param {string} name - The name of the domain to get.
* @param {function(Error,Object)} callback - A callback to call with the domain details.
*/
SkizzeClient.prototype.getDomain = function(name, callback) {
this.client.getDomain({ name: name }, callback);
}
/**
* Create a new Sketch.
* @param {string} name - The name of the Sketch to create.
* @param {function(Error,Object)} callback - A callback to call with the new Sketch.
*/
SkizzeClient.prototype.createSketch = function(name, type, props) {
this.client.createSketch({
name: name,
type: type,
properties: props
}, callback);
}
/**
* Delete a Sketch.
* @param {string} name - The name of the sketch to delete.
* @param {function(Error)} callback - A callback to call when the operation is complete.
*/
SkizzeClient.prototype.deleteSketch = function(name, callback) {
this.client.deleteSketch({ name: name }, function(err) {
callback(err);
});
}
/**
* Get details of an existing sketch.
* @param {string} name - The name of the sketch to get.
* @param {function(Error,Object)} callback - A callback to call with the sketch details.
*/
SkizzeClient.prototype.getSketch = function(name, callback) {
this.client.getSketch({ name: name }, callback);
}
/**
* Add values to a domain.
* @param {string} name - The name of the domain.
* @param {Array(string)} values - The values to add to the domain.
* @param {function(Error)} callback - A callback to call when the operation is complete.
*/
SkizzeClient.prototype.addToDomain = function(name, values, callback) {
this.client.add({
domain: {
name: name
},
values: values
},
function(err) {
callback(err);
});
}
/**
* Add values to a sketch.
* @param {string} name - The name of the sketch.
* @param {Array(string)} values - The values to add to the sketch.
* @param {function(Error)} callback - A callback to call when the operation is complete.
*/
SkizzeClient.prototype.addToSketch = function(name, values, callback) {
this.client.add({
sketch: {
name: name
},
values: values
},
function(err) {
callback(err);
});
}
/**
* Query the sketch for membership (true/false) for the provided values.
* @param {string} name - The name of the sketch.
* @param {Array(string)} values - The values to query the membership of.
* @param {function(Error,Object)} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getMembership = function(name, values, callback) {
this.client.getMembership({
sketches: [
{ name: name, type: sketchType.MEMBERSHIP }
],
values: values
},
function(err, reply) {
callback(err, reply ? reply.results[0].memberships : null);
});
}
/**
* Query the sketches for membership (true/false) for the provided values.
* @param {Array(string)} names - The names of the sketches to check.
* @param {Array(string)} values - The values to query the membership of.
* @param {function(Error,Array(Object))} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getMultiMembership = function(names, values, callback) {
var sketches = [];
names.forEach(function(name) {
sketches.push({
name: name,
type: sketchType.MEMBERSHIP
});
});
this.client.getMembership({
sketches: sketches,
values: values
},
function(err, reply) {
var ret = [];
if (reply) {
reply.results.forEach(function(result) {
ret.push(result.memberships);
});
}
callback(err, err ? null : ret)
});
}
/**
* Query the sketch for frequency of the provided values.
* @param {string} name - The name of the sketch.
* @param {Array(string)} values - The values to query the frequency of.
* @param {function(Error,Object)} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getFrequency = function(name, values, callback) {
this.client.getFrequency({
sketches: [
{ name: name, type: sketchType.FREQUENCY }
],
values: values
},
function(err, reply) {
callback(err, reply ? unpackIntegerResults(reply.results[0].frequencies) : null);
});
}
/**
* Query the sketches for frequency of the provided values.
* @param {Array(string)} names - The names of the sketches to check.
* @param {Array(string)} values - The values to query the frequency of.
* @param {function(Error,Array(Object))} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getMultiFrequency = function(names, values, callback) {
var sketches = [];
names.forEach(function(name) {
sketches.push({
name: name,
type: sketchType.FREQUENCY
});
});
this.client.getFrequency({
sketches: sketches,
values: values
},
function(err, reply) {
let ret = []
if (reply) {
reply.results.forEach(function(result) {
ret.push(unpackIntegerResults(result.frequencies));
});
}
callback(err, err ? null : ret);
});
}
/**
* Query the sketch for top rankings.
* @param {string} name - The name of the sketch.
* @param {function(Error,Object)} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getRankings = function(name, callback) {
this.client.getRankings({
sketches: [
{ name: name, type: sketchType.RANKINGS }
]
},
function(err, reply) {
callback(err, reply ? unpackIntegerResults(reply.results[0].rankings) : null);
});
}
/**
* Query the sketches top rankings.
* @param {Array(string)} names - The names of the sketches to check.
* @param {function(Error,Array(Object))} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getMultiRankings = function(names, callback) {
var sketches = [];
names.forEach(function(name) {
sketches.push({
name: name,
type: sketchType.RANKINGS
});
});
this.client.getRankings({
sketches: sketches
},
function(err, reply) {
let ret = []
if (reply) {
reply.results.forEach(function(result) {
ret.push(unpackIntegerResults(result.rankings));
});
}
callback(err, err ? null : ret);
});
}
/**
* Query the sketch for cardinality.
* @param {string} name - The name of the sketch.
* @param {function(Error,Object)} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getCardinality = function(name, callback) {
this.client.getCardinality({
sketches: [
{ name: name, type: sketchType.CARDINALITY }
]
},
function(err, reply) {
callback(err, reply ? parseInt(reply.results[0].cardinality) : null);
});
}
/**
* Query the sketches for cardinality.
* @param {Array(string)} names - The names of the sketches to check.
* @param {function(Error,Array(Object))} callback - A callback to call with the results.
*/
SkizzeClient.prototype.getMultiCardinality = function(names, callback) {
var sketches = [];
names.forEach(function(name) {
sketches.push({
name: name,
type: sketchType.CARDINALITY
});
});
this.client.getCardinality({
sketches: sketches
},
function(err, reply) {
let ret = []
if (reply) {
reply.results.forEach(function(result) {
ret.push(parseInt(result.cardinality));
});
}
callback(err, err ? null : ret);
});
}
/**
* Creates and returns a new client connection to Skizze
* @param {string} address - The address of the Skizze server e.g. "127.0.0.1:3596"
* @param {Object} options - Options for configuring the client connection.
* @param {boolean} options.insecure - Whether to create an insecure connection with the server.
* @returns {SkizzeClient} - A SkizzeClient.
*/
var createClient = function(address, options) {
if (typeof address !== 'string') {
throw new Error("Address must be valid.");
}
return new SkizzeClient(address, options || {});
}
function unpackIntegerResults(results) {
var ret = [];
results.forEach(function(result) {
ret.push({ value: result.value, count: parseInt(result.count) });
});
return ret;
}
exports.createClient = createClient;
exports.SkizzeClient = SkizzeClient;
exports.sketchType = sketchType;
exports.snapshotState = snapshotState;