1 #include "caffe2/core/db.h" 2 #include "caffe2/core/logging.h" 3 #include "caffe2/core/flags.h" 4 #include "leveldb/db.h" 5 #include "leveldb/write_batch.h" 8 caffe2_leveldb_block_size,
10 "The caffe2 leveldb block size when writing a leveldb.");
18 : iter_(db->NewIterator(leveldb::ReadOptions())) {
22 void Seek(
const string&
key)
override { iter_->Seek(key); }
23 bool SupportsSeek()
override {
return true; }
25 void Next()
override { iter_->Next(); }
26 string key()
override {
return iter_->key().ToString(); }
27 string value()
override {
return iter_->value().ToString(); }
28 bool Valid()
override {
return iter_->Valid(); }
31 std::unique_ptr<leveldb::Iterator> iter_;
38 batch_.reset(
new leveldb::WriteBatch());
44 batch_->Put(key, value);
47 leveldb::Status status = db_->Write(leveldb::WriteOptions(), batch_.get());
48 batch_.reset(
new leveldb::WriteBatch());
51 "Failed to write batch to leveldb. ", status.ToString());
56 std::unique_ptr<leveldb::WriteBatch> batch_;
63 LevelDB(
const string& source, Mode mode) :
DB(source, mode) {
64 leveldb::Options options;
65 options.block_size = FLAGS_caffe2_leveldb_block_size;
66 options.write_buffer_size = 268435456;
67 options.max_open_files = 100;
68 options.error_if_exists = mode == NEW;
69 options.create_if_missing = mode != READ;
71 leveldb::Status status = leveldb::DB::Open(options, source, &db_temp);
74 "Failed to open leveldb ", source,
". ", status.ToString());
76 VLOG(1) <<
"Opened leveldb " << source;
79 void Close()
override { db_.reset(); }
81 return make_unique<LevelDBCursor>(db_.get());
84 return make_unique<LevelDBTransaction>(db_.get());
88 std::unique_ptr<leveldb::DB> db_;
93 REGISTER_CAFFE2_DB(leveldb,
LevelDB);
An abstract class for the current database transaction while writing.
An abstract class for the cursor of the database while reading.
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
void SeekToFirst() override
Seek to the first key in the database.
void Next() override
Go to the next location in the database.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
void Commit() override
Commits the current writes.
void Seek(const string &key) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
An abstract class for accessing a database of key-value pairs.
string value() override
Returns the current value.
void Close() override
Closes the database.
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
string key() override
Returns the current key.