Caffe2 - C++ API
A deep learning, cross platform ML framework
protodb.cc
1 #include <unordered_set>
2 
3 #include "caffe2/core/db.h"
4 #include "caffe2/utils/proto_utils.h"
5 #include "caffe2/core/logging.h"
6 
7 namespace caffe2 {
8 namespace db {
9 
10 class ProtoDBCursor : public Cursor {
11  public:
12  explicit ProtoDBCursor(const TensorProtos* proto)
13  : proto_(proto), iter_(0) {}
14  ~ProtoDBCursor() override {}
15 
16  void Seek(const string& /*str*/) override {
17  CAFFE_THROW("ProtoDB is not designed to support seeking.");
18  }
19 
20  void SeekToFirst() override { iter_ = 0; }
21  void Next() override { ++iter_; }
22  string key() override { return proto_->protos(iter_).name(); }
23  string value() override {
24  return
25  SerializeAsString_EnforceCheck(proto_->protos(iter_), "ProtoDBCursor");
26  }
27  bool Valid() override { return iter_ < proto_->protos_size(); }
28 
29  private:
30  const TensorProtos* proto_;
31  int iter_;
32 };
33 
35  public:
36  explicit ProtoDBTransaction(TensorProtos* proto)
37  : proto_(proto), existing_names_() {
38  for (const auto& tensor : proto_->protos()) {
39  existing_names_.insert(tensor.name());
40  }
41  }
42  ~ProtoDBTransaction() override {
43  Commit();
44  }
45  void Put(const string& key, const string& value) override {
46  if (existing_names_.count(key)) {
47  CAFFE_THROW("An item with key ", key, " already exists.");
48  }
49  auto* tensor = proto_->add_protos();
50  CAFFE_ENFORCE(
51  tensor->ParseFromString(value),
52  "Cannot parse content from the value string.");
53  CAFFE_ENFORCE(
54  tensor->name() == key,
55  "Passed in key ",
56  key,
57  " does not equal to the tensor name ",
58  tensor->name());
59  }
60  // Commit does nothing. The protocol buffer will be written at destruction
61  // of ProtoDB.
62  void Commit() override {}
63 
64  private:
65  TensorProtos* proto_;
66  std::unordered_set<string> existing_names_;
67 
68  C10_DISABLE_COPY_AND_ASSIGN(ProtoDBTransaction);
69 };
70 
71 class ProtoDB : public DB {
72  public:
73  ProtoDB(const string& source, Mode mode)
74  : DB(source, mode), proto_(), source_(source) {
75  if (mode == READ || mode == WRITE) {
76  // Read the current protobuffer.
77  CAFFE_ENFORCE(
78  ReadProtoFromFile(source, &proto_), "Cannot read protobuffer.");
79  }
80  LOG(INFO) << "Opened protodb " << source;
81  }
82  ~ProtoDB() override {
83  Close();
84  }
85 
86  void Close() override {
87  if (mode_ == NEW || mode_ == WRITE) {
88  WriteProtoToBinaryFile(proto_, source_);
89  }
90  }
91 
92  unique_ptr<Cursor> NewCursor() override {
93  return make_unique<ProtoDBCursor>(&proto_);
94  }
95  unique_ptr<Transaction> NewTransaction() override {
96  return make_unique<ProtoDBTransaction>(&proto_);
97  }
98 
99  private:
100  TensorProtos proto_;
101  string source_;
102 };
103 
104 REGISTER_CAFFE2_DB(ProtoDB, ProtoDB);
105 // For lazy-minded, one can also call with lower-case name.
106 REGISTER_CAFFE2_DB(protodb, ProtoDB);
107 
108 } // namespace db
109 } // namespace caffe2
An abstract class for the current database transaction while writing.
Definition: db.h:61
An abstract class for the cursor of the database while reading.
Definition: db.h:22
void Next() override
Go to the next location in the database.
Definition: protodb.cc:21
string value() override
Returns the current value.
Definition: protodb.cc:23
void Seek(const string &) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
Definition: protodb.cc:16
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
Definition: protodb.cc:27
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
void Commit() override
Commits the current writes.
Definition: protodb.cc:62
An abstract class for accessing a database of key-value pairs.
Definition: db.h:80
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
Definition: protodb.cc:45
string key() override
Returns the current key.
Definition: protodb.cc:22
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
Definition: protodb.cc:92
void Close() override
Closes the database.
Definition: protodb.cc:86
void SeekToFirst() override
Seek to the first key in the database.
Definition: protodb.cc:20
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.
Definition: protodb.cc:95