modify build setup
[c11concurrency-benchmarks.git] / mabain / examples / mb_memory_only_test.cpp
1 /**
2  * Copyright (C) 2018 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_name = "my-memory-only-test";
24
25 // memory-only mode test
26 // No db directory is required. Need an unique name instead.
27 // Note MEMORY_ONLY_MODE does not support multi-process accessing the DB.
28 int main(int argc, char *argv[])
29 {
30     DB db(db_name, CONSTS::WriterOptions() | CONSTS::MEMORY_ONLY_MODE);
31     if(!db.is_open()) {
32         std::cerr << "failed to open mabain db: " << db.StatusStr() << "\n";
33         exit(1);
34     }
35
36     std::string key[3], value[3];
37     int rval;
38
39     key[0] = "Apple";
40     value[0] = "Red";
41     key[1] = "Orange";
42     value[1] = "Yellow";
43     key[2] = "Grape";
44     value[2] = "Purple";
45
46     for(int i = 0; i < 3; i++) {
47         rval = db.Add(key[i], value[i]);
48         if(rval == MBError::SUCCESS) {
49             std::cout << "Add " << key[i] << ": " << value[i] << std::endl;
50         }
51     }
52
53     MBData mbd;
54     for(int i = 0; i < 3; i++) {
55         rval = db.Find(key[i], mbd);
56         if(rval == MBError::SUCCESS) {
57             std::cout << "Lookup " << key[i] << ": " << std::string((char *)mbd.buff, mbd.data_len) << std::endl;
58         }
59     }
60
61     db.Close();
62     return 0;
63 }