2 #ifndef CAFFE_UTIL_DB_LEVELDB_HPP 3 #define CAFFE_UTIL_DB_LEVELDB_HPP 7 #include "leveldb/db.h" 8 #include "leveldb/write_batch.h" 10 #include "caffe/util/db.hpp" 12 namespace caffe {
namespace db {
14 class LevelDBCursor :
public Cursor {
16 explicit LevelDBCursor(leveldb::Iterator* iter)
19 CHECK(iter_->status().ok()) << iter_->status().ToString();
21 ~LevelDBCursor() {
delete iter_; }
22 virtual void SeekToFirst() { iter_->SeekToFirst(); }
23 virtual void Next() { iter_->Next(); }
24 virtual string key() {
return iter_->key().ToString(); }
25 virtual string value() {
return iter_->value().ToString(); }
26 virtual bool valid() {
return iter_->Valid(); }
29 leveldb::Iterator* iter_;
32 class LevelDBTransaction :
public Transaction {
34 explicit LevelDBTransaction(leveldb::DB* db) : db_(db) { CHECK_NOTNULL(db_); }
35 virtual void Put(
const string& key,
const string& value) {
36 batch_.Put(key, value);
38 virtual void Commit() {
39 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch_);
40 CHECK(status.ok()) <<
"Failed to write batch to leveldb " 41 << std::endl << status.ToString();
46 leveldb::WriteBatch batch_;
48 DISABLE_COPY_AND_ASSIGN(LevelDBTransaction);
51 class LevelDB :
public DB {
53 LevelDB() : db_(NULL) { }
54 virtual ~LevelDB() { Close(); }
55 virtual void Open(
const string& source, Mode mode);
56 virtual void Close() {
62 virtual LevelDBCursor* NewCursor() {
63 return new LevelDBCursor(db_->NewIterator(leveldb::ReadOptions()));
65 virtual LevelDBTransaction* NewTransaction() {
66 return new LevelDBTransaction(db_);
77 #endif // CAFFE_UTIL_DB_LEVELDB_HPP A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14