本文整理汇总了C++中DB::NewIterator方法的典型用法代码示例。如果您正苦于以下问题:C++ DB::NewIterator方法的具体用法?C++ DB::NewIterator怎么用?C++ DB::NewIterator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DB
的用法示例。
在下文中一共展示了DB::NewIterator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
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;
}
// read
int i = 0;
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
i++;
cout << it->key().ToString() << " : " << it->value().ToString() << endl;
this_thread::sleep_for(std::chrono::seconds(1));
}
cout << "i=" << i << endl;
if (false == it->status().ok()) {
cerr << "An error was found during the scan" << endl;
cerr << it->status().ToString() << endl;
}
delete it;
// Close the database
delete db;
return 0;
}
示例2: main
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;
}