File: IDBLevelDBCoding.h

package info (click to toggle)
qtwebkit 2.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 143,644 kB
  • sloc: cpp: 913,445; python: 19,625; ansic: 16,252; perl: 11,604; yacc: 3,615; asm: 992; lex: 555; sh: 379; makefile: 375; objc: 347; ruby: 23; xml: 5
file content (278 lines) | stat: -rw-r--r-- 9,477 bytes parent folder | download | duplicates (7)
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
/*
 * 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 ENABLE(LEVELDB)

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

namespace WebCore {

class IDBKey;
class LevelDBSlice;

namespace IDBLevelDBCoding {

const unsigned char kMinimumIndexId = 30;

Vector<char> encodeByte(unsigned char);
Vector<char> maxIDBKey();
Vector<char> minIDBKey();
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);
Vector<char> encodeDouble(double);
const char* decodeDouble(const char* p, const char* limit, double*);
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>&);

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 {
        kGlobalMetaData,
        kDatabaseMetaData,
        kObjectStoreData,
        kExistsEntry,
        kIndexData,
        kInvalidType
    };

    Type type() const;

    int64_t m_databaseId;
    int64_t m_objectStoreId;
    int64_t m_indexId;

    static const int64_t kInvalidId = -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);
    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);
    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 {
        kOriginName = 0,
        kDatabaseName = 1,
        kUserVersion = 2,
        kMaxObjectStoreId = 3
    };

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

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

private:
    int64_t m_objectStoreId;
    int64_t m_metaDataType; // FIXME: Make this a byte.
};

class IndexMetaDataKey {
public:
    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);
    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);
    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);
    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 kSpecialIndexNumber;

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 kSpecialIndexNumber;

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, int64_t sequenceNumber);
    static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, const IDBKey& userKey, int64_t sequenceNumber);
    static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
    int compare(const IndexDataKey& other, bool ignoreSequenceNumber);
    int64_t databaseId() const;
    int64_t objectStoreId() const;
    int64_t indexId() const;
    PassRefPtr<IDBKey> userKey() const;

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

} // namespace IDBLevelDBCoding

} // namespace WebCore

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

#endif // IDBLevelDBCoding_h