IndexedDB 2.0: Prefetch cursor records in the server.
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / server / SQLiteIDBBackingStore.h
1 /*
2  * Copyright (C) 2016 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 #pragma once
27
28 #if ENABLE(INDEXED_DATABASE)
29
30 #include "IDBBackingStore.h"
31 #include "IDBDatabaseIdentifier.h"
32 #include "IDBDatabaseInfo.h"
33 #include "IDBResourceIdentifier.h"
34 #include "SQLiteIDBTransaction.h"
35 #include <JavaScriptCore/Strong.h>
36 #include <wtf/HashMap.h>
37
38 namespace WebCore {
39
40 class IndexKey;
41 class SQLiteDatabase;
42 class SQLiteStatement;
43
44 namespace IDBServer {
45
46 class SQLiteIDBCursor;
47
48 class SQLiteIDBBackingStore : public IDBBackingStore {
49 public:
50     SQLiteIDBBackingStore(const IDBDatabaseIdentifier&, const String& databaseRootDirectory, IDBBackingStoreTemporaryFileHandler&);
51     
52     ~SQLiteIDBBackingStore() final;
53
54     IDBError getOrEstablishDatabaseInfo(IDBDatabaseInfo&) final;
55
56     IDBError beginTransaction(const IDBTransactionInfo&) final;
57     IDBError abortTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
58     IDBError commitTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
59     IDBError createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&) final;
60     IDBError deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
61     IDBError renameObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName) final;
62     IDBError clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
63     IDBError createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&) final;
64     IDBError deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier) final;
65     IDBError renameIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName) final;
66     IDBError keyExistsInObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, bool& keyExists) final;
67     IDBError deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&) final;
68     IDBError addRecord(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&, const IDBKeyData&, const IDBValue&) final;
69     IDBError getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&, IDBGetRecordDataType, IDBGetResult& outValue) final;
70     IDBError getAllRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue) final;
71     IDBError getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&, IDBGetResult& outValue) final;
72     IDBError getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&, uint64_t& outCount) final;
73     IDBError generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber) final;
74     IDBError revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber) final;
75     IDBError maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber) final;
76     IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) final;
77     IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) final;
78     bool prefetchCursor(const IDBResourceIdentifier&, const IDBResourceIdentifier&) final;
79
80     IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
81     void deleteBackingStore() final;
82
83     bool supportsSimultaneousTransactions() final { return false; }
84     bool isEphemeral() final { return false; }
85
86     void unregisterCursor(SQLiteIDBCursor&);
87
88     String fullDatabaseDirectory() const;
89
90     IDBBackingStoreTemporaryFileHandler& temporaryFileHandler() const { return m_temporaryFileHandler; }
91
92     IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
93
94     static String databaseNameFromEncodedFilename(const String&);
95
96 private:
97     String filenameForDatabaseName() const;
98     String fullDatabasePath() const;
99
100     bool ensureValidRecordsTable();
101     bool ensureValidIndexRecordsTable();
102     bool ensureValidIndexRecordsIndex();
103     bool ensureValidBlobTables();
104     std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
105     std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
106
107     IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
108     IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
109     IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
110
111     IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
112     IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
113     IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
114     IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
115     IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
116     IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
117
118     IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
119
120     IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
121     IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
122
123     void closeSQLiteDB();
124
125     enum class SQL : size_t {
126         CreateObjectStoreInfo,
127         CreateObjectStoreKeyGenerator,
128         DeleteObjectStoreInfo,
129         DeleteObjectStoreKeyGenerator,
130         DeleteObjectStoreRecords,
131         DeleteObjectStoreIndexInfo,
132         DeleteObjectStoreIndexRecords,
133         DeleteObjectStoreBlobRecords,
134         RenameObjectStore,
135         ClearObjectStoreRecords,
136         ClearObjectStoreIndexRecords,
137         CreateIndexInfo,
138         DeleteIndexInfo,
139         HasIndexRecord,
140         PutIndexRecord,
141         GetIndexRecordForOneKey,
142         DeleteIndexRecords,
143         RenameIndex,
144         KeyExistsInObjectStore,
145         GetUnusedBlobFilenames,
146         DeleteUnusedBlobs,
147         GetObjectStoreRecordID,
148         DeleteBlobRecord,
149         DeleteObjectStoreRecord,
150         DeleteObjectStoreIndexRecord,
151         AddObjectStoreRecord,
152         AddBlobRecord,
153         BlobFilenameForBlobURL,
154         AddBlobFilename,
155         GetBlobURL,
156         GetKeyGeneratorValue,
157         SetKeyGeneratorValue,
158         GetAllKeyRecordsLowerOpenUpperOpen,
159         GetAllKeyRecordsLowerOpenUpperClosed,
160         GetAllKeyRecordsLowerClosedUpperOpen,
161         GetAllKeyRecordsLowerClosedUpperClosed,
162         GetValueRecordsLowerOpenUpperOpen,
163         GetValueRecordsLowerOpenUpperClosed,
164         GetValueRecordsLowerClosedUpperOpen,
165         GetValueRecordsLowerClosedUpperClosed,
166         GetKeyRecordsLowerOpenUpperOpen,
167         GetKeyRecordsLowerOpenUpperClosed,
168         GetKeyRecordsLowerClosedUpperOpen,
169         GetKeyRecordsLowerClosedUpperClosed,
170         Count
171     };
172
173     SQLiteStatement* cachedStatement(SQL, const char*);
174     SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
175
176     std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Count)];
177
178     JSC::VM& vm();
179     JSC::JSGlobalObject& globalObject();
180     void initializeVM();
181
182     IDBDatabaseIdentifier m_identifier;
183     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
184     std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
185
186     std::unique_ptr<SQLiteDatabase> m_sqliteDB;
187
188     HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
189     HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
190
191     String m_absoluteDatabaseDirectory;
192
193     RefPtr<JSC::VM> m_vm;
194     JSC::Strong<JSC::JSGlobalObject> m_globalObject;
195
196     IDBBackingStoreTemporaryFileHandler& m_temporaryFileHandler;
197 };
198
199 } // namespace IDBServer
200 } // namespace WebCore
201
202 #endif // ENABLE(INDEXED_DATABASE)