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 PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT 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     PerconaFT 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 PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 /*
40  * Test that different compression methods can be used.
41  */
42 
43 #include <db.h>
44 #include "test.h"
45 
46 static const int VAL_SIZE = 248;
47 static const int NUM_ROWS = 1 << 12;
48 
49 static int
insert(DB_ENV * env,DB * db,void * UU (extra))50 insert(DB_ENV *env, DB *db, void *UU(extra))
51 {
52     assert(VAL_SIZE%sizeof(int)==0);
53     int val[VAL_SIZE/sizeof(int)];
54     memset(val, 0, sizeof val);
55     DB_TXN *txn;
56     int r = env->txn_begin(env, 0, &txn, 0);
57     CKERR(r);
58     for (int i = 0; i < NUM_ROWS; ++i) {
59         DBT k, v;
60         val[0] = i;
61         r = db->put(db, txn, dbt_init(&k, &i, sizeof i), dbt_init(&v, val, sizeof val), 0);
62         CKERR(r);
63     }
64     r = txn->commit(txn, 0);
65     CKERR(r);
66     return 0;
67 }
68 
69 static int
lookup(DB_ENV * env,DB * db,void * UU (extra))70 lookup(DB_ENV *env, DB *db, void *UU(extra))
71 {
72     DB_TXN *txn;
73     int r = env->txn_begin(env, 0, &txn, 0);
74     CKERR(r);
75     for (int i = 0; i < NUM_ROWS; ++i) {
76         DBT k, v;
77         r = db->get(db, txn, dbt_init(&k, &i, sizeof i), dbt_init(&v, NULL, 0), 0);
78         CKERR(r);
79         assert(v.size == (size_t) VAL_SIZE);
80         assert(*(int *) v.data == i);
81     }
82     r = txn->commit(txn, 0);
83     CKERR(r);
84     return 0;
85 }
86 
87 typedef int (*db_callback)(DB_ENV *env, DB *db, void *extra);
88 static int
with_open_db(db_callback cb,void * cb_extra,bool set_method,enum toku_compression_method method)89 with_open_db(db_callback cb, void *cb_extra, bool set_method, enum toku_compression_method method)
90 {
91     DB_ENV *env;
92     DB *db;
93     int r;
94     r = db_env_create(&env, 0);
95     CKERR(r);
96     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO);
97     CKERR(r);
98     r = db_create(&db, env, 0);
99     CKERR(r);
100     {
101         DB_TXN *txn;
102         r = env->txn_begin(env, 0, &txn, 0);
103         CKERR(r);
104         if (set_method) {
105             r = db->set_compression_method(db, method);
106             CKERR(r);
107         }
108         r = db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
109         CKERR(r);
110         r = txn->commit(txn, 0);
111         CKERR(r);
112     }
113 
114     {
115         enum toku_compression_method saved_method;
116         r = db->get_compression_method(db, &saved_method);
117         CKERR(r);
118         assert(saved_method == method);
119     }
120 
121     int cr = cb(env, db, cb_extra);
122 
123     r = db->close(db, 0);
124     CKERR(r);
125     r = env->close(env, 0);
126     CKERR(r);
127 
128     return cr;
129 }
130 
131 static void
run_test(enum toku_compression_method method)132 run_test(enum toku_compression_method method)
133 {
134     int r;
135     toku_os_recursive_delete(TOKU_TEST_FILENAME);
136     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
137     CKERR(r);
138 
139     r = with_open_db(insert, NULL, true, method);
140     CKERR(r);
141     r = with_open_db(lookup, NULL, false, method);
142     CKERR(r);
143 }
144 
145 int
test_main(int argc,char * const argv[])146 test_main(int argc, char *const argv[])
147 {
148     parse_args(argc, argv);
149     run_test(TOKU_NO_COMPRESSION);
150     run_test(TOKU_ZLIB_METHOD);
151     run_test(TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD);
152     run_test(TOKU_QUICKLZ_METHOD);
153     run_test(TOKU_LZMA_METHOD);
154     return 0;
155 }
156