leveldb_database.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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#include <vector>
10
11#include "base/files/file_path.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/strings/string16.h"
14#include "base/strings/string_piece.h"
15#include "content/common/content_export.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 LevelDBDatabase {
45 public:
46  static leveldb::Status Open(const base::FilePath& file_name,
47                              const LevelDBComparator* comparator,
48                              scoped_ptr<LevelDBDatabase>* db,
49                              bool* is_disk_full = 0);
50  static scoped_ptr<LevelDBDatabase> OpenInMemory(
51      const LevelDBComparator* comparator);
52  static bool Destroy(const base::FilePath& file_name);
53  virtual ~LevelDBDatabase();
54
55  bool Put(const base::StringPiece& key, std::string* value);
56  bool Remove(const base::StringPiece& key);
57  virtual bool Get(const base::StringPiece& key,
58                   std::string* value,
59                   bool* found,
60                   const LevelDBSnapshot* = 0);
61  bool Write(const LevelDBWriteBatch& write_batch);
62  scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0);
63  const LevelDBComparator* Comparator() const;
64
65 protected:
66  LevelDBDatabase();
67
68 private:
69  friend class LevelDBSnapshot;
70
71  scoped_ptr<leveldb::Env> env_;
72  scoped_ptr<leveldb::Comparator> comparator_adapter_;
73  scoped_ptr<leveldb::DB> db_;
74  const LevelDBComparator* comparator_;
75};
76
77}  // namespace content
78
79#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
80