leveldb_database.h revision 868fa2fe829687343ffae624259930155e16dbd8
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 "content/common/content_export.h"
15
16namespace leveldb {
17class Comparator;
18class DB;
19class Env;
20class Snapshot;
21}
22
23namespace content {
24
25class LevelDBComparator;
26class LevelDBDatabase;
27class LevelDBIterator;
28class LevelDBSlice;
29class LevelDBWriteBatch;
30
31class LevelDBSnapshot {
32 private:
33  friend class LevelDBDatabase;
34  friend class LevelDBTransaction;
35
36  explicit LevelDBSnapshot(LevelDBDatabase* db);
37  ~LevelDBSnapshot();
38
39  leveldb::DB* db_;
40  const leveldb::Snapshot* snapshot_;
41};
42
43class CONTENT_EXPORT LevelDBDatabase {
44 public:
45  static scoped_ptr<LevelDBDatabase> Open(const base::FilePath& file_name,
46                                          const LevelDBComparator* comparator,
47                                          bool* is_disk_full = 0);
48  static scoped_ptr<LevelDBDatabase> OpenInMemory(
49      const LevelDBComparator* comparator);
50  static bool Destroy(const base::FilePath& file_name);
51  virtual ~LevelDBDatabase();
52
53  bool Put(const LevelDBSlice& key, std::vector<char>* value);
54  bool Remove(const LevelDBSlice& key);
55  virtual bool Get(const LevelDBSlice& key,
56                   std::string* value,
57                   bool* found,
58                   const LevelDBSnapshot* = 0);
59  bool Write(const LevelDBWriteBatch& write_batch);
60  scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0);
61  const LevelDBComparator* Comparator() const;
62
63 protected:
64  LevelDBDatabase();
65
66 private:
67  friend class LevelDBSnapshot;
68
69  scoped_ptr<leveldb::Env> env_;
70  scoped_ptr<leveldb::Comparator> comparator_adapter_;
71  scoped_ptr<leveldb::DB> db_;
72  const LevelDBComparator* comparator_;
73};
74
75}  // namespace content
76
77#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
78