initial import
[vuplus_webkit] / Source / WebCore / storage / IDBLevelDBCoding.h
1 /*
2  * Copyright (C) 2011 Google 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IDBLevelDBCoding_h
27 #define IDBLevelDBCoding_h
28
29 #if ENABLE(INDEXED_DATABASE)
30 #if ENABLE(LEVELDB)
31
32 #include <wtf/RefPtr.h>
33 #include <wtf/Vector.h>
34 #include <wtf/text/WTFString.h>
35
36 namespace WebCore {
37
38 class IDBKey;
39 class LevelDBSlice;
40
41 namespace IDBLevelDBCoding {
42
43 const unsigned char kMinimumIndexId = 30;
44
45 Vector<char> encodeByte(unsigned char);
46 Vector<char> maxIDBKey();
47 Vector<char> minIDBKey();
48 Vector<char> encodeInt(int64_t);
49 int64_t decodeInt(const char* begin, const char* end);
50 Vector<char> encodeVarInt(int64_t);
51 const char* decodeVarInt(const char *p, const char* limit, int64_t& foundInt);
52 Vector<char> encodeString(const String&);
53 String decodeString(const char* p, const char* end);
54 Vector<char> encodeStringWithLength(const String&);
55 const char* decodeStringWithLength(const char* p, const char* limit, String& foundString);
56 Vector<char> encodeDouble(double);
57 const char* decodeDouble(const char* p, const char* limit, double*);
58 Vector<char> encodeIDBKey(const IDBKey&);
59 const char* decodeIDBKey(const char* p, const char* limit, RefPtr<IDBKey>& foundKey);
60 const char* extractEncodedIDBKey(const char* start, const char* limit, Vector<char>* result);
61 int compareEncodedIDBKeys(const Vector<char>&, const Vector<char>&);
62
63 int compare(const LevelDBSlice&, const LevelDBSlice&, bool indexKeys = false);
64
65 class KeyPrefix {
66 public:
67     KeyPrefix();
68     KeyPrefix(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
69
70     static const char* decode(const char* start, const char* limit, KeyPrefix* result);
71     Vector<char> encode() const;
72     int compare(const KeyPrefix& other) const;
73
74     enum Type {
75         kGlobalMetaData,
76         kDatabaseMetaData,
77         kObjectStoreData,
78         kExistsEntry,
79         kIndexData,
80         kInvalidType
81     };
82
83     Type type() const;
84
85     int64_t m_databaseId;
86     int64_t m_objectStoreId;
87     int64_t m_indexId;
88
89     static const int64_t kInvalidId = -1;
90 };
91
92 class SchemaVersionKey {
93 public:
94     static Vector<char> encode();
95 };
96
97 class MaxDatabaseIdKey {
98 public:
99     static Vector<char> encode();
100 };
101
102 class DatabaseFreeListKey {
103 public:
104     DatabaseFreeListKey();
105     static const char* decode(const char* start, const char* limit, DatabaseFreeListKey* result);
106     static Vector<char> encode(int64_t databaseId);
107     static Vector<char> encodeMaxKey();
108     int64_t databaseId() const;
109     int compare(const DatabaseFreeListKey& other) const;
110
111 private:
112     int64_t m_databaseId;
113 };
114
115 class DatabaseNameKey {
116 public:
117     static const char* decode(const char* start, const char* limit, DatabaseNameKey* result);
118     static Vector<char> encode(const String& origin, const String& databaseName);
119     String origin() const { return m_origin; }
120     String databaseName() const { return m_databaseName; }
121     int compare(const DatabaseNameKey& other);
122
123 private:
124     String m_origin; // FIXME: Store encoded strings, or just pointers.
125     String m_databaseName;
126 };
127
128 class DatabaseMetaDataKey {
129 public:
130     enum MetaDataType {
131         kOriginName = 0,
132         kDatabaseName = 1,
133         kUserVersion = 2,
134         kMaxObjectStoreId = 3
135     };
136
137     static Vector<char> encode(int64_t databaseId, MetaDataType);
138 };
139
140 class ObjectStoreMetaDataKey {
141 public:
142     ObjectStoreMetaDataKey();
143     static const char* decode(const char* start, const char* limit, ObjectStoreMetaDataKey* result);
144     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t metaDataType);
145     static Vector<char> encodeMaxKey(int64_t databaseId);
146     int64_t objectStoreId() const;
147     int64_t metaDataType() const;
148     int compare(const ObjectStoreMetaDataKey& other);
149
150 private:
151     int64_t m_objectStoreId;
152     int64_t m_metaDataType; // FIXME: Make this a byte.
153 };
154
155 class IndexMetaDataKey {
156 public:
157     IndexMetaDataKey();
158     static const char* decode(const char* start, const char* limit, IndexMetaDataKey* result);
159     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, unsigned char metaDataType);
160     static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
161     int compare(const IndexMetaDataKey& other);
162     int64_t indexId() const;
163     unsigned char metaDataType() const { return m_metaDataType; }
164
165 private:
166     int64_t m_objectStoreId;
167     int64_t m_indexId;
168     unsigned char m_metaDataType;
169 };
170
171 class ObjectStoreFreeListKey {
172 public:
173     ObjectStoreFreeListKey();
174     static const char* decode(const char* start, const char* limit, ObjectStoreFreeListKey* result);
175     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId);
176     static Vector<char> encodeMaxKey(int64_t databaseId);
177     int64_t objectStoreId() const;
178     int compare(const ObjectStoreFreeListKey& other);
179
180 private:
181     int64_t m_objectStoreId;
182 };
183
184 class IndexFreeListKey {
185 public:
186     IndexFreeListKey();
187     static const char* decode(const char* start, const char* limit, IndexFreeListKey* result);
188     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
189     static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
190     int compare(const IndexFreeListKey& other);
191     int64_t objectStoreId() const;
192     int64_t indexId() const;
193
194 private:
195     int64_t m_objectStoreId;
196     int64_t m_indexId;
197 };
198
199 class ObjectStoreNamesKey {
200 public:
201     // FIXME: We never use this to look up object store ids, because a mapping
202     // is kept in the IDBDatabaseBackendImpl. Can the mapping become unreliable?
203     // Can we remove this?
204     static const char* decode(const char* start, const char* limit, ObjectStoreNamesKey* result);
205     static Vector<char> encode(int64_t databaseId, const String& objectStoreName);
206     int compare(const ObjectStoreNamesKey& other);
207     String objectStoreName() const { return m_objectStoreName; }
208
209 private:
210     String m_objectStoreName; // FIXME: Store the encoded string, or just pointers to it.
211 };
212
213 class IndexNamesKey {
214 public:
215     IndexNamesKey();
216     // FIXME: We never use this to look up index ids, because a mapping
217     // is kept at a higher level.
218     static const char* decode(const char* start, const char* limit, IndexNamesKey* result);
219     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const String& indexName);
220     int compare(const IndexNamesKey& other);
221     String indexName() const { return m_indexName; }
222
223 private:
224     int64_t m_objectStoreId;
225     String m_indexName;
226 };
227
228 class ObjectStoreDataKey {
229 public:
230     static const char* decode(const char* start, const char* end, ObjectStoreDataKey* result);
231     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const Vector<char> encodedUserKey);
232     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const IDBKey& userKey);
233     int compare(const ObjectStoreDataKey& other);
234     PassRefPtr<IDBKey> userKey() const;
235     static const int64_t kSpecialIndexNumber;
236
237 private:
238     Vector<char> m_encodedUserKey;
239 };
240
241 class ExistsEntryKey {
242 public:
243     static const char* decode(const char* start, const char* end, ExistsEntryKey* result);
244     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const Vector<char>& encodedKey);
245     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, const IDBKey& userKey);
246     int compare(const ExistsEntryKey& other);
247     PassRefPtr<IDBKey> userKey() const;
248
249     static const int64_t kSpecialIndexNumber;
250
251 private:
252     Vector<char> m_encodedUserKey;
253 };
254
255 class IndexDataKey {
256 public:
257     IndexDataKey();
258     static const char* decode(const char* start, const char* limit, IndexDataKey* result);
259     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, const Vector<char>& encodedUserKey, int64_t sequenceNumber);
260     static Vector<char> encode(int64_t databaseId, int64_t objectStoreId, int64_t indexId, const IDBKey& userKey, int64_t sequenceNumber);
261     static Vector<char> encodeMaxKey(int64_t databaseId, int64_t objectStoreId);
262     int compare(const IndexDataKey& other, bool ignoreSequenceNumber);
263     int64_t databaseId() const;
264     int64_t objectStoreId() const;
265     int64_t indexId() const;
266     PassRefPtr<IDBKey> userKey() const;
267
268 private:
269     int64_t m_databaseId;
270     int64_t m_objectStoreId;
271     int64_t m_indexId;
272     Vector<char> m_encodedUserKey;
273     int64_t m_sequenceNumber;
274 };
275
276 } // namespace IDBLevelDBCoding
277
278 } // namespace WebCore
279
280 #endif // ENABLE(LEVELDB)
281 #endif // ENABLE(INDEXED_DATABASE)
282
283 #endif // IDBLevelDBCoding_h