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 #include "test.h"
40 
test_main(int argc,char * const argv[])41 int test_main(int argc, char * const argv[])
42 {
43     int r;
44     DB * db;
45     DB_ENV * env;
46     (void) argc;
47     (void) argv;
48 
49     toku_os_recursive_delete(TOKU_TEST_FILENAME);
50     r = toku_os_mkdir(TOKU_TEST_FILENAME, 0755); { int chk_r = r; CKERR(chk_r); }
51 
52     // set things up
53     r = db_env_create(&env, 0);
54     CKERR(r);
55     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE, 0755);
56     CKERR(r);
57     r = db_create(&db, env, 0);
58     CKERR(r);
59     r = db->open(db, NULL, "foo.db", NULL, DB_BTREE, DB_CREATE, 0644);
60     CKERR(r);
61 
62 
63     DB_TXN* txn = NULL;
64     r = env->txn_begin(env, 0, &txn, DB_TXN_SNAPSHOT);
65     CKERR(r);
66 
67     int k = 1;
68     int v = 10;
69     DBT key, val;
70     r = db->put(
71         db,
72         txn,
73         dbt_init(&key, &k, sizeof k),
74         dbt_init(&val, &v, sizeof v),
75         0
76         );
77     CKERR(r);
78     k = 2;
79     v = 20;
80     r = db->put(
81         db,
82         txn,
83         dbt_init(&key, &k, sizeof k),
84         dbt_init(&val, &v, sizeof v),
85         0
86         );
87     CKERR(r);
88     r = txn->commit(txn, 0);
89     CKERR(r);
90 
91     r = env->txn_begin(env, 0, &txn, DB_TXN_SNAPSHOT | DB_TXN_READ_ONLY);
92     CKERR(r);
93     DBC* cursor = NULL;
94     r = db->cursor(db, txn, &cursor, 0);
95     CKERR(r);
96     DBT key1, val1;
97     memset(&key1, 0, sizeof key1);
98     memset(&val1, 0, sizeof val1);
99     r = cursor->c_get(cursor, &key1, &val1, DB_FIRST);
100     CKERR(r);
101     invariant(key1.size == sizeof(int));
102     invariant(*(int *)key1.data == 1);
103     invariant(val1.size == sizeof(int));
104     invariant(*(int *)val1.data == 10);
105 
106     r = cursor->c_get(cursor, &key1, &val1, DB_NEXT);
107     CKERR(r);
108     invariant(key1.size == sizeof(int));
109     invariant(*(int *)key1.data == 2);
110     invariant(val1.size == sizeof(int));
111     invariant(*(int *)val1.data == 20);
112 
113     r = cursor->c_close(cursor);
114     CKERR(r);
115     r = txn->commit(txn, 0);
116     CKERR(r);
117 
118     // clean things up
119     r = db->close(db, 0);
120     CKERR(r);
121     r = env->close(env, 0);
122     CKERR(r);
123 
124     return 0;
125 }
126