Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: content/browser/indexed_db/indexed_db_backing_store.h

Issue 15564008: Migrate the IndexedDB backend from Blink to Chromium (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Accessor naming, use LevelDBSlice ctor explicitly Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_INDEXED_DB_BACKING_STORE_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_
7
8 #include <vector>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/browser/indexed_db/indexed_db.h"
14 #include "content/browser/indexed_db/indexed_db_metadata.h"
15 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h"
16 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h"
17 #include "content/common/content_export.h"
18 #include "content/common/indexed_db/indexed_db_key.h"
19 #include "content/common/indexed_db/indexed_db_key_path.h"
20 #include "content/common/indexed_db/indexed_db_key_range.h"
21
22 namespace content {
23
24 class LevelDBComparator;
25 class LevelDBDatabase;
26
27 class LevelDBFactory {
28 public:
29 virtual scoped_ptr<LevelDBDatabase> OpenLevelDB(const string16& file_name,
30 const LevelDBComparator*) = 0;
31 virtual bool DestroyLevelDB(const string16& file_name) = 0;
32 };
33
34 class CONTENT_EXPORT IndexedDBBackingStore
35 : public base::RefCounted<IndexedDBBackingStore> {
36 public:
37 class Transaction;
38
39 static scoped_refptr<IndexedDBBackingStore> Open(
40 const string16& database_identifier,
41 const string16& path_base,
42 const string16& file_identifier);
43 static scoped_refptr<IndexedDBBackingStore> Open(
44 const string16& database_identifier,
45 const string16& path_base,
46 const string16& file_identifier,
47 LevelDBFactory*);
48 static scoped_refptr<IndexedDBBackingStore> OpenInMemory(
49 const string16& identifier);
50 static scoped_refptr<IndexedDBBackingStore> OpenInMemory(
51 const string16& identifier,
52 LevelDBFactory*);
53 base::WeakPtr<IndexedDBBackingStore> GetWeakPtr() {
54 return weak_factory_.GetWeakPtr();
55 }
56
57 virtual std::vector<string16> GetDatabaseNames();
58 virtual bool GetIDBDatabaseMetaData(const string16& name,
59 IndexedDBDatabaseMetadata*,
60 bool& success) WARN_UNUSED_RESULT;
61 virtual bool CreateIDBDatabaseMetaData(const string16& name,
62 const string16& version,
63 int64_t int_version,
64 int64_t& row_id);
65 virtual bool UpdateIDBDatabaseMetaData(IndexedDBBackingStore::Transaction*,
66 int64_t row_id,
67 const string16& version);
68 virtual bool UpdateIDBDatabaseIntVersion(IndexedDBBackingStore::Transaction*,
69 int64_t row_id,
70 int64_t int_version);
71 virtual bool DeleteDatabase(const string16& name);
72
73 bool GetObjectStores(int64_t database_id,
74 IndexedDBDatabaseMetadata::ObjectStoreMap*)
75 WARN_UNUSED_RESULT;
76 virtual bool CreateObjectStore(IndexedDBBackingStore::Transaction*,
77 int64_t database_id,
78 int64_t object_store_id,
79 const string16& name,
80 const IndexedDBKeyPath&,
81 bool auto_increment);
82 virtual bool DeleteObjectStore(IndexedDBBackingStore::Transaction*,
83 int64_t database_id,
84 int64_t object_store_id) WARN_UNUSED_RESULT;
85
86 class RecordIdentifier {
87 DISALLOW_COPY_AND_ASSIGN(RecordIdentifier);
jamesr 2013/05/21 23:56:06 in chromium, this is nearly always the last thing
jsbell 2013/05/22 17:54:44 Done.
88
89 public:
90 RecordIdentifier(const std::vector<char>& primary_key, int64_t version);
91 RecordIdentifier();
92 ~RecordIdentifier();
93
94 const std::vector<char>& primary_key() const { return primary_key_; }
95 int64_t version() const { return version_; }
96 void Reset(const std::vector<char>& primary_key, int64_t version) {
97 primary_key_ = primary_key;
98 version_ = version;
99 }
100
101 private:
102 std::vector<char> primary_key_; // TODO: Make it more clear that this is
jamesr 2013/05/21 23:56:06 TODOs in chromium should have a name in parens, e.
jsbell 2013/05/22 17:54:44 Done.
103 // the *encoded* version of the key.
104 int64_t version_;
105 };
106
107 virtual bool GetRecord(IndexedDBBackingStore::Transaction*,
108 int64_t database_id,
109 int64_t object_store_id,
110 const IndexedDBKey&,
111 std::vector<char>& record) WARN_UNUSED_RESULT;
112 virtual bool PutRecord(IndexedDBBackingStore::Transaction*,
113 int64_t database_id,
114 int64_t object_store_id,
115 const IndexedDBKey&,
116 const std::vector<char>& value,
117 RecordIdentifier*) WARN_UNUSED_RESULT;
118 virtual bool ClearObjectStore(IndexedDBBackingStore::Transaction*,
119 int64_t database_id,
120 int64_t object_store_id) WARN_UNUSED_RESULT;
121 virtual bool DeleteRecord(IndexedDBBackingStore::Transaction*,
122 int64_t database_id,
123 int64_t object_store_id,
124 const RecordIdentifier&) WARN_UNUSED_RESULT;
125 virtual bool GetKeyGeneratorCurrentNumber(IndexedDBBackingStore::Transaction*,
126 int64_t database_id,
127 int64_t object_store_id,
128 int64_t& current_number)
129 WARN_UNUSED_RESULT;
130 virtual bool MaybeUpdateKeyGeneratorCurrentNumber(
131 IndexedDBBackingStore::Transaction*,
132 int64_t database_id,
133 int64_t object_store_id,
134 int64_t new_state,
135 bool check_current) WARN_UNUSED_RESULT;
136 virtual bool KeyExistsInObjectStore(IndexedDBBackingStore::Transaction*,
137 int64_t database_id,
138 int64_t object_store_id,
139 const IndexedDBKey&,
140 RecordIdentifier* found_record_identifier,
141 bool& found) WARN_UNUSED_RESULT;
142
143 virtual bool CreateIndex(IndexedDBBackingStore::Transaction*,
144 int64_t database_id,
145 int64_t object_store_id,
146 int64_t index_id,
147 const string16& name,
148 const IndexedDBKeyPath&,
149 bool is_unique,
150 bool is_multi_entry) WARN_UNUSED_RESULT;
151 virtual bool DeleteIndex(IndexedDBBackingStore::Transaction*,
152 int64_t database_id,
153 int64_t object_store_id,
154 int64_t index_id) WARN_UNUSED_RESULT;
155 virtual bool PutIndexDataForRecord(IndexedDBBackingStore::Transaction*,
156 int64_t database_id,
157 int64_t object_store_id,
158 int64_t index_id,
159 const IndexedDBKey&,
160 const RecordIdentifier&)
161 WARN_UNUSED_RESULT;
162 virtual bool GetPrimaryKeyViaIndex(IndexedDBBackingStore::Transaction*,
163 int64_t database_id,
164 int64_t object_store_id,
165 int64_t index_id,
166 const IndexedDBKey&,
167 scoped_ptr<IndexedDBKey>* primary_key)
168 WARN_UNUSED_RESULT;
169 virtual bool KeyExistsInIndex(IndexedDBBackingStore::Transaction*,
170 int64_t database_id,
171 int64_t object_store_id,
172 int64_t index_id,
173 const IndexedDBKey& index_key,
174 scoped_ptr<IndexedDBKey>* found_primary_key,
175 bool& exists) WARN_UNUSED_RESULT;
176
177 class Cursor {
178 public:
179 virtual ~Cursor();
180
181 enum IteratorState {
182 Ready = 0,
jamesr 2013/05/21 23:56:06 enums should SHOUT
jsbell 2013/05/22 17:54:44 Done.
183 Seek
184 };
185
186 struct CursorOptions {
187 CursorOptions();
188 ~CursorOptions();
189 int64_t database_id;
190 int64_t object_store_id;
191 int64_t index_id;
192 std::vector<char> low_key;
193 bool low_open;
194 std::vector<char> high_key;
195 bool high_open;
196 bool forward;
197 bool unique;
198 };
199
200 const IndexedDBKey& key() const { return *current_key_; }
201 bool ContinueFunction(const IndexedDBKey* = 0, IteratorState = Seek);
202 bool Advance(unsigned long);
203 bool FirstSeek();
204
205 virtual Cursor* Clone() = 0;
206 virtual const IndexedDBKey& primary_key() const;
207 virtual std::vector<char>* Value() = 0;
208 virtual const RecordIdentifier& record_identifier() const;
209 virtual bool LoadCurrentRow() = 0;
210
211 protected:
212 Cursor(LevelDBTransaction* transaction,
213 const CursorOptions& cursor_options);
214 explicit Cursor(const IndexedDBBackingStore::Cursor* other);
215
216 virtual std::vector<char> EncodeKey(const IndexedDBKey&) = 0;
217
218 bool IsPastBounds() const;
219 bool HaveEnteredRange() const;
220
221 LevelDBTransaction* transaction_;
222 const CursorOptions cursor_options_;
223 scoped_ptr<LevelDBIterator> iterator_;
224 scoped_ptr<IndexedDBKey> current_key_;
225 IndexedDBBackingStore::RecordIdentifier record_identifier_;
226 };
227
228 virtual scoped_ptr<Cursor> OpenObjectStoreKeyCursor(
229 IndexedDBBackingStore::Transaction*,
230 int64_t database_id,
231 int64_t object_store_id,
232 const IndexedDBKeyRange&,
233 IndexedDB::CursorDirection);
234 virtual scoped_ptr<Cursor> OpenObjectStoreCursor(
235 IndexedDBBackingStore::Transaction*,
236 int64_t database_id,
237 int64_t object_store_id,
238 const IndexedDBKeyRange&,
239 IndexedDB::CursorDirection);
240 virtual scoped_ptr<Cursor> OpenIndexKeyCursor(
241 IndexedDBBackingStore::Transaction*,
242 int64_t database_id,
243 int64_t object_store_id,
244 int64_t index_id,
245 const IndexedDBKeyRange&,
246 IndexedDB::CursorDirection);
247 virtual scoped_ptr<Cursor> OpenIndexCursor(
248 IndexedDBBackingStore::Transaction*,
249 int64_t database_id,
250 int64_t object_store_id,
251 int64_t index_id,
252 const IndexedDBKeyRange&,
253 IndexedDB::CursorDirection);
254
255 class Transaction {
256 public:
257 explicit Transaction(IndexedDBBackingStore*);
258 ~Transaction();
259 void begin();
260 bool Commit();
261 void Rollback();
262 void Reset() {
263 backing_store_ = NULL;
264 transaction_ = NULL;
265 }
266
267 static LevelDBTransaction* LevelDBTransactionFrom(
268 Transaction* transaction) {
269 return transaction->transaction_.get();
270 }
271
272 private:
273 IndexedDBBackingStore* backing_store_;
274 scoped_refptr<LevelDBTransaction> transaction_;
275 };
276
277 protected:
278 IndexedDBBackingStore(const string16& identifier,
279 scoped_ptr<LevelDBDatabase>,
280 scoped_ptr<LevelDBComparator>);
281 virtual ~IndexedDBBackingStore();
282 friend class base::RefCounted<IndexedDBBackingStore>;
283
284 private:
285 static scoped_refptr<IndexedDBBackingStore> Create(
286 const string16& identifier,
287 scoped_ptr<LevelDBDatabase>,
288 scoped_ptr<LevelDBComparator>);
289
290 bool FindKeyInIndex(IndexedDBBackingStore::Transaction*,
291 int64_t database_id,
292 int64_t object_store_id,
293 int64_t index_id,
294 const IndexedDBKey&,
295 std::vector<char>& found_encoded_primary_key,
296 bool& found);
297 bool GetIndexes(int64_t database_id,
298 int64_t object_store_id,
299 IndexedDBObjectStoreMetadata::IndexMap*) WARN_UNUSED_RESULT;
300
301 string16 identifier_;
302
303 scoped_ptr<LevelDBDatabase> db_;
304 scoped_ptr<LevelDBComparator> comparator_;
305 base::WeakPtrFactory<IndexedDBBackingStore> weak_factory_;
306 };
307
308 } // namespace content
309
310 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698