9cb98c63bc9c5f0243e700cc3802f970aac5cb3c
[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 void MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort(MemoryObjectStore& objectStore)
205 {
206     LOG(IndexedDB, "MemoryIDBBackingStore::removeObjectStoreForVersionChangeAbort");
207
208     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
209     ASSERT(m_objectStoresByIdentifier.get(objectStore.info().identifier()) == &objectStore);
210
211     unregisterObjectStore(objectStore);
212 }
213
214 void MemoryIDBBackingStore::restoreObjectStoreForVersionChangeAbort(std::unique_ptr<MemoryObjectStore>&& objectStore)
215 {
216     registerObjectStore(WTF::move(objectStore));
217 }
218
219 IDBError MemoryIDBBackingStore::keyExistsInObjectStore(const IDBResourceIdentifier&, uint64_t objectStoreIdentifier, const IDBKeyData& keyData, bool& keyExists)
220 {
221     LOG(IndexedDB, "MemoryIDBBackingStore::keyExistsInObjectStore");
222
223     ASSERT(objectStoreIdentifier);
224
225     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
226     RELEASE_ASSERT(objectStore);
227
228     keyExists = objectStore->containsRecord(keyData);
229     return IDBError();
230 }
231
232 IDBError MemoryIDBBackingStore::deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range)
233 {
234     LOG(IndexedDB, "MemoryIDBBackingStore::deleteRange");
235
236     ASSERT(objectStoreIdentifier);
237
238     if (!m_transactions.contains(transactionIdentifier))
239         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to delete from"));
240
241     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
242     if (!objectStore)
243         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
244
245     objectStore->deleteRange(range);
246     return IDBError();
247 }
248
249 IDBError MemoryIDBBackingStore::addRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData& keyData, const ThreadSafeDataBuffer& value)
250 {
251     LOG(IndexedDB, "MemoryIDBBackingStore::addRecord");
252
253     ASSERT(objectStoreIdentifier);
254
255     auto transaction = m_transactions.get(transactionIdentifier);
256     if (!transaction)
257         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to put record"));
258
259     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
260     if (!objectStore)
261         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found to put record"));
262
263     return objectStore->addRecord(*transaction, keyData, value);
264 }
265
266 IDBError MemoryIDBBackingStore::getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData& range, ThreadSafeDataBuffer& outValue)
267 {
268     LOG(IndexedDB, "MemoryIDBBackingStore::getRecord");
269
270     ASSERT(objectStoreIdentifier);
271
272     if (!m_transactions.contains(transactionIdentifier))
273         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get record"));
274
275     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
276     if (!objectStore)
277         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
278
279     outValue = objectStore->valueForKeyRange(range);
280     return IDBError();
281 }
282
283 IDBError MemoryIDBBackingStore::getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType recordType, const IDBKeyRangeData& range, IDBGetResult& outValue)
284 {
285     LOG(IndexedDB, "MemoryIDBBackingStore::getIndexRecord");
286
287     ASSERT(objectStoreIdentifier);
288
289     if (!m_transactions.contains(transactionIdentifier))
290         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get record"));
291
292     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
293     if (!objectStore)
294         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
295
296     outValue = objectStore->indexValueForKeyRange(indexIdentifier, recordType, range);
297     return IDBError();
298 }
299
300 IDBError MemoryIDBBackingStore::getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData& range, uint64_t& outCount)
301 {
302     LOG(IndexedDB, "MemoryIDBBackingStore::getCount");
303
304     ASSERT(objectStoreIdentifier);
305
306     if (!m_transactions.contains(transactionIdentifier))
307         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found to get count"));
308
309     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
310     if (!objectStore)
311         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
312
313     outCount = objectStore->countForKeyRange(indexIdentifier, range);
314
315     return IDBError();
316 }
317
318 IDBError MemoryIDBBackingStore::generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber)
319 {
320     LOG(IndexedDB, "MemoryIDBBackingStore::generateKeyNumber");
321     ASSERT(objectStoreIdentifier);
322     ASSERT_UNUSED(transactionIdentifier, m_transactions.contains(transactionIdentifier));
323     ASSERT_UNUSED(transactionIdentifier, m_transactions.get(transactionIdentifier)->isWriting());
324
325     MemoryObjectStore* objectStore = m_objectStoresByIdentifier.get(objectStoreIdentifier);
326     RELEASE_ASSERT(objectStore);
327
328     keyNumber = objectStore->currentKeyGeneratorValue();
329     objectStore->setKeyGeneratorValue(keyNumber + 1);
330
331     return IDBError();
332 }
333
334 IDBError MemoryIDBBackingStore::openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo& info, IDBGetResult& outData)
335 {
336     LOG(IndexedDB, "MemoryIDBBackingStore::openCursor");
337
338     ASSERT(!MemoryCursor::cursorForIdentifier(info.identifier()));
339
340     if (!m_transactions.contains(transactionIdentifier))
341         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found in which to open a cursor"));
342
343     switch (info.cursorSource()) {
344     case IndexedDB::CursorSource::ObjectStore: {
345         auto* objectStore = m_objectStoresByIdentifier.get(info.sourceIdentifier());
346         if (!objectStore)
347             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
348
349         MemoryCursor* cursor = objectStore->maybeOpenCursor(info);
350         if (!cursor)
351             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("Could not create object store cursor in backing store"));
352
353         cursor->currentData(outData);
354         break;
355     }
356     case IndexedDB::CursorSource::Index:
357         auto* objectStore = m_objectStoresByIdentifier.get(info.objectStoreIdentifier());
358         if (!objectStore)
359             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store object store found"));
360
361         auto* index = objectStore->indexForIdentifier(info.sourceIdentifier());
362         if (!index)
363             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store index found"));
364
365         MemoryCursor* cursor = index->maybeOpenCursor(info);
366         if (!cursor)
367             return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("Could not create index cursor in backing store"));
368
369         cursor->currentData(outData);
370         break;
371     }
372
373     return { };
374 }
375
376 IDBError MemoryIDBBackingStore::iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBKeyData& key, uint32_t count, IDBGetResult& outData)
377 {
378     LOG(IndexedDB, "MemoryIDBBackingStore::iterateCursor");
379
380     if (!m_transactions.contains(transactionIdentifier))
381         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store transaction found in which to iterate cursor"));
382
383     auto* cursor = MemoryCursor::cursorForIdentifier(cursorIdentifier);
384     if (!cursor)
385         return IDBError(IDBExceptionCode::Unknown, ASCIILiteral("No backing store cursor found in which to iterate cursor"));
386
387     cursor->iterate(key, count, outData);
388
389     return { };
390 }
391
392 void MemoryIDBBackingStore::registerObjectStore(std::unique_ptr<MemoryObjectStore>&& objectStore)
393 {
394     ASSERT(objectStore);
395     ASSERT(!m_objectStoresByIdentifier.contains(objectStore->info().identifier()));
396     ASSERT(!m_objectStoresByName.contains(objectStore->info().name()));
397
398     m_objectStoresByName.set(objectStore->info().name(), objectStore.get());
399     m_objectStoresByIdentifier.set(objectStore->info().identifier(), WTF::move(objectStore));
400 }
401
402 void MemoryIDBBackingStore::unregisterObjectStore(MemoryObjectStore& objectStore)
403 {
404     ASSERT(m_objectStoresByIdentifier.contains(objectStore.info().identifier()));
405     ASSERT(m_objectStoresByName.contains(objectStore.info().name()));
406
407     m_objectStoresByName.remove(objectStore.info().name());
408     m_objectStoresByIdentifier.remove(objectStore.info().identifier());
409 }
410
411 std::unique_ptr<MemoryObjectStore> MemoryIDBBackingStore::takeObjectStoreByName(const String& name)
412 {
413     auto rawObjectStore = m_objectStoresByName.take(name);
414     if (!rawObjectStore)
415         return nullptr;
416
417     auto objectStore = m_objectStoresByIdentifier.take(rawObjectStore->info().identifier());
418     ASSERT(objectStore);
419
420     return objectStore;
421 }
422
423 } // namespace IDBServer
424 } // namespace WebCore
425
426 #endif // ENABLE(INDEXED_DATABASE)