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 /* test the update functionality. */
40 
41 #include "test.h"
42 
43 DB_ENV *env;
44 
45 // the commands are: byte 1 is "nop" "add" or "del".  Byte 2 is the amount to add.
46 enum cmd { CNOP, CADD, CDEL };
47 
increment_update(DB * db,const DBT * key,const DBT * old_val,const DBT * extra,void (* set_val)(const DBT * new_val,void * set_extra),void * set_extra)48 static int increment_update (DB *db __attribute__((__unused__)),
49                              const DBT *key __attribute__((__unused__)),
50                              const DBT *old_val, const DBT *extra,
51                              void (*set_val)(const DBT *new_val,
52                                              void *set_extra),
53                              void *set_extra) {
54     assert (extra->size==2);
55     assert (old_val->size==4);
56     unsigned char *CAST_FROM_VOIDP(extra_data, extra->data);
57     switch ((enum cmd)(extra_data[0])) {
58     case CNOP:
59         return 0;
60     case CADD: {
61         unsigned int data = *(unsigned int*)old_val->data;
62         data += extra_data[1];
63         DBT new_val = {.data=&data, .size=4, .ulen=0, .flags=0};
64         set_val(&new_val, set_extra);
65         return 0;
66     }
67     case CDEL:
68         set_val(NULL, set_extra);
69         return 0;
70     }
71     assert(0); return 0; // enumeration failed.
72 }
73 
setup(void)74 static void setup (void) {
75     toku_os_recursive_delete(TOKU_TEST_FILENAME);
76     { int r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);   CKERR(r); }
77     { int r=db_env_create(&env, 0);                           CKERR(r); }
78     env->set_errfile(env, stderr);
79     env->set_update(env, increment_update);
80 }
81 
cleanup(void)82 static void cleanup (void) {
83     { int r = env->close(env, 0);                             CKERR(r); }
84 }
85 
test_main(int argc,char * const argv[])86 int test_main (int argc __attribute__((__unused__)), char *const argv[] __attribute__((__unused__))) {
87 
88     setup();
89     cleanup();
90     return 0;
91 }
92