2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef IDBLevelDBCoding_h
27 #define IDBLevelDBCoding_h
29 #if ENABLE(INDEXED_DATABASE)
32 #include <wtf/RefPtr.h>
33 #include <wtf/Vector.h>
34 #include <wtf/text/WTFString.h>
41 namespace IDBLevelDBCoding {
43 const unsigned char kMinimumIndexId = 30;
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>&);
63 int compare(const LevelDBSlice&, const LevelDBSlice&, bool indexKeys = false);
68 KeyPrefix(int64_t databaseId, int64_t objectStoreId, int64_t indexId);
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;
86 int64_t m_objectStoreId;
89 static const int64_t kInvalidId = -1;
92 class SchemaVersionKey {
94 static Vector<char> encode();
97 class MaxDatabaseIdKey {
99 static Vector<char> encode();
102 class DatabaseFreeListKey {
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;
112 int64_t m_databaseId;
115 class DatabaseNameKey {
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);
124 String m_origin; // FIXME: Store encoded strings, or just pointers.
125 String m_databaseName;
128 class DatabaseMetaDataKey {
134 kMaxObjectStoreId = 3
137 static Vector<char> encode(int64_t databaseId, MetaDataType);
140 class ObjectStoreMetaDataKey {
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);
151 int64_t m_objectStoreId;
152 int64_t m_metaDataType; // FIXME: Make this a byte.
155 class 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; }
166 int64_t m_objectStoreId;
168 unsigned char m_metaDataType;
171 class ObjectStoreFreeListKey {
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);
181 int64_t m_objectStoreId;
184 class 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;
195 int64_t m_objectStoreId;
199 class ObjectStoreNamesKey {
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; }
210 String m_objectStoreName; // FIXME: Store the encoded string, or just pointers to it.
213 class 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; }
224 int64_t m_objectStoreId;
228 class ObjectStoreDataKey {
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;
238 Vector<char> m_encodedUserKey;
241 class ExistsEntryKey {
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;
249 static const int64_t kSpecialIndexNumber;
252 Vector<char> m_encodedUserKey;
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;
269 int64_t m_databaseId;
270 int64_t m_objectStoreId;
272 Vector<char> m_encodedUserKey;
273 int64_t m_sequenceNumber;
276 } // namespace IDBLevelDBCoding
278 } // namespace WebCore
280 #endif // ENABLE(LEVELDB)
281 #endif // ENABLE(INDEXED_DATABASE)
283 #endif // IDBLevelDBCoding_h