1 #include "caffe2/core/db.h" 2 #include "caffe2/core/logging.h" 4 #include "leveldb/db.h" 5 #include "leveldb/write_batch.h" 7 CAFFE2_DEFINE_int(caffe2_leveldb_block_size, 65536,
8 "The caffe2 leveldb block size when writing a leveldb.");
16 : iter_(db->NewIterator(leveldb::ReadOptions())) {
20 void Seek(
const string&
key)
override { iter_->Seek(key); }
21 bool SupportsSeek()
override {
return true; }
23 void Next()
override { iter_->Next(); }
24 string key()
override {
return iter_->key().ToString(); }
25 string value()
override {
return iter_->value().ToString(); }
26 bool Valid()
override {
return iter_->Valid(); }
29 std::unique_ptr<leveldb::Iterator> iter_;
36 batch_.reset(
new leveldb::WriteBatch());
40 batch_->Put(key, value);
43 leveldb::Status status = db_->Write(leveldb::WriteOptions(), batch_.get());
44 batch_.reset(
new leveldb::WriteBatch());
47 "Failed to write batch to leveldb. ", status.ToString());
52 std::unique_ptr<leveldb::WriteBatch> batch_;
59 LevelDB(
const string& source, Mode mode) :
DB(source, mode) {
60 leveldb::Options options;
61 options.block_size = FLAGS_caffe2_leveldb_block_size;
62 options.write_buffer_size = 268435456;
63 options.max_open_files = 100;
64 options.error_if_exists = mode == NEW;
65 options.create_if_missing = mode != READ;
67 leveldb::Status status = leveldb::DB::Open(options, source, &db_temp);
70 "Failed to open leveldb ", source,
". ", status.ToString());
72 VLOG(1) <<
"Opened leveldb " << source;
75 void Close()
override { db_.reset(); }
77 return make_unique<LevelDBCursor>(db_.get());
80 return make_unique<LevelDBTransaction>(db_.get());
84 std::unique_ptr<leveldb::DB> db_;
89 REGISTER_CAFFE2_DB(leveldb,
LevelDB);
An abstract class for accessing a database of key-value pairs.
string key() override
Returns the current key.
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.
An abstract class for the cursor of the database while reading.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
void Seek(const string &key) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
string value() override
Returns the current value.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
void Commit() override
Commits the current writes.
void Close() override
Closes the database.
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
Commandline flags support for Caffe2.
An abstract class for the current database transaction while writing.
void Next() override
Go to the next location in the database.