-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.cpp
8340 lines (8113 loc) · 297 KB
/
index.cpp
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
// $Id$
// Author: John Wu <John.Wu at ACM.org>
// Copyright 2000-2014 the Regents of the University of California
//
// This file contains the implementation of the classes defined in index.h
// The primary function from the database point of view is a function
// called estimate. It evaluates a given range condition and produces
// two bit vectors representing the range where the actual solution lies.
// The bulk of the code is devoted to maintain and update the indexes.
//
#if defined(_WIN32) && defined(_MSC_VER)
#pragma warning(disable:4786) // some identifier longer than 256 characters
#endif
#include "index.h"
#include "ibin.h"
#include "irelic.h"
#include "ikeywords.h"
#include "part.h"
#include "category.h"
#include "resource.h"
#include "bitvector64.h"
#include <memory> // std::unique_ptr
#include <queue> // priority queue
#include <algorithm> // std::sort
#include <sstream> // std::ostringstream
#include <typeinfo> // typeid
namespace ibis {
#if defined(TEST_SUMBINS_OPTIONS)
//a temporary variable for testing the various options in sumBits
extern int _sumBits_option;
#endif
}
namespace std { // specialize the std::less struct
template <> struct less<std::pair<ibis::bitvector*, bool> > {
bool operator()
(const std::pair<ibis::bitvector*, bool> &x,
const std::pair<ibis::bitvector*, bool> &y) const {
return (x.first->bytes() > y.first->bytes());
}
};
template <> struct less<ibis::bitvector* > {
bool operator()(const ibis::bitvector *x,
const ibis::bitvector *y) const {
return (x->bytes() < y->bytes());
}
};
}
////////////////////////////////////////////////////////////////////////
// functions from ibis::index
/// Index factory. It creates a specific concrete index object. It
/// attempts to read the existing index if a location is specified. If it
/// fails to read an index or no expilicit location is given, it attempts
/// to create a new index based on the current data file and index
/// specification. Any newly created index will be written to a file.
///
/// @param c a pointer to a ibis::column object. This argument must be
/// present.
///
/// @param dfname data file name, may also be the name of the index file,
/// or the directory containing the data file. If the name ends with
/// '.idx' is treated as an index file, and the content of the file is
/// read. If the name does not end with '.idx', it is assumed to be the
/// data file name, unless it is determined to be a directory name. If it
/// is a directory name, the data file is assumed to be in the specified
/// directory with the same name as the column. Once a data file is found,
/// the content of the data file is read to construct a new index according
/// to the return value of function indexSpec. The argument dfname can be
/// nil, in which case, the data file name is constructed by concatenate
/// the return from partition()->currentDataDir() and the column name.
///
/// @param spec the index specification. This string contains the
/// parameters for how to create an index. The most general form is
///\verbatim
/// <binning .../> <encoding .../> <compression .../>.
///\endverbatim
/// Here is one example (it is the default for some integer columns)
///\verbatim
/// <binning none /> <encoding equality />
///\endverbatim
/// FastBit always compresses every bitmap it ever generates. The
/// compression option is to instruct it to uncompress some bitmaps or
/// not compress indexes at all. The compress option is usually not
/// used.
///
/// If the argument @c spec is not specified, this function checks the
/// specification in the following order.
///
/// -- use the index specification for the column being indexed;
/// -- use the index specification for the table containing the
/// column being indexed;
/// -- use the most specific index specification relates to the
/// column be indexed in the global resources (gParameters).
///
/// It stops looking as soon as it finds the first non-empty string, which
/// follows the principle of a more specific index specification override a
/// general specification.
///
/// @param readopt Depending on whether this value is positive, zero or
/// negative, the index is read from the index file in three different
/// ways.
///
/// (1) If this value is positive, the content of the index file is read
/// into memory and there is no need for further I/O operation to use the
/// index.
///
/// (2) If this value is zero, the content of a large index file is loaded
/// into memory through memory map and the content of a small index file
/// will be read into memory. This is the default option.
///
/// (3) If this value is negative, then only the metadata is read into
/// memory. This option requires the least amount of memory, but requires
/// more I/O operations later when bitmaps are needed to answer queries.
/// To use option (1) and (2), there must be enough memory to hold the
/// index file in memory. Furthermore, to use the memory map option,
/// FastBit must be able to hold the index file open indefinitely and the
/// operating system must support memory map function mmap.
///
/// These three options have different start up cost and different query
/// processing cost. Typically, reading the whole file in this function
/// will take the longest time, but since it requires no further I/O
/// operations, its query processing cost is likely the lowest. The memory
/// map option only need to load the page table into memory and read part
/// of the metadata, it is likely to be relatively inexpensive to
/// reconstruct the index object this way. Since the memory map option can
/// read the necessary portion of the index file into memory pretty
/// efficiently, the query processing cost should have reasonable
/// performance. The third of reading metadata only in this function
/// requires the least amount of memory, but it might actually read more
/// bytes in this function than the memory map option because this option
/// actually needs to read all the bytes representing the metadata while
/// the memory map option only need to create a memory map for the index
/// file. Later when the index is used, the needed bitmaps are read into
/// memory, which is likely take more time than accessing the memory mapped
/// bitmaps. Additionally, the third option also causes the bitmaps to be
/// placed in unpredictable memory locations, while the first two options
/// place all bitmaps of an index consecutively in memory. This difference
/// in memory layout could cause the memory accesses to take different
/// amounts of time; typically, accessing consecutive memory locations is
/// more efficient.
///
/// The default value of @c readopt is 0, which prefers the memory map
/// option.
///
/// @return This function returns a pointer to the index created. The
/// caller is responsible for freeing the pointer. In case of error, it
/// returns a nil pointer. It captures and absorbs exceptions in most
/// cases.
///
/// @note An index can NOT be built correctly if it does not fit in memory!
/// This is the most likely reason for the function to fail. If this does
/// happen, try to build indexes one at a time, use a machine with more
/// memory, or break up a large partition into a number of smaller ones.
/// Normally, we recommand one to not put more than 100 million rows in a
/// data partition.
///
/// @note Set @c dfname to null to build a brand new index and discard
/// the existing index.
///
/// @note The index specification passed to this function will be attached
/// to the column object if a new index is to be built. This is the only
/// possible change to the column object.
ibis::index* ibis::index::create(const ibis::column* c, const char* dfname,
const char* spec, int readopt) {
ibis::index* ind = 0;
if (c == 0) // can not procede
return ind;
if (c->partition() == 0)
return ind;
if (c->partition()->nRows() == 0)
return ind;
if (spec == 0 || *spec == static_cast<char>(0))
spec = c->indexSpec(); // index spec of the column
if (spec == 0 || *spec == static_cast<char>(0))
spec = c->partition()->indexSpec(); // index spec of the table
if (spec == 0 || *spec == static_cast<char>(0)) {
// attempt to retrieve the value of tableName.columnName.index for
// the index specification in the global resource
std::string idxnm(c->partition()->name());
idxnm += '.';
idxnm += c->name();
idxnm += ".index";
spec = ibis::gParameters()[idxnm.c_str()];
}
if (spec) {
// skip leading spaces
while (spec && isspace(*spec)) ++ spec;
// no index is to be used if the index specification start
// with "noindex", "null" or "none".
if (strncmp(spec, "noindex", 7) == 0 ||
strncmp(spec, "null", 4) == 0 ||
strncmp(spec, "none", 4) == 0) {
return ind;
}
}
int ierr;
bool isRead = false;
ibis::horometer timer;
if (ibis::gVerbose > 1)
timer.start();
std::string evt = "index::create";
if (ibis::gVerbose > 0) {
evt += '(';
evt += c->partition()->name();
evt += '.';
evt += c->name();
evt += ')';
}
try {
if (dfname != 0 && *dfname != 0) { // first attempt to read the index
ibis::fileManager::storage* st=0;
std::string file;
const char* header = 0;
char buf[12];
const size_t dfnlen = std::strlen(dfname);
if (dfnlen > 4 && dfname[dfnlen-4] == '.' &&
dfname[dfnlen-3] == 'i' && dfname[dfnlen-2] == 'd' &&
dfname[dfnlen-1] == 'x') {
file = dfname;
}
else {
c->dataFileName(file, dfname);
if (! file.empty())
file += ".idx";
}
if (! file.empty()) {
bool useGetFile = (readopt >= 0);
ibis::fileManager::ACCESS_PREFERENCE prf =
(readopt > 0 ? ibis::fileManager::PREFER_READ :
ibis::fileManager::MMAP_LARGE_FILES);
if (readopt == 0) { // default option, check parameters
std::string key(c->partition()->name());
key += ".";
key += c->name();
key += ".preferMMapIndex";
if (ibis::gParameters().isTrue(key.c_str())) {
useGetFile = true;
prf = ibis::fileManager::PREFER_MMAP;
}
else {
key = c->partition()->name();
key += ".";
key += c->name();
key += ".preferReadIndex";
if (ibis::gParameters().isTrue(key.c_str())) {
useGetFile = true;
prf = ibis::fileManager::PREFER_READ;
}
}
}
if (useGetFile) {
// manage the index file as a whole
ierr = ibis::fileManager::instance().tryGetFile
(file.c_str(), &st, prf);
if (ierr != 0) {
LOGGER(ibis::gVerbose > 7)
<< evt << " tryGetFile(" << file
<< ") failed with return code " << ierr;
st = 0;
}
if (st)
header = st->begin();
}
if (header == 0) {
// attempt to read the file using read(2)
int fdes = UnixOpen(file.c_str(), OPEN_READONLY);
if (fdes >= 0) {
#if defined(_WIN32) && defined(_MSC_VER)
(void)_setmode(fdes, _O_BINARY);
#endif
if (8 == UnixRead(fdes, static_cast<void*>(buf), 8)) {
header = buf;
}
UnixClose(fdes);
}
}
if (header) { // verify header
const bool check = (header[0] == '#' && header[1] == 'I' &&
header[2] == 'B' && header[3] == 'I' &&
header[4] == 'S' &&
(header[6] == 8 || header[6] == 4) &&
header[7] == static_cast<char>(0));
if (!check) {
c->logWarning("readIndex", "index file \"%s\" "
"contains an incorrect header "
"(%c%c%c%c%c:%i.%i.%i)",
file.c_str(),
header[0], header[1], header[2],
header[3], header[4],
(int)header[5], (int)header[6],
(int)header[7]);
header = 0;
}
}
if (header) { // reconstruct index from st
isRead = true;
ibis::horometer tm4;
if (ibis::gVerbose > 2)
tm4.start();
ind = readOld(c, file.c_str(), st,
static_cast<INDEX_TYPE>(header[5]));
if (ind == 0) {
ibis::fileManager::instance().flushFile(file.c_str());
(void) remove(file.c_str());
}
else if (ibis::gVerbose > 2) {
tm4.stop();
LOGGER(1) << evt << " reading the existing index took "
<< tm4.realTime() << " sec";
}
}
}
} // if (dfname != 0 && *dfname != 0)
if (dfname == 0) {
// user has passed in an explicit nil pointer, purge index files
c->purgeIndexFile();
}
if (ind == 0) {
isRead = false;
ibis::horometer tm3;
if (ibis::gVerbose > 2)
tm3.start();
ind = buildNew(c, dfname, spec);
if (ind != 0 && ibis::gVerbose > 2) {
tm3.stop();
LOGGER(1) << evt << " building a new index took "
<< tm3.realTime() << " sec";
}
}
if (ind == 0) {
LOGGER(ibis::gVerbose > 0)
<< evt << " failed to create an index for " << c->name();
}
else if (ind->getNRows() == 0) {
delete ind;
ind = 0;
LOGGER(ibis::gVerbose > 0)
<< evt << " create an empty index for " << c->name();
}
else if (ind->getNRows() == c->partition()->nRows()) {
// having built a valid index, write out its content
try {
if (! isRead) {
ibis::horometer tm2;
if (ibis::gVerbose > 2)
tm2.start();
ierr = ind->write(dfname);
if (ierr >= 0 && ibis::gVerbose > 2) {
tm2.stop();
LOGGER(1) << evt << " writing the index took "
<< tm2.realTime() << " sec";
}
}
else {
ierr = 0;
}
if (ierr < 0) {
std::string idxname;
ind->indexFileName(idxname, dfname);
remove(idxname.c_str());
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt
<< " failed to write the index ("
<< ind->name() << ") to " << idxname << ", ierr = "
<< ierr;
}
}
catch (...) {
std::string idxname;
ind->indexFileName(idxname, dfname);
remove(idxname.c_str());
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt
<< " failed to write the index (" << ind->name()
<< ") to " << idxname << ", received an exception";
}
}
else {
LOGGER(ibis::gVerbose > 0)
<< evt << " created an index with "
<< ind->getNRows() << " row"
<< (ind->getNRows() > 1 ? "s" : "")
<< ", but the data partition has "
<< c->partition()->nRows() << " row"
<< (c->partition()->nRows() > 1 ? "s" : "");
}
}
catch (const char* s) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt << " received a string exception -- " << s;
delete ind;
ind = 0;
}
catch (const ibis::bad_alloc& e) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt << " failed to allocate memory -- "
<< e.what();
delete ind;
ind = 0;
}
catch (const std::exception& e) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt << " received an std::exception -- "
<< e.what();
delete ind;
ind = 0;
}
catch (...) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- " << evt << " received a unexpected exception";
delete ind;
ind = 0;
}
if (ind == 0) {
LOGGER(ibis::gVerbose >= 0)
<< "Warning -- " << evt << " failed to create an index of type "
<< (spec != 0 && *spec != 0 ? spec : "default");
}
else if (ibis::gVerbose > 1) {
timer.stop();
ibis::util::logger lg;
lg() << evt << " -- the " << ind->name() << " index for column "
<< c->partition()->name() << '.' << c->name();
if (isRead) {
lg() << " was read from " << dfname;
}
else if (c->partition()->currentDataDir()) {
lg() << " was created from data in "
<< c->partition()->currentDataDir();
}
else {
lg() << " was created from in-memory data";
}
lg() << " in " << timer.CPUTime() <<" sec(CPU), "<< timer.realTime()
<< " sec(elapsed)";
if (ibis::gVerbose > 3) {
lg() << "\n";
ind->print(lg());
}
}
return ind;
} // ibis::index::create
/// Read an index of the specified type from the incoming data file. The
/// index type t has been determined by the caller. Furthermore, the
/// caller might have read the index file into storage object st.
ibis::index* ibis::index::readOld(const ibis::column* c,
const char* f,
ibis::fileManager::storage* st,
ibis::index::INDEX_TYPE t) {
LOGGER(ibis::gVerbose > 3)
<< "index::create -- attempt to read index type #"
<< (int)t << " from "
<< (f ? f : c->partition()->currentDataDir())
<< " for column " << c->partition()->name() << '.' << c->name();
ibis::index *ind = 0;
switch (t) {
case ibis::index::BINNING: // ibis::bin
if (st) {
ind = new ibis::bin(c, st);
}
else {
ind = new ibis::bin(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::RANGE: // new range index
if (st) {
ind = new ibis::range(c, st);
}
else {
ind = new ibis::range(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::AMBIT: // multilevel range index
if (st) {
ind = new ibis::ambit(c, st);
}
else {
ind = new ibis::ambit(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::PALE: // two-level bin/range index
if (st) {
ind = new ibis::pale(c, st);
}
else {
ind = new ibis::pale(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::PACK: // two-level range/bin index
if (st) {
ind = new ibis::pack(c, st);
}
else {
ind = new ibis::pack(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::ZONE: // two-level bin/bin index
if (st) {
ind = new ibis::zone(c, st);
}
else {
ind = new ibis::zone(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::MESA: // one-level (interval) encoded index
if (st) {
ind = new ibis::mesa(c, st);
}
else {
ind = new ibis::mesa(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::RELIC: // the basic bitmap index
if (st) {
ind = new ibis::relic(c, st);
}
else {
ind = new ibis::relic(c, f);
}
break;
case ibis::index::SKIVE: // binary encoded index
if (st) {
ind = new ibis::skive(c, st);
}
else {
ind = new ibis::skive(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::SLICE: // bit-slice
if (st) {
ind = new ibis::slice(c, st);
}
else {
ind = new ibis::slice(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::FADE: // multicomponent range-encoded
if (st) {
ind = new ibis::fade(c, st);
}
else {
ind = new ibis::fade(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::SAPID: // multicomponent equality-encoded
if (st) {
ind = new ibis::sapid(c, st);
}
else {
ind = new ibis::sapid(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::SBIAD: // multicomponent interval-encoded
if (st) {
ind = new ibis::sbiad(c, st);
}
else {
ind = new ibis::sbiad(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::EGALE:
// multicomponent equality code on bins
if (st) {
ind = new ibis::egale(c, st);
}
else {
ind = new ibis::egale(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::MOINS:
// multicomponent equality code on bins
if (st) {
ind = new ibis::moins(c, st);
}
else {
ind = new ibis::moins(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::ENTRE:
// multicomponent equality code on bins
if (st) {
ind = new ibis::entre(c, st);
}
else {
ind = new ibis::entre(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::BAK:
// equality code on reduced precision values
if (st) {
ind = new ibis::bak(c, st);
}
else {
ind = new ibis::bak(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::BAK2:
// equality code on reduced precision values, split bins
if (st) {
ind = new ibis::bak2(c, st);
}
else {
ind = new ibis::bak2(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::KEYWORDS:
// boolean term-document matrix
if (st) {
ind = new ibis::keywords(c, st);
}
else {
ind = new ibis::keywords(c, f);
}
break;
case ibis::index::DIREKTE:
if (st) {
ind = new ibis::direkte(c, st);
}
else {
ind = new ibis::direkte(0);
ind->col = c;
int ierr = ind->read(f);
if (ierr < 0) {
delete ind;
ind = 0;
}
}
break;
case ibis::index::BYLT:
if (st) {
ind = new ibis::bylt(c, st);
}
else {
ind = new ibis::bylt(c, f);
}
break;
case ibis::index::ZONA:
if (st) {
ind = new ibis::zona(c, st);
}
else {
ind = new ibis::zona(c, f);
}
break;
case ibis::index::FUZZ:
if (st) {
ind = new ibis::fuzz(c, st);
}
else {
ind = new ibis::fuzz(c, f);
}
break;
case ibis::index::FUGE:
if (st) {
ind = new ibis::fuge(c, st);
}
else {
ind = new ibis::fuge(c, f);
}
break;
default:
LOGGER(ibis::gVerbose > 1)
<< "Warning -- index::create can not process index type "
<< (int)t << " from " << f;
}
return ind;
} // ibis::index::readOld
/// Build a new index from attribute values.
ibis::index* ibis::index::buildNew
(const ibis::column *c, const char* dfname, const char* spec) {
if (c->type() == ibis::CATEGORY) {
// special handling
return reinterpret_cast<const ibis::category*>(c)->
fillIndex(dfname);
//return ind;
}
else if (c->type() == ibis::TEXT) {
if (spec != 0 && *spec != 0)
const_cast<column*>(c)->indexSpec(spec);
return new ibis::keywords(c, dfname);
}
if (spec == 0 || *spec == 0) {
switch (c->type()) {
case ibis::USHORT:
case ibis::SHORT:
case ibis::UBYTE:
case ibis::BYTE:
case ibis::UINT:
case ibis::INT:
case ibis::OID:
case ibis::ULONG:
case ibis::LONG:
case ibis::FLOAT:
case ibis::DOUBLE: {
spec = "default";
break;}
case ibis::CATEGORY: {
spec = "direkte";
break;}
case ibis::TEXT: {
spec = "keywords delimiters=','";
break;}
default: {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- index::create can not work with column type "
<< ibis::TYPESTRING[(int)c->type()];
return 0;}
}
}
else if (c->indexSpec() == 0 || *(c->indexSpec()) == 0 ||
std::strcmp(c->indexSpec(), spec) != 0) {
const_cast<column*>(c)->indexSpec(spec);
}
LOGGER(ibis::gVerbose > 3)
<< "index::create -- attempt to build a new index with spec `"
<< spec << "' on data from directory "
<< (dfname ? dfname : (c->partition()->currentDataDir() ?
c->partition()->currentDataDir() : "?"))
<< " for column " << c->partition()->name() << '.' << c->name();
ibis::index *ind = 0;
bool usebin = (strstr(spec, "bin") != 0 && strstr(spec, "none") == 0);
if (usebin) {
unsigned nb = ibis::bin::parseNbins(*c);
const unsigned nr = c->partition()->nRows();
if (nb >= nr) {
usebin = false;
}
else if (nb >= (nr >> 1) && c->isInteger()) {
usebin = false;
}
}
uint32_t ncomp = 0;
const char* ptr = strstr(spec, "ncomp=");
if (ptr != 0) {
ptr += 6;
while (isspace(*ptr)) { // skip till the first digit
++ ptr;
}
if (*ptr) {
if (isdigit(*ptr)) { // string --> number
ncomp = strtol(ptr, 0, 0);
if (ncomp == 0) {
LOGGER(errno == EINVAL && ibis::gVerbose > 0)
<< "Warning -- index::create failed to extract "
"the number of components from " << ptr
<< ", use the default value 2";
ncomp = 2; // default to 2
}
}
else {
ncomp = 1;
}
}
}
bool dflt = false;
if (c->type() == ibis::CATEGORY) {
dflt = true;
}
else if (spec == 0) {
dflt = true;
}
else if (*spec == 0) {
dflt = true;
}
else {
while (*spec != 0 && isspace(*spec)) ++ spec;
if (strstr(spec, "automatic") != 0 ||
strstr(spec, "default") != 0) {
dflt = true;
}
else {
dflt = (*spec == 0);
}
}
if (dflt) {
switch (c->type()) {
case ibis::ULONG:
case ibis::LONG:
case ibis::UINT:
case ibis::INT: {
double amin = c->lowerBound();
double amax = c->upperBound();
if (!(amin <= amax) && amin >= 0) {
const_cast<ibis::column*>(c)->computeMinMax();
amin = c->lowerBound();
amax = c->upperBound();
}
if (amax - amin < 1e3 ||
amax - amin < c->partition()->nRows()*0.1) {
if (amin >= 0.0 && amin <= ceil(amax*0.01))
ind = new ibis::direkte(c, dfname);
else if (amax >= amin+1e2)
ind = new ibis::fuzz(c, dfname);
else
ind = new ibis::relic(c, dfname);
}
else {
ind = new ibis::bin(c, dfname);
}
break;}
case ibis::FLOAT:
case ibis::DOUBLE: {
ind = new ibis::bin(c, dfname);
break;}
case ibis::USHORT:
case ibis::SHORT:
case ibis::UBYTE:
case ibis::BYTE: {
ind = new ibis::relic(c, dfname);
break;}
case ibis::CATEGORY: {
ind = reinterpret_cast<const ibis::category*>(c)->
fillIndex(dfname);
break;}
case ibis::TEXT: {
ind = new ibis::keywords(c, dfname);
break;}
default: {
c->logWarning("createIndex", "not able to "
"generate for this column type");
break;}
}
}
else if (ncomp > 1 || strstr(spec, "mcbin") != 0 ||
strstr(spec, "multicomponent") != 0) {
INDEX_TYPE t = SAPID; // default to equality encoding
if (strstr(spec, "equal")) {
t = SAPID;
}
else if (strstr(spec, "range")) {
t = FADE;
}
else if (strstr(spec, "interval")) {
t = SBIAD;
}
switch (t) {
default:
case SBIAD:
if (usebin)
ind = new ibis::entre(c, dfname, ncomp);
else
ind = new ibis::sbiad(c, dfname, ncomp);
break;
case SAPID:
if (usebin)
ind = new ibis::egale(c, dfname, ncomp);
else
ind = new ibis::sapid(c, dfname, ncomp);
break;
case FADE:
if (usebin)
ind = new ibis::moins(c, dfname, ncomp);
else
ind = new ibis::fade(c, dfname, ncomp);
break;
}
}
else if (!usebin) { // <binning none> is specified explicitly
INDEX_TYPE t = RELIC;
const char* str = strstr(spec, "<encoding ");
double lo = c->lowerBound(), hi = c->upperBound();
if (str) {
str += 10; // skip "<encoding "
if (strstr(str, "range/equality") ||
strstr(str, "range-equality")) {
if (c->lowerBound() < c->upperBound()) {
t = BYLT;
}
else {
bool asc;
c->computeMinMax(dfname, lo, hi, asc);
if (lo < hi)
t = BYLT;
}
}
else if (strstr(str, "equality/equality") ||
strstr(str, "equality-equality")) {
t = ZONA;
}
else if (strstr(str, "interval/equality") ||
strstr(str, "interval-equality")) {
t = FUZZ;