c9abc6ded2c91705aadae62220158cca2f6daec2
[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 "IDBGetAllRecordsData.h"
33 #include "IDBGetResult.h"
34 #include "IDBIndexInfo.h"
35 #include "IDBKeyRangeData.h"
36 #include "Logging.h"
37 #include "MemoryIndexCursor.h"
38 #include "MemoryObjectStore.h"
39 #include "MemoryObjectStoreCursor.h"
40
41 namespace WebCore {
42 namespace IDBServer {
43
44 // The IndexedDB spec states the value you can get from the key generator is 2^53
45 static uint64_t maxGeneratedKeyValue = 0x20000000000000;
46
47 std::unique_ptr<MemoryIDBBackingStore> MemoryIDBBackingStore::create(const IDBDatabaseIdentifier& identifier)
48 {
49     return std::make_unique<MemoryIDBBackingStore>(identifier);
50 }
51
52 MemoryIDBBackingStore::MemoryIDBBackingStore(const IDBDatabaseIdentifier& identifier)
53     : m_identifier(identifier)
54 {
55 }
56
57 MemoryIDBBackingStore::~MemoryIDBBackingStore()
58 {
59 }
60
61 IDBError MemoryIDBBackingStore::getOrEstablishDatabaseInfo(IDBDatabaseInfo& info)
62 {
63     if (!m_databaseInfo)
64         m_databaseInfo = std::make_unique<IDBDatabaseInfo>(m_identifier.databaseName(), 0);
65
66     info = *m_databaseInfo;
67     return { };
68 }
69
70 void MemoryIDBBackingStore::setDatabaseInfo(const IDBDatabaseInfo& info)
71 {
72     // It is not valid to directly set database info on a backing store that hasn't already set its own database info.
73     ASSERT(m_databaseInfo);
74
75     m_databaseInfo = std::make_unique<IDBDatabaseInfo>(info);
76 }
77
78 IDBError MemoryIDBBackingStore::beginTransaction(const IDBTransactionInfo& info)
79 {
80     LOG(IndexedDB, "MemoryIDBBackingStore::beginTransaction");
81
82     if (m_transactions.contains(info.identifier()))
83         return { IDBDatabaseException::InvalidStateError, "Backing store asked to create transaction it already has a record of" };
84
85     auto transaction = MemoryBackingStoreTransaction::create(*this, info);
86
87     // VersionChange transactions are scoped to "every object store".
88     if (transaction->isVersionChange()) {
89         for (auto& objectStore : m_objectStoresByIdentifier.values())
90             transaction->addExistingObjectStore(*objectStore);
91     } else if (transaction->isWriting()) {
92         for (auto& iterator : m_objectStoresByName) {
93             if (info.objectStores().contains(iterator.key))
94                 transaction->addExistingObjectStore(*iterator.value);
95         }
96     }
97
98     m_transactions.set(info.identifier(), WTFMove(transaction));
99
100     return { };
101 }
102
103 IDBError MemoryIDBBackingStore::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
104 {
105     LOG(IndexedDB, "MemoryIDBBackingStore::abortTransaction - %s", transactionIdentifier.loggingString().utf8().data());
106
107     auto transaction = m_transactions.take(transactionIdentifier);
108     if (!transaction)
109         return { IDBDatabaseException::InvalidStateError, "Backing store asked to abort transaction it didn't have record of" };
110
111     transaction->abort();
112
113     return { };
114 }
115
116 IDBError MemoryIDBBackingStore::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
117 {
118     LOG(IndexedDB, "MemoryIDBBackingStore::commitTransaction - %s", transactionIdentifier.loggingString().utf8().data());
119
120     auto transaction = m_transactions.take(transactionIdentifier);
121     if (!transaction)
122         return { IDBDatabaseException::InvalidStateError, "Backing store asked to commit transaction it didn't have record of" };
123
124     transaction->commit();
125
126     return { };
127 }
128
129 IDBError MemoryIDBBackingStore::createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo& info)
130 {
131     LOG(IndexedDB, "MemoryIDBBackingStore::createObjectStore - adding OS %s with ID %" PRIu64, info.name().utf8().data(), info.identifier());
132
133     ASSERT(m_databaseInfo);
134     if (m_databaseInfo->hasObjectStore(info.name()))
135         return { IDBDatabaseException::ConstraintError };
136
137     ASSERT(!m_objectStoresByIdentifier.contains(info.identifier()));
138     auto objectStore = MemoryObjectStore::create(info);
139
140     m_databaseInfo->addExistingObjectStore(info);
141
142     auto rawTransaction = m_transactions.get(transactionIdentifier);
143     ASSERT(rawTransaction);
144     ASSERT(rawTransaction->isVersionChange());
145
146     rawTransaction->addNewObjectStore(objectStore.get());
147     registerObjectStore(WTFMove(objectStore));
148
149     return { };
150 }
151
152 IDBError MemoryIDBBackingStore::deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier)
153 {
154     LOG(IndexedDB, "MemoryIDBBackingStore::deleteObjectStore");
155
156     ASSERT(m_databaseInfo);
157     if (!m_databaseInfo->infoForExistingObjectStore(objectStoreIdentifier))
158         return { IDBDatabaseException::ConstraintError };
159
160     auto transaction = m_transactions.get(transactionIdentifier);
161     ASSERT(transaction);
162     ASSERT(transaction->isVersionChange());
163
164     auto objectStore = takeObjectStoreByIdentifier(objectStoreIdentifier);
165     ASSERT(objectStore);
166     if (!objectStore)
167         return { IDBDatabaseException::ConstraintError };
168
169     m_databaseInfo->deleteObjectStore(objectStore->info().name());
170     transaction->objectStoreDeleted(*objectStore);
171
172     return { };
173 }
174
175 IDBError MemoryIDBBackingStore::renameObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName)
176 {
177     LOG(IndexedDB, "MemoryIDBBackingStore::renameObjectStore");
178
179     ASSERT(m_databaseInfo);
180     if (!m_databaseInfo->infoForExistingObjectStore(objectStoreIdentifier))
181         return { IDBDatabaseException::ConstraintError };
182
183     auto transaction = m_transactions.get(transactionIdentifier);
184     ASSERT(transaction);
185     ASSERT(transaction->isVersionChange());
186
187     auto objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
188     ASSERT(objectStore);
189     if (!objectStore)
190         return { IDBDatabaseException::ConstraintError };
191
192     String oldName = objectStore->info().name();
193     objectStore->rename(newName);
194     transaction->objectStoreRenamed(*objectStore, oldName);
195
196     m_databaseInfo->renameObjectStore(objectStoreIdentifier, newName);
197
198     return { };
199 }
200
201 IDBError MemoryIDBBackingStore::clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier)
202 {
203     LOG(IndexedDB, "MemoryIDBBackingStore::clearObjectStore");
204     ASSERT(objectStoreIdentifier);
205
206     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
207
208 #if !LOG_DISABLED
209     auto transaction = m_transactions.get(transactionIdentifier);
210     ASSERT(transaction->isWriting());
211 #endif
212
213     auto objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
214     if (!objectStore)
215         return { IDBDatabaseException::ConstraintError };
216
217     objectStore->clear();
218
219     return { };
220 }
221
222 IDBError MemoryIDBBackingStore::createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo& info)
223 {
224     LOG(IndexedDB, "MemoryIDBBackingStore::createIndex");
225
226     auto rawTransaction = m_transactions.get(transactionIdentifier);
227     ASSERT(rawTransaction);
228     ASSERT(rawTransaction->isVersionChange());
229
230     auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
231     if (!objectStore)
232         return { IDBDatabaseException::ConstraintError };
233
234     return objectStore->createIndex(*rawTransaction, info);
235 }
236
237 IDBError MemoryIDBBackingStore::deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier)
238 {
239     LOG(IndexedDB, "MemoryIDBBackingStore::deleteIndex");
240
241     auto rawTransaction = m_transactions.get(transactionIdentifier);
242     ASSERT(rawTransaction);
243     ASSERT(rawTransaction->isVersionChange());
244
245     auto* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
246     if (!objectStore)
247         return { IDBDatabaseException::ConstraintError };
248
249     return objectStore->deleteIndex(*rawTransaction, indexIdentifier);
250 }
251
252 IDBError MemoryIDBBackingStore::renameIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName)
253 {
254     LOG(IndexedDB, "MemoryIDBBackingStore::renameIndex");
255
256     ASSERT(m_databaseInfo);
257     auto* objectStoreInfo = m_databaseInfo->infoForExistingObjectStore(objectStoreIdentifier);
258     if (!objectStoreInfo)
259         return { IDBDatabaseException::ConstraintError };
260
261     auto* indexInfo = objectStoreInfo->infoForExistingIndex(indexIdentifier);
262     if (!indexInfo)
263         return { IDBDatabaseException::ConstraintError };
264
265     auto transaction = m_transactions.get(transactionIdentifier);
266     ASSERT(transaction);
267     ASSERT(transaction->isVersionChange());
268
269     auto objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
270     ASSERT(objectStore);
271     if (!objectStore)
272         return { IDBDatabaseException::ConstraintError };
273
274     auto* index = objectStore->indexForIdentifier(indexIdentifier);
275     ASSERT(index);
276     if (!index)
277         return { IDBDatabaseException::ConstraintError };
278
279     String oldName = index->info().name();
280     objectStore->renameIndex(*index, newName);
281     transaction->indexRenamed(*index, oldName);
282
283     indexInfo->rename(newName);
284
285     return { };
286 }
287
288 void MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort(MemoryObjectStore& objectStore)
289 {
290     LOG(IndexedDB, "MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort");
291
292     if (!m_objectStoresByIdentifier.contains(objectStore.info().identifier()))
293         return;
294
295     ASSERT(m_objectStoresByIdentifier.get(objectStore.info().identifier()) == &objectStore);
296
297     unregisterObjectStore(objectStore);
298 }
299
300 void MemoryIDBBackingStore::restoreObjectStoreForVersionChangeAbort(Ref<MemoryObjectStore>&& objectStore)
301 {
302     registerObjectStore(WTFMove(objectStore));
303 }
304
305 IDBError MemoryIDBBackingStore::keyExistsInObjectStore(const IDBResourceIdentifier&, uint64_t objectStoreIdentifier, const IDBKeyData& keyData, bool& keyExists)
306 {
307     LOG(IndexedDB, "MemoryIDBBackingStore::keyExistsInObjectStore");
308
309     ASSERT(objectStoreIdentifier);
310
311     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
312     RELEASE_ASSERT(objectStore);
313
314     keyExists = objectStore->containsRecord(keyData);
315     return { };
316 }
317
318 IDBError MemoryIDBBackingStore::deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range)
319 {
320     LOG(IndexedDB, "MemoryIDBBackingStore::deleteRange");
321
322     ASSERT(objectStoreIdentifier);
323
324     if (!m_transactions.contains(transactionIdentifier))
325         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to delete from") };
326
327     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
328     if (!objectStore)
329         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
330
331     objectStore->deleteRange(range);
332     return { };
333 }
334
335 IDBError MemoryIDBBackingStore::addRecord(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo& objectStoreInfo, const IDBKeyData& keyData, const IDBValue& value)
336 {
337     LOG(IndexedDB, "MemoryIDBBackingStore::addRecord");
338
339     ASSERT(objectStoreInfo.identifier());
340
341     auto transaction = m_transactions.get(transactionIdentifier);
342     if (!transaction)
343         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to put record") };
344
345     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreInfo.identifier());
346     if (!objectStore)
347         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found to put record") };
348
349     return objectStore->addRecord(*transaction, keyData, value);
350 }
351
352 IDBError MemoryIDBBackingStore::getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range, IDBGetResult& outValue)
353 {
354     LOG(IndexedDB, "MemoryIDBBackingStore::getRecord");
355
356     ASSERT(objectStoreIdentifier);
357
358     if (!m_transactions.contains(transactionIdentifier))
359         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get record") };
360
361     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
362     if (!objectStore)
363         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
364
365     outValue = objectStore->valueForKeyRange(range);
366     return { };
367 }
368
369 IDBError MemoryIDBBackingStore::getAllRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData& getAllRecordsData, IDBGetAllResult& result)
370 {
371     LOG(IndexedDB, "MemoryIDBBackingStore::getAllRecords");
372
373     ASSERT(getAllRecordsData.objectStoreIdentifier);
374
375     if (!m_transactions.contains(transactionIdentifier))
376         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get all records") };
377
378     auto* objectStore = m_objectStoresByIdentifier.get(getAllRecordsData.objectStoreIdentifier);
379     if (!objectStore)
380         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
381
382     if (getAllRecordsData.indexIdentifier) {
383         auto* index = objectStore->indexForIdentifier(getAllRecordsData.indexIdentifier);
384         if (!index)
385             return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store index found") };
386
387         index->getAllRecords(getAllRecordsData.keyRangeData, getAllRecordsData.count, getAllRecordsData.getAllType, result);
388     } else
389         objectStore->getAllRecords(getAllRecordsData.keyRangeData, getAllRecordsData.count, getAllRecordsData.getAllType, result);
390
391     return { };
392 }
393
394 IDBError MemoryIDBBackingStore::getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType recordType, const IDBKeyRangeData& range, IDBGetResult& outValue)
395 {
396     LOG(IndexedDB, "MemoryIDBBackingStore::getIndexRecord");
397
398     ASSERT(objectStoreIdentifier);
399
400     if (!m_transactions.contains(transactionIdentifier))
401         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get record") };
402
403     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
404     if (!objectStore)
405         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
406
407     outValue = objectStore->indexValueForKeyRange(indexIdentifier, recordType, range);
408     return { };
409 }
410
411 IDBError MemoryIDBBackingStore::getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData& range, uint64_t& outCount)
412 {
413     LOG(IndexedDB, "MemoryIDBBackingStore::getCount");
414
415     ASSERT(objectStoreIdentifier);
416
417     if (!m_transactions.contains(transactionIdentifier))
418         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get count") };
419
420     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
421     if (!objectStore)
422         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
423
424     outCount = objectStore->countForKeyRange(indexIdentifier, range);
425
426     return { };
427 }
428
429 IDBError MemoryIDBBackingStore::generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber)
430 {
431     LOG(IndexedDB, "MemoryIDBBackingStore::generateKeyNumber");
432     ASSERT(objectStoreIdentifier);
433     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
434     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
435
436     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
437     RELEASE_ASSERT(objectStore);
438
439     keyNumber = objectStore->currentKeyGeneratorValue();
440     if (keyNumber > maxGeneratedKeyValue)
441         return { IDBDatabaseException::ConstraintError, "Cannot generate new key value over 2^53 for object store operation" };
442
443     objectStore->setKeyGeneratorValue(keyNumber + 1);
444
445     return { };
446 }
447
448 IDBError MemoryIDBBackingStore::revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber)
449 {
450     LOG(IndexedDB, "MemoryIDBBackingStore::revertGeneratedKeyNumber");
451     ASSERT(objectStoreIdentifier);
452     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
453     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
454
455     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
456     RELEASE_ASSERT(objectStore);
457
458     objectStore->setKeyGeneratorValue(keyNumber);
459
460     return { };
461 }
462
463 IDBError MemoryIDBBackingStore::maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber)
464 {
465     LOG(IndexedDB, "MemoryIDBBackingStore::maybeUpdateKeyGeneratorNumber");
466     ASSERT(objectStoreIdentifier);
467     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
468     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
469
470     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
471     RELEASE_ASSERT(objectStore);
472
473     if (newKeyNumber < objectStore->currentKeyGeneratorValue())
474         return { };
475
476     uint64_t newKeyInteger(newKeyNumber);
477     if (newKeyInteger <= uint64_t(newKeyNumber))
478         ++newKeyInteger;
479
480     ASSERT(newKeyInteger > uint64_t(newKeyNumber));
481
482     objectStore->setKeyGeneratorValue(newKeyInteger);
483
484     return { };
485 }
486
487 IDBError MemoryIDBBackingStore::openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo& info, IDBGetResult& outData)
488 {
489     LOG(IndexedDB, "MemoryIDBBackingStore::openCursor");
490
491     ASSERT(!MemoryCursor::cursorForIdentifier(info.identifier()));
492
493     if (!m_transactions.contains(transactionIdentifier))
494         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found in which to open a cursor") };
495
496     switch (info.cursorSource()) {
497     case IndexedDB::CursorSource::ObjectStore: {
498         auto* objectStore = m_objectStoresByIdentifier.get(info.sourceIdentifier());
499         if (!objectStore)
500             return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
501
502         MemoryCursor* cursor = objectStore->maybeOpenCursor(info);
503         if (!cursor)
504             return { IDBDatabaseException::UnknownError, ASCIILiteral("Could not create object store cursor in backing store") };
505
506         cursor->currentData(outData);
507         break;
508     }
509     case IndexedDB::CursorSource::Index:
510         auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
511         if (!objectStore)
512             return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found") };
513
514         auto* index = objectStore->indexForIdentifier(info.sourceIdentifier());
515         if (!index)
516             return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store index found") };
517
518         MemoryCursor* cursor = index->maybeOpenCursor(info);
519         if (!cursor)
520             return { IDBDatabaseException::UnknownError, ASCIILiteral("Could not create index cursor in backing store") };
521
522         cursor->currentData(outData);
523         break;
524     }
525
526     return { };
527 }
528
529 IDBError MemoryIDBBackingStore::iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBKeyData& key, uint32_t count, IDBGetResult& outData)
530 {
531     LOG(IndexedDB, "MemoryIDBBackingStore::iterateCursor");
532
533     if (!m_transactions.contains(transactionIdentifier))
534         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found in which to iterate cursor") };
535
536     auto* cursor = MemoryCursor::cursorForIdentifier(cursorIdentifier);
537     if (!cursor)
538         return { IDBDatabaseException::UnknownError, ASCIILiteral("No backing store cursor found in which to iterate cursor") };
539
540     cursor->iterate(key, count, outData);
541
542     return { };
543 }
544
545 void MemoryIDBBackingStore::registerObjectStore(Ref<MemoryObjectStore>&& objectStore)
546 {
547     ASSERT(!m_objectStoresByIdentifier.contains(objectStore->info().identifier()));
548     ASSERT(!m_objectStoresByName.contains(objectStore->info().name()));
549
550     m_objectStoresByName.set(objectStore->info().name(), &objectStore.get());
551     m_objectStoresByIdentifier.set(objectStore->info().identifier(), WTFMove(objectStore));
552 }
553
554 void MemoryIDBBackingStore::unregisterObjectStore(MemoryObjectStore& objectStore)
555 {
556     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
557     ASSERT(m_objectStoresByName.contains(objectStore.info().name()));
558
559     m_objectStoresByName.remove(objectStore.info().name());
560     m_objectStoresByIdentifier.remove(objectStore.info().identifier());
561 }
562
563 RefPtr<MemoryObjectStore> MemoryIDBBackingStore::takeObjectStoreByIdentifier(uint64_t identifier)
564 {
565     auto objectStoreByIdentifier = m_objectStoresByIdentifier.take(identifier);
566     if (!objectStoreByIdentifier)
567         return nullptr;
568
569     auto objectStore = m_objectStoresByName.take(objectStoreByIdentifier->info().name());
570     ASSERT_UNUSED(objectStore, objectStore);
571
572     return objectStoreByIdentifier;
573 }
574
575 IDBObjectStoreInfo* MemoryIDBBackingStore::infoForObjectStore(uint64_t objectStoreIdentifier)
576 {
577     ASSERT(m_databaseInfo);
578     return m_databaseInfo->infoForExistingObjectStore(objectStoreIdentifier);
579 }
580
581 void MemoryIDBBackingStore::deleteBackingStore()
582 {
583     // The in-memory IDB backing store doesn't need to do any cleanup when it is deleted.
584 }
585
586 } // namespace IDBServer
587 } // namespace WebCore
588
589 #endif // ENABLE(INDEXED_DATABASE)