示例#1
0
bool snpDeviceImpl::init(uint16 cellSize, uint32 cellsPerPU, uint32 numberOfPU)
{
    // setup device settings
    m_cellSize = cellSize;
    m_cellsPerPU = cellsPerPU;
    m_numberOfPU = numberOfPU;
    s_iCellIndex = kCellNotFound;

    // initialize database
    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;

    // initialize database
    Status dbStatus = DB::Open(options, kDatabasePath + std::to_string(m_cellSize), &s_database);
    if (dbStatus.ok())
    {
        // fillup database with default values
        rocksdb::WriteBatch initBatch; Status dbStatus;

        std::vector<uint32> defaultVector;
        for(uint16 index = 0; index < m_cellSize; index++)
        {
            defaultVector.push_back(0);
        }

        std::string defaultValue = bitfield2string(defaultVector);
        for (uint32 index = 0; index < getCellsPerPU() * getNumberOfPU(); index++)
        {
            std::string key = std::to_string(index);
            std::string value;

            dbStatus = s_database->Get(rocksdb::ReadOptions(), key, &value);
            if (!dbStatus.ok() || string2bitfield(value).size() < m_cellSize)
            {
                initBatch.Put(key, defaultValue);
            }
        }

        dbStatus = s_database->Write(rocksdb::WriteOptions(), &initBatch);
    }
    return dbStatus.ok();
}
示例#2
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;
}