1 #include <unordered_set> 3 #include "caffe2/core/db.h" 4 #include "caffe2/utils/proto_utils.h" 5 #include "caffe2/core/logging.h" 13 : proto_(proto), iter_(0) {}
16 void Seek(
const string& )
override {
17 CAFFE_THROW(
"ProtoDB is not designed to support seeking.");
21 void Next()
override { ++iter_; }
22 string key()
override {
return proto_->protos(iter_).name(); }
25 SerializeAsString_EnforceCheck(proto_->protos(iter_),
"ProtoDBCursor");
27 bool Valid()
override {
return iter_ < proto_->protos_size(); }
30 const TensorProtos* proto_;
37 : proto_(proto), existing_names_() {
38 for (
const auto& tensor : proto_->protos()) {
39 existing_names_.insert(tensor.name());
46 if (existing_names_.count(key)) {
47 CAFFE_THROW(
"An item with key ", key,
" already exists.");
49 auto* tensor = proto_->add_protos();
51 tensor->ParseFromString(value),
52 "Cannot parse content from the value string.");
54 tensor->name() ==
key,
57 " does not equal to the tensor name ",
66 std::unordered_set<string> existing_names_;
73 ProtoDB(
const string& source, Mode mode)
74 :
DB(source, mode), proto_(), source_(source) {
75 if (mode == READ || mode == WRITE) {
78 ReadProtoFromFile(source, &proto_),
"Cannot read protobuffer.");
80 LOG(INFO) <<
"Opened protodb " << source;
87 if (mode_ == NEW || mode_ == WRITE) {
88 WriteProtoToBinaryFile(proto_, source_);
93 return make_unique<ProtoDBCursor>(&proto_);
96 return make_unique<ProtoDBTransaction>(&proto_);
106 REGISTER_CAFFE2_DB(protodb,
ProtoDB);
An abstract class for the current database transaction while writing.
An abstract class for the cursor of the database while reading.
void Next() override
Go to the next location in the database.
string value() override
Returns the current value.
void Seek(const string &) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void Commit() override
Commits the current writes.
An abstract class for accessing a database of key-value pairs.
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
string key() override
Returns the current key.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
void Close() override
Closes the database.
void SeekToFirst() override
Seek to the first key in the database.
unique_ptr< Transaction > NewTransaction() override
Returns a transaction to write data to the database.