forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_leveldb.hpp
More file actions
73 lines (59 loc) · 1.79 KB
/
db_leveldb.hpp
File metadata and controls
73 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef CAFFE_UTIL_DB_LEVELDB_HPP
#define CAFFE_UTIL_DB_LEVELDB_HPP
#include <string>
#include "leveldb/db.h"
#include "leveldb/write_batch.h"
#include "caffe/util/db.hpp"
namespace caffe { namespace db {
class LevelDBCursor : public Cursor {
public:
explicit LevelDBCursor(leveldb::Iterator* iter)
: iter_(iter) { SeekToFirst(); }
~LevelDBCursor() { delete iter_; }
virtual void SeekToFirst() { iter_->SeekToFirst(); }
virtual void Next() { iter_->Next(); }
virtual string key() { return iter_->key().ToString(); }
virtual string value() { return iter_->value().ToString(); }
virtual bool valid() { return iter_->Valid(); }
private:
leveldb::Iterator* iter_;
};
class LevelDBTransaction : public Transaction {
public:
explicit LevelDBTransaction(leveldb::DB* db) : db_(db) { CHECK_NOTNULL(db_); }
virtual void Put(const string& key, const string& value) {
batch_.Put(key, value);
}
virtual void Commit() {
leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch_);
CHECK(status.ok()) << "Failed to write batch to leveldb "
<< std::endl << status.ToString();
}
private:
leveldb::DB* db_;
leveldb::WriteBatch batch_;
DISABLE_COPY_AND_ASSIGN(LevelDBTransaction);
};
class LevelDB : public DB {
public:
LevelDB() : db_(NULL) { }
virtual ~LevelDB() { Close(); }
virtual void Open(const string& source, Mode mode);
virtual void Close() {
if (db_ != NULL) {
delete db_;
db_ = NULL;
}
}
virtual LevelDBCursor* NewCursor() {
return new LevelDBCursor(db_->NewIterator(leveldb::ReadOptions()));
}
virtual LevelDBTransaction* NewTransaction() {
return new LevelDBTransaction(db_);
}
private:
leveldb::DB* db_;
};
} // namespace db
} // namespace caffe
#endif // CAFFE_UTIL_DB_LEVELDB_HPP