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