17 #include "caffe2/core/db.h" 18 #include "caffe2/core/logging.h" 19 #include "caffe2/core/module.h" 20 #include "caffe2/core/flags.h" 21 #include "rocksdb/db.h" 22 #include "rocksdb/utilities/leveldb_options.h" 25 caffe2_rocksdb_block_size,
27 "The caffe2 rocksdb block size when writing a rocksdb.");
35 : iter_(db->NewIterator(rocksdb::ReadOptions())) {
39 void Seek(
const string&
key)
override { iter_->Seek(key); }
40 bool SupportsSeek()
override {
return true; }
42 void Next()
override { iter_->Next(); }
43 string key()
override {
return iter_->key().ToString(); }
44 string value()
override {
return iter_->value().ToString(); }
45 bool Valid()
override {
return iter_->Valid(); }
48 std::unique_ptr<rocksdb::Iterator> iter_;
55 batch_.reset(
new rocksdb::WriteBatch());
59 batch_->Put(key, value);
62 rocksdb::Status status = db_->Write(rocksdb::WriteOptions(), batch_.get());
63 batch_.reset(
new rocksdb::WriteBatch());
65 status.ok(),
"Failed to write batch to rocksdb: " + status.ToString());
70 std::unique_ptr<rocksdb::WriteBatch> batch_;
77 RocksDB(
const string& source, Mode mode) :
DB(source, mode) {
78 rocksdb::LevelDBOptions options;
79 options.block_size = FLAGS_caffe2_rocksdb_block_size;
80 options.write_buffer_size = 268435456;
81 options.max_open_files = 100;
82 options.error_if_exists = mode == NEW;
83 options.create_if_missing = mode != READ;
84 rocksdb::Options rocksdb_options = rocksdb::ConvertOptions(options);
87 rocksdb::Status status = rocksdb::DB::Open(
88 rocksdb_options, source, &db_temp);
91 "Failed to open rocksdb ",
96 VLOG(1) <<
"Opened rocksdb " << source;
99 void Close()
override { db_.reset(); }
101 return make_unique<RocksDBCursor>(db_.get());
104 return make_unique<RocksDBTransaction>(db_.get());
108 std::unique_ptr<rocksdb::DB> db_;
113 REGISTER_CAFFE2_DB(rocksdb,
RocksDB);
117 CAFFE2_MODULE(caffe2_rocksdb,
"RocksDB implementation for caffe2::DB.");
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
void Next() override
Go to the next location in the database.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
An abstract class for the current database transaction while writing.
An abstract class for the cursor of the database while reading.
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
string value() override
Returns the current value.
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.
void SeekToFirst() override
Seek to the first key in the database.
void Put(const string &key, const string &value) override
Puts the key value pair 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).
void Commit() override
Commits the current writes.
string key() override
Returns the current key.
void Close() override
Closes the database.