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. 4 5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_ 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/macros.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "third_party/leveldatabase/src/include/leveldb/slice.h" 14 15 namespace leveldb { 16 class DB; 17 class Iterator; 18 class Slice; 19 class Status; 20 class WriteBatch; 21 } 22 23 namespace sync_file_system { 24 namespace drive_backend { 25 26 class SliceComparator { 27 public: 28 bool operator()(const leveldb::Slice& a, const leveldb::Slice& b) const { 29 return a.compare(b) < 0; 30 } 31 }; 32 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, 43 }; 44 typedef std::pair<Operation, std::string> Transaction; 45 typedef std::map<std::string, Transaction, SliceComparator> 46 PendingOperationMap; 47 48 public: 49 class Iterator { 50 public: 51 explicit Iterator(LevelDBWrapper* db); 52 ~Iterator(); 53 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(); 62 63 private: 64 // Advances internal iterators to be valid. 65 void AdvanceIterators(); 66 67 LevelDBWrapper* db_; // do not own 68 scoped_ptr<leveldb::Iterator> db_iterator_; 69 PendingOperationMap::iterator map_iterator_; 70 71 DISALLOW_COPY_AND_ASSIGN(Iterator); 72 }; 73 74 explicit LevelDBWrapper(scoped_ptr<leveldb::DB> db); 75 ~LevelDBWrapper(); 76 77 // Wrapping methods of leveldb::WriteBatch 78 void Put(const std::string& key, const std::string& value); 79 void Delete(const std::string& key); 80 81 // Wrapping methods of leveldb::DB 82 leveldb::Status Get(const std::string& key, std::string* value); 83 scoped_ptr<Iterator> NewIterator(); 84 85 // Commits pending transactions to |db_| and clears cached transactions. 86 // Returns true if the commitment succeeds. 87 leveldb::Status Commit(); 88 89 // Clears pending transactions. 90 void Clear(); 91 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_; } 97 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(); 102 103 private: 104 scoped_ptr<leveldb::DB> db_; 105 106 PendingOperationMap pending_; 107 int64 num_puts_; 108 int64 num_deletes_; 109 110 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapper); 111 }; 112 113 } // namespace drive_backend 114 } // namespace sync_file_system 115 116 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_LEVELDB_WRAPPER_H_ 117