#include "caffe/common.hpp"
#include "caffe/database.hpp"
+#include "caffe/proto/caffe.pb.h"
namespace caffe {
void commit();
void close();
+ const_iterator begin() const;
+ const_iterator cbegin() const;
+ const_iterator end() const;
+ const_iterator cend() const;
+
~LeveldbDatabase() { this->close(); }
protected:
void increment(shared_ptr<DatabaseState> state) const;
pair<string, string>& dereference(shared_ptr<DatabaseState> state) const;
- const_iterator begin() const;
- const_iterator cbegin() const;
- const_iterator end() const;
- const_iterator cend() const;
-
- protected:
shared_ptr<leveldb::DB> db_;
shared_ptr<leveldb::WriteBatch> batch_;
};
class LmdbDatabase : public Database {
public:
LmdbDatabase()
- : dbi_(0) { }
+ : env_(NULL),
+ dbi_(0),
+ txn_(NULL) { }
~LmdbDatabase() { this->close(); }
void open(const string& filename, Mode mode);
void commit();
void close();
+ const_iterator begin() const;
+ const_iterator cbegin() const;
+ const_iterator end() const;
+ const_iterator cend() const;
+
protected:
class LmdbState : public Database::DatabaseState {
public:
void increment(shared_ptr<DatabaseState> state) const;
pair<string, string>& dereference(shared_ptr<DatabaseState> state) const;
- protected:
- const_iterator begin() const;
- const_iterator cbegin() const;
- const_iterator end() const;
- const_iterator cend() const;
-
- MDB_env *env_ = NULL;
+ MDB_env *env_;
MDB_dbi dbi_;
- MDB_txn *txn_ = NULL;
+ MDB_txn *txn_;
};
} // namespace caffe
return shared_ptr<Database>(new LmdbDatabase());
default:
LOG(FATAL) << "Unknown database type " << type;
+ return shared_ptr<Database>();
}
}
return DatabaseFactory(DataParameter_DB_LMDB);
} else {
LOG(FATAL) << "Unknown database type " << type;
+ return shared_ptr<Database>();
}
}
DataLayer<Dtype>::~DataLayer<Dtype>() {
this->JoinPrefetchThread();
// clean up the database resources
+
+ // Very important to invalidate iterators before closing the database.
+ // TODO(kmatzen): Figure out a better design to avoid this.
iter_ = database_->end();
database_->close();
}