9ad4b8d6e36baf34e6e3d9651a911b75172a829e
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / IDBIndexBackendImpl.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27 #include "IDBIndexBackendImpl.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "CrossThreadTask.h"
32 #include "IDBBackingStore.h"
33 #include "IDBCallbacks.h"
34 #include "IDBCursorBackendImpl.h"
35 #include "IDBDatabaseBackendImpl.h"
36 #include "IDBDatabaseException.h"
37 #include "IDBKey.h"
38 #include "IDBKeyRange.h"
39 #include "IDBMetadata.h"
40 #include "IDBObjectStoreBackendImpl.h"
41 #include "IDBTracing.h"
42 #include "IDBTransactionBackendImpl.h"
43
44 namespace WebCore {
45
46 class IDBIndexBackendImpl::OpenIndexCursorOperation {
47 public:
48     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, IDBCursorBackendInterface::CursorType cursorType, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
49     {
50         return createCallbackTask(&OpenIndexCursorOperation::perform, index, keyRange, direction, cursorType, callbacks, transaction);
51     }
52 private:
53     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, unsigned short untypedDirection, IDBCursorBackendInterface::CursorType, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
54 };
55
56 class IDBIndexBackendImpl::IndexCountOperation {
57 public:
58     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
59     {
60         return createCallbackTask(&IndexCountOperation::perform, index, keyRange, callbacks, transaction);
61     }
62 private:
63     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
64 };
65
66 class IDBIndexBackendImpl::IndexReferencedValueRetrievalOperation {
67 public:
68     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
69     {
70         return createCallbackTask(&IndexReferencedValueRetrievalOperation::perform, index, keyRange, callbacks, transaction);
71     }
72 private:
73     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
74 };
75
76 class IDBIndexBackendImpl::IndexValueRetrievalOperation {
77 public:
78     static PassOwnPtr<ScriptExecutionContext::Task> create(PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
79     {
80         return createCallbackTask(&IndexValueRetrievalOperation::perform, index, keyRange, callbacks, transaction);
81     }
82 private:
83     static void perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl>, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBTransactionBackendImpl>);
84 };
85
86
87 IDBIndexBackendImpl::IDBIndexBackendImpl(const IDBDatabaseBackendImpl* database, IDBObjectStoreBackendImpl* objectStoreBackend, const IDBIndexMetadata& metadata)
88     : m_database(database)
89     , m_objectStoreBackend(objectStoreBackend)
90     , m_metadata(metadata)
91 {
92 }
93
94 IDBIndexBackendImpl::~IDBIndexBackendImpl()
95 {
96 }
97
98 void IDBIndexBackendImpl::OpenIndexCursorOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> range, unsigned short untypedDirection, IDBCursorBackendInterface::CursorType cursorType, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
99 {
100     IDB_TRACE("OpenIndexCursorOperation");
101     IDBCursor::Direction direction = static_cast<IDBCursor::Direction>(untypedDirection);
102
103     RefPtr<IDBBackingStore::Cursor> backingStoreCursor;
104
105     switch (cursorType) {
106     case IDBCursorBackendInterface::IndexKeyCursor:
107         backingStoreCursor = index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), direction);
108         break;
109     case IDBCursorBackendInterface::IndexCursor:
110         backingStoreCursor = index->backingStore()->openIndexCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), direction);
111         break;
112     case IDBCursorBackendInterface::ObjectStoreCursor:
113     case IDBCursorBackendInterface::InvalidCursorType:
114         ASSERT_NOT_REACHED();
115         break;
116     }
117
118     if (!backingStoreCursor) {
119         callbacks->onSuccess(static_cast<SerializedScriptValue*>(0));
120         return;
121     }
122
123     RefPtr<IDBCursorBackendImpl> cursor = IDBCursorBackendImpl::create(backingStoreCursor.get(), cursorType, transaction.get(), index->m_objectStoreBackend);
124     callbacks->onSuccess(cursor, cursor->key(), cursor->primaryKey(), cursor->value());
125 }
126
127 void IDBIndexBackendImpl::openCursor(PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
128 {
129     IDB_TRACE("IDBIndexBackendImpl::openCursor");
130     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
131     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
132     if (!transaction->scheduleTask(OpenIndexCursorOperation::create(this, keyRange, direction, IDBCursorBackendInterface::IndexCursor, callbacks, transaction)))
133         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
134 }
135
136 void IDBIndexBackendImpl::openKeyCursor(PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
137 {
138     IDB_TRACE("IDBIndexBackendImpl::openKeyCursor");
139     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
140     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
141     if (!transaction->scheduleTask(OpenIndexCursorOperation::create(this, keyRange, direction, IDBCursorBackendInterface::IndexKeyCursor, callbacks, transaction)))
142         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
143 }
144
145 void IDBIndexBackendImpl::IndexCountOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> range, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
146 {
147     IDB_TRACE("IndexCountOperation");
148     uint32_t count = 0;
149
150     RefPtr<IDBBackingStore::Cursor> backingStoreCursor = index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), range.get(), IDBCursor::NEXT);
151     if (!backingStoreCursor) {
152         callbacks->onSuccess(count);
153         return;
154     }
155
156     do {
157         ++count;
158     } while (backingStoreCursor->continueFunction(0));
159
160     callbacks->onSuccess(count);
161 }
162
163 void IDBIndexBackendImpl::count(PassRefPtr<IDBKeyRange> range, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
164 {
165     IDB_TRACE("IDBIndexBackendImpl::count");
166     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
167     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
168     if (!transaction->scheduleTask(IndexCountOperation::create(this, range, callbacks, transaction)))
169         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
170 }
171
172 void IDBIndexBackendImpl::IndexReferencedValueRetrievalOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
173 {
174     IDB_TRACE("IndexReferencedValueRetrievalOperation");
175
176     RefPtr<IDBKey> key;
177
178     if (keyRange->isOnlyKey())
179         key = keyRange->lower();
180     else {
181         RefPtr<IDBBackingStore::Cursor> backingStoreCursor = index->backingStore()->openIndexCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), keyRange.get(), IDBCursor::NEXT);
182
183         if (!backingStoreCursor) {
184             callbacks->onSuccess();
185             return;
186         }
187         key = backingStoreCursor->key();
188     }
189
190     RefPtr<IDBKey> primaryKey = index->backingStore()->getPrimaryKeyViaIndex(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), *key);
191
192     String value = index->backingStore()->getRecord(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), *primaryKey);
193
194     if (value.isNull()) {
195         callbacks->onSuccess();
196         return;
197     }
198     if (index->m_objectStoreBackend->autoIncrement() && !index->m_objectStoreBackend->keyPath().isNull()) {
199         callbacks->onSuccess(SerializedScriptValue::createFromWire(value),
200                              primaryKey, index->m_objectStoreBackend->keyPath());
201         return;
202     }
203     callbacks->onSuccess(SerializedScriptValue::createFromWire(value));
204 }
205
206
207 void IDBIndexBackendImpl::IndexValueRetrievalOperation::perform(ScriptExecutionContext*, PassRefPtr<IDBIndexBackendImpl> index, PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBTransactionBackendImpl> transaction)
208 {
209     IDB_TRACE("IndexValueRetrievalOperation");
210
211     RefPtr<IDBBackingStore::Cursor> backingStoreCursor =
212             index->backingStore()->openIndexKeyCursor(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), keyRange.get(), IDBCursor::NEXT);
213
214     if (!backingStoreCursor) {
215         callbacks->onSuccess(static_cast<IDBKey*>(0));
216         return;
217     }
218
219     RefPtr<IDBKey> keyResult = index->backingStore()->getPrimaryKeyViaIndex(transaction->backingStoreTransaction(), index->databaseId(), index->m_objectStoreBackend->id(), index->id(), *backingStoreCursor->key());
220     if (!keyResult) {
221         callbacks->onSuccess(static_cast<IDBKey*>(0));
222         return;
223     }
224     callbacks->onSuccess(keyResult.get());
225 }
226
227 void IDBIndexBackendImpl::get(PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
228 {
229     IDB_TRACE("IDBIndexBackendImpl::get");
230     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
231     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
232     if (!transaction->scheduleTask(IndexReferencedValueRetrievalOperation::create(this, keyRange, callbacks, transaction)))
233         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
234 }
235
236 void IDBIndexBackendImpl::getKey(PassRefPtr<IDBKeyRange> keyRange, PassRefPtr<IDBCallbacks> prpCallbacks, IDBTransactionBackendInterface* transactionPtr, ExceptionCode&)
237 {
238     IDB_TRACE("IDBIndexBackendImpl::getKey");
239     RefPtr<IDBCallbacks> callbacks = prpCallbacks;
240     RefPtr<IDBTransactionBackendImpl> transaction = IDBTransactionBackendImpl::from(transactionPtr);
241     if (!transaction->scheduleTask(IndexValueRetrievalOperation::create(this, keyRange, callbacks, transaction)))
242         callbacks->onError(IDBDatabaseError::create(IDBDatabaseException::AbortError));
243 }
244
245 } // namespace WebCore
246
247 #endif // ENABLE(INDEXED_DATABASE)