benchmarks compiles with clang
[c11concurrency-benchmarks.git] / mabain / examples / mb_remove_test.cpp
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include <mabain/db.h>
20
21 using namespace mabain;
22
23 const char *db_dir = "./tmp_dir/";
24
25 // Remove key-value pair to mabain db
26 int main(int argc, char *argv[])
27 {
28     if(argc == 2) {
29         db_dir = argv[1];
30     }
31
32     mabain::DB::SetLogFile("/var/tmp/mabain_test/mabain.log");
33     DB db(db_dir, CONSTS::WriterOptions());
34     if(!db.is_open()) {
35         std::cerr << "failed to open mabain db: " << db.StatusStr() << "\n";
36         exit(1);
37     }
38
39     std::string key[3], value[3];
40     int rval;
41
42     key[0] = "Apple";
43     key[1] = "Orange";
44     key[2] = "Grape";
45     value[0] = "Red";
46     value[1] = "Yellow";
47     value[2] = "Purple";
48
49     // Add
50     for(int i = 0; i < 3; i++) {
51         db.Add(key[i], value[i]);
52     }
53
54     // Remove
55     for(int i = 0; i < 3; i++) {
56         rval = db.Remove(key[i]);
57         if(rval != MBError::SUCCESS)
58             std::cerr << "failed to remove key " << key[i] << "\n";
59         else
60             std::cout << "Removed " << key[i] << ": " << value[i] << "\n";
61     }
62
63     // Query
64     MBData mb_data;
65     for(int i = 0; i < 3; i++) {
66         rval = db.Find(key[i], mb_data);
67         if(rval != MBError::SUCCESS)
68             std::cout << "key " << key[i] << " not found\n";
69     }
70
71     db.PrintStats(std::cout);
72     db.Close();
73     mabain::DB::CloseLogFile();
74     return 0;
75 }