1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of TokuDB
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     TokuDBis is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     TokuDB is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with TokuDB.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ======= */
23 
24 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
25 
26 // test tokudb cardinality in status dictionary
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <assert.h>
31 #include <memory.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <db.h>
35 typedef unsigned long long ulonglong;
36 #include <tokudb_status.h>
37 #include <tokudb_buffer.h>
38 
39 #include "fake_mysql.h"
40 
41 #if __APPLE__
42 typedef unsigned long ulong;
43 #endif
44 #include <tokudb_card.h>
45 
test_no_keys()46 static void test_no_keys() {
47     TABLE_SHARE s = { 0, 0, 0, NULL };
48     TABLE t = { &s, NULL };
49     assert(tokudb::compute_total_key_parts(&s) == 0);
50     tokudb::set_card_in_key_info(&t, 0, NULL);
51 }
52 
test_simple_pk()53 static void test_simple_pk() {
54     const uint keys = 1;
55     const uint key_parts = 1;
56     uint64_t pk_rec_per_key[keys] = { 0 };
57     KEY_INFO pk = { 0, key_parts, pk_rec_per_key, (char *) "PRIMARY" };
58     TABLE_SHARE s = { 0, keys, key_parts, &pk };
59     TABLE t = { &s, &pk };
60     assert(tokudb::compute_total_key_parts(&s) == key_parts);
61     uint64_t computed_rec_per_key[keys] = { 2 };
62     tokudb::set_card_in_key_info(&t, keys, computed_rec_per_key);
63     assert(t.key_info[0].rec_per_key[0] == 1);
64 }
65 
test_pk_2()66 static void test_pk_2() {
67     const uint keys = 1;
68     const uint key_parts = 2;
69     uint64_t pk_rec_per_key[keys * key_parts] = { 0 };
70     KEY_INFO pk = { 0, key_parts, pk_rec_per_key, (char *) "PRIMARY" };
71     TABLE_SHARE s = { 0, keys, key_parts, &pk };
72     TABLE t = { &s, &pk };
73     assert(tokudb::compute_total_key_parts(&s) == key_parts);
74     uint64_t computed_rec_per_key[keys * key_parts] = { 2, 3 };
75     tokudb::set_card_in_key_info(&t, keys * key_parts, computed_rec_per_key);
76     assert(t.key_info[0].rec_per_key[0] == 2);
77     assert(t.key_info[0].rec_per_key[1] == 1);
78 }
79 
test_simple_sk()80 static void test_simple_sk() {
81     const uint keys = 1;
82     const uint key_parts = 1;
83     uint64_t sk_rec_per_key[keys] = { 0 };
84     KEY_INFO sk = { 0, keys, sk_rec_per_key, (char *) "KEY" };
85     TABLE_SHARE s = { MAX_KEY, keys, key_parts, &sk };
86     TABLE t = { &s, &sk };
87     assert(tokudb::compute_total_key_parts(&s) == 1);
88     uint64_t computed_rec_per_key[keys] = { 2 };
89     tokudb::set_card_in_key_info(&t, keys, computed_rec_per_key);
90     assert(t.key_info[0].rec_per_key[0] == 2);
91 }
92 
test_simple_unique_sk()93 static void test_simple_unique_sk() {
94     const uint keys = 1;
95     uint64_t sk_rec_per_key[keys] = { 0 };
96     KEY_INFO sk = { HA_NOSAME, keys, sk_rec_per_key, (char *) "KEY" };
97     TABLE_SHARE s = { MAX_KEY, keys, keys, &sk };
98     TABLE t = { &s, &sk };
99     assert(tokudb::compute_total_key_parts(&s) == 1);
100     uint64_t computed_rec_per_key[keys] = { 2 };
101     tokudb::set_card_in_key_info(&t, keys, computed_rec_per_key);
102     assert(t.key_info[0].rec_per_key[0] == 1);
103 }
104 
test_simple_pk_sk()105 static void test_simple_pk_sk() {
106     const uint keys = 2;
107     uint64_t rec_per_key[keys] = { 0 };
108     KEY_INFO key_info[keys] = {
109         { 0, 1, &rec_per_key[0], (char *) "PRIMARY" },
110         { 0, 1, &rec_per_key[1], (char *) "KEY" },
111     };
112     TABLE_SHARE s = { 0, keys, keys, key_info };
113     TABLE t = { &s, key_info };
114     assert(tokudb::compute_total_key_parts(&s) == 2);
115     uint64_t computed_rec_per_key[keys] = { 100, 200 };
116     tokudb::set_card_in_key_info(&t, keys, computed_rec_per_key);
117     assert(t.key_info[0].rec_per_key[0] == 1);
118     assert(t.key_info[1].rec_per_key[0] == 200);
119 }
120 
test_simple_sk_pk()121 static void test_simple_sk_pk() {
122     const uint keys = 2;
123     uint64_t rec_per_key[keys] = { 0 };
124     KEY_INFO key_info[keys] = {
125         { 0, 1, &rec_per_key[0], (char *) "KEY" },
126         { 0, 1, &rec_per_key[1], (char *) "PRIMARY" },
127     };
128     TABLE_SHARE s = { 1, keys, keys, key_info };
129     TABLE t = { &s, key_info };
130     assert(tokudb::compute_total_key_parts(&s) == 2);
131     uint64_t computed_rec_per_key[keys] = { 100, 200 };
132     tokudb::set_card_in_key_info(&t, keys, computed_rec_per_key);
133     assert(t.key_info[0].rec_per_key[0] == 100);
134     assert(t.key_info[1].rec_per_key[0] == 1);
135 }
136 
main()137 int main() {
138     test_no_keys();
139     test_simple_pk();
140     test_pk_2();
141     test_simple_sk();
142     test_simple_unique_sk();
143     test_simple_pk_sk();
144     test_simple_sk_pk();
145     return 0;
146 }
147