1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
/*
* Copyright (C) 2014, 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "SQLiteIDBCursor.h"
#if ENABLE(INDEXED_DATABASE)
#include "IDBCursorInfo.h"
#include "IDBGetResult.h"
#include "IDBSerialization.h"
#include "Logging.h"
#include "SQLiteIDBBackingStore.h"
#include "SQLiteIDBTransaction.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include <sqlite3.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
namespace IDBServer {
static const size_t prefetchLimit = 8;
std::unique_ptr<SQLiteIDBCursor> SQLiteIDBCursor::maybeCreate(SQLiteIDBTransaction& transaction, const IDBCursorInfo& info)
{
auto cursor = std::make_unique<SQLiteIDBCursor>(transaction, info);
if (!cursor->establishStatement())
return nullptr;
if (!cursor->advance(1))
return nullptr;
return cursor;
}
std::unique_ptr<SQLiteIDBCursor> SQLiteIDBCursor::maybeCreateBackingStoreCursor(SQLiteIDBTransaction& transaction, const uint64_t objectStoreID, const uint64_t indexID, const IDBKeyRangeData& range)
{
auto cursor = std::make_unique<SQLiteIDBCursor>(transaction, objectStoreID, indexID, range);
if (!cursor->establishStatement())
return nullptr;
if (!cursor->advance(1))
return nullptr;
return cursor;
}
SQLiteIDBCursor::SQLiteIDBCursor(SQLiteIDBTransaction& transaction, const IDBCursorInfo& info)
: m_transaction(&transaction)
, m_cursorIdentifier(info.identifier())
, m_objectStoreID(info.objectStoreIdentifier())
, m_indexID(info.cursorSource() == IndexedDB::CursorSource::Index ? info.sourceIdentifier() : IDBIndexInfo::InvalidId)
, m_cursorDirection(info.cursorDirection())
, m_cursorType(info.cursorType())
, m_keyRange(info.range())
{
ASSERT(m_objectStoreID);
}
SQLiteIDBCursor::SQLiteIDBCursor(SQLiteIDBTransaction& transaction, const uint64_t objectStoreID, const uint64_t indexID, const IDBKeyRangeData& range)
: m_transaction(&transaction)
, m_cursorIdentifier(transaction.transactionIdentifier())
, m_objectStoreID(objectStoreID)
, m_indexID(indexID ? indexID : IDBIndexInfo::InvalidId)
, m_cursorDirection(IndexedDB::CursorDirection::Next)
, m_cursorType(IndexedDB::CursorType::KeyAndValue)
, m_keyRange(range)
, m_backingStoreCursor(true)
{
ASSERT(m_objectStoreID);
}
SQLiteIDBCursor::~SQLiteIDBCursor()
{
if (m_backingStoreCursor)
m_transaction->closeCursor(*this);
}
void SQLiteIDBCursor::currentData(IDBGetResult& result)
{
ASSERT(!m_fetchedRecords.isEmpty());
auto& currentRecord = m_fetchedRecords.first();
if (currentRecord.completed) {
ASSERT(!currentRecord.errored);
result = { };
return;
}
result = { currentRecord.record.key, currentRecord.record.primaryKey, currentRecord.record.value ? *currentRecord.record.value : IDBValue() };
}
static String buildIndexStatement(const IDBKeyRangeData& keyRange, IndexedDB::CursorDirection cursorDirection)
{
StringBuilder builder;
builder.appendLiteral("SELECT rowid, key, value FROM IndexRecords WHERE indexID = ? AND objectStoreID = ? AND key ");
if (!keyRange.lowerKey.isNull() && !keyRange.lowerOpen)
builder.appendLiteral(">=");
else
builder.append('>');
builder.appendLiteral(" CAST(? AS TEXT) AND key ");
if (!keyRange.upperKey.isNull() && !keyRange.upperOpen)
builder.appendLiteral("<=");
else
builder.append('<');
builder.appendLiteral(" CAST(? AS TEXT) ORDER BY key");
if (cursorDirection == IndexedDB::CursorDirection::Prev || cursorDirection == IndexedDB::CursorDirection::Prevunique)
builder.appendLiteral(" DESC");
builder.appendLiteral(", value");
if (cursorDirection == IndexedDB::CursorDirection::Prev)
builder.appendLiteral(" DESC");
builder.append(';');
return builder.toString();
}
static String buildObjectStoreStatement(const IDBKeyRangeData& keyRange, IndexedDB::CursorDirection cursorDirection)
{
StringBuilder builder;
builder.appendLiteral("SELECT rowid, key, value FROM Records WHERE objectStoreID = ? AND key ");
if (!keyRange.lowerKey.isNull() && !keyRange.lowerOpen)
builder.appendLiteral(">=");
else
builder.append('>');
builder.appendLiteral(" CAST(? AS TEXT) AND key ");
if (!keyRange.upperKey.isNull() && !keyRange.upperOpen)
builder.appendLiteral("<=");
else
builder.append('<');
builder.appendLiteral(" CAST(? AS TEXT) ORDER BY key");
if (cursorDirection == IndexedDB::CursorDirection::Prev || cursorDirection == IndexedDB::CursorDirection::Prevunique)
builder.appendLiteral(" DESC");
builder.append(';');
return builder.toString();
}
bool SQLiteIDBCursor::establishStatement()
{
ASSERT(!m_statement);
String sql;
if (m_indexID != IDBIndexInfo::InvalidId) {
sql = buildIndexStatement(m_keyRange, m_cursorDirection);
m_boundID = m_indexID;
} else {
sql = buildObjectStoreStatement(m_keyRange, m_cursorDirection);
m_boundID = m_objectStoreID;
}
m_currentLowerKey = m_keyRange.lowerKey.isNull() ? IDBKeyData::minimum() : m_keyRange.lowerKey;
m_currentUpperKey = m_keyRange.upperKey.isNull() ? IDBKeyData::maximum() : m_keyRange.upperKey;
return createSQLiteStatement(sql);
}
bool SQLiteIDBCursor::createSQLiteStatement(const String& sql)
{
LOG(IndexedDB, "Creating cursor with SQL query: \"%s\"", sql.utf8().data());
ASSERT(!m_currentLowerKey.isNull());
ASSERT(!m_currentUpperKey.isNull());
ASSERT(m_transaction->sqliteTransaction());
m_statement = std::make_unique<SQLiteStatement>(m_transaction->sqliteTransaction()->database(), sql);
if (m_statement->prepare() != SQLITE_OK) {
LOG_ERROR("Could not create cursor statement (prepare/id) - '%s'", m_transaction->sqliteTransaction()->database().lastErrorMsg());
return false;
}
return bindArguments();
}
void SQLiteIDBCursor::objectStoreRecordsChanged()
{
if (m_statementNeedsReset)
return;
// If ObjectStore or Index contents changed, we need to reset the statement and bind new parameters to it.
// This is to pick up any changes that might exist.
// We also need to throw away any fetched records as they may no longer be valid.
m_statementNeedsReset = true;
ASSERT(!m_fetchedRecords.isEmpty());
if (m_cursorDirection == IndexedDB::CursorDirection::Next || m_cursorDirection == IndexedDB::CursorDirection::Nextunique) {
m_currentLowerKey = m_fetchedRecords.first().record.key;
if (!m_keyRange.lowerOpen) {
m_keyRange.lowerOpen = true;
m_keyRange.lowerKey = m_currentLowerKey;
m_statement = nullptr;
}
} else {
m_currentUpperKey = m_fetchedRecords.first().record.key;
if (!m_keyRange.upperOpen) {
m_keyRange.upperOpen = true;
m_keyRange.upperKey = m_currentUpperKey;
m_statement = nullptr;
}
}
m_currentKeyForUniqueness = m_fetchedRecords.first().record.key;
m_fetchedRecords.clear();
}
void SQLiteIDBCursor::resetAndRebindStatement()
{
ASSERT(!m_currentLowerKey.isNull());
ASSERT(!m_currentUpperKey.isNull());
ASSERT(m_transaction->sqliteTransaction());
ASSERT(m_statementNeedsReset);
m_statementNeedsReset = false;
if (!m_statement && !establishStatement()) {
LOG_ERROR("Unable to establish new statement for cursor iteration");
return;
}
if (m_statement->reset() != SQLITE_OK) {
LOG_ERROR("Could not reset cursor statement to respond to object store changes");
return;
}
bindArguments();
}
bool SQLiteIDBCursor::bindArguments()
{
LOG(IndexedDB, "Cursor is binding lower key '%s' and upper key '%s'", m_currentLowerKey.loggingString().utf8().data(), m_currentUpperKey.loggingString().utf8().data());
int currentBindArgument = 1;
if (m_statement->bindInt64(currentBindArgument++, m_boundID) != SQLITE_OK) {
LOG_ERROR("Could not bind id argument (bound ID)");
return false;
}
if (m_indexID != IDBIndexInfo::InvalidId && m_statement->bindInt64(currentBindArgument++, m_objectStoreID) != SQLITE_OK) {
LOG_ERROR("Could not bind object store id argument for an index cursor");
return false;
}
RefPtr<SharedBuffer> buffer = serializeIDBKeyData(m_currentLowerKey);
if (m_statement->bindBlob(currentBindArgument++, buffer->data(), buffer->size()) != SQLITE_OK) {
LOG_ERROR("Could not create cursor statement (lower key)");
return false;
}
buffer = serializeIDBKeyData(m_currentUpperKey);
if (m_statement->bindBlob(currentBindArgument++, buffer->data(), buffer->size()) != SQLITE_OK) {
LOG_ERROR("Could not create cursor statement (upper key)");
return false;
}
return true;
}
bool SQLiteIDBCursor::prefetch()
{
LOG(IndexedDB, "SQLiteIDBCursor::prefetch() - Cursor already has %zu fetched records", m_fetchedRecords.size());
if (m_fetchedRecords.isEmpty() || m_fetchedRecords.size() >= prefetchLimit || m_fetchedRecords.last().isTerminalRecord())
return false;
m_currentKeyForUniqueness = m_fetchedRecords.last().record.key;
fetch();
return m_fetchedRecords.size() < prefetchLimit;
}
bool SQLiteIDBCursor::advance(uint64_t count)
{
LOG(IndexedDB, "SQLiteIDBCursor::advance() - Count %" PRIu64 ", %zu fetched records", count, m_fetchedRecords.size());
ASSERT(count);
if (!m_fetchedRecords.isEmpty() && m_fetchedRecords.first().isTerminalRecord()) {
LOG_ERROR("Attempt to advance a completed cursor");
return false;
}
if (!m_fetchedRecords.isEmpty())
m_currentKeyForUniqueness = m_fetchedRecords.last().record.key;
// Drop already-fetched records up to `count` to see if we've already fetched the record we're looking for.
bool hadCurrentRecord = !m_fetchedRecords.isEmpty();
for (; count && !m_fetchedRecords.isEmpty(); --count) {
if (m_fetchedRecords.first().isTerminalRecord())
break;
m_fetchedRecords.removeFirst();
}
// If we still have any records left, the first record is our new current record.
if (!m_fetchedRecords.isEmpty())
return true;
ASSERT(m_fetchedRecords.isEmpty());
// If we started out with a current record, we burnt a count on removing it.
// Replace that count now.
if (hadCurrentRecord)
++count;
for (; count; --count) {
if (!m_fetchedRecords.isEmpty()) {
ASSERT(m_fetchedRecords.size() == 1);
m_currentKeyForUniqueness = m_fetchedRecords.first().record.key;
m_fetchedRecords.removeFirst();
}
if (!fetch())
return false;
ASSERT(!m_fetchedRecords.isEmpty());
ASSERT(!m_fetchedRecords.first().errored);
if (m_fetchedRecords.first().completed)
break;
}
return true;
}
bool SQLiteIDBCursor::fetch()
{
ASSERT(m_fetchedRecords.isEmpty() || !m_fetchedRecords.last().isTerminalRecord());
m_fetchedRecords.append({ });
bool isUnique = m_cursorDirection == IndexedDB::CursorDirection::Nextunique || m_cursorDirection == IndexedDB::CursorDirection::Prevunique;
if (!isUnique)
return fetchNextRecord(m_fetchedRecords.last());
while (!m_fetchedRecords.last().completed) {
if (!fetchNextRecord(m_fetchedRecords.last()))
return false;
// If the new current key is different from the old current key, we're done.
if (m_currentKeyForUniqueness.compare(m_fetchedRecords.last().record.key))
return true;
}
return false;
}
bool SQLiteIDBCursor::fetchNextRecord(SQLiteCursorRecord& record)
{
if (m_statementNeedsReset)
resetAndRebindStatement();
FetchResult result;
do {
result = internalFetchNextRecord(record);
} while (result == FetchResult::ShouldFetchAgain);
return result == FetchResult::Success;
}
void SQLiteIDBCursor::markAsErrored(SQLiteCursorRecord& record)
{
record.record = { };
record.completed = true;
record.errored = true;
record.rowID = 0;
}
SQLiteIDBCursor::FetchResult SQLiteIDBCursor::internalFetchNextRecord(SQLiteCursorRecord& record)
{
ASSERT(m_transaction->sqliteTransaction());
ASSERT(m_statement);
ASSERT(!m_fetchedRecords.isEmpty());
ASSERT(!m_fetchedRecords.last().isTerminalRecord());
record.record.value = nullptr;
int result = m_statement->step();
if (result == SQLITE_DONE) {
// When a cursor reaches its end, that is indicated by having undefined keys/values
record = { };
record.completed = true;
return FetchResult::Success;
}
if (result != SQLITE_ROW) {
LOG_ERROR("Error advancing cursor - (%i) %s", result, m_transaction->sqliteTransaction()->database().lastErrorMsg());
markAsErrored(record);
return FetchResult::Failure;
}
record.rowID = m_statement->getColumnInt64(0);
ASSERT(record.rowID);
Vector<uint8_t> keyData;
m_statement->getColumnBlobAsVector(1, keyData);
if (!deserializeIDBKeyData(keyData.data(), keyData.size(), record.record.key)) {
LOG_ERROR("Unable to deserialize key data from database while advancing cursor");
markAsErrored(record);
return FetchResult::Failure;
}
m_statement->getColumnBlobAsVector(2, keyData);
// The primaryKey of an ObjectStore cursor is the same as its key.
if (m_indexID == IDBIndexInfo::InvalidId) {
record.record.primaryKey = record.record.key;
Vector<String> blobURLs, blobFilePaths;
auto error = m_transaction->backingStore().getBlobRecordsForObjectStoreRecord(record.rowID, blobURLs, blobFilePaths);
if (!error.isNull()) {
LOG_ERROR("Unable to fetch blob records from database while advancing cursor");
markAsErrored(record);
return FetchResult::Failure;
}
if (m_cursorType == IndexedDB::CursorType::KeyAndValue)
record.record.value = std::make_unique<IDBValue>(ThreadSafeDataBuffer::adoptVector(keyData), blobURLs, blobFilePaths);
} else {
if (!deserializeIDBKeyData(keyData.data(), keyData.size(), record.record.primaryKey)) {
LOG_ERROR("Unable to deserialize value data from database while advancing index cursor");
markAsErrored(record);
return FetchResult::Failure;
}
SQLiteStatement objectStoreStatement(m_statement->database(), "SELECT value FROM Records WHERE key = CAST(? AS TEXT) and objectStoreID = ?;");
if (objectStoreStatement.prepare() != SQLITE_OK
|| objectStoreStatement.bindBlob(1, keyData.data(), keyData.size()) != SQLITE_OK
|| objectStoreStatement.bindInt64(2, m_objectStoreID) != SQLITE_OK) {
LOG_ERROR("Could not create index cursor statement into object store records (%i) '%s'", m_statement->database().lastError(), m_statement->database().lastErrorMsg());
markAsErrored(record);
return FetchResult::Failure;
}
int result = objectStoreStatement.step();
if (result == SQLITE_ROW) {
objectStoreStatement.getColumnBlobAsVector(0, keyData);
record.record.value = std::make_unique<IDBValue>(ThreadSafeDataBuffer::adoptVector(keyData));
} else if (result == SQLITE_DONE) {
// This indicates that the record we're trying to retrieve has been removed from the object store.
// Skip over it.
return FetchResult::ShouldFetchAgain;
} else {
LOG_ERROR("Could not step index cursor statement into object store records (%i) '%s'", m_statement->database().lastError(), m_statement->database().lastErrorMsg());
markAsErrored(record);
return FetchResult::Failure;
}
}
return FetchResult::Success;
}
bool SQLiteIDBCursor::iterate(const IDBKeyData& targetKey, const IDBKeyData& targetPrimaryKey)
{
ASSERT(m_transaction->sqliteTransaction());
ASSERT(m_statement);
bool result = advance(1);
ASSERT(!m_fetchedRecords.isEmpty());
// Iterating with no key is equivalent to advancing 1 step.
if (targetKey.isNull() || !result)
return result;
while (!m_fetchedRecords.first().isTerminalRecord()) {
if (!result)
return false;
// Search for the next key >= the target if the cursor is a Next cursor, or the next key <= if the cursor is a Previous cursor.
if (m_cursorDirection == IndexedDB::CursorDirection::Next || m_cursorDirection == IndexedDB::CursorDirection::Nextunique) {
if (m_fetchedRecords.first().record.key.compare(targetKey) >= 0)
break;
} else if (m_fetchedRecords.first().record.key.compare(targetKey) <= 0)
break;
result = advance(1);
}
if (targetPrimaryKey.isValid()) {
while (!m_fetchedRecords.first().isTerminalRecord() && !m_fetchedRecords.first().record.key.compare(targetKey)) {
if (!result)
return false;
// Search for the next primary key >= the primary target if the cursor is a Next cursor, or the next key <= if the cursor is a Previous cursor.
if (m_cursorDirection == IndexedDB::CursorDirection::Next || m_cursorDirection == IndexedDB::CursorDirection::Nextunique) {
if (m_fetchedRecords.first().record.primaryKey.compare(targetPrimaryKey) >= 0)
break;
} else if (m_fetchedRecords.first().record.primaryKey.compare(targetPrimaryKey) <= 0)
break;
result = advance(1);
}
}
return result;
}
const IDBKeyData& SQLiteIDBCursor::currentKey() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().record.key;
}
const IDBKeyData& SQLiteIDBCursor::currentPrimaryKey() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().record.primaryKey;
}
IDBValue* SQLiteIDBCursor::currentValue() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().record.value.get();
}
bool SQLiteIDBCursor::didComplete() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().completed;
}
bool SQLiteIDBCursor::didError() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().errored;
}
int64_t SQLiteIDBCursor::currentRecordRowID() const
{
ASSERT(!m_fetchedRecords.isEmpty());
return m_fetchedRecords.first().rowID;
}
} // namespace IDBServer
} // namespace WebCore
#endif // ENABLE(INDEXED_DATABASE)
|