Home | History | Annotate | Download | only in leveldb
      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 
     17 namespace leveldb {
     18 class Comparator;
     19 class DB;
     20 class Env;
     21 class Snapshot;
     22 }
     23 
     24 namespace content {
     25 
     26 class LevelDBComparator;
     27 class LevelDBDatabase;
     28 class LevelDBIterator;
     29 class LevelDBWriteBatch;
     30 
     31 class 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 
     43 class CONTENT_EXPORT LevelDBLock {
     44 public:
     45   virtual ~LevelDBLock() {}
     46 };
     47 
     48 class CONTENT_EXPORT LevelDBDatabase {
     49  public:
     50   static leveldb::Status Open(const base::FilePath& file_name,
     51                               const LevelDBComparator* comparator,
     52                               scoped_ptr<LevelDBDatabase>* db,
     53                               bool* is_disk_full = 0);
     54   static scoped_ptr<LevelDBDatabase> OpenInMemory(
     55       const LevelDBComparator* comparator);
     56   static bool Destroy(const base::FilePath& file_name);
     57   static scoped_ptr<LevelDBLock> LockForTesting(
     58       const base::FilePath& file_name);
     59   virtual ~LevelDBDatabase();
     60 
     61   bool Put(const base::StringPiece& key, std::string* value);
     62   bool Remove(const base::StringPiece& key);
     63   virtual bool Get(const base::StringPiece& key,
     64                    std::string* value,
     65                    bool* found,
     66                    const LevelDBSnapshot* = 0);
     67   bool Write(const LevelDBWriteBatch& write_batch);
     68   scoped_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0);
     69   const LevelDBComparator* Comparator() const;
     70 
     71  protected:
     72   LevelDBDatabase();
     73 
     74  private:
     75   friend class LevelDBSnapshot;
     76 
     77   scoped_ptr<leveldb::Env> env_;
     78   scoped_ptr<leveldb::Comparator> comparator_adapter_;
     79   scoped_ptr<leveldb::DB> db_;
     80   const LevelDBComparator* comparator_;
     81 };
     82 
     83 }  // namespace content
     84 
     85 #endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
     86