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 = 100000;
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) {
49 // assert(db_r->Add(kv, kv) == MBError::SUCCESS);
51 stop_processing = true;
56 // Reader must unregister the async writer pointer
61 void SetTestStatus(bool success)
65 cmd = std::string("touch ") + mbdir + "/_success";
67 cmd = std::string("rm ") + mbdir + "/_success >" + mbdir + "/out 2>" + mbdir + "/err";
69 if(system(cmd.c_str()) != 0) {
75 TestKey mkey(MABAIN_TEST_KEY_TYPE_INT);
77 DB *db_r = new DB(mbdir.c_str(), CONSTS::ReaderOptions(), 128LL*1024*1024, 128LL*1024*1024);
78 assert(db_r->is_open());
81 for(int i = 0; i < max_key; i++) {
84 // assert(db_r->Find(kv, mbd) == MBError::SUCCESS);
85 // assert(kv == std::string((const char *)mbd.buff, mbd.data_len));
91 // Multiple threads performing DB insertion/deletion/updating
92 int main(int argc, char *argv[])
101 mbdir = std::string(argv[1]);
102 std::cout << "Mabain test db directory " << mbdir << "\n";
105 max_key = atoi(argv[2]);
106 std::cout << "Setting number of keys to be " << max_key << "\n";
109 // SetTestStatus(false);
110 mabain::DB::SetLogFile(mbdir + "/mabain.log");
112 write_index.store(0, std::memory_order_release);
113 // Writer needs to enable async writer mode.
114 int options = CONSTS::WriterOptions() | CONSTS::ASYNC_WRITER_MODE;
115 DB *db = new DB(mbdir.c_str(), options, 128LL*1024*1024, 128LL*1024*1024);
116 assert(db->is_open());
119 for(int i = 0; i < nthread; i++) {
120 if(pthread_create(&pid[i], NULL, insert_thread, db) != 0) {
121 std::cout << "failed to create thread\n";
126 for(int i = 0; i < nthread; i++) {
127 pthread_join(pid[i], NULL);
130 assert(db->Close() == MBError::SUCCESS);
135 mabain::DB::CloseLogFile();
136 // SetTestStatus(true);