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 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 #include <unistd.h>
45 #include <memory.h>
46 #include <errno.h>
47 #include <sys/stat.h>
48 #include <db.h>
49 
50 
51 static void
test_cursor(void)52 test_cursor (void) {
53     if (verbose) printf("test_cursor\n");
54 
55     DB_ENV * env;
56     DB *db;
57     DB_TXN * const null_txn = 0;
58     const char * const fname = "test.cursor.ft_handle";
59     int r;
60 
61     /* create the dup database file */
62     r = db_env_create(&env, 0);        assert(r == 0);
63     env->set_errfile(env, stderr);
64     r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE|DB_INIT_MPOOL|DB_THREAD|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
65     r = db_create(&db, env, 0); assert(r == 0);
66     db->set_errfile(db,stderr); // Turn off those annoying errors
67     r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); assert(r == 0);
68 
69     int i;
70     int n = 42;
71     for (i=0; i<n; i++) {
72         int k = htonl(i);
73         int v = htonl(i);
74         DBT key, val;
75         r = db->put(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
76         assert(r == 0);
77     }
78 
79     int ncursors = 2;
80     DBC *cursor[ncursors];
81     r = db->cursor(db, null_txn, &cursor[0], 0); assert(r == 0);
82     r = db->cursor(db, null_txn, &cursor[1], 0); assert(r == 0);
83 
84     DBT k0; memset(&k0, 0, sizeof k0);
85     DBT v0; memset(&v0, 0, sizeof v0);
86     r = cursor[0]->c_get(cursor[0], &k0, &v0, DB_FIRST); assert(r == 0);
87     if (verbose) {
88 	printf("k0:%p:%u\n", k0.data, k0.size);
89 	printf("v0:%p:%u\n", v0.data, v0.size);
90     }
91 
92     DBT k1; memset(&k1, 0, sizeof k1);
93     DBT v1; memset(&v1, 0, sizeof v1);
94     r = cursor[1]->c_get(cursor[1], &k1, &v1, DB_FIRST); assert(r == 0);
95     if (verbose) {
96 	printf("k1:%p:%u\n", k1.data, k1.size);
97 	printf("v1:%p:%u\n", v1.data, v1.size);
98     }
99 
100     r = cursor[0]->c_get(cursor[0], &k0, &v0, DB_NEXT); assert(r == 0);
101     if (verbose) {
102 	printf("k0:%p:%u\n", k0.data, k0.size);
103 	printf("v0:%p:%u\n", v0.data, v0.size);
104     }
105 
106     assert(k0.data != k1.data);
107     assert(v0.data != v1.data);
108 
109     r = cursor[0]->c_close(cursor[0]); assert(r == 0);
110     r = cursor[1]->c_close(cursor[1]); assert(r == 0);
111 
112     r = db->close(db, 0); assert(r == 0);
113     r = env->close(env, 0); assert(r == 0);
114 }
115 
116 int
test_main(int argc,char * const argv[])117 test_main(int argc, char *const argv[]) {
118 
119     parse_args(argc, argv);
120 
121     toku_os_recursive_delete(TOKU_TEST_FILENAME);
122     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
123 
124     test_cursor();
125 
126     return 0;
127 }
128