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_TRANSACTION_H_
      6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/strings/string_piece.h"
     15 #include "content/browser/indexed_db/leveldb/avltree.h"
     16 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h"
     17 #include "content/browser/indexed_db/leveldb/leveldb_database.h"
     18 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h"
     19 
     20 namespace content {
     21 
     22 class LevelDBWriteBatch;
     23 
     24 class CONTENT_EXPORT LevelDBTransaction
     25     : public base::RefCounted<LevelDBTransaction> {
     26  public:
     27   explicit LevelDBTransaction(LevelDBDatabase* db);
     28 
     29   void Put(const base::StringPiece& key, std::string* value);
     30   void Remove(const base::StringPiece& key);
     31   bool Get(const base::StringPiece& key, std::string* value, bool* found);
     32   bool Commit();
     33   void Rollback();
     34 
     35   scoped_ptr<LevelDBIterator> CreateIterator();
     36 
     37  private:
     38   virtual ~LevelDBTransaction();
     39   friend class base::RefCounted<LevelDBTransaction>;
     40 
     41   struct AVLTreeNode {
     42     AVLTreeNode();
     43     ~AVLTreeNode();
     44     std::string key;
     45     std::string value;
     46     bool deleted;
     47 
     48     AVLTreeNode* less;
     49     AVLTreeNode* greater;
     50     int balance_factor;
     51     DISALLOW_COPY_AND_ASSIGN(AVLTreeNode);
     52   };
     53 
     54   struct AVLTreeAbstractor {
     55     typedef AVLTreeNode* handle;
     56     typedef size_t size;
     57     typedef base::StringPiece key;
     58 
     59     handle GetLess(handle h) { return h->less; }
     60     void SetLess(handle h, handle less) { h->less = less; }
     61     handle GetGreater(handle h) { return h->greater; }
     62     void SetGreater(handle h, handle greater) { h->greater = greater; }
     63 
     64     int GetBalanceFactor(handle h) { return h->balance_factor; }
     65     void SetBalanceFactor(handle h, int bf) { h->balance_factor = bf; }
     66 
     67     int CompareKeyKey(const key& ka, const key& kb) {
     68       return comparator_->Compare(ka, kb);
     69     }
     70     int CompareKeyNode(const key& k, handle h) {
     71       return CompareKeyKey(k, h->key);
     72     }
     73     int CompareNodeNode(handle ha, handle hb) {
     74       return CompareKeyKey(ha->key, hb->key);
     75     }
     76 
     77     static handle Null() { return 0; }
     78 
     79     const LevelDBComparator* comparator_;
     80   };
     81 
     82   typedef AVLTree<AVLTreeAbstractor> TreeType;
     83 
     84   class TreeIterator : public LevelDBIterator {
     85    public:
     86     static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction);
     87     virtual ~TreeIterator();
     88 
     89     virtual bool IsValid() const OVERRIDE;
     90     virtual void SeekToLast() OVERRIDE;
     91     virtual void Seek(const base::StringPiece& slice) OVERRIDE;
     92     virtual void Next() OVERRIDE;
     93     virtual void Prev() OVERRIDE;
     94     virtual base::StringPiece Key() const OVERRIDE;
     95     virtual base::StringPiece Value() const OVERRIDE;
     96     bool IsDeleted() const;
     97     void Reset();
     98 
     99    private:
    100     explicit TreeIterator(LevelDBTransaction* transaction);
    101     mutable TreeType::Iterator iterator_;  // Dereferencing this is non-const.
    102     TreeType* tree_;
    103     LevelDBTransaction* transaction_;
    104     std::string key_;
    105   };
    106 
    107   class TransactionIterator : public LevelDBIterator {
    108    public:
    109     virtual ~TransactionIterator();
    110     static scoped_ptr<TransactionIterator> Create(
    111         scoped_refptr<LevelDBTransaction> transaction);
    112 
    113     virtual bool IsValid() const OVERRIDE;
    114     virtual void SeekToLast() OVERRIDE;
    115     virtual void Seek(const base::StringPiece& target) OVERRIDE;
    116     virtual void Next() OVERRIDE;
    117     virtual void Prev() OVERRIDE;
    118     virtual base::StringPiece Key() const OVERRIDE;
    119     virtual base::StringPiece Value() const OVERRIDE;
    120     void TreeChanged();
    121 
    122    private:
    123     explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction);
    124     void HandleConflictsAndDeletes();
    125     void SetCurrentIteratorToSmallestKey();
    126     void SetCurrentIteratorToLargestKey();
    127     void RefreshTreeIterator() const;
    128     bool TreeIteratorIsLower() const;
    129     bool TreeIteratorIsHigher() const;
    130 
    131     scoped_refptr<LevelDBTransaction> transaction_;
    132     const LevelDBComparator* comparator_;
    133     mutable scoped_ptr<TreeIterator> tree_iterator_;
    134     scoped_ptr<LevelDBIterator> db_iterator_;
    135     LevelDBIterator* current_;
    136 
    137     enum Direction {
    138       FORWARD,
    139       REVERSE
    140     };
    141     Direction direction_;
    142     mutable bool tree_changed_;
    143   };
    144 
    145   void Set(const base::StringPiece& key, std::string* value, bool deleted);
    146   void ClearTree();
    147   void RegisterIterator(TransactionIterator* iterator);
    148   void UnregisterIterator(TransactionIterator* iterator);
    149   void NotifyIteratorsOfTreeChange();
    150 
    151   LevelDBDatabase* db_;
    152   const LevelDBSnapshot snapshot_;
    153   const LevelDBComparator* comparator_;
    154   TreeType tree_;
    155   bool finished_;
    156   std::set<TransactionIterator*> iterators_;
    157 };
    158 
    159 class LevelDBWriteOnlyTransaction {
    160  public:
    161   static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db);
    162 
    163   ~LevelDBWriteOnlyTransaction();
    164   void Remove(const base::StringPiece& key);
    165   bool Commit();
    166 
    167  private:
    168   explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db);
    169 
    170   LevelDBDatabase* db_;
    171   scoped_ptr<LevelDBWriteBatch> write_batch_;
    172   bool finished_;
    173 };
    174 
    175 }  // namespace content
    176 
    177 #endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
    178