# All files bulksearch.js

79.37% Statements 300/378
67.74% Branches 168/248
77.5% Functions 31/40
79.37% Lines 300/378

    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
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    7x

    7x

    7x

    2x

    7x

    5x

    1x

    2x

    1x

    2x

    1x

    1x

    4x

    4x

    1x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x

    1x

    48x

    13x

    48x

    4x

    16x

    16x

    48x

    1x

    17x

    8x

    8x
    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x
    8x
    8x
    8x
    8x

    8x

    3x
    3x

    3x

    3x

    3x

    5x

    5x

    5x

    5x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    1x

    15x

    6x

    6x

    6x

    3x

    6x
    6x
    6x
    6x
    6x

    6x

    1x

    6x

    5x

    6x
    6x
    6x

    6x

    6x

    4x

    4x

    2x

    2x

    2x

    4x
    4x

    4x

    4x

    1x

    6x

    1x

    3x

    3x

    1x

    1x

    33x

    2x

    31x

    6x
    6x

    25x

    31x

    31x

    5x

    5x

    26x

    26x

    2x

    29x

    6x

    6x

    3x
    3x

    6x

    23x

    23x

    4x

    23x

    3x

    23x

    1x

    22x

    22x

    22x

    4x

    22x

    22x

    22x

    22x

    22x

    22x
    22x
    22x

    22x

    30x
    30x

    30x

    30x
    30x
    30x

    30x

    8x

    8x

    10x

    5x
    5x

    10x

    4x

    4x

    30x

    30x
    30x
    30x

    30x

    30x

    3x

    27x

    27x

    22x

    3x

    22x

    19x

    3x

    22x

    22x

    1x

    1x

    1x

    1x

    1x

    6x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    6x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1x

    4x

    4x

    4x

    4x

    4x

    2x

    2x

    2x

    4x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    2x

    2x

    2x

    2x

    2x

    6x

    6x

    6x

    2x
    2x

    2x

    1x

    7x

    7x

    41x

    16x

    16x

    178x

    16x

    6x

    6x

    4x
    4x
    4x

    4x

    48x

    48x

    44x

    44x

    48x

    48x

    4x

    1x

    1x

    6x

    6x

    5x

    6x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

     /**!
     * BulkSearch - Superfast lightweight full text search engine
     * ----------------------------------------------------------
     * @author: Thomas Wilkerling
     * @preserve https://github.com/nextapps-de/bulksearch
     * @version: 0.1.2
     * @license: Apache 2.0 Licence
     */

    ;(function(){

        provide('BulkSearch', (function factory(queue, cache){

            "use strict";

            /**
             * @struct
             * @private
             * @const
             * @final
             */

            var defaults = {

                // bitsize of assigned IDs (data type)
                type: 'integer',

                // type of information
                result: 'id',

                // type-save separator
                separator: '~', // '^'

                // match when starts from beginning
                strict: false,

                // handle multiple words as separated queries
                multi: false,

                // boolean model of multiple words
                boolean: false,

                // matching in strict order (multiple words)
                ordered: false,

                // use flexible cache (scales automatically)
                cache: false,

                // use on of built-in functions
                // or pass custom encoding algorithm
                encode: false,

                // default chunk size
                size: 4000, // 600 -> 2500 -> 4000 -> 5000 -> 10000

                // depth of register
                depth: 0
            };

            /**
             * @type {Array<Array>}
             * @private
             */

            var custom_matchers = [];

            /**
             * @type {number}
             * @private
             */

            var chunk_size = 10000;

            /**
             * @type {number}
             * @private
             */

            var id_counter = 0;

            /**
             * @type {Object<string, number>}
             * @private
             */

            var scores = {};

            /**
             * @param {Object<string, number|string|boolean>=} options
             * @constructor
             * @private
             * @const
             */

            function BulkSearch(options){

                // generate UID

                /** @export */
                this.id = id_counter++;

                // initialize index

                this.init(options || defaults);

                // define functional properties

                Object.defineProperty(this, 'index', {

                    /**
                     * @this {BulkSearch}
                     */

                    get: function(){

                        return this._marker;
                    }
                });

                Object.defineProperty(this, 'length', {

                    /**
                     * @this {BulkSearch}
                     */

                    get: function(){

                        return Object.keys(this._marker).length;
                    }
                });
            }

            /**
             * @param {Object<string, number|string|boolean>=} options
             * @export
             */

            BulkSearch.new = function(options){

                return new this(options);
            };

            /**
             * @param {Object<string, number|string|boolean>=} options
             * @export
             */

            BulkSearch.create = function(options){

                return BulkSearch.new(options);
            };

            /**
             * @param {Object<string, string>} matcher
             * @export
             */

            BulkSearch.addMatcher = function(matcher){

                for(var key in matcher){

                    Eif(matcher.hasOwnProperty(key)){

                        custom_matchers[custom_matchers.length] = [

                            regex(key), matcher[key]
                        ];
                    }
                }
            };

            /**
             * @param {Object<string, number|string|boolean>=} options
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.init = function(options){

                // apply options

                Eif(options){

                    this.type = (

                        options['type'] ||
                        this.type ||
                        defaults.type
                    );

                    this.result = (

                        options['result'] ||
                        this.result ||
                        defaults.result
                    );

                    this.separator = (

                        options['separator'] ||
                        this.separator ||
                        defaults.separator
                    );

                    this.strict = (

                        options['strict'] ||
                        this.strict ||
                        defaults.strict
                    );

                    this.ordered = (

                        options['ordered'] ||
                        this.ordered ||
                        defaults.ordered
                    );

                    this.multi = (

                        options['multi'] ||
                        this.multi ||
                        defaults.multi
                    );

                    this.boolean = (

                        options['boolean'] === 'or' ||
                        this.boolean ||
                        defaults.boolean
                    );

                    this.cache = (

                        options['cache'] ||
                        this.cache ||
                        defaults.cache
                    );

                    this.depth = (

                        options['depth'] ||
                        this.depth ||
                        defaults.depth
                    );

                    /** @export */
                    this.encoder = (

                        (options['encode'] && encoder[options['encode']]) ||
                        this.encoder ||
                        (defaults.encode && encoder[defaults.encode]) ||
                        options['encode']
                    );

                    chunk_size = /** @type {number} */ (options['size'] || defaults.size);
                }

                // initialize index

                this._index = [create_typed_array(this.type, chunk_size)];
                this._marker = {};
                this._fragment = {};
                this._register = {};
                this._bulk = [""];
                this._chunk = 0;
                this._status = true;
                this._fragmented = 0;
                this._cache = this.cache ?

                    (new cache(30 * 1000, 50, true))
                :
                    false;
            };

            /**
             * @param {!string} value
             * @returns {string}
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.encode = function(value){

                if(this.encoder){

                    value = this.encoder(value);
                }

                if(custom_matchers.length){

                    for(var i = 0; i < custom_matchers.length; i++){

                        var matcher = custom_matchers[i];

                        value = value.replace(matcher[0], matcher[1]);
                    }
                }

                return value;
            };

            /**
             * @param {!number|string} id
             * @param {!string} content
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.add = function(id, content){

                if((typeof content === 'string') && content && (id || (id === 0))){

                    // check if index ID already exist

                    Iif(this._marker[id]){

                        this.update(id, content);
                    }
                    else{

                        var dupes = {};
                        var words = content.split(' ');

                        content = "";

                        for(var i = 0; i < words.length; i++){

                            /** @type {string} */
                            var value = words[i];

                            Eif(value){

                                Eif(this.encode){

                                    value = this.encode(value);
                                }

                                Eif(value){

                                    Eif(!dupes[value]){

                                        dupes[value] = "1";

                                        content += value;
                                    }
                                }
                            }
                        }

                        Iif(!content){

                            return;
                        }

                        var bulk = this._bulk;
                        var chunk_index = this._chunk;
                        var fragment = this._fragment[content.length];
                        var index = 0;
                        var marker = 0;

                        // check fragments

                        // TODO: split fragments into reusable parts
                        // if(!fragment && this._fragmented){
                        //
                        //     var fragment_keys = Object.keys(this._fragment);
                        //
                        //     for(var i = 0; i < fragment_keys.length; i++){
                        //
                        //         if(content.length < fragment_keys[i]){
                        //
                        //             fragment = this._fragment[fragment_keys[i]];
                        //             break;
                        //         }
                        //     }
                        // }

                        if(fragment && fragment.length){

                            marker = this._marker[id] = fragment.pop();
                            index = /** @type  {number} */ (marker[1]);

                            var chunk = bulk[marker[0]];

                            bulk[marker[0]] = (

                                chunk.substring(0, index) +
                                content +
                                chunk.substring(marker[2])
                            );

                            this._fragmented -= content.length;
                        }
                        else{

                            index = bulk[chunk_index].length;

                            // check chunk size limit

                            Iif((index + content.length) > chunk_size){

                                if(index < (chunk_size / 2)){

                                    chunk_size *= 2;
                                }

                                this._chunk = ++chunk_index;

                                index = 0;
                                bulk[chunk_index] = "";
                                this._index[chunk_index] = create_typed_array(this.type, chunk_size);
                            }

                            // 32 bytes per marker --> Uint16Array = 8 bytes per marker
                            marker = this._marker[id] = [

                                chunk_index,
                                index,
                                0,
                                0
                            ];

                            bulk[chunk_index] += content + this.separator;
                        }

                        Iif(this._index[chunk_index].constructor === Array){

                            for(var i = 0; i < content.length; i++){

                                this._index[chunk_index][index++] = id;
                            }
                        }
                        else{

                            console.log(this._index[chunk_index].fill);

                            this._index[chunk_index].fill(id, index, index + content.length + 1);

                            index += content.length;
                        }

                        // assign end marker

                        marker[2] = index;

                        // push marker to the register

                        Iif(this.depth){

                            for(var i = this.depth; i > 1; i--){

                                var key = content.substring(0, i);

                                this._register[key] || (this._register[key] = []);
                                this._register[key].push(marker);
                            }
                        }

                        // update status

                        this._status = false;
                    }
                }
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.update = function(id, content){

                if((typeof content === 'string') && (id || (id === 0))){

                    var marker = this._marker[id];

                    Eif(marker){

                        if(content && this.encode){

                            content = this.encode(content);
                        }

                        var min = marker[1];
                        var max = marker[2];
                        var old_length = max - min;
                        var overlap = content.length - old_length;
                        var old_content = content;

                        if(overlap > 0){

                            // clear content (bypass)

                            content = "";
                        }

                        // right-padding invalid index

                        while(content.length < old_length){

                            content = (content + "                                                  ").substring(0, old_length);
                        }

                        var bulk = this._bulk;
                        var chunk_index = marker[0];
                        var chunk = bulk[chunk_index];

                        // submerge updated content to bulk

                        bulk[chunk_index] = chunk.substring(0, min) +
                                            content +
                                            chunk.substring(max);

                        // check if content length has enlarged

                        if(overlap > 0 || !old_content){

                            // get fragments

                            var current_fragment_length = this._fragment[old_length];

                            if(!current_fragment_length){

                                this._fragment[old_length] = [];

                                current_fragment_length = 0;
                            }
                            else{

                                current_fragment_length = current_fragment_length.length;
                            }

                            // add fragment

                            this._fragment[old_length][current_fragment_length] = marker;
                            this._fragmented += old_length;

                            // delete marker

                            this._marker[id] = null;

                            // add overlapping contents to the end

                            if(old_content){

                                this.add(id, old_content);
                            }
                        }

                        // update status

                        this._status = false;
                    }
                }
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.remove = function(id){

                this.update(id, '');

                delete this._marker[id];
            };

            var regex_split = regex("[ -/]");

            /**
             * @param {!string} query
             * @param {number|Function=} limit
             * @param {Function=} callback
             * @returns {Array}
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.search = function(query, limit, callback){

                if((typeof query !== 'string') || !query){

                    return [];
                }
                else if(typeof limit === 'function'){

                    callback = limit;
                    limit = 1000;
                }
                else{

                    limit || (limit = 1000);
                }

                var _query = query;

                // invalidate cache

                if(!this._status){

                    Iif(this.cache){

                        this._last_empty_query = "";
                        this._cache.reset();
                    }

                    this._status = true;
                }

                // validate cache

                else Iif(this.cache){

                    var cache = this._cache.get(query);

                    if(cache){

                        return cache;
                    }
                }

                // validate last input

                else if(this._last_empty_query && (query.indexOf(this._last_empty_query) !== -1)){

                    return [];
                }

                if(callback){

                    /** @type {BulkSearch} */
                    var self = this;

                    queue(function(){

                        callback(self.search(query, limit));
                        self = null;

                    }, 1, 'search-' + this.id);

                    return null;
                }

                // remove trailing spaces

                var spaces = 0;

                while(query[spaces] === " "){

                    spaces++;
                }

                if(spaces){

                    _query = query.substring(spaces);
                }

                if(!_query){

                    return [];
                }

                // convert words into single components

                var words = (

                    this.multi ?

                        _query.split(regex_split)
                    :
                        [_query]
                );

                var length = words.length;

                // sort words by length

                if(length > 1){

                    words.sort(sort_by_length_down);
                }

                var encoded = new Array(length);

                // encode query

                Eif(this.encode){

                    words[0] = this.encode(words[0]);
                }

                // perform search

                var result = [];

                for(var z = 0; z < this._bulk.length; z++){

                    var start = 0;
                    var pos = 0;
                    var bulk = this._bulk[z];

                    while((pos = bulk.indexOf(words[0], start)) !== -1){

                        var current_id = this._index[z][pos];
                        var marker = this._marker[current_id];

                        Eif(marker){

                            var min = marker[1];
                            var max = marker[2];
                            var matched = true;
                            var sub_bulk;

                            // check multiple word components

                            if(length > 1){

                                sub_bulk = bulk.substring(min, max);

                                for(var i = 1; i < length; i++){

                                    // encode query

                                    if(this.encode && !encoded[i]){

                                        words[i] = this.encode(words[i]);
                                        encoded[i] = true;
                                    }

                                    if(words[i]){

                                        Iif(sub_bulk.indexOf(words[i]) === -1){

                                            // boolean and:

                                            if(!this.boolean){

                                                matched = false;
                                                break;
                                            }
                                            else{

                                                matched = false;
                                            }
                                        }
                                        else{

                                            // boolean or:

                                            Iif(this.boolean){

                                                matched = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            Eif(matched){

                                // increase score

                                sub_bulk || (sub_bulk = bulk.substring(min, max));
                                sub_bulk = sub_bulk.substring(0, this.depth);
                                scores[sub_bulk] ? scores[sub_bulk]++ : scores[sub_bulk] = 1;

                                // enrich result
                                // result[result.length] = {
                                //
                                //     'id': current_id,
                                //     'rank': scores[sub_bulk],
                                //     'score': 1.0
                                // };

                                result[result.length] = current_id;

                                // apply limit

                                if(limit && (result.length === limit)){

                                    break;
                                }
                            }

                            pos = max;
                        }

                        start = pos + 1;
                    }

                    if(limit && (result.length === limit)){

                        break;
                    }
                }

                if(result.length){

                    this._last_empty_query = "";
                }
                else{

                    this._last_empty_query || (this._last_empty_query = query);
                }

                // store result to cache

                Iif(this.cache){

                    this._cache.set(query, result);
                }

                return result;
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.optimize = function(){

                var old_bulk = this._bulk;
                var old_marker = this._marker;
                var old_cache = this._cache;
                var old_register = this._register;

                // reset index

                this.reset();

                // get ids

                // var score_keys = Object.keys(/** @type {!Object} */ (scores));
                // var register_keys = Object.keys(old_register);

                // re-add contents

                // for(var i = 0; i < score_keys.length; i++){
                //
                //     var key = score_keys[i];
                //     var score = scores[key];
                //
                //     if(score){
                //
                //         for(var a = 0; a < register_keys.length; a++){
                //
                //             if(score_keys[i] === register_keys[a]){
                //
                //
                //             }
                //         }
                //     }
                // }

                // get ids

                var marker_keys = Object.keys(old_marker);

                // re-add contents

                for(var i = 0; i < marker_keys.length; i++){

                    var key = marker_keys[i];
                    var marker = old_marker[key];

                    if(marker){

                        var bulk = old_bulk[marker[0]];
                        var current_value = bulk.substring(marker[1], marker[2]);

                        this.add(key, current_value);
                    }
                    else{

                        delete old_marker[key];
                    }
                }

                // assign valid cache

                if(old_cache){

                    this._cache = old_cache;
                }
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.info = function(){

                var fragmented = this._fragmented;
                var marker_length = Object.keys(this._marker).length;
                var bytes = 0;
                var length = 0;

                for(var z = 0; z < this._bulk.length; z++){

                    length += this._bulk[z].length;
                }

                if(length){

                    fragmented = ((100 / length * fragmented * 100) | 0) / 100;

                    bytes = (

                        this._index[0].constructor === Array ?

                            32
                        :
                            this._index[0].byteLength
                    );

                    bytes = length * 2 + marker_length * 8 * 3 + this._bulk.length * chunk_size * bytes;
                }

                return {

                    'id': this.id,
                    'length': marker_length,
                    'chunks': this._bulk.length,
                    'register': Object.keys(this._register).length,
                    'depth': this.depth,
                    'size': chunk_size,
                    'bytes': bytes,
                    'fragments': Object.keys(this._fragment).length,
                    'fragmented': fragmented,
                    'status': this._status,
                    'matchers': custom_matchers.length
                };
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.reset = function(){

                // destroy index

                this.destroy();

                // initialize index

                this.init();
            };

            /**
             * @this {BulkSearch}
             * @export
             */

            BulkSearch.prototype.destroy = function(){

                // cleanup cache

                if(this.cache){

                    this._cache.reset();
                }

                // release references

                this._index = null;
                this._marker = null;
                this._fragment = null;
                this._bulk = null;
                this._register = null;
                this._cache = null;
            };

            /**
             * Phonetic Encoders
             * @enum {Function}
             * @private
             * @const
             * @final
             */

            var encoder = {

                // case insensitive search

                'icase': function(value){

                    return value.toLowerCase();
                },

                // simple phonetic normalization (latin)

                'simple': (function(){

                    var regex_strip = regex('[^a-z0-9 ]'),
                        regex_split = regex('[-/]'),
                        regex_a = regex('[àáâãäå]'),
                        regex_e = regex('[èéêë]'),
                        regex_i = regex('[ìíîï]'),
                        regex_o = regex('[òóôõöő]'),
                        regex_u = regex('[ùúûüű]'),
                        regex_y = regex('[ýŷÿ]'),
                        regex_n = regex('ñ'),
                        regex_c = regex('ç'),
                        regex_s = regex('ß');

                    var regex_pairs = [

                        regex_a, 'a',
                        regex_e, 'e',
                        regex_i, 'i',
                        regex_o, 'o',
                        regex_u, 'u',
                        regex_y, 'y',
                        regex_n, 'n',
                        regex_c, 'c',
                        regex_s, 's',
                        regex_split, ' ',
                        regex_strip, ''
                    ];

                    return function(str){

                        return (

                            replace(str.toLowerCase(), regex_pairs)
                        );
                    };
                }()),

                // advanced phonetic transformation (latin)

                'advanced': (function(){

                    var regex_space = regex(' '),
                        regex_ae = regex('ae'),
                        regex_ai = regex('ai'),
                        regex_ay = regex('ay'),
                        regex_ey = regex('ey'),
                        regex_oe = regex('oe'),
                        regex_ue = regex('ue'),
                        regex_ie = regex('ie'),
                        regex_sz = regex('sz'),
                        regex_zs = regex('zs'),
                        regex_ck = regex('ck'),
                        regex_cc = regex('cc'),
                        regex_sh = regex('sh'),
                        //regex_th = regex('th'),
                        regex_dt = regex('dt'),
                        regex_ph = regex('ph'),
                        regex_ou = regex('ou'),
                        regex_uo = regex('uo');

                    var regex_pairs = [

                        regex_ae, 'a',
                        regex_ai, 'ei',
                        regex_ay, 'ei',
                        regex_ey, 'ei',
                        regex_oe, 'o',
                        regex_ue, 'u',
                        regex_ie, 'i',
                        regex_sz, 's',
                        regex_zs, 's',
                        regex_sh, 's',
                        regex_ck, 'k',
                        regex_cc, 'k',
                        //regex_th, 't',
                        regex_dt, 't',
                        regex_ph, 'f',
                        regex_ou, 'o',
                        regex_uo, 'u'
                    ];

                    return function(string, _skip_post_processing){

                        Iif(!string){

                            return string;
                        }

                        // perform simple encoding
                        string = encoder['simple'](string);

                        // normalize special pairs
                        Eif(string.length > 2){

                            string = replace(string, regex_pairs)
                        }

                        if(!_skip_post_processing){

                            // remove white spaces
                            string = string.replace(regex_space, '');

                            // delete all repeating chars
                            Eif(string.length > 1){

                                string = collapseRepeatingChars(string);
                            }
                        }

                        return string;
                    };

                })(),

                'extra': (function(){

                    var soundex_b = regex('p'),
                        soundex_c = regex('[sz]'),
                        soundex_k = regex('[gq]'),
                        soundex_i = regex('[jy]'),
                        soundex_m = regex('n'),
                        soundex_t = regex('d'),
                        soundex_f = regex('[vw]');

                    var regex_vowel = regex('[aeiouy]');

                    var regex_pairs = [

                        soundex_b, 'b',
                        soundex_c, 'c',
                        soundex_k, 'k',
                        soundex_i, 'i',
                        soundex_m, 'm',
                        soundex_t, 't',
                        soundex_f, 'f',
                        regex_vowel, ''
                    ];

                    return function(str){

                        Iif(!str){

                            return str;
                        }

                        // perform advanced encoding
                        str = encoder['advanced'](str, /* skip post processing? */ true);

                        Eif(str.length > 1){

                            str = str.split(" ");

                            for(var i = 0; i < str.length; i++){

                                var current = str[i];

                                Eif(current.length > 1){

                                    // remove all vowels after 2nd char
                                    str[i] = current[0] + replace(current.substring(1), regex_pairs)
                                }
                            }

                            str = str.join("");
                            str = collapseRepeatingChars(str);
                        }

                        return str;
                    };
                })()

                // TODO: provide some common encoder plugins
                // soundex
                // cologne
                // metaphone
                // caverphone
                // levinshtein
                // hamming
                // matchrating
                // ngram
            };

            return BulkSearch;

            // ---------------------------------------------------------
            // Helpers

            function create_typed_array(type, size){

                /** @type {Function} */

                var array_type = (

                    (type === 'int') || (type === 'integer') ?

                        Uint32Array
                    :(
                        type === 'short' ?

                            Uint16Array
                        :(
                            (type === 'float') || (type === 'double') ?

                                Float64Array
                            :(
                                type === 'byte' ?

                                    Uint8Array
                                :
                                    Array
                            )
                        )
                    )

                ) || Array;

                return new array_type(size);
            }

            /**
             * @param {!string} str
             * @returns {RegExp}
             */

            function regex(str){

                return new RegExp(str, 'g');
            }

            /**
             * @param {!string} str
             * @param {RegExp|Array} regex
             * @param {string=} replacement
             * @returns {string}
             */

            function replace(str, regex, replacement){

                Eif(typeof replacement === 'undefined'){

                    for(var i = 0; i < /** @type {Array} */ (regex).length; i += 2){

                        str = str.replace(regex[i], regex[i + 1]);
                    }

                    return str;
                }
                else{

                    return str.replace(/** @type {!RegExp} */ (regex), replacement);
                }
            }

            /**
             * @param {!Array|string} a
             * @param {!Array|string} b
             * @returns {number}
             */

            function sort_by_length_down(a, b){

                var diff = a.length - b.length;

                return (

                    diff < 0 ?

                        1
                    :(
                        diff > 0 ?

                            -1
                        :
                            0
                    )
                );
            }

            /**
             * @param {!string} string
             * @returns {string}
             */

            function collapseRepeatingChars(string){

                var collapsed_string = '',
                    char_prev = '',
                    char_next = '';

                for(var i = 0; i < string.length; i++){

                    var char = string[i];

                    if(char !== char_prev){

                        Iif(i > 0 && char === 'h'){

                            var char_prev_is_vowel = (

                                char_prev === 'a' ||
                                char_prev === 'e' ||
                                char_prev === 'i' ||
                                char_prev === 'o' ||
                                char_prev === 'u' ||
                                char_prev === 'y'
                            );

                            var char_next_is_vowel = (

                                char_next === 'a' ||
                                char_next === 'e' ||
                                char_next === 'i' ||
                                char_next === 'o' ||
                                char_next === 'u' ||
                                char_next === 'y'
                            );

                            if(char_prev_is_vowel && char_next_is_vowel){

                                collapsed_string += char;
                            }
                        }
                        else{

                            collapsed_string += char;
                        }
                    }

                    char_next = (

                        (i === string.length - 1) ?

                            ''
                        :
                            string[i + 1]
                    );

                    char_prev = char;
                }

                return collapsed_string;
            }
        })(
            // Xone Async Handler Fallback

            (function(){

                var stack = {};

                return function(fn, delay, id){

                    var timer = stack[id];

                    if(timer){

                        clearTimeout(timer);
                    }

                    return (

                        stack[id] = setTimeout(fn, delay)
                    );
                };
            })(),

            // Xone Flexi-Cache Handler Fallback

            (function(){

                /** @this {Cache} */
                function Cache(){

                    this.cache = {};
                }

                /** @this {Cache} */
                Cache.prototype.reset = function(){

                    this.cache = {};
                };

                /** @this {Cache} */
                Cache.prototype.set = function(id, value){

                    this.cache[id] = value;
                };

                /** @this {Cache} */
                Cache.prototype.get = function(id){

                    return this.cache[id];
                };

                return Cache;
            })()

        ), this);

        /** --------------------------------------------------------------------------------------
         * UMD Wrapper for Browser and Node.js
         * @param {!string} name
         * @param {!Function|Object} factory
         * @param {!Function|Object=} root
         * @suppress {checkVars}
         * @const
         */

        function provide(name, factory, root){

            root || (root = this);

            var prop;

            // AMD (RequireJS)
            Iif((prop = root['define']) && prop['amd']){

                prop([], function(){

                    return factory;
                });
            }
            // Closure (Xone)
            else Iif((prop = root['modules'])){

                prop[name.toLowerCase()] = factory;
            }
            // CommonJS (Node.js)
            else Eif(typeof module !== 'undefined'){

                /** @export */
                module.exports = factory;
            }
            // Global (window)
            else{

                root[name] = factory;
            }
        }

    }).call(this);