示例#1
0
文件: writer.cpp 项目: foobarhe/ipc
int main(int argc, char** argv)
{
    DB* db;
    Options options;
    options.create_if_missing = true;

    Status status = DB::Open(options, "/tmp/leveldbtest", &db);
    if (false == status.ok()) {
        cerr << "Unable to open/create test database" << endl;
        cerr << status.ToString() << endl;
        return 1;
    }

    // write
    WriteOptions writeOptions;
    for (unsigned int i = 0; i < 256; ++i) {
        ostringstream keyStream;
        keyStream << "Key" << i;

        ostringstream valueStream;
        valueStream << "Test data value: " << i;

        db->Put(writeOptions, keyStream.str(), valueStream.str());
        this_thread::sleep_for(std::chrono::seconds(1));
    }

    // Close the database
    delete db;

    return 0;
}
示例#2
0
int main()
{
    int ret;
    DB *db;
    struct Options op;
    op.create_if_missing = true;
    Status s = DB::Open(op, "/tmp/testdb", &db);
    char *value = (char *)malloc(1024 * 1024);
    if (value == NULL)
    {
        cout << "malloc error" << endl;
        return -1;
    }
    memset(value, 0, 1024*1024);

    if (s.ok())
    {
        cout << "create db successfully!"<<endl;

        int i = 0;
        for (i = 0; i < 50; i++)
        {
            char temp[10] = {0};
            sprintf(temp, "%d", i);
            s = db->Put(WriteOptions(), temp, value);
            if (s.ok())
            {
                cout << "put " << temp << " successful" << endl;
            }
            else
            {
                cout << "put " << temp << " failed" << endl;
            }
        }

        Iterator *iter = db->NewIterator(ReadOptions());
        for (iter->SeekToFirst(); iter->Valid(); iter->Next())
        {
            cout << "key is " << iter->key().data() << ", value is " << iter->value().data() << endl;
            iter->Next();
        }
        delete iter;
    }
    else
    {
        cout << "create failed " << endl;
    }

    delete db;
    return 0;
}
示例#3
0
文件: testdb.cpp 项目: nonego/ldb
int main( int argc, char* argv[])
{
    DB* db;
    Options options;
    options.create_if_missing = true;

    leveldb::Status status = leveldb::DB::Open(options, "db/testdb", &db);
    string line;
    int n=0;
    while( getline(cin, line))
    {
        size_t i=line.find('\t' );
        if( i == string::npos )
            continue;
        db->Put(leveldb::WriteOptions(), line.substr(0, i), line.substr(i+1));
        ++n;
        if( ( n & 0xFFF ) == 0 )
            cout << n << endl;
    }

    return 0;

}
示例#4
0
文件: test.cpp 项目: zoozo/test
int main(int argc, char** argv) {
    srand (time(NULL));

    if(argc != 3){
        cout<<"invalid parameter!!"<<endl;
        return 0;
    }
    string key, val;
    int type = atoi(argv[1]);
    int counter = 0;

    if(type == 3){
        key = argv[2];
    }
    else{
        counter = atoi(argv[2]);
    }

    cout<<"type:"<<type<<endl;
    cout<<"counter:"<<counter<<endl;


  DB* db;
  Options options;
  // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
  options.IncreaseParallelism();
  options.OptimizeLevelStyleCompaction();
  // create the DB if it's not already present
  options.create_if_missing = true;

  // open DB
  Status s = DB::Open(options, kDBPath, &db);
  assert(s.ok());


  if(type == 3){
      s = db->Get(ReadOptions(), key, &val);
      cout<<"ret:"<<val<<endl;
  }
  else if(type == 1){
      time_t now = time(NULL);
      for(int i = 0; i < counter ; i++){
          key = "key_" + boost::lexical_cast<string>(i);
          val = "val_" + boost::lexical_cast<string>(i)+"_" +boost::lexical_cast<string>(now);

          s = db->Put(WriteOptions(), key, val);
      }
  }
  else if(type == 2){
      int k;
      for(int i = 0; i < counter ; i++){
          k = rand() % counter;

          key = "key_" + boost::lexical_cast<string>(k);

          s = db->Get(ReadOptions(), key, &val);
      }
      cout<<key<<":"<<val<<endl;
  }
    delete db;
    return 0;
}