-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtestIndexStat.cpp
More file actions
2380 lines (2180 loc) · 55.6 KB
/
Copy pathtestIndexStat.cpp
File metadata and controls
2380 lines (2180 loc) · 55.6 KB
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
/*
Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ndb_global.h>
#include <ndb_opts.h>
#include <NdbApi.hpp>
#include <NdbIndexStat.hpp>
#include <NdbTest.hpp>
#include <ndb_version.h>
#include <NDBT_Stats.hpp>
#include <math.h>
#undef min
#undef max
#define min(a, b) ((a) <= (b) ? (a) : (b))
#define max(a, b) ((a) >= (b) ? (a) : (b))
inline NdbOut&
NdbOut::operator<<(double x)
{
char buf[100];
sprintf(buf, "%.2f", x);
*this << buf;
return *this;
}
struct Opts {
int loglevel;
uint seed;
uint attrs;
uint loops;
uint rows;
uint ops;
uint nullkeys;
uint rpk;
uint rpkvar;
uint scanpct;
uint eqscans;
my_bool keeptable;
my_bool abort;
const char* dump;
Opts() :
loglevel(0),
seed(0),
attrs(3),
loops(1),
rows(10000),
ops(100),
nullkeys(10),
rpk(10),
rpkvar(10),
scanpct(10),
eqscans(30),
keeptable(false),
abort(false),
dump(0)
{}
};
static Opts g_opts;
static uint g_loop = 0;
static const char* g_tabname = "ts1";
static const char* g_indname = "ts1x1";
static const uint g_numattrs = 3;
static const uint g_charlen = 10;
static const char* g_csname = "latin1_swedish_ci";
static CHARSET_INFO* g_cs;
// keys nullability
static const bool g_b_nullable = true;
static const bool g_c_nullable = true;
static const bool g_d_nullable = true;
// value limits
struct Lim {
bool all_nullable;
uint b_min;
uint b_max;
const char* c_char;
uint d_min;
uint d_max;
};
static Lim g_lim_val;
static Lim g_lim_bnd;
static Ndb_cluster_connection* g_ncc = 0;
static Ndb* g_ndb = 0;
static Ndb* g_ndb_sys = 0;
static NdbDictionary::Dictionary* g_dic = 0;
static const NdbDictionary::Table* g_tab = 0;
static const NdbDictionary::Index* g_ind = 0;
static const NdbRecord* g_tab_rec = 0;
static const NdbRecord* g_ind_rec = 0;
struct my_record
{
Uint8 m_null_bm;
Uint8 fill[3];
Uint32 m_a;
Uint32 m_b;
char m_c[1+g_charlen];
Uint16 m_d;
};
static const Uint32 g_ndbrec_a_offset=offsetof(my_record, m_a);
static const Uint32 g_ndbrec_b_offset=offsetof(my_record, m_b);
static const Uint32 g_ndbrec_b_nb_offset=1;
static const Uint32 g_ndbrec_c_offset=offsetof(my_record, m_c);
static const Uint32 g_ndbrec_c_nb_offset=2;
static const Uint32 g_ndbrec_d_offset=offsetof(my_record, m_d);
static const Uint32 g_ndbrec_d_nb_offset=3;
static const Uint32 g_ndbrecord_bytes=sizeof(my_record);
static NdbTransaction* g_con = 0;
static NdbOperation* g_op = 0;
static NdbScanOperation* g_scan_op = 0;
static NdbIndexScanOperation* g_rangescan_op = 0;
static NdbIndexStat* g_is = 0;
static bool g_has_created_stat_tables = false;
static bool g_has_created_stat_events = false;
static uint
urandom()
{
uint r = (uint)random();
return r;
}
static uint
urandom(uint m)
{
if (m == 0)
return 0;
uint r = urandom();
r = r % m;
return r;
}
static int& g_loglevel = g_opts.loglevel; // default log level
#define chkdb(x) \
do { if (likely(x)) break; ndbout << "line " << __LINE__ << " FAIL " << #x << endl; errdb(); if (g_opts.abort) abort(); return -1; } while (0)
#define chker(x) \
do { if (likely(x)) break; ndbout << "line " << __LINE__ << " FAIL " << #x << endl; ndbout << "errno: " << errno; if (g_opts.abort) abort(); return -1; } while (0)
#define chkrc(x) \
do { if (likely(x)) break; ndbout << "line " << __LINE__ << " FAIL " << #x << endl; if (g_opts.abort) abort(); return -1; } while (0)
#define llx(n, x) \
do { if (likely(g_loglevel < n)) break; ndbout << x << endl; } while (0)
#define ll0(x) llx(0, x)
#define ll1(x) llx(1, x)
#define ll2(x) llx(2, x)
#define ll3(x) llx(3, x)
static void
errdb()
{
uint any = 0;
if (g_ncc != 0) {
NdbError e;
e.code = g_ncc->get_latest_error();
e.message = g_ncc->get_latest_error_msg();
if (e.code != 0)
ll0(++any << " ncc: error" << e);
}
if (g_ndb != 0) {
const NdbError& e = g_ndb->getNdbError();
if (e.code != 0)
ll0(++any << " ndb: error " << e);
}
if (g_dic != 0) {
const NdbError& e = g_dic->getNdbError();
if (e.code != 0)
ll0(++any << " dic: error " << e);
}
if (g_con != 0) {
const NdbError& e = g_con->getNdbError();
if (e.code != 0)
ll0(++any << " con: error " << e);
}
if (g_op != 0) {
const NdbError& e = g_op->getNdbError();
if (e.code != 0)
ll0(++any << " op: error " << e);
}
if (g_scan_op != 0) {
const NdbError& e = g_scan_op->getNdbError();
if (e.code != 0)
ll0(++any << " scan_op: error " << e);
}
if (g_rangescan_op != 0) {
const NdbError& e = g_rangescan_op->getNdbError();
if (e.code != 0)
ll0(++any << " rangescan_op: error " << e);
}
if (g_is != 0) {
const NdbIndexStat::Error& e = g_is->getNdbError();
if (e.code != 0)
ll0(++any << " stat: error " << e);
}
if (! any)
ll0("unknown db error");
}
/* Methods to create NdbRecord structs for the table and index */
static int
createNdbRecords()
{
ll1("createNdbRecords");
const Uint32 numCols=4;
const Uint32 numIndexCols=3;
NdbDictionary::RecordSpecification recSpec[numCols];
recSpec[0].column= g_tab->getColumn("a"); // 4 bytes
recSpec[0].offset= g_ndbrec_a_offset;
recSpec[0].nullbit_byte_offset= ~(Uint32)0;
recSpec[0].nullbit_bit_in_byte= ~(Uint32)0;
recSpec[1].column= g_tab->getColumn("b"); // 4 bytes
recSpec[1].offset= g_ndbrec_b_offset;
if (g_b_nullable) {
recSpec[1].nullbit_byte_offset= 0;
recSpec[1].nullbit_bit_in_byte= g_ndbrec_b_nb_offset;
} else {
recSpec[1].nullbit_byte_offset= ~(Uint32)0;
recSpec[1].nullbit_bit_in_byte= ~(Uint32)0;
}
recSpec[2].column= g_tab->getColumn("c"); // Varchar(10) -> ~12 bytes
recSpec[2].offset= g_ndbrec_c_offset;
if (g_c_nullable) {
recSpec[2].nullbit_byte_offset= 0;
recSpec[2].nullbit_bit_in_byte= g_ndbrec_c_nb_offset;
} else {
recSpec[2].nullbit_byte_offset= ~(Uint32)0;
recSpec[2].nullbit_bit_in_byte= ~(Uint32)0;
}
recSpec[3].column= g_tab->getColumn("d"); // 2 bytes
recSpec[3].offset= g_ndbrec_d_offset;
if (g_d_nullable) {
recSpec[3].nullbit_byte_offset= 0;
recSpec[3].nullbit_bit_in_byte= g_ndbrec_d_nb_offset;
} else {
recSpec[3].nullbit_byte_offset= ~(Uint32)0;
recSpec[3].nullbit_bit_in_byte= ~(Uint32)0;
}
g_dic = g_ndb->getDictionary();
g_tab_rec= g_dic->createRecord(g_tab,
&recSpec[0],
numCols,
sizeof(NdbDictionary::RecordSpecification),
0);
chkdb(g_tab_rec != NULL);
g_ind_rec= g_dic->createRecord(g_ind,
&recSpec[1],
numIndexCols,
sizeof(NdbDictionary::RecordSpecification),
0);
chkdb(g_ind_rec != NULL);
g_dic = 0;
return 0;
}
// create table ts0 (
// a int unsigned,
// b int unsigned, c varchar(10), d smallint unsigned,
// primary key using hash (a), index (b, c, d) )
static int
createtable()
{
ll1("createtable");
NdbDictionary::Table tab(g_tabname);
tab.setLogging(false);
{
NdbDictionary::Column col("a");
col.setType(NdbDictionary::Column::Unsigned);
col.setPrimaryKey(true);
tab.addColumn(col);
}
{
NdbDictionary::Column col("b");
col.setType(NdbDictionary::Column::Unsigned);
col.setNullable(g_b_nullable);
tab.addColumn(col);
}
{
NdbDictionary::Column col("c");
col.setType(NdbDictionary::Column::Varchar);
col.setLength(g_charlen);
col.setCharset(g_cs);
col.setNullable(g_c_nullable);
tab.addColumn(col);
}
{
NdbDictionary::Column col("d");
col.setType(NdbDictionary::Column::Smallunsigned);
col.setNullable(g_d_nullable);
tab.addColumn(col);
}
g_dic = g_ndb->getDictionary();
if (g_dic->getTable(g_tabname) != 0)
chkdb(g_dic->dropTable(g_tabname) == 0);
chkdb(g_dic->createTable(tab) == 0);
chkdb((g_tab = g_dic->getTable(g_tabname)) != 0);
g_dic = 0;
return 0;
}
static int
createindex()
{
ll1("createindex");
NdbDictionary::Index ind(g_indname);
ind.setTable(g_tabname);
ind.setType(NdbDictionary::Index::OrderedIndex);
ind.setLogging(false);
ind.addColumnName("b");
ind.addColumnName("c");
ind.addColumnName("d");
g_dic = g_ndb->getDictionary();
chkdb(g_dic->createIndex(ind) == 0);
chkdb((g_ind = g_dic->getIndex(g_indname, g_tabname)) != 0);
g_dic = 0;
return 0;
}
static int
droptable()
{
ll1("droptable");
g_dic = g_ndb->getDictionary();
chkdb(g_dic->dropTable(g_tabname) == 0);
g_dic = 0;
return 0;
}
// values for keys and bounds
struct Val {
uint8 m_numattrs;
int8 b_null;
int8 c_null;
int8 d_null;
Uint32 b;
uchar c[1 + g_charlen];
Uint16 d;
Val();
void init();
void copy(const Val& val2);
void make(uint numattrs, const Lim& lim);
int cmp(const Val& val2, uint numattrs = g_numattrs, uint* num_eq = 0) const;
void fromib(const NdbIndexScanOperation::IndexBound& ib, uint j);
private:
Val& operator=(const Val&);
Val(const Val&);
};
static NdbOut&
operator<<(NdbOut& out, const Val& val)
{
out << "[";
if (val.m_numattrs >= 1) {
if (val.b_null)
out << "NULL";
else
out << val.b;
}
if (val.m_numattrs >= 2) {
out << " ";
if (val.c_null)
out << "NULL";
else {
char buf[1 + g_charlen];
sprintf(buf, "%.*s", val.c[0], &val.c[1]);
out << "'" << buf << "'";
}
}
if (val.m_numattrs >= 3) {
out << " ";
if (val.d_null)
out <<" NULL";
else
out << val.d;
}
out << "]";
return out;
}
Val::Val()
{
init();
}
void
Val::init()
{
m_numattrs = 0;
// junk rest
b_null = -1;
c_null = -1;
d_null = -1;
b = ~(Uint32)0;
memset(c, 0xff, sizeof(c));
d = ~(Uint16)0;
}
void
Val::copy(const Val& val2)
{
require(this != &val2);
init();
m_numattrs = val2.m_numattrs;
if (m_numattrs >= 1) {
require(val2.b_null == 0 || val2.b_null == 1);
b_null = val2.b_null;
if (!b_null)
b = val2.b;
}
if (m_numattrs >= 2) {
require(val2.c_null == 0 || val2.c_null == 1);
c_null = val2.c_null;
if (!c_null)
memcpy(c, val2.c, sizeof(c));
}
if (m_numattrs >= 3) {
require(val2.d_null == 0 || val2.d_null == 1);
d_null = val2.d_null;
if (!d_null)
d = val2.d;
}
}
void
Val::make(uint numattrs, const Lim& lim)
{
require(numattrs <= g_numattrs);
if (numattrs >= 1) {
const bool nullable = g_b_nullable || lim.all_nullable;
if (nullable && urandom(100) < g_opts.nullkeys)
b_null = 1;
else {
require(lim.b_min <= lim.b_max);
b = lim.b_min + urandom(lim.b_max - lim.b_min + 1);
b_null = 0;
}
}
if (numattrs >= 2) {
const bool nullable = g_c_nullable || lim.all_nullable;
if (nullable && urandom(100) < g_opts.nullkeys)
c_null = 1;
else {
// prefer shorter
const uint len = urandom(urandom(g_charlen + 1) + 1);
c[0] = len;
for (uint j = 0; j < len; j++) {
uint k = urandom(strlen(lim.c_char));
c[1 + j] = lim.c_char[k];
}
c_null = 0;
}
}
if (numattrs >= 3) {
const bool nullable = g_d_nullable || lim.all_nullable;
if (nullable && urandom(100) < g_opts.nullkeys)
d_null = 1;
else {
require(lim.d_min <= lim.d_max);
d = lim.d_min + urandom(lim.d_max - lim.d_min + 1);
d_null = 0;
}
}
m_numattrs = numattrs;
}
int
Val::cmp(const Val& val2, uint numattrs, uint* num_eq) const
{
require(numattrs <= m_numattrs);
require(numattrs <= val2.m_numattrs);
uint n = 0; // attr index where differs
uint k = 0;
if (k == 0 && numattrs >= 1) {
if (! b_null && ! val2.b_null) {
if (b < val2.b)
k = -1;
else if (b > val2.b)
k = +1;
} else if (! b_null) {
k = +1;
} else if (! val2.b_null) {
k = -1;
}
if (k == 0)
n++;
}
if (k == 0 && numattrs >= 2) {
if (! c_null && ! val2.c_null) {
const uchar* s1 = &c[1];
const uchar* s2 = &val2.c[1];
const uint l1 = (uint)c[0];
const uint l2 = (uint)val2.c[0];
assert(l1 <= g_charlen && l2 <= g_charlen);
k = g_cs->coll->strnncollsp(g_cs, s1, l1, s2, l2, 0);
} else if (! c_null) {
k = +1;
} else if (! val2.c_null) {
k = -1;
}
if (k == 0)
n++;
}
if (k == 0 && numattrs >= 3) {
if (! d_null && ! val2.d_null) {
if (d < val2.d)
k = -1;
else if (d > val2.d)
k = +1;
} else if (! d_null) {
k = +1;
} else if (! val2.d_null) {
k = -1;
}
if (k == 0)
n++;
}
require(n <= numattrs);
if (num_eq != 0)
*num_eq = n;
return k;
}
void
Val::fromib(const NdbIndexScanOperation::IndexBound& ib, uint j)
{
const char* key = (j == 0 ? ib.low_key : ib.high_key);
const uint numattrs = (j == 0 ? ib.low_key_count : ib.high_key_count);
const Uint8 nullbits = *(const Uint8*)key;
require(numattrs <= g_numattrs);
if (numattrs >= 1) {
if (nullbits & (1 << g_ndbrec_b_nb_offset))
b_null = 1;
else {
memcpy(&b, &key[g_ndbrec_b_offset], sizeof(b));
b_null = 0;
}
}
if (numattrs >= 2) {
if (nullbits & (1 << g_ndbrec_c_nb_offset))
c_null = 1;
else {
memcpy(c, &key[g_ndbrec_c_offset], sizeof(c));
c_null = 0;
}
}
if (numattrs >= 3) {
if (nullbits & (1 << g_ndbrec_d_nb_offset))
d_null = 1;
else {
memcpy(&d, &key[g_ndbrec_d_offset], sizeof(d));
d_null = 0;
}
}
m_numattrs = numattrs;
}
// index keys
struct Key {
Val m_val;
int8 m_flag; // temp use
Key();
private:
Key& operator=(const Key&);
Key(const Key&);
};
static NdbOut&
operator<<(NdbOut& out, const Key& key)
{
out << key.m_val;
if (key.m_flag != -1)
out << " flag: " << key.m_flag;
return out;
}
Key::Key()
{
m_flag = -1;
}
static Key* g_keys = 0;
static uint* g_sortkeys = 0;
static void
freekeys()
{
delete [] g_keys;
delete [] g_sortkeys;
g_keys = 0;
g_sortkeys = 0;
}
static void
allockeys()
{
freekeys();
g_keys = new Key [g_opts.rows];
g_sortkeys = new uint [g_opts.rows];
require(g_keys != 0 && g_sortkeys != 0);
memset(g_sortkeys, 0xff, sizeof(uint) * g_opts.rows);
}
static int
cmpkeys(const void* p1, const void* p2)
{
const uint i1 = *(const uint*)p1;
const uint i2 = *(const uint*)p2;
require(i1 < g_opts.rows && i2 < g_opts.rows);
const Key& key1 = g_keys[i1];
const Key& key2 = g_keys[i2];
const int k = key1.m_val.cmp(key2.m_val, g_opts.attrs);
return k;
}
static void
sortkeys()
{
ll2("sortkeys");
uint i;
// sort
for (i = 0; i < g_opts.rows; i++)
g_sortkeys[i] = i;
qsort(g_sortkeys, g_opts.rows, sizeof(uint), cmpkeys);
// verify
uint unique = 1;
for (i = 1; i < g_opts.rows; i++) {
const uint i1 = g_sortkeys[i - 1];
const uint i2 = g_sortkeys[i];
require(i1 < g_opts.rows && i2 < g_opts.rows);
const Key& key1 = g_keys[i1];
const Key& key2 = g_keys[i2];
const int k = key1.m_val.cmp(key2.m_val, g_opts.attrs);
require(k <= 0);
if (k < 0)
unique++;
}
// show min max key
ll1("minkey:" << g_keys[g_sortkeys[0]]);
ll1("maxkey:" << g_keys[g_sortkeys[g_opts.rows - 1]]);
ll1("unique:" << unique);
}
static void
makekeys()
{
ll1("makekeys");
uint initrows = g_opts.rows / g_opts.rpk;
require(initrows != 0);
// distinct keys
uint i = 0;
while (i < initrows) {
Key& key = g_keys[i];
key.m_val.make(g_numattrs, g_lim_val);
i++;
}
// remaining keys
while (i < g_opts.rows) {
// if rpkvar is 10, multiply rpk by number between 0.1 and 10.0
double a = (double)(1 + urandom(g_opts.rpkvar * g_opts.rpkvar));
double b = a / (double)g_opts.rpkvar;
double c = b * (double)g_opts.rpk;
const uint n = (uint)(c + 0.5);
// select random key to duplicate from initrows
const uint k = urandom(initrows);
uint j = 0;
while (i < g_opts.rows && j < n) {
g_keys[i].m_val.copy(g_keys[k].m_val);
j++;
i++;
}
}
// shuffle
i = 0;
while (i < g_opts.rows) {
uint j = urandom(g_opts.rows);
if (i != j) {
Key tmp;
tmp.m_val.copy(g_keys[i].m_val);
g_keys[i].m_val.copy(g_keys[j].m_val);
g_keys[j].m_val.copy(tmp.m_val);
}
i++;
}
// sort
sortkeys();
}
// data loading
static int
verifydata()
{
ll3("verifydata");
chkdb((g_con = g_ndb->startTransaction()) != 0);
chkdb((g_scan_op = g_con->getNdbScanOperation(g_tab)) != 0);
chkdb(g_scan_op->readTuples(NdbScanOperation::LM_CommittedRead) == 0);
Uint32 a;
Val val;
val.m_numattrs = g_numattrs;
char* a_addr = (char*)&a;
char* b_addr = (char*)&val.b;
char* c_addr = (char*)val.c;
char* d_addr = (char*)&val.d;
Uint32 no = 0;
NdbRecAttr* b_ra;
NdbRecAttr* c_ra;
NdbRecAttr* d_ra;
chkdb(g_scan_op->getValue(no++, a_addr) != 0);
chkdb((b_ra = g_scan_op->getValue(no++, b_addr)) != 0);
chkdb((c_ra = g_scan_op->getValue(no++, c_addr)) != 0);
chkdb((d_ra = g_scan_op->getValue(no++, d_addr)) != 0);
chkdb(g_con->execute(NdbTransaction::NoCommit) == 0);
uint count = 0;
uint i;
for (i = 0; i < g_opts.rows; i++) {
Key& key = g_keys[i];
key.m_flag = false; // not scanned
}
while (1) {
int ret;
a = ~(Uint32)0;
chkdb((ret = g_scan_op->nextResult()) == 0 || ret == 1);
if (ret == 1)
break;
val.b_null = b_ra->isNULL();
val.c_null = c_ra->isNULL();
val.d_null = d_ra->isNULL();
require(val.b_null == 0 || (g_b_nullable && val.b_null == 1));
require(val.c_null == 0 || (g_c_nullable && val.c_null == 1));
require(val.d_null == 0 || (g_d_nullable && val.d_null == 1));
i = (uint)a;
chkrc(i < g_opts.rows);
Key& key = g_keys[i];
chkrc(key.m_val.cmp(val) == 0);
chkrc(key.m_flag == false);
key.m_flag = true;
count++;
}
g_ndb->closeTransaction(g_con);
g_con = 0;
g_scan_op = 0;
for (i = 0; i < g_opts.rows; i++) {
Key& key = g_keys[i];
chkrc(key.m_flag == true);
key.m_flag = -1; // forget
}
assert(count == g_opts.rows);
ll3("verifydata: " << g_opts.rows << " rows");
return 0;
}
static int
loaddata(bool update)
{
ll1("loaddata: update: " << update);
const uint batch = 512;
chkdb((g_con = g_ndb->startTransaction()) != 0);
uint i = 0;
while (i < g_opts.rows) {
chkdb((g_op = g_con->getNdbOperation(g_tab)) != 0);
if (!update)
chkdb(g_op->insertTuple() == 0);
else
chkdb(g_op->updateTuple() == 0);
Uint32 a = i;
const Val& val = g_keys[i].m_val;
const char* a_addr = (const char*)&a;
const char* b_addr = ! val.b_null ? (const char*)&val.b : 0;
const char* c_addr = ! val.c_null ? (const char*)val.c : 0;
const char* d_addr = ! val.d_null ? (const char*)&val.d : 0;
Uint32 no = 0;
chkdb(g_op->equal(no++, a_addr) == 0);
chkdb(g_op->setValue(no++, b_addr) == 0);
chkdb(g_op->setValue(no++, c_addr) == 0);
chkdb(g_op->setValue(no++, d_addr) == 0);
if (i++ % batch == 0) {
chkdb(g_con->execute(NdbTransaction::Commit) == 0);
g_ndb->closeTransaction(g_con);
g_con = 0;
g_op = 0;
chkdb((g_con = g_ndb->startTransaction()) != 0);
}
}
chkdb(g_con->execute(NdbTransaction::Commit) == 0);
g_ndb->closeTransaction(g_con);
g_con = 0;
g_op = 0;
// check data and cmp routines
chkrc(verifydata() == 0);
for (uint i = 0; i < g_opts.rows; i++)
ll3("load " << i << ": " << g_keys[i]);
ll0("loaddata: " << g_opts.rows << " rows");
return 0;
}
// bounds
struct Bnd {
Val m_val;
/*
* A bound is a partial key value (0 to g_numattrs attributes).
* It is not equal to any key value. Instead, it has a "side".
*
* side = 0 if the bound is empty
* side = -1 if the bound is "just before" its value
* side = +1 if the bound is "just after" its value
*
* This is another way of looking at strictness of non-empty
* start and end keys in a range.
*
* start key is strict if side = +1
* end key is strict if side = -1
*
* NDB API specifies strictness in the bound type of the last
* index attribute which is part of the start/end key.
*
* LE (0) - strict: n - side: -1
* LT (1) - strict: y - side: +1
* GE (2) - strict: n - side: +1
* GT (3) - strict: y - side: -1
*
* A non-empty bound divides keys into 2 disjoint subsets:
* keys before (cmp() == -1) and keys after (cmp() == +1).
*/
int8 m_side;
int8 m_lohi; // 0-lo 1-hi as part of Rng
Bnd();
bool isempty() const;
void copy(const Bnd& bnd2); // does not copy m_lohi
Bnd& make(uint minattrs);
Bnd& make(uint minattrs, const Val& theval);
int cmp(const Key& key) const;
int cmp(const Bnd& bnd2);
int type(uint colno) const; // for setBound
void fromib(const NdbIndexScanOperation::IndexBound& ib, uint j);
private:
Bnd& operator=(const Bnd&);
Bnd(const Bnd&);
};
static NdbOut&
operator<<(NdbOut& out, const Bnd& bnd)
{
if (bnd.m_lohi == 0)
out << "L";
else if (bnd.m_lohi == 1)
out << "H";
else
out << bnd.m_lohi << "?";
out << bnd.m_val;
if (bnd.m_side == 0)
;
else if (bnd.m_side == -1)
out << "-";
else if (bnd.m_side == +1)
out << "+";
return out;
}
Bnd::Bnd()
{
m_side = 0;
m_lohi = -1;
}
bool
Bnd::isempty() const
{
return m_val.m_numattrs == 0;
}
void
Bnd::copy(const Bnd& bnd2)
{
m_val.copy(bnd2.m_val);
m_side = bnd2.m_side;
}
Bnd&
Bnd::make(uint minattrs)
{
require(minattrs <= g_opts.attrs);
require(m_lohi == 0 || m_lohi == 1);
uint numattrs = minattrs + urandom(g_numattrs - minattrs + 1);
m_val.make(numattrs, g_lim_bnd);
m_side = m_val.m_numattrs == 0 ? 0 : urandom(2) == 0 ? -1 : +1;
return *this;
}
Bnd&
Bnd::make(uint minattrs, const Val& theval)
{
uint numattrs = minattrs + urandom(g_numattrs - minattrs);
m_val.copy(theval);
m_val.m_numattrs = numattrs;
m_side = m_val.m_numattrs == 0 ? 0 : urandom(2) == 0 ? -1 : +1;
return *this;
}
int
Bnd::cmp(const Key& key) const
{
int place; // debug
int ret;
do {
int k = key.m_val.cmp(m_val, m_val.m_numattrs);
if (k != 0) {
place = 1;
ret = k;
break;
}
if (m_side != 0) {
place = 2;
ret = (-1) * m_side;
break;
}
place = 3;
ret = 0;
assert(m_val.m_numattrs == 0);
} while (0);
ll3("bnd: " << *this << " cmp key: " << key
<< " ret: " << ret << " place: " << place);
return ret;
}
int
Bnd::cmp(const Bnd& bnd2)
{
int place; // debug
int ret;
const Bnd& bnd1 = *this;
const Val& val1 = bnd1.m_val;
const Val& val2 = bnd2.m_val;
const uint numattrs1 = val1.m_numattrs;
const uint numattrs2 = val2.m_numattrs;
const uint n = (numattrs1 < numattrs2 ? numattrs1 : numattrs2);
do {
int k = val1.cmp(val2, n);
if (k != 0) {
place = 1;
ret = k;
break;
}
if (numattrs1 < numattrs2) {
place = 2;
ret = (+1) * bnd1.m_side;
break;
}