2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "IDBTransactionCoordinator.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "IDBDatabaseCallbacks.h"
32 #include "IDBTransactionBackend.h"
36 PassOwnPtr<IDBTransactionCoordinator> IDBTransactionCoordinator::create()
38 return adoptPtr(new IDBTransactionCoordinator());
41 IDBTransactionCoordinator::IDBTransactionCoordinator()
45 IDBTransactionCoordinator::~IDBTransactionCoordinator()
49 void IDBTransactionCoordinator::didCreateTransaction(IDBTransactionBackend* transaction)
51 ASSERT(!m_transactions.contains(transaction));
52 m_transactions.add(transaction, transaction);
55 void IDBTransactionCoordinator::didStartTransaction(IDBTransactionBackend* transaction)
57 ASSERT(m_transactions.contains(transaction));
59 m_queuedTransactions.add(transaction);
60 processStartedTransactions();
63 void IDBTransactionCoordinator::didFinishTransaction(IDBTransactionBackend* transaction)
65 ASSERT(m_transactions.contains(transaction));
67 if (m_queuedTransactions.contains(transaction)) {
68 ASSERT(!m_startedTransactions.contains(transaction));
69 m_queuedTransactions.remove(transaction);
70 } else if (m_startedTransactions.contains(transaction))
71 m_startedTransactions.remove(transaction);
73 m_transactions.remove(transaction);
75 processStartedTransactions();
79 // Verifies internal consistiency while returning whether anything is found.
80 bool IDBTransactionCoordinator::isActive(IDBTransactionBackend* transaction)
83 if (m_queuedTransactions.contains(transaction))
85 if (m_startedTransactions.contains(transaction)) {
89 ASSERT(found == m_transactions.contains(transaction));
94 void IDBTransactionCoordinator::processStartedTransactions()
96 if (m_queuedTransactions.isEmpty())
99 ASSERT(m_startedTransactions.isEmpty() || (*m_startedTransactions.begin())->mode() != IndexedDB::TransactionVersionChange);
101 ListHashSet<IDBTransactionBackend*>::const_iterator it = m_queuedTransactions.begin();
102 while (it != m_queuedTransactions.end()) {
103 IDBTransactionBackend* transaction = *it;
105 if (canRunTransaction(transaction)) {
106 m_queuedTransactions.remove(transaction);
107 m_startedTransactions.add(transaction);
113 static bool doScopesOverlap(const HashSet<int64_t>& scope1, const HashSet<int64_t>& scope2)
115 for (HashSet<int64_t>::const_iterator it = scope1.begin(); it != scope1.end(); ++it) {
116 if (scope2.contains(*it))
122 bool IDBTransactionCoordinator::canRunTransaction(IDBTransactionBackend* transaction)
124 ASSERT(m_queuedTransactions.contains(transaction));
125 switch (transaction->mode()) {
126 case IndexedDB::TransactionVersionChange:
127 ASSERT(m_queuedTransactions.size() == 1);
128 ASSERT(m_startedTransactions.isEmpty());
131 case IndexedDB::TransactionReadOnly:
134 case IndexedDB::TransactionReadWrite:
135 for (HashSet<IDBTransactionBackend*>::const_iterator it = m_startedTransactions.begin(); it != m_startedTransactions.end(); ++it) {
136 if ((*it)->mode() == IndexedDB::TransactionReadWrite && doScopesOverlap(transaction->scope(), (*it)->scope()))
139 for (ListHashSet<IDBTransactionBackend*>::const_iterator it = m_queuedTransactions.begin(); *it != transaction; ++it) {
140 ASSERT(it != m_queuedTransactions.end());
141 if ((*it)->mode() == IndexedDB::TransactionReadWrite && doScopesOverlap(transaction->scope(), (*it)->scope()))
146 ASSERT_NOT_REACHED();
152 #endif // ENABLE(INDEXED_DATABASE)