Modern IDB: IDBObjectStore.deleteIndex() support.
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / server / MemoryIDBBackingStore.cpp
1 /*
2  * Copyright (C) 2015 Apple 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "MemoryIDBBackingStore.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBCursorInfo.h"
32 #include "IDBIndexInfo.h"
33 #include "IDBKeyRangeData.h"
34 #include "Logging.h"
35 #include "MemoryObjectStore.h"
36 #include "MemoryObjectStoreCursor.h"
37
38 namespace WebCore {
39 namespace IDBServer {
40
41 std::unique_ptr<MemoryIDBBackingStore> MemoryIDBBackingStore::create(const IDBDatabaseIdentifier& identifier)
42 {
43     return std::make_unique<MemoryIDBBackingStore>(identifier);
44 }
45
46 MemoryIDBBackingStore::MemoryIDBBackingStore(const IDBDatabaseIdentifier& identifier)
47     : m_identifier(identifier)
48 {
49 }
50
51 MemoryIDBBackingStore::~MemoryIDBBackingStore()
52 {
53 }
54
55 const IDBDatabaseInfo& MemoryIDBBackingStore::getOrEstablishDatabaseInfo()
56 {
57     if (!m_databaseInfo)
58         m_databaseInfo = std::make_unique<IDBDatabaseInfo>(m_identifier.databaseName(), 0);
59
60     return *m_databaseInfo;
61 }
62
63 void MemoryIDBBackingStore::setDatabaseInfo(const IDBDatabaseInfo& info)
64 {
65     // It is not valid to directly set database info on a backing store that hasn't already set its own database info.
66     ASSERT(m_databaseInfo);
67
68     m_databaseInfo = std::make_unique<IDBDatabaseInfo>(info);
69 }
70
71 IDBError MemoryIDBBackingStore::beginTransaction(const IDBTransactionInfo& info)
72 {
73     LOG(IndexedDB, "MemoryIDBBackingStore::beginTransaction");
74
75     if (m_transactions.contains(info.identifier()))
76         return IDBError(IDBExceptionCode::InvalidStateError, "Backing store asked to create transaction it already has a record of");
77
78     auto transaction = MemoryBackingStoreTransaction::create(*this, info);
79
80     // VersionChange transactions are scoped to "every object store".
81     if (transaction->isVersionChange()) {
82         for (auto& objectStore : m_objectStoresByIdentifier.values())
83             transaction->addExistingObjectStore(*objectStore);
84     } else if (transaction->isWriting()) {
85         for (auto& iterator : m_objectStoresByName) {
86             if (info.objectStores().contains(iterator.key))
87                 transaction->addExistingObjectStore(*iterator.value);
88         }
89     }
90
91     m_transactions.set(info.identifier(), WTF::move(transaction));
92
93     return IDBError();
94 }
95
96 IDBError MemoryIDBBackingStore::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
97 {
98     LOG(IndexedDB, "MemoryIDBBackingStore::abortTransaction");
99
100     auto transaction = m_transactions.take(transactionIdentifier);
101     if (!transaction)
102         return IDBError(IDBExceptionCode::InvalidStateError, "Backing store asked to abort transaction it didn't have record of");
103
104     transaction->abort();
105
106     return IDBError();
107 }
108
109 IDBError MemoryIDBBackingStore::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
110 {
111     LOG(IndexedDB, "MemoryIDBBackingStore::commitTransaction");
112
113     auto transaction = m_transactions.take(transactionIdentifier);
114     if (!transaction)
115         return IDBError(IDBExceptionCode::InvalidStateError, "Backing store asked to commit transaction it didn't have record of");
116
117     transaction->commit();
118
119     return IDBError();
120 }
121
122 IDBError MemoryIDBBackingStore::createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo& info)
123 {
124     LOG(IndexedDB, "MemoryIDBBackingStore::createObjectStore - adding OS %s with ID %" PRIu64, info.name().utf8().data(), info.identifier());
125
126     ASSERT(m_databaseInfo);
127     if (m_databaseInfo->hasObjectStore(info.name()))
128         return IDBError(IDBExceptionCode::ConstraintError);
129
130     ASSERT(!m_objectStoresByIdentifier.contains(info.identifier()));
131     auto objectStore = MemoryObjectStore::create(info);
132
133     m_databaseInfo->addExistingObjectStore(info);
134
135     auto rawTransaction = m_transactions.get(transactionIdentifier);
136     ASSERT(rawTransaction);
137     ASSERT(rawTransaction->isVersionChange());
138
139     rawTransaction->addNewObjectStore(*objectStore);
140     registerObjectStore(WTF::move(objectStore));
141
142     return IDBError();
143 }
144
145 IDBError MemoryIDBBackingStore::deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, const String& objectStoreName)
146 {
147     LOG(IndexedDB, "MemoryIDBBackingStore::deleteObjectStore");
148
149     ASSERT(m_databaseInfo);
150     if (!m_databaseInfo->hasObjectStore(objectStoreName))
151         return IDBError(IDBExceptionCode::ConstraintError);
152
153     auto transaction = m_transactions.get(transactionIdentifier);
154     ASSERT(transaction);
155     ASSERT(transaction->isVersionChange());
156
157     auto objectStore = takeObjectStoreByName(objectStoreName);
158     ASSERT(objectStore);
159     if (!objectStore)
160         return IDBError(IDBExceptionCode::ConstraintError);
161
162     m_databaseInfo->deleteObjectStore(objectStoreName);
163     transaction->objectStoreDeleted(WTF::move(objectStore));
164
165     return IDBError();
166 }
167
168 IDBError MemoryIDBBackingStore::clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier)
169 {
170     LOG(IndexedDB, "MemoryIDBBackingStore::clearObjectStore");
171     ASSERT(objectStoreIdentifier);
172
173     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
174
175 #ifndef NDEBUG
176     auto transaction = m_transactions.get(transactionIdentifier);
177     ASSERT(transaction->isWriting());
178 #endif
179
180     auto objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
181     if (!objectStore)
182         return IDBError(IDBExceptionCode::ConstraintError);
183
184     objectStore->clear();
185
186     return IDBError();
187 }
188
189 IDBError MemoryIDBBackingStore::createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo& info)
190 {
191     LOG(IndexedDB, "MemoryIDBBackingStore::createIndex");
192
193     auto rawTransaction = m_transactions.get(transactionIdentifier);
194     ASSERT(rawTransaction);
195     ASSERT(rawTransaction->isVersionChange());
196
197     auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
198     if (!objectStore)
199         return IDBError(IDBExceptionCode::ConstraintError);
200
201     return objectStore->createIndex(*rawTransaction, info);
202 }
203
204 IDBError MemoryIDBBackingStore::deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& indexName)
205 {
206     LOG(IndexedDB, "MemoryIDBBackingStore::deleteIndex");
207
208     auto rawTransaction = m_transactions.get(transactionIdentifier);
209     ASSERT(rawTransaction);
210     ASSERT(rawTransaction->isVersionChange());
211
212     auto* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
213     if (!objectStore)
214         return IDBError(IDBExceptionCode::ConstraintError);
215
216     return objectStore->deleteIndex(*rawTransaction, indexName);
217 }
218
219 void MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort(MemoryObjectStore& objectStore)
220 {
221     LOG(IndexedDB, "MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort");
222
223     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
224     ASSERT(m_objectStoresByIdentifier.get(objectStore.info().identifier()) == &objectStore);
225
226     unregisterObjectStore(objectStore);
227 }
228
229 void MemoryIDBBackingStore::restoreObjectStoreForVersionChangeAbort(std::unique_ptr<MemoryObjectStore>&& objectStore)
230 {
231     registerObjectStore(WTF::move(objectStore));
232 }
233
234 IDBError MemoryIDBBackingStore::keyExistsInObjectStore(const IDBResourceIdentifier&, uint64_t objectStoreIdentifier, const IDBKeyData& keyData, bool& keyExists)
235 {
236     LOG(IndexedDB, "MemoryIDBBackingStore::keyExistsInObjectStore");
237
238     ASSERT(objectStoreIdentifier);
239
240     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
241     RELEASE_ASSERT(objectStore);
242
243     keyExists = objectStore->containsRecord(keyData);
244     return IDBError();
245 }
246
247 IDBError MemoryIDBBackingStore::deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range)
248 {
249     LOG(IndexedDB, "MemoryIDBBackingStore::deleteRange");
250
251     ASSERT(objectStoreIdentifier);
252
253     if (!m_transactions.contains(transactionIdentifier))
254         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to delete from"));
255
256     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
257     if (!objectStore)
258         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
259
260     objectStore->deleteRange(range);
261     return IDBError();
262 }
263
264 IDBError MemoryIDBBackingStore::addRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData& keyData, const ThreadSafeDataBuffer& value)
265 {
266     LOG(IndexedDB, "MemoryIDBBackingStore::addRecord");
267
268     ASSERT(objectStoreIdentifier);
269
270     auto transaction = m_transactions.get(transactionIdentifier);
271     if (!transaction)
272         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to put record"));
273
274     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
275     if (!objectStore)
276         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found to put record"));
277
278     return objectStore->addRecord(*transaction, keyData, value);
279 }
280
281 IDBError MemoryIDBBackingStore::getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range, ThreadSafeDataBuffer& outValue)
282 {
283     LOG(IndexedDB, "MemoryIDBBackingStore::getRecord");
284
285     ASSERT(objectStoreIdentifier);
286
287     if (!m_transactions.contains(transactionIdentifier))
288         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get record"));
289
290     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
291     if (!objectStore)
292         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
293
294     outValue = objectStore->valueForKeyRange(range);
295     return IDBError();
296 }
297
298 IDBError MemoryIDBBackingStore::getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType recordType, const IDBKeyRangeData& range, IDBGetResult& outValue)
299 {
300     LOG(IndexedDB, "MemoryIDBBackingStore::getIndexRecord");
301
302     ASSERT(objectStoreIdentifier);
303
304     if (!m_transactions.contains(transactionIdentifier))
305         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get record"));
306
307     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
308     if (!objectStore)
309         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
310
311     outValue = objectStore->indexValueForKeyRange(indexIdentifier, recordType, range);
312     return IDBError();
313 }
314
315 IDBError MemoryIDBBackingStore::getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData& range, uint64_t& outCount)
316 {
317     LOG(IndexedDB, "MemoryIDBBackingStore::getCount");
318
319     ASSERT(objectStoreIdentifier);
320
321     if (!m_transactions.contains(transactionIdentifier))
322         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get count"));
323
324     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
325     if (!objectStore)
326         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
327
328     outCount = objectStore->countForKeyRange(indexIdentifier, range);
329
330     return IDBError();
331 }
332
333 IDBError MemoryIDBBackingStore::generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber)
334 {
335     LOG(IndexedDB, "MemoryIDBBackingStore::generateKeyNumber");
336     ASSERT(objectStoreIdentifier);
337     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
338     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
339
340     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
341     RELEASE_ASSERT(objectStore);
342
343     keyNumber = objectStore->currentKeyGeneratorValue();
344     objectStore->setKeyGeneratorValue(keyNumber + 1);
345
346     return IDBError();
347 }
348
349 IDBError MemoryIDBBackingStore::openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo& info, IDBGetResult& outData)
350 {
351     LOG(IndexedDB, "MemoryIDBBackingStore::openCursor");
352
353     ASSERT(!MemoryCursor::cursorForIdentifier(info.identifier()));
354
355     if (!m_transactions.contains(transactionIdentifier))
356         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found in which to open a cursor"));
357
358     switch (info.cursorSource()) {
359     case IndexedDB::CursorSource::ObjectStore: {
360         auto* objectStore = m_objectStoresByIdentifier.get(info.sourceIdentifier());
361         if (!objectStore)
362             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
363
364         MemoryCursor* cursor = objectStore->maybeOpenCursor(info);
365         if (!cursor)
366             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("Could not create object store cursor in backing store"));
367
368         cursor->currentData(outData);
369         break;
370     }
371     case IndexedDB::CursorSource::Index:
372         auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
373         if (!objectStore)
374             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
375
376         auto* index = objectStore->indexForIdentifier(info.sourceIdentifier());
377         if (!index)
378             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store index found"));
379
380         MemoryCursor* cursor = index->maybeOpenCursor(info);
381         if (!cursor)
382             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("Could not create index cursor in backing store"));
383
384         cursor->currentData(outData);
385         break;
386     }
387
388     return { };
389 }
390
391 IDBError MemoryIDBBackingStore::iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBKeyData& key, uint32_t count, IDBGetResult& outData)
392 {
393     LOG(IndexedDB, "MemoryIDBBackingStore::iterateCursor");
394
395     if (!m_transactions.contains(transactionIdentifier))
396         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found in which to iterate cursor"));
397
398     auto* cursor = MemoryCursor::cursorForIdentifier(cursorIdentifier);
399     if (!cursor)
400         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store cursor found in which to iterate cursor"));
401
402     cursor->iterate(key, count, outData);
403
404     return { };
405 }
406
407 void MemoryIDBBackingStore::registerObjectStore(std::unique_ptr<MemoryObjectStore>&& objectStore)
408 {
409     ASSERT(objectStore);
410     ASSERT(!m_objectStoresByIdentifier.contains(objectStore->info().identifier()));
411     ASSERT(!m_objectStoresByName.contains(objectStore->info().name()));
412
413     m_objectStoresByName.set(objectStore->info().name(), objectStore.get());
414     m_objectStoresByIdentifier.set(objectStore->info().identifier(), WTF::move(objectStore));
415 }
416
417 void MemoryIDBBackingStore::unregisterObjectStore(MemoryObjectStore& objectStore)
418 {
419     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
420     ASSERT(m_objectStoresByName.contains(objectStore.info().name()));
421
422     m_objectStoresByName.remove(objectStore.info().name());
423     m_objectStoresByIdentifier.remove(objectStore.info().identifier());
424 }
425
426 std::unique_ptr<MemoryObjectStore> MemoryIDBBackingStore::takeObjectStoreByName(const String& name)
427 {
428     auto rawObjectStore = m_objectStoresByName.take(name);
429     if (!rawObjectStore)
430         return nullptr;
431
432     auto objectStore = m_objectStoresByIdentifier.take(rawObjectStore->info().identifier());
433     ASSERT(objectStore);
434
435     return objectStore;
436 }
437
438 } // namespace IDBServer
439 } // namespace WebCore
440
441 #endif // ENABLE(INDEXED_DATABASE)