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

Unified Diff: third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp

Issue 2349413002: Minor IndexedDB refactorings. (Closed)
Patch Set: Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp
diff --git a/third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp b/third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp
index 7c80f4cc645bc75ffc457d6978f6289ef672e18f..948685554fff0ba9ab3a0bdb809be6d42f26a45f 100644
--- a/third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp
+++ b/third_party/WebKit/Source/modules/indexeddb/IDBObjectStore.cpp
@@ -57,11 +57,17 @@ using blink::WebVector;
namespace blink {
+namespace {
+
+using IndexKeys = HeapVector<Member<IDBKey>>;
cmumford 2016/09/19 23:26:48 I'm OK with defining |IndexKeys|, and would also b
pwnall 2016/09/20 09:11:14 If you're OK with defining it, I would rather go t
+
+}
+
IDBObjectStore::IDBObjectStore(const IDBObjectStoreMetadata& metadata, IDBTransaction* transaction)
: m_metadata(metadata)
, m_transaction(transaction)
{
- ASSERT(m_transaction);
+ DCHECK(m_transaction);
}
DEFINE_TRACE(IDBObjectStore)
@@ -71,7 +77,7 @@ DEFINE_TRACE(IDBObjectStore)
visitor->trace(m_createdIndexes);
}
-void IDBObjectStore::setName(const String& name, ExceptionState& exceptionState)
+void IDBObjectStore::setName(const String& newName, ExceptionState& exceptionState)
{
if (!RuntimeEnabledFeatures::indexedDBExperimentalEnabled())
return;
@@ -94,9 +100,9 @@ void IDBObjectStore::setName(const String& name, ExceptionState& exceptionState)
return;
}
- if (m_metadata.name == name)
+ if (name() == newName)
return;
- if (m_transaction->db()->containsObjectStore(name)) {
+ if (m_transaction->db()->containsObjectStore(newName)) {
exceptionState.throwDOMException(ConstraintError, IDBDatabase::objectStoreNameTakenErrorMessage);
return;
}
@@ -105,25 +111,25 @@ void IDBObjectStore::setName(const String& name, ExceptionState& exceptionState)
return;
}
- backendDB()->renameObjectStore(m_transaction->id(), id(), name);
- m_transaction->objectStoreRenamed(m_metadata.name, name);
- m_metadata.name = name;
+ backendDB()->renameObjectStore(m_transaction->id(), id(), newName);
+ m_transaction->objectStoreRenamed(m_metadata.name, newName);
+ m_metadata.name = newName;
// The name inside the database's version of the object store metadata is used by IDBDatabase.objectStoreNames().
// If the transaction is aborted, this name will be reverted when the metadata is overwritten with the previousMetadata in IDBTransaction.
- m_transaction->db()->objectStoreRenamed(id(), name);
+ m_transaction->db()->objectStoreRenamed(id(), newName);
}
ScriptValue IDBObjectStore::keyPath(ScriptState* scriptState) const
{
- return ScriptValue::from(scriptState, m_metadata.keyPath);
+ return ScriptValue::from(scriptState, metadata().keyPath);
}
DOMStringList* IDBObjectStore::indexNames() const
{
IDB_TRACE("IDBObjectStore::indexNames");
DOMStringList* indexNames = DOMStringList::create(DOMStringList::IndexedDB);
- for (const auto& it : m_metadata.indexes)
+ for (const auto& it : metadata().indexes)
indexNames->append(it.value.name);
indexNames->sort();
return indexNames;
@@ -265,7 +271,7 @@ IDBRequest* IDBObjectStore::getAllKeys(ScriptState* scriptState, const ScriptVal
return request;
}
-static void generateIndexKeysForValue(v8::Isolate* isolate, const IDBIndexMetadata& indexMetadata, const ScriptValue& objectValue, IDBObjectStore::IndexKeys* indexKeys)
+static void generateIndexKeysForValue(v8::Isolate* isolate, const IDBIndexMetadata& indexMetadata, const ScriptValue& objectValue, IndexKeys* indexKeys)
{
ASSERT(indexKeys);
NonThrowableExceptionState exceptionState;
@@ -340,7 +346,7 @@ IDBRequest* IDBObjectStore::put(ScriptState* scriptState, WebIDBPutMode putMode,
// clone lazily since the operation may be expensive.
ScriptValue clone;
- const IDBKeyPath& keyPath = m_metadata.keyPath;
+ const IDBKeyPath& keyPath = idbKeyPath();
const bool usesInLineKeys = !keyPath.isNull();
const bool hasKeyGenerator = autoIncrement();
@@ -402,7 +408,7 @@ IDBRequest* IDBObjectStore::put(ScriptState* scriptState, WebIDBPutMode putMode,
Vector<int64_t> indexIds;
HeapVector<IndexKeys> indexKeys;
- for (const auto& it : m_metadata.indexes) {
+ for (const auto& it : metadata().indexes) {
if (clone.isEmpty())
clone = deserializeScriptValue(scriptState, serializedValue.get(), &blobInfo);
IndexKeys keys;
@@ -521,10 +527,12 @@ private:
{
}
+ const IDBIndexMetadata& indexMetadata() { return m_indexMetadata; }
cmumford 2016/09/19 23:26:48 const.
pwnall 2016/09/20 09:11:14 Done. Thank you! There should be a warning for th
+
void handleEvent(ExecutionContext* executionContext, Event* event) override
{
- ASSERT(m_scriptState->getExecutionContext() == executionContext);
- ASSERT(event->type() == EventTypeNames::success);
+ DCHECK_EQ(m_scriptState->getExecutionContext(), executionContext);
+ DCHECK_EQ(event->type(), EventTypeNames::success);
EventTarget* target = event->target();
IDBRequest* request = static_cast<IDBRequest*>(target);
@@ -537,17 +545,17 @@ private:
cursor = cursorAny->idbCursorWithValue();
Vector<int64_t> indexIds;
- indexIds.append(m_indexMetadata.id);
+ indexIds.append(indexMetadata().id);
if (cursor && !cursor->isDeleted()) {
cursor->continueFunction(nullptr, nullptr, ASSERT_NO_EXCEPTION);
IDBKey* primaryKey = cursor->idbPrimaryKey();
ScriptValue value = cursor->value(m_scriptState.get());
- IDBObjectStore::IndexKeys indexKeys;
- generateIndexKeysForValue(m_scriptState->isolate(), m_indexMetadata, value, &indexKeys);
+ IndexKeys indexKeys;
+ generateIndexKeysForValue(m_scriptState->isolate(), indexMetadata(), value, &indexKeys);
- HeapVector<IDBObjectStore::IndexKeys> indexKeysList;
+ HeapVector<IndexKeys> indexKeysList;
indexKeysList.append(indexKeys);
m_database->backend()->setIndexKeys(m_transactionId, m_objectStoreId, primaryKey, indexIds, indexKeysList);
@@ -605,16 +613,17 @@ IDBIndex* IDBObjectStore::createIndex(ScriptState* scriptState, const String& na
}
int64_t indexId = m_metadata.maxIndexId + 1;
+ DCHECK(indexId != IDBIndexMetadata::InvalidId);
backendDB()->createIndex(m_transaction->id(), id(), indexId, name, keyPath, options.unique(), options.multiEntry());
++m_metadata.maxIndexId;
- IDBIndexMetadata metadata(name, indexId, keyPath, options.unique(), options.multiEntry());
- IDBIndex* index = IDBIndex::create(metadata, this, m_transaction.get());
+ IDBIndexMetadata indexMetadata(name, indexId, keyPath, options.unique(), options.multiEntry());
+ IDBIndex* index = IDBIndex::create(indexMetadata, this, m_transaction.get());
m_indexMap.set(name, index);
m_createdIndexes.add(index);
- m_metadata.indexes.set(indexId, metadata);
- m_transaction->db()->indexCreated(id(), metadata);
+ m_metadata.indexes.set(indexId, indexMetadata);
+ m_transaction->db()->indexCreated(id(), indexMetadata);
ASSERT(!exceptionState.hadException());
if (exceptionState.hadException())
@@ -624,7 +633,7 @@ IDBIndex* IDBObjectStore::createIndex(ScriptState* scriptState, const String& na
indexRequest->preventPropagation();
// This is kept alive by being the success handler of the request, which is in turn kept alive by the owning transaction.
- IndexPopulator* indexPopulator = IndexPopulator::create(scriptState, transaction()->db(), m_transaction->id(), id(), metadata);
+ IndexPopulator* indexPopulator = IndexPopulator::create(scriptState, transaction()->db(), m_transaction->id(), id(), indexMetadata);
indexRequest->setOnsuccess(indexPopulator);
return index;
}
@@ -651,17 +660,9 @@ IDBIndex* IDBObjectStore::index(const String& name, ExceptionState& exceptionSta
return nullptr;
}
- const IDBIndexMetadata* indexMetadata(nullptr);
- for (const auto& it : m_metadata.indexes) {
- if (it.value.name == name) {
- indexMetadata = &it.value;
- break;
- }
- }
- ASSERT(indexMetadata);
- ASSERT(indexMetadata->id != IDBIndexMetadata::InvalidId);
-
- IDBIndex* index = IDBIndex::create(*indexMetadata, this, m_transaction.get());
+ DCHECK(metadata().indexes.contains(indexId));
+ const IDBIndexMetadata& indexMetadata = metadata().indexes.get(indexId);
+ IDBIndex* index = IDBIndex::create(indexMetadata, this, m_transaction.get());
m_indexMap.set(name, index);
return index;
}
@@ -807,6 +808,12 @@ IDBRequest* IDBObjectStore::count(ScriptState* scriptState, const ScriptValue& r
return request;
}
+void IDBObjectStore::markDeleted()
+{
+ DCHECK(m_transaction->isVersionChange()) << " an object store got deleted outside a versionchange transaction";
+ m_deleted = true;
+}
+
void IDBObjectStore::abort()
{
for (auto& index : m_createdIndexes)
@@ -815,7 +822,7 @@ void IDBObjectStore::abort()
void IDBObjectStore::transactionFinished()
{
- ASSERT(m_transaction->isFinished());
+ DCHECK(!m_transaction->isActive());
// Break reference cycles.
// TODO(jsbell): This can be removed c/o Oilpan.
@@ -828,22 +835,21 @@ void IDBObjectStore::indexRenamed(int64_t indexId, const String& newName)
DCHECK(m_transaction->isVersionChange());
DCHECK(m_transaction->isActive());
- IDBObjectStoreMetadata::IndexMap::iterator metadataIterator = m_metadata.indexes.find(indexId);
+ auto metadataIterator = m_metadata.indexes.find(indexId);
DCHECK(metadataIterator != m_metadata.indexes.end()) << "Invalid indexId";
const String& oldName = metadataIterator->value.name;
DCHECK(m_indexMap.contains(oldName)) << "The index had to be accessed in order to be renamed.";
DCHECK(!m_indexMap.contains(newName));
- IDBIndexMap::iterator it = m_indexMap.find(oldName);
- m_indexMap.set(newName, it->value);
- m_indexMap.remove(oldName);
+ IDBIndex* index = m_indexMap.take(oldName);
cmumford 2016/09/19 23:26:48 Nit: How about: m_indexMap.set(newName, m_index
pwnall 2016/09/20 09:11:14 Done. I made this change in IDBTransaction::object
+ m_indexMap.set(newName, index);
metadataIterator->value.name = newName;
}
int64_t IDBObjectStore::findIndexId(const String& name) const
{
- for (const auto& it : m_metadata.indexes) {
+ for (const auto& it : metadata().indexes) {
if (it.value.name == name) {
ASSERT(it.key != IDBIndexMetadata::InvalidId);
return it.key;

Powered by Google App Engine
This is Rietveld 408576698