summaryrefslogtreecommitdiff
path: root/Source/WebCore/Modules/indexeddb/IDBLevelDBCoding.h
blob: 1b1666ae8d9ac71656bf9025363f0d2fc0e8553f (plain)
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
/*
 * Copyright (C) 2011 Google 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 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 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.
 */

#ifndef IDBLevelDBCoding_h
#define IDBLevelDBCoding_h

#if ENABLE(INDEXED_DATABASE)
#if USE(LEVELDB)

#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>

namespace WebCore {

class IDBKey;
class IDBKeyPath;
class LevelDBSlice;

namespace IDBLevelDBCoding {

const unsigned char MinimumIndexId = 30;

// As most of the IDBKeys and encoded values are short, we initialize some Vectors with a default inline buffer size
// to reduce the memory re-allocations when the Vectors are appended.
static const size_t DefaultInlineBufferSize = 32;

Vector<char> encodeByte(unsigned char);
const char* decodeByte(const char* p, const char* limit, unsigned char& foundChar);
Vector<char> maxIDBKey();
Vector<char> minIDBKey();
Vector<char> encodeBool(bool);
bool decodeBool(const char* begin, const char* end);
Vector<char> encodeInt(int64_t);
int64_t decodeInt(const char* begin, const char* end);
Vector<char> encodeVarInt(int64_t);
const char* decodeVarInt(const char* p, const char* limit, int64_t& foundInt);
Vector<char> encodeString(const String&);
String decodeString(const char* p, const char* end);
Vector<char> encodeStringWithLength(const String&);
const char* decodeStringWithLength(const char* p, const char* limit, String& foundString);
int compareEncodedStringsWithLength(const char*& p, const char* limitP, const char*& q, const char* limitQ);
Vector<char> encodeDouble(double);
const char* decodeDouble(const char* p, const char* limit, double*);
void encodeIDBKey(const IDBKey&, Vector<char, DefaultInlineBufferSize>& into);
Vector<char> encodeIDBKey(const IDBKey&);
const char* decodeIDBKey(const char* p, const char* limit, RefPtr<IDBKey>& foundKey);
const char* extractEncodedIDBKey(const char* start, const char* limit, Vector<char>* result);
int compareEncodedIDBKeys(const Vector<char>&, const Vector<char>&);
Vector<char> encodeIDBKeyPath(const IDBKeyPath&);
IDBKeyPath decodeIDBKeyPath(const char*, const char*);

int compare(const LevelDBSlice&, const LevelDBSlice&, bool indexKeys = false);

class KeyPrefix {
public:
    KeyPrefix();
    KeyPrefix(int64_t databaseId, int64_t objectStoreId, int64_t indexId);

    static const char* decode(const char* start, const char* limit, KeyPrefix* result);
    Vector<char> encode() const;
    int compare(const KeyPrefix& other) const;

    enum Type {
        GlobalMetaData,
        DatabaseMetaData,
        ObjectStoreData,
        ExistsEntry,
        IndexData,
        InvalidType
    };

    Type type() const;

    int64_t m_databaseId;
    int64_t m_objectStoreId;
    int64_t m_indexId;

    static const int64_t InvalidId = -1;
};

class SchemaVersionKey {
public:
    static Vector<char> encode();
};

class MaxDatabaseIdKey {
public:
    static Vector<char> encode();
};

class DatabaseFreeListKey {
public:
    DatabaseFreeListKey();
    static const char* decode(const char* start, const char* limit, DatabaseFreeListKey* result);
    static Vector<char> encode(int64_t databaseId);
    static Vector<char> encodeMaxKey();
    int64_t databaseId() const;
    int compare(const DatabaseFreeListKey& other) const;

private:
    int64_t m_databaseId;
};

class DatabaseNameKey {
public:
    static const char* decode(const char* start, const char* limit, DatabaseNameKey* result);
    static Vector<char> encode(const String& origin, const String& databaseName);
    static Vector<char> encodeMinKeyForOrigin(const String& origin);
    static Vector<char> encodeStopKeyForOrigin(const String& origin);
    String origin() const { return m_origin; }
    String databaseName() const { return m_databaseName; }
    int compare(const DatabaseNameKey& other);

private:
    String m_origin; // FIXME: Store encoded strings, or just pointers.
    String m_databaseName;
};

class DatabaseMetaDataKey {
public:
    enum MetaDataType {
        OriginName = 0,
        DatabaseName = 1,
        UserVersion = 2,
        MaxObjectStoreId = 3,
        UserIntVersion = 4
    };

    static Vector<char> encode(int64_t databaseId, MetaDataType);
};

class ObjectStoreMetaDataKey {
public:
    enum MetaDataType {
        Name = 0,
        KeyPath = 1,
        AutoIncrement = 2,
        Evictable = 3,
        LastVersion = 4,
        MaxIndexId = 5,
        HasKeyPath = 6,
        KeyGeneratorCurrentNumber = 7
    };

    ObjectStoreMetaDataKey();
    static const char* decode(const char* start, const char* limit, ObjectStoreMetaDataKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, unsigned char metaDataType);
    static Vector<char> encodeMaxKey(int64_t databaseId);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
    int64_t objectStoreId() const;
    unsigned char metaDataType() const;
    int compare(const ObjectStoreMetaDataKey& other);

private:
    int64_t m_objectStoreId;
    unsigned char m_metaDataType;
};

class IndexMetaDataKey {
public:
    enum MetaDataType {
        Name = 0,
        Unique = 1,
        KeyPath = 2,
        MultiEntry = 3
    };

    IndexMetaDataKey();
    static const char* decode(const char* start, const char* limit, IndexMetaDataKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, unsigned char metaDataType);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
    int compare(const IndexMetaDataKey& other);
    int64_t indexId() const;
    unsigned char metaDataType() const { return m_metaDataType; }

private:
    int64_t m_objectStoreId;
    int64_t m_indexId;
    unsigned char m_metaDataType;
};

class ObjectStoreFreeListKey {
public:
    ObjectStoreFreeListKey();
    static const char* decode(const char* start, const char* limit, ObjectStoreFreeListKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId);
    static Vector<char> encodeMaxKey(int64_t databaseId);
    int64_t objectStoreId() const;
    int compare(const ObjectStoreFreeListKey& other);

private:
    int64_t m_objectStoreId;
};

class IndexFreeListKey {
public:
    IndexFreeListKey();
    static const char* decode(const char* start, const char* limit, IndexFreeListKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
    int compare(const IndexFreeListKey& other);
    int64_t objectStoreId() const;
    int64_t indexId() const;

private:
    int64_t m_objectStoreId;
    int64_t m_indexId;
};

class ObjectStoreNamesKey {
public:
    // FIXME: We never use this to look up object store ids, because a mapping
    // is kept in the IDBDatabaseBackendImpl. Can the mapping become unreliable?
    // Can we remove this?
    static const char* decode(const char* start, const char* limit, ObjectStoreNamesKey* result);
    static Vector<char> encode(int64_t databaseId, const String& objectStoreName);
    int compare(const ObjectStoreNamesKey& other);
    String objectStoreName() const { return m_objectStoreName; }

private:
    String m_objectStoreName; // FIXME: Store the encoded string, or just pointers to it.
};

class IndexNamesKey {
public:
    IndexNamesKey();
    // FIXME: We never use this to look up index ids, because a mapping
    // is kept at a higher level.
    static const char* decode(const char* start, const char* limit, IndexNamesKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const String& indexName);
    int compare(const IndexNamesKey& other);
    String indexName() const { return m_indexName; }

private:
    int64_t m_objectStoreId;
    String m_indexName;
};

class ObjectStoreDataKey {
public:
    static const char* decode(const char* start, const char* end, ObjectStoreDataKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const Vector<char> encodedUserKey);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const IDBKey& userKey);
    int compare(const ObjectStoreDataKey& other);
    PassRefPtr<IDBKey> userKey() const;
    static const int64_t SpecialIndexNumber;

private:
    Vector<char> m_encodedUserKey;
};

class ExistsEntryKey {
public:
    static const char* decode(const char* start, const char* end, ExistsEntryKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const Vector<char>& encodedKey);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const IDBKey& userKey);
    int compare(const ExistsEntryKey& other);
    PassRefPtr<IDBKey> userKey() const;

    static const int64_t SpecialIndexNumber;

private:
    Vector<char> m_encodedUserKey;
};

class IndexDataKey {
public:
    IndexDataKey();
    static const char* decode(const char* start, const char* limit, IndexDataKey* result);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, const Vector<char>& encodedUserKey, const Vector<char>& encodedPrimaryKey, int64_t sequenceNumber = 0);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, const IDBKey& userKey);
    static Vector<char> encodeMinKey(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
    int compare(const IndexDataKey& other, bool ignoreDuplicates);
    int64_t databaseId() const;
    int64_t objectStoreId() const;
    int64_t indexId() const;
    PassRefPtr<IDBKey> userKey() const;
    PassRefPtr<IDBKey> primaryKey() const;

private:
    int64_t m_databaseId;
    int64_t m_objectStoreId;
    int64_t m_indexId;
    Vector<char> m_encodedUserKey;
    Vector<char> m_encodedPrimaryKey;
    int64_t m_sequenceNumber;
};

} // namespace IDBLevelDBCoding

} // namespace WebCore

#endif // USE(LEVELDB)
#endif // ENABLE(INDEXED_DATABASE)

#endif // IDBLevelDBCoding_h