Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / leveldb_wrapper.h
blob7b6c51e11f6c29b74fc62d31f530c2791d28ca2d
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_
8 #include <map>
9 #include <string>
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
15 namespace leveldb {
16 class DB;
17 class Iterator;
18 class Slice;
19 class Status;
20 class WriteBatch;
23 namespace sync_file_system {
24 namespace drive_backend {
26 class SliceComparator {
27 public:
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 {
39 private:
40 enum Operation {
41 PUT_OPERATION,
42 DELETE_OPERATION,
44 typedef std::pair<Operation, std::string> Transaction;
45 typedef std::map<std::string, Transaction, SliceComparator>
46 PendingOperationMap;
48 public:
49 class Iterator {
50 public:
51 explicit Iterator(LevelDBWrapper* db);
52 ~Iterator();
54 bool Valid();
55 void Seek(const std::string& target);
56 void SeekToFirst();
57 void SeekToLast();
58 void Next();
59 void Delete();
60 leveldb::Slice key();
61 leveldb::Slice value();
63 private:
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);
75 ~LevelDBWrapper();
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.
90 void Clear();
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();
103 private:
104 scoped_ptr<leveldb::DB> db_;
106 PendingOperationMap pending_;
107 int64 num_puts_;
108 int64 num_deletes_;
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_