1 // Copyright (c) 2012 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_VALUE_STORE_LEVELDB_VALUE_STORE_H_ 6 #define CHROME_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "chrome/browser/value_store/value_store.h" 15 #include "third_party/leveldatabase/src/include/leveldb/db.h" 16 17 // Value store area, backed by a leveldb database. 18 // All methods must be run on the FILE thread. 19 class LeveldbValueStore : public ValueStore { 20 public: 21 // Creates a database bound to |path|. The underlying database won't be 22 // opened (i.e. may not be created) until one of the get/set/etc methods are 23 // called - this is because opening the database may fail, and extensions 24 // need to be notified of that, but we don't want to permanently give up. 25 // 26 // Must be created on the FILE thread. 27 explicit LeveldbValueStore(const base::FilePath& path); 28 29 // Must be deleted on the FILE thread. 30 virtual ~LeveldbValueStore(); 31 32 // ValueStore implementation. 33 virtual size_t GetBytesInUse(const std::string& key) OVERRIDE; 34 virtual size_t GetBytesInUse(const std::vector<std::string>& keys) OVERRIDE; 35 virtual size_t GetBytesInUse() OVERRIDE; 36 virtual ReadResult Get(const std::string& key) OVERRIDE; 37 virtual ReadResult Get(const std::vector<std::string>& keys) OVERRIDE; 38 virtual ReadResult Get() OVERRIDE; 39 virtual WriteResult Set( 40 WriteOptions options, 41 const std::string& key, 42 const Value& value) OVERRIDE; 43 virtual WriteResult Set( 44 WriteOptions options, const DictionaryValue& values) OVERRIDE; 45 virtual WriteResult Remove(const std::string& key) OVERRIDE; 46 virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE; 47 virtual WriteResult Clear() OVERRIDE; 48 49 private: 50 // Tries to open the database if it hasn't been opened already. Returns the 51 // error message on failure, or "" on success (guaranteeding that |db_| is 52 // non-NULL), 53 std::string EnsureDbIsOpen(); 54 55 // Reads a setting from the database. Returns the error message on failure, 56 // or "" on success in which case |setting| will be reset to the Value read 57 // from the database. This value may be NULL. 58 std::string ReadFromDb( 59 leveldb::ReadOptions options, 60 const std::string& key, 61 // Will be reset() with the result, if any. 62 scoped_ptr<Value>* setting); 63 64 // Adds a setting to a WriteBatch, and logs the change in |changes|. For use 65 // with WriteToDb. Returns the error message on failure, or "" on success. 66 std::string AddToBatch( 67 ValueStore::WriteOptions options, 68 const std::string& key, 69 const base::Value& value, 70 leveldb::WriteBatch* batch, 71 ValueStoreChangeList* changes); 72 73 // Commits the changes in |batch| to the database, returning the error message 74 // on failure or "" on success. 75 std::string WriteToDb(leveldb::WriteBatch* batch); 76 77 // Returns whether the database is empty. 78 bool IsEmpty(); 79 80 // The location of the leveldb backend. 81 const base::FilePath db_path_; 82 83 // leveldb backend. 84 scoped_ptr<leveldb::DB> db_; 85 86 DISALLOW_COPY_AND_ASSIGN(LeveldbValueStore); 87 }; 88 89 #endif // CHROME_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_ 90