summaryrefslogtreecommitdiff
path: root/node/ejdb.js
blob: 66abc00e8588728fb205c426e369a286a286e9bd (plain)
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
var ejdblib;
try {
    ejdblib = require("../build/Release/ejdb_native.node");
} catch (e) {
    ejdblib = require("../build/Debug/ejdb_native.node");
    console.error("Warning: Using the DEBUG version of EJDB nodejs binding");
}
var EJDBImpl = ejdblib.NodeEJDB;

const DEFAULT_OPEN_MODE = (ejdblib.JBOWRITER | ejdblib.JBOCREAT);
var EJDB = function(dbFile, openMode) {
    Object.defineProperty(this, "_impl", {
        value : new EJDBImpl(dbFile, (openMode > 0) ? openMode : DEFAULT_OPEN_MODE),
        configurable : false,
        enumerable : false,
        writable : false
    });
    return this;
};

for (var k in ejdblib) { //Export constants
    if (k.indexOf("JB") === 0) {
        EJDB[k] = ejdblib[k];
    }
}
EJDB.DEFAULT_OPEN_MODE = DEFAULT_OPEN_MODE;

/**
 * Open database.
 * Return database instance handle object .
 *
 * Default open mode: JBOWRITER | JBOCREAT
 *
 * This is blocking function.
 *
 * @param {String} dbFile Database main file name
 * @param {Number} [openMode=JBOWRITER | JBOCREAT] Bitmast of open modes:
 *      - `JBOREADER` Open as a reader.
 *      - `JBOWRITER` Open as a writer.
 *      - `JBOCREAT` Create db if it not exists
 *      - `JBOTRUNC` Truncate db.
 * @returns {EJDB} EJDB database wrapper
 */

EJDB.open = function(dbFile, openMode) {
    return new EJDB(dbFile, openMode);
};


/**
 * Returns true if argument is valid object id (OID) string.
 * @param {String} oid Object id.
 * @return {Boolean}
 */
EJDB.isValidOID = function(oid) {
    if (typeof oid !== "string" || oid.length !== 24) {
        return false;
    }
    var i = 0;
    //noinspection StatementWithEmptyBodyJS
    for (; ((oid[i] >= 0x30 && oid[i] <= 0x39) || /* 1 - 9 */
            (oid[i] >= 0x61 && oid[i] <= 0x66)); /* a - f */
           ++i);
    return (i === 24);
};

/**
 * Close database.
 * If database was not opened it does nothing.
 *
 * This is blocking function.
 */
EJDB.prototype.close = function() {
    return this._impl.close();
};

/**
 * Check if database in opened state.
 */
EJDB.prototype.isOpen = function() {
    return this._impl.isOpen();
};

/**
 * Automatically creates new collection if it does't exists.
 * Collection options `copts`
 * are applied only for newly created collection.
 * For existing collections `copts` takes no effect.
 *
 * Collection options (copts):
 *  {
 *      "cachedrecords" : Max number of cached records in shared memory segment. Default: 0
 *      "records" : Estimated number of records in this collection. Default: 65535.
 *      "large" : Specifies that the size of the database can be larger than 2GB. Default: false
 *      "compress" : If true collection records will be compressed with DEFLATE compression. Default: false.
 *  }
 *
 * This is blocking function.
 *
 * @param {String} cname Name of collection.
 * @param {Object} [copts] Collection options.
 * @return {*}
 */
EJDB.prototype.ensureCollection = function(cname, copts) {
    return this._impl.ensureCollection(cname, copts || {});
};

/**
 *  Remove collection.
 *
 *  Call variations:
 *      - removeCollection(cname)
 *      - removeCollection(cname, cb)
 *      - removeCollection(cname, prune, cb)
 *
 * @param {String} cname Name of collection.
 * @param {Boolean} [prune=false] If true the collection data will erased from disk.
 * @param {Function} [cb] Callback function with arguments: (error)
 */
EJDB.prototype.removeCollection = function(cname, prune, cb) {
    if (arguments.length == 2) {
        cb = prune;
        prune = false;
    }
    if (!cb) {
        cb = function() {
        };
    }
    return this._impl.removeCollection(cname, !!prune, cb);
};


/**
 * Save/update specified JSON objects in the collection.
 * If collection with `cname` does not exists it will be created.
 *
 * Each persistent object has unique identifier (OID) placed in the `_id` property.
 * If a saved object does not have `_id` it will be autogenerated.
 * To identify and update object it should contains `_id` property.
 *
 * If callback is not provided this function will be synchronous.
 *
 * Call variations:
 *      - save(cname, <json object>|<Array of json objects>, options)
 *      - save(cname, <json object>|<Array of json objects>, cb)
 *      - save(cname, <json object>|<Array of json objects>, options, cb)
 *
 * @param {String} cname Name of collection.
 * @param {Array|Object} jsarr Signle JSON object or array of JSON objects to save
 * @param {Function} [cb] Callback function with arguments: (error, {Array} of OIDs for saved objects)
 * @return {Array} of OIDs of saved objects in synchronous mode otherwise returns {undefined}.
 */
EJDB.prototype.save = function(cname, jsarr, opts, cb) {
    if (!jsarr) {
        return [];
    }
    if (jsarr.constructor !== Array) {
        jsarr = [jsarr];
    }
    if (typeof opts == "function") {
        cb = opts;
        opts = null;
    }
    var postprocess = function(oids) {
        //Assign _id property for newly created objects
        for (var i = jsarr.length - 1; i >= 0; --i) {
            var so = jsarr[i];
            if (so != null && so["_id"] !== oids[i]) {
                so["_id"] = oids[i];
            }
        }
    };
    if (cb == null) {
        postprocess(this._impl.save(cname, jsarr, (opts || {})));
        return jsarr;
    } else {
        return this._impl.save(cname, jsarr, (opts || {}), function(err, oids) {
            if (err) {
                cb(err);
                return;
            }
            postprocess(oids);
            cb(null, oids);
        });
    }
};


/**
 * Loads JSON object identified by OID from the collection.
 * If callback is not provided this function will be synchronous.
 *
 * @param {String} cname Name of collection
 * @param {String} oid Object identifier (OID)
 * @param {Function} [cb]  Callback function with arguments: (error, obj)
 *        `obj`:  Retrieved JSON object or NULL if it is not found.
 * @return JSON object or {null} if it is not found in synchronous mode otherwise return {undefined}.
 */
EJDB.prototype.load = function(cname, oid, cb) {
    return this._impl.load(cname, oid, cb);
};

/**
 * Removes JSON object from the collection.
 * If callback is not provided this function will be synchronous.
 *
 * @param {String} cname Name of collection
 * @param {String} oid Object identifier (OID)
 * @param {Function} [cb]  Callback function with arguments: (error)
 * @return {undefined}
 */
EJDB.prototype.remove = function(cname, oid, cb) {
    return this._impl.remove(cname, oid, cb);
};


/*
 * - (cname, [cb])
 * - (cname, qobj, [cb])
 * - (cname, qobj, hints, [cb])
 * - (cname, qobj, qobjarr, [cb])
 * - (cname, qobj, qobjarr, hints, [cb])
 */
function parseQueryArgs(args) {
    var cname, qobj, orarr, hints, cb;
    var i = 0;
    cname = args[i++];
    if (typeof cname !== "string") {
        throw new Error("Collection name 'cname' argument must be specified");
    }
    var next = args[i++];
    if (typeof next === "function") {
        cb = next;
    } else {
        qobj = next;
    }
    next = args[i++];
    if (next !== undefined) {
        if (next.constructor === Array) {
            orarr = next;
            next = args[i++];
        } else if (typeof next === "object") {
            hints = next;
            orarr = null;
            next = args[i++];
        }
        if (!hints && typeof next === "object") {
            hints = next;
            next = args[i++];
        }
        if (typeof next === "function") {
            cb = next;
        }
    }
    return [cname, (qobj || {}), (orarr || []), (hints || {}), (cb || null)];
}

/**
 * Execute query on collection.
 *
 * EJDB queries inspired by MongoDB (mongodb.org) and follows same philosophy.
 *
 *  - Supported queries:
 *      - Simple matching of String OR Number OR Array value:
 *          -   {'json.field.path' : 'val', ...}
 *      - $not Negate operation.
 *          -   {'json.field.path' : {'$not' : val}} //Field not equal to val
 *          -   {'json.field.path' : {'$not' : {'$begin' : prefix}}} //Field not begins with val
 *      - $begin String starts with prefix
 *          -   {'json.field.path' : {'$begin' : prefix}}
 *      - $gt, $gte (>, >=) and $lt, $lte for number types:
 *          -   {'json.field.path' : {'$gt' : number}, ...}
 *      - $bt Between for number types:
 *          -   {'json.field.path' : {'$bt' : [num1, num2]}}
 *      - $in String OR Number OR Array val matches to value in specified array:
 *          -   {'json.field.path' : {'$in' : [val1, val2, val3]}}
 *      - $nin - Not IN
 *      - $strand String tokens OR String array val matches all tokens in specified array:
 *          -   {'json.field.path' : {'$strand' : [val1, val2, val3]}}
 *      - $stror String tokens OR String array val matches any token in specified array:
 *          -   {'json.field.path' : {'$stror' : [val1, val2, val3]}}
 *      - $exists Field existence matching:
 *          -   {'json.field.path' : {'$exists' : true|false}}
 *      - $icase Case insensitive string matching:
 *          -    {'json.field.path' : {'$icase' : 'val1'}} //icase matching
 *          icase matching with '$in' operation:
 *          -    {'name' : {'$icase' : {'$in' : ['tHéâtre - театр', 'heLLo WorlD']}}}
 *          For case insensitive matching you can create special type of string index.
 *      - $elemMatch The $elemMatch operator matches more than one component within an array element.
 *          -    { array: { $elemMatch: { value1 : 1, value2 : { $gt: 1 } } } }
 *          Restriction: only one $elemMatch allowed in context of one array field.
 *
 *  - Queries can be used to update records:
 *
 *      $set Field set operation.
 *          - {.., '$set' : {'field1' : val1, 'fieldN' : valN}}
 *      $inc Increment operation. Only number types are supported.
 *          - {.., '$inc' : {'field1' : number, ...,  'field1' : number}
 *      $dropall In-place record removal operation.
 *          - {.., '$dropall' : true}
 *      $addToSet Atomically adds value to the array only if its not in the array already.
 *                  If containing array is missing it will be created.
 *          - {.., '$addToSet' : {'json.field.path' : val1, 'json.field.pathN' : valN, ...}}
 *      $pull Atomically removes all occurrences of value from field, if field is an array.
 *          - {.., '$pull' : {'json.field.path' : val1, 'json.field.pathN' : valN, ...}}
 *
 *
 *  NOTE: It is better to execute update queries with `$onlycount=true` hint flag
 *        or use the special `update()` method to avoid unnecessarily data fetching.
 *  NOTE: Negate operations: $not and $nin not using indexes
 *        so they can be slow in comparison to other matching operations.
 *  NOTE: Only one index can be used in search query operation.
 *  NOTE: If callback is not provided this function will be synchronous.
 *
 *  QUERY HINTS (specified by `hints` argument):
 *      - $max Maximum number in the result set
 *      - $skip Number of skipped results in the result set
 *      - $orderby Sorting order of query fields.
 *      - $onlycount true|false If `true` only count of matching records will be returned
 *                              without placing records in result set.
 *      - $fields Set subset of fetched fields
 *          Example:
 *          hints:    {
 *                      "$orderby" : { //ORDER BY field1 ASC, field2 DESC
 *                          "field1" : 1,
 *                          "field2" : -1
 *                      },
 *                      "$fields" : { //SELECT ONLY {_id, field1, field2}
 *                          "field1" : 1,
 *                          "field2" : 1
 *                      }
 *                    }
 *
 * Many C API query examples can be found in `tcejdb/testejdb/t2.c` test case.
 *
 * To traverse selected records cursor object is used:
 *      - Cursor#next() Move cursor to the next record and returns true if next record exists.
 *      - Cursor#hasNext() Returns true if cursor can be placed to the next record.
 *      - Cursor#field(name) Retrieve value of the specified field of the current JSON object record.
 *      - Cursor#object() Retrieve whole JSON object with all fields.
 *      - Cursor#reset() Reset cursor to its initial state.
 *      - Cursor#length Read-only property: Number of records placed into cursor.
 *      - Cursor#pos Read/Write property: You can set cursor position: 0 <= pos < length
 *      - Cursor#close() Closes cursor and free cursor resources. Cursor cant be used in closed state.
 *
 * Call variations of find():
 *       - find(cname, [cb])
 *       - find(cname, qobj, [cb])
 *       - find(cname, qobj, hints, [cb])
 *       - find(cname, qobj, qobjarr, [cb])
 *       - find(cname, qobj, qobjarr, hints, [cb])
 *
 * @param {String} cname Name of collection
 * @param {Object} qobj Main JSON query object
 * @param {Array} [orarr] Array of additional OR query objects (joined with OR predicate).
 * @param {Object} [hints] JSON object with query hints.
 * @param {Function} [cb] Callback function with arguments: (error, cursor, count) where:
 *          `cursor`: Cursor object to traverse records
 *          `count`:  Total number of selected records.
 * @return If callback is provided returns {undefined}.
 *         If no callback and $onlycount hint is set returns count {Number}.
 *         If no callback and no $onlycount hint returns cursor {Object}.
 *
 */
EJDB.prototype.find = function() {
    //[cname, qobj, orarr, hints, cb]
    var qa = parseQueryArgs(arguments);
    return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]),
            (qa[3]["$onlycount"] ? ejdblib.JBQRYCOUNT : 0),
            qa[4]);
};

/**
 * Same as #find() but retrieves only one matching JSON object.
 * If callback is not provided this function will be synchronous.
 *
 * Call variations of findOne():
 *       - findOne(cname, [cb])
 *       - findOne(cname, qobj, [cb])
 *       - findOne(cname, qobj, hints, [cb])
 *       - findOne(cname, qobj, qobjarr, [cb])
 *       - findOne(cname, qobj, qobjarr, hints, [cb])
 *
 * @param {String} cname Name of collection
 * @param {Object} qobj Main JSON query object
 * @param {Array} [orarr] Array of additional OR query objects (joined with OR predicate).
 * @param {Object} [hints] JSON object with query hints.
 * @param {Function} [cb] Callback function with arguments: (error, obj) where:
 *          `obj`:  Retrieved JSON object or NULL if it is not found.
 * @return  If callback is provided returns {undefined}.
 *          If no callback is provided returns found {Object} or {null}.
 */

EJDB.prototype.findOne = function() {
    //[cname, qobj, orarr, hints, cb]
    var qa = parseQueryArgs(arguments);
    qa[3]["$max"] = 1;
    var cb = qa[4];
    if (cb) {
        return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), 0,
                function(err, cursor) {
                    if (err) {
                        cb(err);
                        return;
                    }
                    if (cursor.next()) {
                        try {
                            cb(null, cursor.object());
                        } finally {
                            cursor.close();
                        }
                    } else {
                        cb(null, null);
                    }
                });
    } else {
        var ret = null;
        var cursor = this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), 0, cb);
        if (cursor && typeof cursor === "object") {
            if (cursor.next()) {
                ret = cursor.object();
            }
            cursor.close();
        }
        return ret;
    }
};


/**
 * Convenient method to execute update queries.
 * If callback is not provided this function will be synchronous.
 *
 * The following update operations are supported:
 *      $set Field set operation.
 *          - {.., '$set' : {'field1' : val1, 'fieldN' : valN}}
 *      $inc Increment operation. Only number types are supported.
 *          - {.., '$inc' : {'field1' : number, ...,  'field1' : number}
 *      $dropall In-place record removal operation.
 *          - {.., '$dropall' : true}
 *      $addToSet Atomically adds value to the array only if its not in the array already.
 *                  If containing array is missing it will be created.
 *          - {.., '$addToSet' : {'json.field.path' : val1, 'json.field.pathN' : valN, ...}}
 *      $pull Atomically removes all occurrences of value from field, if field is an array.
 *          - {.., '$pull' : {'json.field.path' : val1, 'json.field.pathN' : valN, ...}}
 *
 * Call variations of update():
 *    update(cname, qobj, [cb])
 *    update(cname, qobj, hints, [cb])
 *    update(cname, qobj, qobjarr, [cb])
 *    update(cname, qobj, qobjarr, hints, [cb])
 *
 * @param {String} cname Name of collection
 * @param {Object} qobj Main JSON query object
 * @param {Array} [orarr] Array of additional OR query objects (joined with OR predicate).
 * @param {Object} [hints] JSON object with query hints.
 * @param {Function} [cb] Callback function with arguments: (error, count) where:
 *          `count`:  The number of updated records.
 *
 * @return  If callback is provided returns {undefined}.
 *          If no callback is provided returns {Number} of updated objects.
 */
EJDB.prototype.update = function() {
    //[cname, qobj, orarr, hints, cb]
    var qa = parseQueryArgs(arguments);
    var cb = qa[4];
    if (cb) {
        return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), ejdblib.JBQRYCOUNT,
                function(err, cursor, count, log) {
                    if (err) {
                        cb(err, null, log);
                        return;
                    }
                    cb(null, count, log);
                });
    } else {
        return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), ejdblib.JBQRYCOUNT, cb);
    }
};

/**
 * Convenient count(*) operation.
 *
 * Call variations of count():
 *       - count(cname, [cb])
 *       - count(cname, qobj, [cb])
 *       - count(cname, qobj, hints, [cb])
 *       - count(cname, qobj, qobjarr, [cb])
 *       - count(cname, qobj, qobjarr, hints, [cb])
 *
 * @param {String} cname Name of collection
 * @param {Object} qobj Main JSON query object
 * @param {Array} [orarr] Array of additional OR query objects (joined with OR predicate).
 * @param {Object} [hints] JSON object with query hints.
 * @param {Function} [cb] Callback function with arguments: (error, count) where:
 *          `count`:  Number of matching records.
 *
 * @return  If callback is provided returns {undefined}.
 *          If no callback is provided returns {Number} of matched object.
 */
EJDB.prototype.count = function() {
    //[cname, qobj, orarr, hints, cb]
    var qa = parseQueryArgs(arguments);
    var cb = qa[4];
    if (cb) {
        return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), ejdblib.JBQRYCOUNT,
                function(err, cursor, count, log) {
                    if (err) {
                        cb(err, null, log);
                        return;
                    }
                    cb(null, count, log);
                });
    } else {
        return this._impl.query(qa[0], [qa[1]].concat(qa[2], qa[3]), ejdblib.JBQRYCOUNT, cb);
    }
};

/**
 * Synchronize entire EJDB database and
 * all its collections with storage.
 * If callback is not provided this function will be synchronous.
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.sync = function(cb) {
    return this._impl.sync(cb);
};

/**
 * DROP indexes of all types for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.dropIndexes = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXDROPALL, cb);
};

/**
 * OPTIMIZE indexes of all types for JSON field path.
 *  Performs B+ tree index file optimization.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.optimizeIndexes = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXOP, cb);
};

/**
 * Ensure index presence of String type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.ensureStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXSTR, cb);
};

/**
 * Rebuild index of String type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.rebuildStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXSTR | ejdblib.JBIDXREBLD, cb);
};

/**
 * Drop index of String type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.dropStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXSTR | ejdblib.JBIDXDROP, cb);
};

/**
 * Ensure case insensitive String index for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.ensureIStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXISTR, cb);
};

/**
 * Rebuild case insensitive String index for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.rebuildIStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXISTR | ejdblib.JBIDXREBLD, cb);
};

/**
 * Drop case insensitive String index for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.dropIStringIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXISTR | ejdblib.JBIDXDROP, cb);
};

/**
 * Ensure index presence of Number type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.ensureNumberIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXNUM, cb);
};

/**
 * Rebuild index of Number type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.rebuildNumberIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXNUM | ejdblib.JBIDXREBLD, cb);
};

/**
 * Drop index of Number type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.dropNumberIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXNUM | ejdblib.JBIDXDROP, cb);
};

/**
 * Ensure index presence of Array type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.ensureArrayIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXARR, cb);
};

/**
 * Rebuild index of Array type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.rebuildArrayIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXARR | ejdblib.JBIDXREBLD, cb);
};

/**
 * Drop index of Array type for JSON field path.
 * If callback is not provided this function will be synchronous.
 * @param {String} cname Name of collection
 * @param {String} path  JSON field path
 * @param {Function} [cb] Optional callback function. Callback args: (error)
 */
EJDB.prototype.dropArrayIndex = function(cname, path, cb) {
    return this._impl.setIndex(cname, path, ejdblib.JBIDXARR | ejdblib.JBIDXDROP, cb);
};


/**
 * Get description of EJDB database and its collections.
 */
EJDB.prototype.getDBMeta = function() {
    return this._impl.dbMeta();
};

module.exports = EJDB;