IndexedDB 2.0: Support IDBObjectStore getAll/getAllKeys.
[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 IDBError(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 IDBError();
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 IDBError(IDBDatabaseException::InvalidStateError, "Backing store asked to abort transaction it didn't have record of");
110
111     transaction->abort();
112
113     return IDBError();
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 IDBError(IDBDatabaseException::InvalidStateError, "Backing store asked to commit transaction it didn't have record of");
123
124     transaction->commit();
125
126     return IDBError();
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 IDBError(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 IDBError();
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 IDBError(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 IDBError(IDBDatabaseException::ConstraintError);
168
169     m_databaseInfo->deleteObjectStore(objectStore->info().name());
170     transaction->objectStoreDeleted(*objectStore);
171
172     return IDBError();
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 IDBError(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 IDBError(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 IDBError();
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 IDBError(IDBDatabaseException::ConstraintError);
216
217     objectStore->clear();
218
219     return IDBError();
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 IDBError(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 IDBError(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 IDBError(IDBDatabaseException::ConstraintError);
260
261     auto* indexInfo = objectStoreInfo->infoForExistingIndex(indexIdentifier);
262     if (!indexInfo)
263         return IDBError(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 IDBError(IDBDatabaseException::ConstraintError);
273
274     auto* index = objectStore->indexForIdentifier(indexIdentifier);
275     ASSERT(index);
276     if (!index)
277         return IDBError(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 IDBError();
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 IDBError();
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 IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to delete from"));
326
327     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
328     if (!objectStore)
329         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
330
331     objectStore->deleteRange(range);
332     return IDBError();
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 IDBError(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 IDBError(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 IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get record"));
360
361     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
362     if (!objectStore)
363         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
364
365     outValue = objectStore->valueForKeyRange(range);
366     return IDBError();
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 IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get all records"));
377
378     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(getAllRecordsData.objectStoreIdentifier);
379     if (!objectStore)
380         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
381
382     objectStore->getAllRecords(getAllRecordsData.keyRangeData, getAllRecordsData.count, getAllRecordsData.getAllType, result);
383     return { };
384 }
385
386 IDBError MemoryIDBBackingStore::getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType recordType, const IDBKeyRangeData& range, IDBGetResult& outValue)
387 {
388     LOG(IndexedDB, "MemoryIDBBackingStore::getIndexRecord");
389
390     ASSERT(objectStoreIdentifier);
391
392     if (!m_transactions.contains(transactionIdentifier))
393         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get record"));
394
395     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
396     if (!objectStore)
397         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
398
399     outValue = objectStore->indexValueForKeyRange(indexIdentifier, recordType, range);
400     return IDBError();
401 }
402
403 IDBError MemoryIDBBackingStore::getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData& range, uint64_t& outCount)
404 {
405     LOG(IndexedDB, "MemoryIDBBackingStore::getCount");
406
407     ASSERT(objectStoreIdentifier);
408
409     if (!m_transactions.contains(transactionIdentifier))
410         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found to get count"));
411
412     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
413     if (!objectStore)
414         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
415
416     outCount = objectStore->countForKeyRange(indexIdentifier, range);
417
418     return IDBError();
419 }
420
421 IDBError MemoryIDBBackingStore::generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber)
422 {
423     LOG(IndexedDB, "MemoryIDBBackingStore::generateKeyNumber");
424     ASSERT(objectStoreIdentifier);
425     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
426     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
427
428     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
429     RELEASE_ASSERT(objectStore);
430
431     keyNumber = objectStore->currentKeyGeneratorValue();
432     if (keyNumber > maxGeneratedKeyValue)
433         return { IDBDatabaseException::ConstraintError, "Cannot generate new key value over 2^53 for object store operation" };
434
435     objectStore->setKeyGeneratorValue(keyNumber + 1);
436
437     return IDBError();
438 }
439
440 IDBError MemoryIDBBackingStore::revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber)
441 {
442     LOG(IndexedDB, "MemoryIDBBackingStore::revertGeneratedKeyNumber");
443     ASSERT(objectStoreIdentifier);
444     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
445     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
446
447     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
448     RELEASE_ASSERT(objectStore);
449
450     objectStore->setKeyGeneratorValue(keyNumber);
451
452     return { };
453 }
454
455 IDBError MemoryIDBBackingStore::maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber)
456 {
457     LOG(IndexedDB, "MemoryIDBBackingStore::maybeUpdateKeyGeneratorNumber");
458     ASSERT(objectStoreIdentifier);
459     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
460     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
461
462     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
463     RELEASE_ASSERT(objectStore);
464
465     if (newKeyNumber < objectStore->currentKeyGeneratorValue())
466         return { };
467
468     uint64_t newKeyInteger(newKeyNumber);
469     if (newKeyInteger <= uint64_t(newKeyNumber))
470         ++newKeyInteger;
471
472     ASSERT(newKeyInteger > uint64_t(newKeyNumber));
473
474     objectStore->setKeyGeneratorValue(newKeyInteger);
475
476     return { };
477 }
478
479 IDBError MemoryIDBBackingStore::openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo& info, IDBGetResult& outData)
480 {
481     LOG(IndexedDB, "MemoryIDBBackingStore::openCursor");
482
483     ASSERT(!MemoryCursor::cursorForIdentifier(info.identifier()));
484
485     if (!m_transactions.contains(transactionIdentifier))
486         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found in which to open a cursor"));
487
488     switch (info.cursorSource()) {
489     case IndexedDB::CursorSource::ObjectStore: {
490         auto* objectStore = m_objectStoresByIdentifier.get(info.sourceIdentifier());
491         if (!objectStore)
492             return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
493
494         MemoryCursor* cursor = objectStore->maybeOpenCursor(info);
495         if (!cursor)
496             return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("Could not create object store cursor in backing store"));
497
498         cursor->currentData(outData);
499         break;
500     }
501     case IndexedDB::CursorSource::Index:
502         auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
503         if (!objectStore)
504             return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store object store found"));
505
506         auto* index = objectStore->indexForIdentifier(info.sourceIdentifier());
507         if (!index)
508             return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store index found"));
509
510         MemoryCursor* cursor = index->maybeOpenCursor(info);
511         if (!cursor)
512             return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("Could not create index cursor in backing store"));
513
514         cursor->currentData(outData);
515         break;
516     }
517
518     return { };
519 }
520
521 IDBError MemoryIDBBackingStore::iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBKeyData& key, uint32_t count, IDBGetResult& outData)
522 {
523     LOG(IndexedDB, "MemoryIDBBackingStore::iterateCursor");
524
525     if (!m_transactions.contains(transactionIdentifier))
526         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store transaction found in which to iterate cursor"));
527
528     auto* cursor = MemoryCursor::cursorForIdentifier(cursorIdentifier);
529     if (!cursor)
530         return IDBError(IDBDatabaseException::UnknownError, ASCIILiteral("No backing store cursor found in which to iterate cursor"));
531
532     cursor->iterate(key, count, outData);
533
534     return { };
535 }
536
537 void MemoryIDBBackingStore::registerObjectStore(Ref<MemoryObjectStore>&& objectStore)
538 {
539     ASSERT(!m_objectStoresByIdentifier.contains(objectStore->info().identifier()));
540     ASSERT(!m_objectStoresByName.contains(objectStore->info().name()));
541
542     m_objectStoresByName.set(objectStore->info().name(), &objectStore.get());
543     m_objectStoresByIdentifier.set(objectStore->info().identifier(), WTFMove(objectStore));
544 }
545
546 void MemoryIDBBackingStore::unregisterObjectStore(MemoryObjectStore& objectStore)
547 {
548     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
549     ASSERT(m_objectStoresByName.contains(objectStore.info().name()));
550
551     m_objectStoresByName.remove(objectStore.info().name());
552     m_objectStoresByIdentifier.remove(objectStore.info().identifier());
553 }
554
555 RefPtr<MemoryObjectStore> MemoryIDBBackingStore::takeObjectStoreByIdentifier(uint64_t identifier)
556 {
557     auto objectStoreByIdentifier = m_objectStoresByIdentifier.take(identifier);
558     if (!objectStoreByIdentifier)
559         return nullptr;
560
561     auto objectStore = m_objectStoresByName.take(objectStoreByIdentifier->info().name());
562     ASSERT_UNUSED(objectStore, objectStore);
563
564     return objectStoreByIdentifier;
565 }
566
567 IDBObjectStoreInfo* MemoryIDBBackingStore::infoForObjectStore(uint64_t objectStoreIdentifier)
568 {
569     ASSERT(m_databaseInfo);
570     return m_databaseInfo->infoForExistingObjectStore(objectStoreIdentifier);
571 }
572
573 void MemoryIDBBackingStore::deleteBackingStore()
574 {
575     // The in-memory IDB backing store doesn't need to do any cleanup when it is deleted.
576 }
577
578 } // namespace IDBServer
579 } // namespace WebCore
580
581 #endif // ENABLE(INDEXED_DATABASE)