leveldb_database.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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 Env;
22class Snapshot;
23}
24
25namespace content {
26
27class LevelDBComparator;
28class LevelDBDatabase;
29class LevelDBIterator;
30class LevelDBWriteBatch;
31
32class LevelDBSnapshot {
33 private:
34  friend class LevelDBDatabase;
35  friend class LevelDBTransaction;
36
37  explicit LevelDBSnapshot(LevelDBDatabase* db);
38  ~LevelDBSnapshot();
39
40  leveldb::DB* db_;
41  const leveldb::Snapshot* snapshot_;
42};
43
44class CONTENT_EXPORT LevelDBLock {
45public:
46  virtual ~LevelDBLock() {}
47};
48
49class CONTENT_EXPORT LevelDBDatabase {
50 public:
51  class ComparatorAdapter : public leveldb::Comparator {
52   public:
53    explicit ComparatorAdapter(const LevelDBComparator* comparator);
54
55    virtual int Compare(const leveldb::Slice& a,
56                        const leveldb::Slice& b) const OVERRIDE;
57
58    virtual const char* Name() const OVERRIDE;
59
60    virtual void FindShortestSeparator(std::string* start,
61                                       const leveldb::Slice& limit) const
62        OVERRIDE;
63    virtual void FindShortSuccessor(std::string* key) const OVERRIDE;
64
65   private:
66    const LevelDBComparator* comparator_;
67  };
68
69  static leveldb::Status Open(const base::FilePath& file_name,
70                              const LevelDBComparator* comparator,
71                              scoped_ptr<LevelDBDatabase>* db,
72                              bool* is_disk_full = 0);
73  static scoped_ptr<LevelDBDatabase> OpenInMemory(
74      const LevelDBComparator* comparator);
75  static leveldb::Status Destroy(const base::FilePath& file_name);
76  static scoped_ptr<LevelDBLock> LockForTesting(
77      const base::FilePath& file_name);
78  virtual ~LevelDBDatabase();
79
80  leveldb::Status Put(const base::StringPiece& key, std::string* value);
81  leveldb::Status Remove(const base::StringPiece& key);
82  virtual leveldb::Status Get(const base::StringPiece& key,
83                              std::string* value,
84                              bool* found,
85                              const LevelDBSnapshot* = 0);
86  leveldb::Status Write(const LevelDBWriteBatch& write_batch);
87  scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0);
88  const LevelDBComparator* Comparator() const;
89  void Compact(const base::StringPiece& start, const base::StringPiece& stop);
90
91 protected:
92  LevelDBDatabase();
93
94 private:
95  friend class LevelDBSnapshot;
96
97  scoped_ptr<leveldb::Env> env_;
98  scoped_ptr<leveldb::Comparator> comparator_adapter_;
99  scoped_ptr<leveldb::DB> db_;
100  const LevelDBComparator* comparator_;
101};
102
103}  // namespace content
104
105#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
106