OLD | NEW |
(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 public: |
| 88 RecordIdentifier(const std::vector<char>& primary_key, int64_t version); |
| 89 RecordIdentifier(); |
| 90 ~RecordIdentifier(); |
| 91 |
| 92 const std::vector<char>& primary_key() const { return primary_key_; } |
| 93 int64_t version() const { return version_; } |
| 94 void Reset(const std::vector<char>& primary_key, int64_t version) { |
| 95 primary_key_ = primary_key; |
| 96 version_ = version; |
| 97 } |
| 98 |
| 99 private: |
| 100 std::vector<char> primary_key_; // TODO: Make it more clear that this is |
| 101 // the *encoded* version of the key. |
| 102 int64_t version_; |
| 103 DISALLOW_COPY_AND_ASSIGN(RecordIdentifier); |
| 104 }; |
| 105 |
| 106 virtual bool GetRecord(IndexedDBBackingStore::Transaction*, |
| 107 int64_t database_id, |
| 108 int64_t object_store_id, |
| 109 const IndexedDBKey&, |
| 110 std::vector<char>& record) WARN_UNUSED_RESULT; |
| 111 virtual bool PutRecord(IndexedDBBackingStore::Transaction*, |
| 112 int64_t database_id, |
| 113 int64_t object_store_id, |
| 114 const IndexedDBKey&, |
| 115 const std::vector<char>& value, |
| 116 RecordIdentifier*) WARN_UNUSED_RESULT; |
| 117 virtual bool ClearObjectStore(IndexedDBBackingStore::Transaction*, |
| 118 int64_t database_id, |
| 119 int64_t object_store_id) WARN_UNUSED_RESULT; |
| 120 virtual bool DeleteRecord(IndexedDBBackingStore::Transaction*, |
| 121 int64_t database_id, |
| 122 int64_t object_store_id, |
| 123 const RecordIdentifier&) WARN_UNUSED_RESULT; |
| 124 virtual bool GetKeyGeneratorCurrentNumber(IndexedDBBackingStore::Transaction*, |
| 125 int64_t database_id, |
| 126 int64_t object_store_id, |
| 127 int64_t& current_number) |
| 128 WARN_UNUSED_RESULT; |
| 129 virtual bool MaybeUpdateKeyGeneratorCurrentNumber( |
| 130 IndexedDBBackingStore::Transaction*, |
| 131 int64_t database_id, |
| 132 int64_t object_store_id, |
| 133 int64_t new_state, |
| 134 bool check_current) WARN_UNUSED_RESULT; |
| 135 virtual bool KeyExistsInObjectStore(IndexedDBBackingStore::Transaction*, |
| 136 int64_t database_id, |
| 137 int64_t object_store_id, |
| 138 const IndexedDBKey&, |
| 139 RecordIdentifier* found_record_identifier, |
| 140 bool& found) WARN_UNUSED_RESULT; |
| 141 |
| 142 virtual bool CreateIndex(IndexedDBBackingStore::Transaction*, |
| 143 int64_t database_id, |
| 144 int64_t object_store_id, |
| 145 int64_t index_id, |
| 146 const string16& name, |
| 147 const IndexedDBKeyPath&, |
| 148 bool is_unique, |
| 149 bool is_multi_entry) WARN_UNUSED_RESULT; |
| 150 virtual bool DeleteIndex(IndexedDBBackingStore::Transaction*, |
| 151 int64_t database_id, |
| 152 int64_t object_store_id, |
| 153 int64_t index_id) WARN_UNUSED_RESULT; |
| 154 virtual bool PutIndexDataForRecord(IndexedDBBackingStore::Transaction*, |
| 155 int64_t database_id, |
| 156 int64_t object_store_id, |
| 157 int64_t index_id, |
| 158 const IndexedDBKey&, |
| 159 const RecordIdentifier&) |
| 160 WARN_UNUSED_RESULT; |
| 161 virtual bool GetPrimaryKeyViaIndex(IndexedDBBackingStore::Transaction*, |
| 162 int64_t database_id, |
| 163 int64_t object_store_id, |
| 164 int64_t index_id, |
| 165 const IndexedDBKey&, |
| 166 scoped_ptr<IndexedDBKey>* primary_key) |
| 167 WARN_UNUSED_RESULT; |
| 168 virtual bool KeyExistsInIndex(IndexedDBBackingStore::Transaction*, |
| 169 int64_t database_id, |
| 170 int64_t object_store_id, |
| 171 int64_t index_id, |
| 172 const IndexedDBKey& index_key, |
| 173 scoped_ptr<IndexedDBKey>* found_primary_key, |
| 174 bool& exists) WARN_UNUSED_RESULT; |
| 175 |
| 176 class Cursor { |
| 177 public: |
| 178 virtual ~Cursor(); |
| 179 |
| 180 enum IteratorState { |
| 181 Ready = 0, |
| 182 Seek |
| 183 }; |
| 184 |
| 185 struct CursorOptions { |
| 186 CursorOptions(); |
| 187 ~CursorOptions(); |
| 188 int64_t database_id; |
| 189 int64_t object_store_id; |
| 190 int64_t index_id; |
| 191 std::vector<char> low_key; |
| 192 bool low_open; |
| 193 std::vector<char> high_key; |
| 194 bool high_open; |
| 195 bool forward; |
| 196 bool unique; |
| 197 }; |
| 198 |
| 199 const IndexedDBKey& key() const { return *current_key_; } |
| 200 bool ContinueFunction(const IndexedDBKey* = 0, IteratorState = Seek); |
| 201 bool Advance(unsigned long); |
| 202 bool FirstSeek(); |
| 203 |
| 204 virtual Cursor* Clone() = 0; |
| 205 virtual const IndexedDBKey& primary_key() const; |
| 206 virtual std::vector<char>* Value() = 0; |
| 207 virtual const RecordIdentifier& record_identifier() const; |
| 208 virtual bool LoadCurrentRow() = 0; |
| 209 |
| 210 protected: |
| 211 Cursor(LevelDBTransaction* transaction, |
| 212 const CursorOptions& cursor_options); |
| 213 explicit Cursor(const IndexedDBBackingStore::Cursor* other); |
| 214 |
| 215 virtual std::vector<char> EncodeKey(const IndexedDBKey&) = 0; |
| 216 |
| 217 bool IsPastBounds() const; |
| 218 bool HaveEnteredRange() const; |
| 219 |
| 220 LevelDBTransaction* transaction_; |
| 221 const CursorOptions cursor_options_; |
| 222 scoped_ptr<LevelDBIterator> iterator_; |
| 223 scoped_ptr<IndexedDBKey> current_key_; |
| 224 IndexedDBBackingStore::RecordIdentifier record_identifier_; |
| 225 }; |
| 226 |
| 227 virtual scoped_ptr<Cursor> OpenObjectStoreKeyCursor( |
| 228 IndexedDBBackingStore::Transaction*, |
| 229 int64_t database_id, |
| 230 int64_t object_store_id, |
| 231 const IndexedDBKeyRange&, |
| 232 IndexedDB::CursorDirection); |
| 233 virtual scoped_ptr<Cursor> OpenObjectStoreCursor( |
| 234 IndexedDBBackingStore::Transaction*, |
| 235 int64_t database_id, |
| 236 int64_t object_store_id, |
| 237 const IndexedDBKeyRange&, |
| 238 IndexedDB::CursorDirection); |
| 239 virtual scoped_ptr<Cursor> OpenIndexKeyCursor( |
| 240 IndexedDBBackingStore::Transaction*, |
| 241 int64_t database_id, |
| 242 int64_t object_store_id, |
| 243 int64_t index_id, |
| 244 const IndexedDBKeyRange&, |
| 245 IndexedDB::CursorDirection); |
| 246 virtual scoped_ptr<Cursor> OpenIndexCursor( |
| 247 IndexedDBBackingStore::Transaction*, |
| 248 int64_t database_id, |
| 249 int64_t object_store_id, |
| 250 int64_t index_id, |
| 251 const IndexedDBKeyRange&, |
| 252 IndexedDB::CursorDirection); |
| 253 |
| 254 class Transaction { |
| 255 public: |
| 256 explicit Transaction(IndexedDBBackingStore* backing_store); |
| 257 ~Transaction(); |
| 258 void begin(); |
| 259 bool Commit(); |
| 260 void Rollback(); |
| 261 void Reset() { |
| 262 backing_store_ = NULL; |
| 263 transaction_ = NULL; |
| 264 } |
| 265 |
| 266 static LevelDBTransaction* LevelDBTransactionFrom( |
| 267 Transaction* transaction) { |
| 268 return transaction->transaction_.get(); |
| 269 } |
| 270 |
| 271 private: |
| 272 IndexedDBBackingStore* backing_store_; |
| 273 scoped_refptr<LevelDBTransaction> transaction_; |
| 274 }; |
| 275 |
| 276 protected: |
| 277 IndexedDBBackingStore(const string16& identifier, |
| 278 scoped_ptr<LevelDBDatabase>, |
| 279 scoped_ptr<LevelDBComparator>); |
| 280 virtual ~IndexedDBBackingStore(); |
| 281 friend class base::RefCounted<IndexedDBBackingStore>; |
| 282 |
| 283 private: |
| 284 static scoped_refptr<IndexedDBBackingStore> Create( |
| 285 const string16& identifier, |
| 286 scoped_ptr<LevelDBDatabase>, |
| 287 scoped_ptr<LevelDBComparator>); |
| 288 |
| 289 bool FindKeyInIndex(IndexedDBBackingStore::Transaction*, |
| 290 int64_t database_id, |
| 291 int64_t object_store_id, |
| 292 int64_t index_id, |
| 293 const IndexedDBKey&, |
| 294 std::vector<char>& found_encoded_primary_key, |
| 295 bool& found); |
| 296 bool GetIndexes(int64_t database_id, |
| 297 int64_t object_store_id, |
| 298 IndexedDBObjectStoreMetadata::IndexMap*) WARN_UNUSED_RESULT; |
| 299 |
| 300 string16 identifier_; |
| 301 |
| 302 scoped_ptr<LevelDBDatabase> db_; |
| 303 scoped_ptr<LevelDBComparator> comparator_; |
| 304 base::WeakPtrFactory<IndexedDBBackingStore> weak_factory_; |
| 305 }; |
| 306 |
| 307 } // namespace content |
| 308 |
| 309 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
OLD | NEW |