Chromium Code Reviews| Index: content/browser/indexed_db/indexed_db_backing_store.h |
| diff --git a/content/browser/indexed_db/indexed_db_backing_store.h b/content/browser/indexed_db/indexed_db_backing_store.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1ef7b6030123690058ea3ae49c2f3d405ccb481 |
| --- /dev/null |
| +++ b/content/browser/indexed_db/indexed_db_backing_store.h |
| @@ -0,0 +1,310 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
| +#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "content/browser/indexed_db/indexed_db.h" |
| +#include "content/browser/indexed_db/indexed_db_metadata.h" |
| +#include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
| +#include "content/browser/indexed_db/leveldb/leveldb_transaction.h" |
| +#include "content/common/content_export.h" |
| +#include "content/common/indexed_db/indexed_db_key.h" |
| +#include "content/common/indexed_db/indexed_db_key_path.h" |
| +#include "content/common/indexed_db/indexed_db_key_range.h" |
| + |
| +namespace content { |
| + |
| +class LevelDBComparator; |
| +class LevelDBDatabase; |
| + |
| +class LevelDBFactory { |
| + public: |
| + virtual scoped_ptr<LevelDBDatabase> OpenLevelDB(const string16& file_name, |
| + const LevelDBComparator*) = 0; |
| + virtual bool DestroyLevelDB(const string16& file_name) = 0; |
| +}; |
| + |
| +class CONTENT_EXPORT IndexedDBBackingStore |
| + : public base::RefCounted<IndexedDBBackingStore> { |
| + public: |
| + class Transaction; |
| + |
| + static scoped_refptr<IndexedDBBackingStore> Open( |
| + const string16& database_identifier, |
| + const string16& path_base, |
| + const string16& file_identifier); |
| + static scoped_refptr<IndexedDBBackingStore> Open( |
| + const string16& database_identifier, |
| + const string16& path_base, |
| + const string16& file_identifier, |
| + LevelDBFactory*); |
| + static scoped_refptr<IndexedDBBackingStore> OpenInMemory( |
| + const string16& identifier); |
| + static scoped_refptr<IndexedDBBackingStore> OpenInMemory( |
| + const string16& identifier, |
| + LevelDBFactory*); |
| + base::WeakPtr<IndexedDBBackingStore> GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| + } |
| + |
| + virtual std::vector<string16> GetDatabaseNames(); |
| + virtual bool GetIDBDatabaseMetaData(const string16& name, |
| + IndexedDBDatabaseMetadata*, |
| + bool& success) WARN_UNUSED_RESULT; |
| + virtual bool CreateIDBDatabaseMetaData(const string16& name, |
| + const string16& version, |
| + int64_t int_version, |
| + int64_t& row_id); |
| + virtual bool UpdateIDBDatabaseMetaData(IndexedDBBackingStore::Transaction*, |
| + int64_t row_id, |
| + const string16& version); |
| + virtual bool UpdateIDBDatabaseIntVersion(IndexedDBBackingStore::Transaction*, |
| + int64_t row_id, |
| + int64_t int_version); |
| + virtual bool DeleteDatabase(const string16& name); |
| + |
| + bool GetObjectStores(int64_t database_id, |
| + IndexedDBDatabaseMetadata::ObjectStoreMap*) |
| + WARN_UNUSED_RESULT; |
| + virtual bool CreateObjectStore(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const string16& name, |
| + const IndexedDBKeyPath&, |
| + bool auto_increment); |
| + virtual bool DeleteObjectStore(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id) WARN_UNUSED_RESULT; |
| + |
| + class RecordIdentifier { |
| + 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.
|
| + |
| + public: |
| + RecordIdentifier(const std::vector<char>& primary_key, int64_t version); |
| + RecordIdentifier(); |
| + ~RecordIdentifier(); |
| + |
| + const std::vector<char>& primary_key() const { return primary_key_; } |
| + int64_t version() const { return version_; } |
| + void Reset(const std::vector<char>& primary_key, int64_t version) { |
| + primary_key_ = primary_key; |
| + version_ = version; |
| + } |
| + |
| + private: |
| + 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.
|
| + // the *encoded* version of the key. |
| + int64_t version_; |
| + }; |
| + |
| + virtual bool GetRecord(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKey&, |
| + std::vector<char>& record) WARN_UNUSED_RESULT; |
| + virtual bool PutRecord(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKey&, |
| + const std::vector<char>& value, |
| + RecordIdentifier*) WARN_UNUSED_RESULT; |
| + virtual bool ClearObjectStore(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id) WARN_UNUSED_RESULT; |
| + virtual bool DeleteRecord(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const RecordIdentifier&) WARN_UNUSED_RESULT; |
| + virtual bool GetKeyGeneratorCurrentNumber(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t& current_number) |
| + WARN_UNUSED_RESULT; |
| + virtual bool MaybeUpdateKeyGeneratorCurrentNumber( |
| + IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t new_state, |
| + bool check_current) WARN_UNUSED_RESULT; |
| + virtual bool KeyExistsInObjectStore(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKey&, |
| + RecordIdentifier* found_record_identifier, |
| + bool& found) WARN_UNUSED_RESULT; |
| + |
| + virtual bool CreateIndex(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const string16& name, |
| + const IndexedDBKeyPath&, |
| + bool is_unique, |
| + bool is_multi_entry) WARN_UNUSED_RESULT; |
| + virtual bool DeleteIndex(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id) WARN_UNUSED_RESULT; |
| + virtual bool PutIndexDataForRecord(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKey&, |
| + const RecordIdentifier&) |
| + WARN_UNUSED_RESULT; |
| + virtual bool GetPrimaryKeyViaIndex(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKey&, |
| + scoped_ptr<IndexedDBKey>* primary_key) |
| + WARN_UNUSED_RESULT; |
| + virtual bool KeyExistsInIndex(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKey& index_key, |
| + scoped_ptr<IndexedDBKey>* found_primary_key, |
| + bool& exists) WARN_UNUSED_RESULT; |
| + |
| + class Cursor { |
| + public: |
| + virtual ~Cursor(); |
| + |
| + enum IteratorState { |
| + Ready = 0, |
|
jamesr
2013/05/21 23:56:06
enums should SHOUT
jsbell
2013/05/22 17:54:44
Done.
|
| + Seek |
| + }; |
| + |
| + struct CursorOptions { |
| + CursorOptions(); |
| + ~CursorOptions(); |
| + int64_t database_id; |
| + int64_t object_store_id; |
| + int64_t index_id; |
| + std::vector<char> low_key; |
| + bool low_open; |
| + std::vector<char> high_key; |
| + bool high_open; |
| + bool forward; |
| + bool unique; |
| + }; |
| + |
| + const IndexedDBKey& key() const { return *current_key_; } |
| + bool ContinueFunction(const IndexedDBKey* = 0, IteratorState = Seek); |
| + bool Advance(unsigned long); |
| + bool FirstSeek(); |
| + |
| + virtual Cursor* Clone() = 0; |
| + virtual const IndexedDBKey& primary_key() const; |
| + virtual std::vector<char>* Value() = 0; |
| + virtual const RecordIdentifier& record_identifier() const; |
| + virtual bool LoadCurrentRow() = 0; |
| + |
| + protected: |
| + Cursor(LevelDBTransaction* transaction, |
| + const CursorOptions& cursor_options); |
| + explicit Cursor(const IndexedDBBackingStore::Cursor* other); |
| + |
| + virtual std::vector<char> EncodeKey(const IndexedDBKey&) = 0; |
| + |
| + bool IsPastBounds() const; |
| + bool HaveEnteredRange() const; |
| + |
| + LevelDBTransaction* transaction_; |
| + const CursorOptions cursor_options_; |
| + scoped_ptr<LevelDBIterator> iterator_; |
| + scoped_ptr<IndexedDBKey> current_key_; |
| + IndexedDBBackingStore::RecordIdentifier record_identifier_; |
| + }; |
| + |
| + virtual scoped_ptr<Cursor> OpenObjectStoreKeyCursor( |
| + IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKeyRange&, |
| + IndexedDB::CursorDirection); |
| + virtual scoped_ptr<Cursor> OpenObjectStoreCursor( |
| + IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKeyRange&, |
| + IndexedDB::CursorDirection); |
| + virtual scoped_ptr<Cursor> OpenIndexKeyCursor( |
| + IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKeyRange&, |
| + IndexedDB::CursorDirection); |
| + virtual scoped_ptr<Cursor> OpenIndexCursor( |
| + IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKeyRange&, |
| + IndexedDB::CursorDirection); |
| + |
| + class Transaction { |
| + public: |
| + explicit Transaction(IndexedDBBackingStore*); |
| + ~Transaction(); |
| + void begin(); |
| + bool Commit(); |
| + void Rollback(); |
| + void Reset() { |
| + backing_store_ = NULL; |
| + transaction_ = NULL; |
| + } |
| + |
| + static LevelDBTransaction* LevelDBTransactionFrom( |
| + Transaction* transaction) { |
| + return transaction->transaction_.get(); |
| + } |
| + |
| + private: |
| + IndexedDBBackingStore* backing_store_; |
| + scoped_refptr<LevelDBTransaction> transaction_; |
| + }; |
| + |
| + protected: |
| + IndexedDBBackingStore(const string16& identifier, |
| + scoped_ptr<LevelDBDatabase>, |
| + scoped_ptr<LevelDBComparator>); |
| + virtual ~IndexedDBBackingStore(); |
| + friend class base::RefCounted<IndexedDBBackingStore>; |
| + |
| + private: |
| + static scoped_refptr<IndexedDBBackingStore> Create( |
| + const string16& identifier, |
| + scoped_ptr<LevelDBDatabase>, |
| + scoped_ptr<LevelDBComparator>); |
| + |
| + bool FindKeyInIndex(IndexedDBBackingStore::Transaction*, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKey&, |
| + std::vector<char>& found_encoded_primary_key, |
| + bool& found); |
| + bool GetIndexes(int64_t database_id, |
| + int64_t object_store_id, |
| + IndexedDBObjectStoreMetadata::IndexMap*) WARN_UNUSED_RESULT; |
| + |
| + string16 identifier_; |
| + |
| + scoped_ptr<LevelDBDatabase> db_; |
| + scoped_ptr<LevelDBComparator> comparator_; |
| + base::WeakPtrFactory<IndexedDBBackingStore> weak_factory_; |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |