Caffe2 - C++ API
A deep learning, cross platform ML framework
rocksdb.cc
1 #include "caffe2/core/db.h"
2 #include "caffe2/core/logging.h"
3 #include "caffe2/core/flags.h"
4 #include "rocksdb/db.h"
5 #include "rocksdb/utilities/leveldb_options.h"
6 
7 CAFFE2_DEFINE_int(caffe2_rocksdb_block_size, 65536,
8  "The caffe2 rocksdb block size when writing a rocksdb.");
9 
10 namespace caffe2 {
11 namespace db {
12 
13 class RocksDBCursor : public Cursor {
14  public:
15  explicit RocksDBCursor(rocksdb::DB* db)
16  : iter_(db->NewIterator(rocksdb::ReadOptions())) {
17  SeekToFirst();
18  }
19  ~RocksDBCursor() {}
20  void Seek(const string& key) override { iter_->Seek(key); }
21  bool SupportsSeek() override { return true; }
22  void SeekToFirst() override { iter_->SeekToFirst(); }
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(); }
27 
28  private:
29  std::unique_ptr<rocksdb::Iterator> iter_;
30 };
31 
33  public:
34  explicit RocksDBTransaction(rocksdb::DB* db) : db_(db) {
35  CAFFE_ENFORCE(db_);
36  batch_.reset(new rocksdb::WriteBatch());
37  }
38  ~RocksDBTransaction() { Commit(); }
39  void Put(const string& key, const string& value) override {
40  batch_->Put(key, value);
41  }
42  void Commit() override {
43  rocksdb::Status status = db_->Write(rocksdb::WriteOptions(), batch_.get());
44  batch_.reset(new rocksdb::WriteBatch());
45  CAFFE_ENFORCE(
46  status.ok(), "Failed to write batch to rocksdb: " + status.ToString());
47  }
48 
49  private:
50  rocksdb::DB* db_;
51  std::unique_ptr<rocksdb::WriteBatch> batch_;
52 
53  DISABLE_COPY_AND_ASSIGN(RocksDBTransaction);
54 };
55 
56 class RocksDB : public DB {
57  public:
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);
66 
67  rocksdb::DB* db_temp;
68  rocksdb::Status status = rocksdb::DB::Open(
69  rocksdb_options, source, &db_temp);
70  CAFFE_ENFORCE(
71  status.ok(),
72  "Failed to open rocksdb ",
73  source,
74  "\n",
75  status.ToString());
76  db_.reset(db_temp);
77  VLOG(1) << "Opened rocksdb " << source;
78  }
79 
80  void Close() override { db_.reset(); }
81  unique_ptr<Cursor> NewCursor() override {
82  return make_unique<RocksDBCursor>(db_.get());
83  }
84  unique_ptr<Transaction> NewTransaction() override {
85  return make_unique<RocksDBTransaction>(db_.get());
86  }
87 
88  private:
89  std::unique_ptr<rocksdb::DB> db_;
90 };
91 
92 REGISTER_CAFFE2_DB(RocksDB, RocksDB);
93 // For lazy-minded, one can also call with lower-case name.
94 REGISTER_CAFFE2_DB(rocksdb, RocksDB);
95 
96 } // namespace db
97 } // namespace caffe2
An abstract class for accessing a database of key-value pairs.
Definition: db.h:80
string value() override
Returns the current value.
Definition: rocksdb.cc:25
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
Definition: rocksdb.cc:81
An abstract class for the cursor of the database while reading.
Definition: db.h:22
string key() override
Returns the current key.
Definition: rocksdb.cc:24
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
Definition: rocksdb.cc:39
void Next() override
Go to the next location in the database.
Definition: rocksdb.cc:23
void Commit() override
Commits the current writes.
Definition: rocksdb.cc:42
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
Definition: rocksdb.cc:84
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...
Definition: rocksdb.cc:26
void Close() override
Closes the database.
Definition: rocksdb.cc:80
void SeekToFirst() override
Seek to the first key in the database.
Definition: rocksdb.cc:22
Commandline flags support for Caffe2.
An abstract class for the current database transaction while writing.
Definition: db.h:61
void Seek(const string &key) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
Definition: rocksdb.cc:20