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 #include <db.h>
41 #include <sys/stat.h>
42 
43 static DB_ENV *env;
44 static DB *db;
45 DB_TXN *txn;
46 
47 static void
test_setup(void)48 test_setup (void) {
49     int r;
50     toku_os_recursive_delete(TOKU_TEST_FILENAME);
51     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);       CKERR(r);
52 
53     r=db_env_create(&env, 0); CKERR(r);
54     env->set_errfile(env, stderr);
55     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); CKERR(r);
56     r=db_create(&db, env, 0); CKERR(r);
57 
58     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
59     r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
60     r=txn->commit(txn, 0);    assert(r==0);
61 }
62 
63 static void
test_shutdown(void)64 test_shutdown (void) {
65     int r;
66     r= db->close(db, 0); CKERR(r);
67     r= env->close(env, 0); CKERR(r);
68 }
69 
70 static void
doit(void)71 doit (void) {
72     DBT key,data;
73     int r;
74     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
75     r=db->put(db, txn, dbt_init(&key, "a", 2), dbt_init(&data, "a", 2), 0);
76     r=db->put(db, txn, dbt_init(&key, "b", 2), dbt_init(&data, "b", 2), 0);
77     r=db->put(db, txn, dbt_init(&key, "c", 2), dbt_init(&data, "c", 2), 0);
78     r=txn->commit(txn, 0);    assert(r==0);
79 
80     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
81     r=db->del(db, txn, dbt_init(&key, "b", 2),  0); assert(r==0);
82     r=txn->commit(txn, 0);    assert(r==0);
83 
84     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
85     DBC *dbc;
86     r = db->cursor(db, txn, &dbc, 0);                           assert(r==0);
87     memset(&key,  0, sizeof(key));
88     memset(&data, 0, sizeof(data));
89     r = dbc->c_get(dbc, &key, &data, DB_FIRST);                 assert(r==0);
90     assert(strcmp((char*)key.data, "a")==0);
91     assert(strcmp((char*)data.data, "a")==0);
92     r = dbc->c_get(dbc, &key, &data, DB_NEXT);                  assert(r==0);
93     assert(strcmp((char*)key.data, "c")==0);
94     assert(strcmp((char*)data.data, "c")==0);
95     r = dbc->c_close(dbc);                                      assert(r==0);
96     r=txn->commit(txn, 0);    assert(r==0);
97 }
98 
99 int
test_main(int argc,char * const argv[])100 test_main (int argc, char * const argv[]) {
101     parse_args(argc, argv);
102 
103     test_setup();
104     doit();
105     test_shutdown();
106 
107     return 0;
108 }
109