KyotoCabinetServer(const std::string& directoryName, bool sync, int64_t mmapSizeMb) : directoryName_(directoryName), sync_(sync), mmapSizeMb_(mmapSizeMb) { // open all the existing databases boost::unique_lock< boost::shared_mutex > writeLock(mutex_);; directory_iterator end_itr; for (directory_iterator itr(directoryName); itr != end_itr; itr++) { if (is_regular_file(itr->status())) { std::string mapName = itr->path().leaf().string(); std::string fileName = itr->path().string(); // skip hidden files if (mapName[0] == '.') { continue; } TreeDB* db = new TreeDB(); db->tune_map(mmapSizeMb_ << 20); uint32_t flags = BasicDB::OWRITER | BasicDB::OAUTOTRAN; if (sync_) { flags |= BasicDB::OAUTOSYNC; } if (!db->open(fileName, flags)) { printf("ERROR: failed to open '%s'\n", fileName.c_str()); exit(1); } maps_.insert(mapName, db); printf("INFO: opened '%s'\n", fileName.c_str()); } } }
ResponseCode::type addMap(const std::string& mapName) { boost::unique_lock< boost::shared_mutex> writeLock(mutex_);; boost::ptr_map<std::string, TreeDB>::iterator itr = maps_.find(mapName); if (itr != maps_.end()) { return ResponseCode::MapExists; } TreeDB* db = new TreeDB(); db->tune_map(mmapSizeMb_ << 20); uint32_t flags = BasicDB::OWRITER | BasicDB::OCREATE | BasicDB::OAUTOTRAN; if (sync_) { flags |= BasicDB::OAUTOSYNC; } if (!db->open(directoryName_ + "/" + mapName, flags)) { return ResponseCode::Error; } std::string mapName_ = mapName; maps_.insert(mapName_, db); return ResponseCode::Success; }
int main(int argc, char** argv) { string line, command; TreeDB treeDB; cout << ">"; getline(cin, line); while (!cin.eof()) { stringstream linestream (line); linestream >> command; //Parsing 'insert' function if (command == "insert") { string name,status; int IPaddress; DBentry *temp; linestream >> name >> IPaddress >> status; // active - passing "1" for the active boolean value // inactive - passing "0" for the active boolean value if (status == "active") temp = new DBentry(name, IPaddress, 1); else temp = new DBentry(name, IPaddress, 0); //inserting to Tree //isSucceeded = 0 when the DBentry already exists bool isSucceeded = treeDB.insert(temp); if (isSucceeded) cout << "Success" << endl; else cout << "Error: entry already exists" << endl; } //Parsing 'find' function else if (command == "find") {
// main routine int main(int argc, char** argv) { // create the database object TreeDB db; // open the database if (!db.open("casket.kch", PolyDB::OWRITER | PolyDB::OCREATE)) { cerr << "open error: " << db.error().name() << endl; } // store records if (!db.set("foo", "hop") || !db.set("bar", "step") || !db.set("baz", "jump")) { cerr << "set error: " << db.error().name() << endl; } // retrieve a record string value; if (db.get("foo", &value)) { cout << value << endl; } else { cerr << "get error: " << db.error().name() << endl; } // traverse records DB::Cursor* cur = db.cursor(); cur->jump(); string ckey, cvalue; while (cur->get(&ckey, &cvalue, true)) { cout << ckey << ":" << cvalue << endl; } delete cur; // close the database if (!db.close()) { cerr << "close error: " << db.error().name() << endl; } return 0; }
int64_t db_size () { return m_db.size (); }
int64_t messages () { return m_db.count (); }