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