1 #include "caffe2/core/db.h" 2 #include "caffe2/core/logging.h" 4 #include "rocksdb/db.h" 5 #include "rocksdb/utilities/leveldb_options.h" 7 CAFFE2_DEFINE_int(caffe2_rocksdb_block_size, 65536,
8 "The caffe2 rocksdb block size when writing a rocksdb.");
16 : iter_(db->NewIterator(rocksdb::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<rocksdb::Iterator> iter_;
36 batch_.reset(
new rocksdb::WriteBatch());
40 batch_->Put(key, value);
43 rocksdb::Status status = db_->Write(rocksdb::WriteOptions(), batch_.get());
44 batch_.reset(
new rocksdb::WriteBatch());
46 status.ok(),
"Failed to write batch to rocksdb: " + status.ToString());
51 std::unique_ptr<rocksdb::WriteBatch> batch_;
58 RocksDB(
const string& source, Mode mode) :
DB(source, mode) {
59 rocksdb::LevelDBOptions options;
60 options.block_size = FLAGS_caffe2_rocksdb_block_size;
61 options.write_buffer_size = 268435456;
62 options.max_open_files = 100;
63 options.error_if_exists = mode == NEW;
64 options.create_if_missing = mode != READ;
65 rocksdb::Options rocksdb_options = rocksdb::ConvertOptions(options);
68 rocksdb::Status status = rocksdb::DB::Open(
69 rocksdb_options, source, &db_temp);
72 "Failed to open rocksdb ",
77 VLOG(1) <<
"Opened rocksdb " << source;
80 void Close()
override { db_.reset(); }
82 return make_unique<RocksDBCursor>(db_.get());
85 return make_unique<RocksDBTransaction>(db_.get());
89 std::unique_ptr<rocksdb::DB> db_;
94 REGISTER_CAFFE2_DB(rocksdb,
RocksDB);
An abstract class for accessing a database of key-value pairs.
string value() override
Returns the current value.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
An abstract class for the cursor of the database while reading.
string key() override
Returns the current key.
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
void Next() override
Go to the next location in the database.
void Commit() override
Commits the current writes.
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
void Close() override
Closes the database.
void SeekToFirst() override
Seek to the first key in the database.
Commandline flags support for Caffe2.
An abstract class for the current database transaction while writing.
void Seek(const string &key) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).