blob: 321bb108616fb56b79474b1196fcf831619d87c8 (
plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
#include <map>
#include <string>
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"
namespace leveldb {
class DB;
class Iterator;
class Slice;
class Status;
class WriteBatch;
}
namespace sync_file_system {
namespace drive_backend {
class SliceComparator {
public:
bool operator()(const leveldb::Slice& a, const leveldb::Slice& b) const {
return a.compare(b) < 0;
}
};
// LevelDBWrapper class wraps leveldb::DB and leveldb::WriteBatch to provide
// transparent access to pre-commit data.
// Put() and Delete() update data on memory, and Get() can access to those data
// and also data on disk. Those data on memory are written down on disk when
// Commit() is called.
class LevelDBWrapper {
private:
enum Operation {
PUT,
DELETE,
};
typedef std::pair<Operation, std::string> Transaction;
typedef std::map<std::string, Transaction, SliceComparator>
PendingOperationMap;
public:
class Iterator {
public:
explicit Iterator(LevelDBWrapper* db);
~Iterator();
bool Valid();
void Seek(const std::string& target);
void Next();
leveldb::Slice key();
leveldb::Slice value();
private:
// Advances internal iterators to be valid.
void AdvanceIterators();
LevelDBWrapper* db_; // do not own
scoped_ptr<leveldb::Iterator> db_iterator_;
PendingOperationMap::iterator map_iterator_;
DISALLOW_COPY_AND_ASSIGN(Iterator);
};
explicit LevelDBWrapper(scoped_ptr<leveldb::DB> db);
~LevelDBWrapper();
// Wrapping methods of leveldb::WriteBatch
void Put(const std::string& key, const std::string& value);
void Delete(const std::string& key);
// Wrapping methods of leveldb::DB
leveldb::Status Get(const std::string& key, std::string* value);
scoped_ptr<Iterator> NewIterator();
// Commits pending transactions to |db_| and clears cached transactions.
// Returns true if the commitment succeeds.
leveldb::Status Commit();
// Clears pending transactions.
void Clear();
leveldb::DB* GetLevelDBForTesting();
private:
scoped_ptr<leveldb::DB> db_;
PendingOperationMap pending_;
DISALLOW_COPY_AND_ASSIGN(LevelDBWrapper);
};
} // namespace drive_backend
} // namespace sync_file_system
#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
|