leveldb_database.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/status.h"
16
17namespace leveldb {
18class Comparator;
19class DB;
20class Env;
21class Snapshot;
22}
23
24namespace content {
25
26class LevelDBComparator;
27class LevelDBDatabase;
28class LevelDBIterator;
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 leveldb::Status Open(const base::FilePath& file_name,
46                              const LevelDBComparator* comparator,
47                              scoped_ptr<LevelDBDatabase>* db,
48                              bool* is_disk_full = 0);
49  static scoped_ptr<LevelDBDatabase> OpenInMemory(
50      const LevelDBComparator* comparator);
51  static bool Destroy(const base::FilePath& file_name);
52  virtual ~LevelDBDatabase();
53
54  bool Put(const base::StringPiece& key, std::string* value);
55  bool Remove(const base::StringPiece& key);
56  virtual bool Get(const base::StringPiece& key,
57                   std::string* value,
58                   bool* found,
59                   const LevelDBSnapshot* = 0);
60  bool Write(const LevelDBWriteBatch& write_batch);
61  scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0);
62  const LevelDBComparator* Comparator() const;
63
64 protected:
65  LevelDBDatabase();
66
67 private:
68  friend class LevelDBSnapshot;
69
70  scoped_ptr<leveldb::Env> env_;
71  scoped_ptr<leveldb::Comparator> comparator_adapter_;
72  scoped_ptr<leveldb::DB> db_;
73  const LevelDBComparator* comparator_;
74};
75
76}  // namespace content
77
78#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
79