Respond with QuotaExceededError when IndexedDB has no disk space on open.
[chromium-blink-merge.git] / content / browser / indexed_db / leveldb / leveldb_transaction.h
blobb7b5d64df1ef08c2dba5796342fbe80da548154a
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.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
8 #include <map>
9 #include <set>
10 #include <string>
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/leveldb_comparator.h"
16 #include "content/browser/indexed_db/leveldb/leveldb_database.h"
17 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h"
19 namespace content {
21 class LevelDBWriteBatch;
23 class CONTENT_EXPORT LevelDBTransaction
24 : public base::RefCounted<LevelDBTransaction> {
25 public:
26 explicit LevelDBTransaction(LevelDBDatabase* db);
28 void Put(const base::StringPiece& key, std::string* value);
29 void Remove(const base::StringPiece& key);
30 bool Get(const base::StringPiece& key, std::string* value, bool* found);
31 bool Commit();
32 void Rollback();
34 scoped_ptr<LevelDBIterator> CreateIterator();
36 private:
37 virtual ~LevelDBTransaction();
38 friend class base::RefCounted<LevelDBTransaction>;
40 struct Record {
41 Record();
42 ~Record();
43 std::string key;
44 std::string value;
45 bool deleted;
48 class Comparator {
49 public:
50 explicit Comparator(const LevelDBComparator* comparator)
51 : comparator_(comparator) {}
52 bool operator()(const base::StringPiece& a,
53 const base::StringPiece& b) const {
54 return comparator_->Compare(a, b) < 0;
57 private:
58 const LevelDBComparator* comparator_;
61 typedef std::map<base::StringPiece, Record*, Comparator> DataType;
63 class DataIterator : public LevelDBIterator {
64 public:
65 static scoped_ptr<DataIterator> Create(LevelDBTransaction* transaction);
66 virtual ~DataIterator();
68 virtual bool IsValid() const OVERRIDE;
69 virtual void SeekToLast() OVERRIDE;
70 virtual void Seek(const base::StringPiece& slice) OVERRIDE;
71 virtual void Next() OVERRIDE;
72 virtual void Prev() OVERRIDE;
73 virtual base::StringPiece Key() const OVERRIDE;
74 virtual base::StringPiece Value() const OVERRIDE;
75 bool IsDeleted() const;
77 private:
78 explicit DataIterator(LevelDBTransaction* transaction);
79 DataType* data_;
80 DataType::iterator iterator_;
83 class TransactionIterator : public LevelDBIterator {
84 public:
85 virtual ~TransactionIterator();
86 static scoped_ptr<TransactionIterator> Create(
87 scoped_refptr<LevelDBTransaction> transaction);
89 virtual bool IsValid() const OVERRIDE;
90 virtual void SeekToLast() OVERRIDE;
91 virtual void Seek(const base::StringPiece& target) 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 void DataChanged();
98 private:
99 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction);
100 void HandleConflictsAndDeletes();
101 void SetCurrentIteratorToSmallestKey();
102 void SetCurrentIteratorToLargestKey();
103 void RefreshDataIterator() const;
104 bool DataIteratorIsLower() const;
105 bool DataIteratorIsHigher() const;
107 scoped_refptr<LevelDBTransaction> transaction_;
108 const LevelDBComparator* comparator_;
109 mutable scoped_ptr<DataIterator> data_iterator_;
110 scoped_ptr<LevelDBIterator> db_iterator_;
111 LevelDBIterator* current_;
113 enum Direction {
114 FORWARD,
115 REVERSE
117 Direction direction_;
118 mutable bool data_changed_;
121 void Set(const base::StringPiece& key, std::string* value, bool deleted);
122 void Clear();
123 void RegisterIterator(TransactionIterator* iterator);
124 void UnregisterIterator(TransactionIterator* iterator);
125 void NotifyIterators();
127 LevelDBDatabase* db_;
128 const LevelDBSnapshot snapshot_;
129 const LevelDBComparator* comparator_;
130 Comparator data_comparator_;
131 DataType data_;
132 bool finished_;
133 std::set<TransactionIterator*> iterators_;
136 class LevelDBWriteOnlyTransaction {
137 public:
138 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db);
140 ~LevelDBWriteOnlyTransaction();
141 void Remove(const base::StringPiece& key);
142 bool Commit();
144 private:
145 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db);
147 LevelDBDatabase* db_;
148 scoped_ptr<LevelDBWriteBatch> write_batch_;
149 bool finished_;
152 } // namespace content
154 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_