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; }
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; }