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;
    }
Example #3
0
// 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;
}