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& str)
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(); }
23 string value()
override {
return proto_->protos(iter_).SerializeAsString(); }
24 bool Valid()
override {
return iter_ < proto_->protos_size(); }
27 const TensorProtos* proto_;
34 : proto_(proto), existing_names_() {
35 for (
const auto& tensor : proto_->protos()) {
36 existing_names_.insert(tensor.name());
41 if (existing_names_.count(key)) {
42 CAFFE_THROW(
"An item with key ", key,
" already exists.");
44 auto* tensor = proto_->add_protos();
46 tensor->ParseFromString(value),
47 "Cannot parse content from the value string.");
49 tensor->name() ==
key,
52 " does not equal to the tensor name ",
61 std::unordered_set<string> existing_names_;
68 ProtoDB(
const string& source, Mode mode)
69 :
DB(source, mode), proto_(), source_(source) {
70 if (mode == READ || mode == WRITE) {
73 ReadProtoFromFile(source, &proto_),
"Cannot read protobuffer.");
75 LOG(INFO) <<
"Opened protodb " << source;
80 if (mode_ == NEW || mode_ == WRITE) {
81 WriteProtoToBinaryFile(proto_, source_);
86 return make_unique<ProtoDBCursor>(&proto_);
89 return make_unique<ProtoDBTransaction>(&proto_);
99 REGISTER_CAFFE2_DB(protodb,
ProtoDB);
An abstract class for accessing a database of key-value pairs.
bool Valid() override
Returns whether the current location is valid - for example, if we have reached the end of the databa...
string value() override
Returns the current value.
An abstract class for the cursor of the database while reading.
void Commit() override
Commits the current writes.
void Seek(const string &str) override
Seek to a specific key (or if the key does not exist, seek to the immediate next).
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...
void Next() override
Go to the next location in the database.
void SeekToFirst() override
Seek to the first key in the database.
void Close() override
Closes the database.
void Put(const string &key, const string &value) override
Puts the key value pair to the database.
An abstract class for the current database transaction while writing.
unique_ptr< Cursor > NewCursor() override
Returns a cursor to read the database.
string key() override
Returns the current key.