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