2 * Copyright (C) 2017 Cisco Inc.
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.
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.
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/>.
17 // @author Changxue Deng <chadeng@cisco.com>
25 #include <mabain/db.h>
29 using namespace mabain;
31 static int max_key = 1000000;
32 static std::atomic<int> write_index;
33 static bool stop_processing = false;
34 static std::string mbdir = "./multi_test/";
36 static void* insert_thread(void *arg)
39 TestKey mkey(MABAIN_TEST_KEY_TYPE_INT);
41 DB *db_r = new DB(mbdir.c_str(), CONSTS::ReaderOptions(), 128LL*1024*1024, 128LL*1024*1024);
42 assert(db_r->is_open());
44 while(!stop_processing) {
45 curr_key = write_index.fetch_add(1, std::memory_order_release);
46 kv = mkey.get_key(curr_key);
47 if(curr_key < max_key) {
48 assert(db_r->Add(kv, kv) == MBError::SUCCESS);
50 stop_processing = true;
55 // Reader must unregister the async writer pointer
60 void SetTestStatus(bool success)
64 cmd = std::string("touch ") + mbdir + "/_success";
66 cmd = std::string("rm ") + mbdir + "/_success >" + mbdir + "/out 2>" + mbdir + "/err";
68 if(system(cmd.c_str()) != 0) {
74 TestKey mkey(MABAIN_TEST_KEY_TYPE_INT);
76 DB *db_r = new DB(mbdir.c_str(), CONSTS::ReaderOptions(), 128LL*1024*1024, 128LL*1024*1024);
77 assert(db_r->is_open());
80 for(int i = 0; i < max_key; i++) {
82 assert(db_r->Find(kv, mbd) == MBError::SUCCESS);
83 assert(kv == std::string((const char *)mbd.buff, mbd.data_len));
89 // Multiple threads performing DB insertion/deletion/updating
90 int main(int argc, char *argv[])
99 mbdir = std::string(argv[1]);
100 std::cout << "Mabain test db directory " << mbdir << "\n";
103 max_key = atoi(argv[2]);
104 std::cout << "Setting number of keys to be " << max_key << "\n";
107 // SetTestStatus(false);
108 mabain::DB::SetLogFile(mbdir + "/mabain.log");
110 write_index.store(0, std::memory_order_release);
111 // Writer needs to enable async writer mode.
112 int options = CONSTS::WriterOptions() | CONSTS::ASYNC_WRITER_MODE;
113 DB *db = new DB(mbdir.c_str(), options, 128LL*1024*1024, 128LL*1024*1024);
114 assert(db->is_open());
117 for(int i = 0; i < nthread; i++) {
118 if(pthread_create(&pid[i], NULL, insert_thread, db) != 0) {
119 std::cout << "failed to create thread\n";
124 for(int i = 0; i < nthread; i++) {
125 pthread_join(pid[i], NULL);
128 assert(db->Close() == MBError::SUCCESS);
133 mabain::DB::CloseLogFile();
134 // SetTestStatus(true);