1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
23 namespace sync_file_system
{
24 namespace drive_backend
{
26 class SliceComparator
{
28 bool operator()(const leveldb::Slice
& a
, const leveldb::Slice
& b
) const {
29 return a
.compare(b
) < 0;
33 // LevelDBWrapper class wraps leveldb::DB and leveldb::WriteBatch to provide
34 // transparent access to pre-commit data.
35 // Put() and Delete() update data on memory, and Get() can access to those data
36 // and also data on disk. Those data on memory are written down on disk when
37 // Commit() is called.
38 class LevelDBWrapper
{
44 typedef std::pair
<Operation
, std::string
> Transaction
;
45 typedef std::map
<std::string
, Transaction
, SliceComparator
>
51 explicit Iterator(LevelDBWrapper
* db
);
55 void Seek(const std::string
& target
);
61 leveldb::Slice
value();
64 // Advances internal iterators to be valid.
65 void AdvanceIterators();
67 LevelDBWrapper
* db_
; // do not own
68 scoped_ptr
<leveldb::Iterator
> db_iterator_
;
69 PendingOperationMap::iterator map_iterator_
;
71 DISALLOW_COPY_AND_ASSIGN(Iterator
);
74 explicit LevelDBWrapper(scoped_ptr
<leveldb::DB
> db
);
77 // Wrapping methods of leveldb::WriteBatch
78 void Put(const std::string
& key
, const std::string
& value
);
79 void Delete(const std::string
& key
);
81 // Wrapping methods of leveldb::DB
82 leveldb::Status
Get(const std::string
& key
, std::string
* value
);
83 scoped_ptr
<Iterator
> NewIterator();
85 // Commits pending transactions to |db_| and clears cached transactions.
86 // Returns true if the commitment succeeds.
87 leveldb::Status
Commit();
89 // Clears pending transactions.
92 // Returns the number of pending PUT/DELETE operations.
93 // Each counter counts operations independently, so operations on a key
94 // may be counted more than once.
95 int64
num_puts() { return num_puts_
; }
96 int64
num_deletes() { return num_deletes_
; }
98 // TODO(peria): Rename this method to GetLevelDBForTesting, after removing
99 // usages of drive_backend::MigrateDatabaseFromVxToVy() under
100 // drive_backend_v1/.
101 leveldb::DB
* GetLevelDB();
104 scoped_ptr
<leveldb::DB
> db_
;
106 PendingOperationMap pending_
;
110 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapper
);
113 } // namespace drive_backend
114 } // namespace sync_file_system
116 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_