leveldb_database.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright (c) 2013 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 CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ 6#define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ 7 8#include <string> 9 10#include "base/files/file_path.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/strings/string16.h" 13#include "base/strings/string_piece.h" 14#include "content/common/content_export.h" 15#include "third_party/leveldatabase/src/include/leveldb/comparator.h" 16#include "third_party/leveldatabase/src/include/leveldb/status.h" 17 18namespace leveldb { 19class Comparator; 20class DB; 21class FilterPolicy; 22class Env; 23class Snapshot; 24} 25 26namespace content { 27 28class LevelDBComparator; 29class LevelDBDatabase; 30class LevelDBIterator; 31class LevelDBWriteBatch; 32 33class LevelDBSnapshot { 34 private: 35 friend class LevelDBDatabase; 36 friend class LevelDBTransaction; 37 38 explicit LevelDBSnapshot(LevelDBDatabase* db); 39 ~LevelDBSnapshot(); 40 41 leveldb::DB* db_; 42 const leveldb::Snapshot* snapshot_; 43 44 DISALLOW_COPY_AND_ASSIGN(LevelDBSnapshot); 45}; 46 47class CONTENT_EXPORT LevelDBLock { 48 public: 49 LevelDBLock() {} 50 virtual ~LevelDBLock() {} 51 52 private: 53 DISALLOW_COPY_AND_ASSIGN(LevelDBLock); 54}; 55 56class CONTENT_EXPORT LevelDBDatabase { 57 public: 58 class ComparatorAdapter : public leveldb::Comparator { 59 public: 60 explicit ComparatorAdapter(const LevelDBComparator* comparator); 61 62 virtual int Compare(const leveldb::Slice& a, 63 const leveldb::Slice& b) const OVERRIDE; 64 65 virtual const char* Name() const OVERRIDE; 66 67 virtual void FindShortestSeparator(std::string* start, 68 const leveldb::Slice& limit) const 69 OVERRIDE; 70 virtual void FindShortSuccessor(std::string* key) const OVERRIDE; 71 72 private: 73 const LevelDBComparator* comparator_; 74 }; 75 76 static leveldb::Status Open(const base::FilePath& file_name, 77 const LevelDBComparator* comparator, 78 scoped_ptr<LevelDBDatabase>* db, 79 bool* is_disk_full = 0); 80 static scoped_ptr<LevelDBDatabase> OpenInMemory( 81 const LevelDBComparator* comparator); 82 static leveldb::Status Destroy(const base::FilePath& file_name); 83 static scoped_ptr<LevelDBLock> LockForTesting( 84 const base::FilePath& file_name); 85 virtual ~LevelDBDatabase(); 86 87 leveldb::Status Put(const base::StringPiece& key, std::string* value); 88 leveldb::Status Remove(const base::StringPiece& key); 89 virtual leveldb::Status Get(const base::StringPiece& key, 90 std::string* value, 91 bool* found, 92 const LevelDBSnapshot* = 0); 93 leveldb::Status Write(const LevelDBWriteBatch& write_batch); 94 scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0); 95 const LevelDBComparator* Comparator() const; 96 void Compact(const base::StringPiece& start, const base::StringPiece& stop); 97 void CompactAll(); 98 99 protected: 100 LevelDBDatabase(); 101 102 private: 103 friend class LevelDBSnapshot; 104 105 scoped_ptr<leveldb::Env> env_; 106 scoped_ptr<leveldb::Comparator> comparator_adapter_; 107 scoped_ptr<leveldb::DB> db_; 108 scoped_ptr<const leveldb::FilterPolicy> filter_policy_; 109 const LevelDBComparator* comparator_; 110}; 111 112} // namespace content 113 114#endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ 115