1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_piece.h"
16 #include "content/common/indexed_db/indexed_db_key.h"
17 #include "content/common/indexed_db/indexed_db_key_path.h"
21 CONTENT_EXPORT
extern const unsigned char kMinimumIndexId
;
23 CONTENT_EXPORT
std::string
MaxIDBKey();
24 CONTENT_EXPORT
std::string
MinIDBKey();
26 CONTENT_EXPORT
void EncodeByte(unsigned char value
, std::string
* into
);
27 CONTENT_EXPORT
void EncodeBool(bool value
, std::string
* into
);
28 CONTENT_EXPORT
void EncodeInt(int64 value
, std::string
* into
);
29 CONTENT_EXPORT
void EncodeVarInt(int64 value
, std::string
* into
);
30 CONTENT_EXPORT
void EncodeString(const base::string16
& value
,
32 CONTENT_EXPORT
void EncodeStringWithLength(const base::string16
& value
,
34 CONTENT_EXPORT
void EncodeBinary(const std::string
& value
, std::string
* into
);
35 CONTENT_EXPORT
void EncodeDouble(double value
, std::string
* into
);
36 CONTENT_EXPORT
void EncodeIDBKey(const IndexedDBKey
& value
, std::string
* into
);
37 CONTENT_EXPORT
void EncodeIDBKeyPath(const IndexedDBKeyPath
& value
,
40 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeByte(base::StringPiece
* slice
,
41 unsigned char* value
);
42 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeBool(base::StringPiece
* slice
,
44 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeInt(base::StringPiece
* slice
,
46 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeVarInt(base::StringPiece
* slice
,
48 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeString(base::StringPiece
* slice
,
49 base::string16
* value
);
50 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeStringWithLength(
51 base::StringPiece
* slice
,
52 base::string16
* value
);
53 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeBinary(base::StringPiece
* slice
,
55 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeDouble(base::StringPiece
* slice
,
57 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeIDBKey(
58 base::StringPiece
* slice
,
59 scoped_ptr
<IndexedDBKey
>* value
);
60 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeIDBKeyPath(
61 base::StringPiece
* slice
,
62 IndexedDBKeyPath
* value
);
64 CONTENT_EXPORT
int CompareEncodedStringsWithLength(base::StringPiece
* slice1
,
65 base::StringPiece
* slice2
,
68 CONTENT_EXPORT WARN_UNUSED_RESULT
bool ExtractEncodedIDBKey(
69 base::StringPiece
* slice
,
72 CONTENT_EXPORT
int CompareEncodedIDBKeys(base::StringPiece
* slice1
,
73 base::StringPiece
* slice2
,
76 CONTENT_EXPORT
int Compare(const base::StringPiece
& a
,
77 const base::StringPiece
& b
,
83 explicit KeyPrefix(int64 database_id
);
84 KeyPrefix(int64 database_id
, int64 object_store_id
);
85 KeyPrefix(int64 database_id
, int64 object_store_id
, int64 index_id
);
86 static KeyPrefix
CreateWithSpecialIndex(int64 database_id
,
87 int64 object_store_id
,
90 static bool Decode(base::StringPiece
* slice
, KeyPrefix
* result
);
91 std::string
Encode() const;
92 static std::string
EncodeEmpty();
93 int Compare(const KeyPrefix
& other
) const;
104 static const size_t kMaxDatabaseIdSizeBits
= 3;
105 static const size_t kMaxObjectStoreIdSizeBits
= 3;
106 static const size_t kMaxIndexIdSizeBits
= 2;
108 static const size_t kMaxDatabaseIdSizeBytes
=
109 1ULL << kMaxDatabaseIdSizeBits
; // 8
110 static const size_t kMaxObjectStoreIdSizeBytes
=
111 1ULL << kMaxObjectStoreIdSizeBits
; // 8
112 static const size_t kMaxIndexIdSizeBytes
= 1ULL << kMaxIndexIdSizeBits
; // 4
114 static const size_t kMaxDatabaseIdBits
=
115 kMaxDatabaseIdSizeBytes
* 8 - 1; // 63
116 static const size_t kMaxObjectStoreIdBits
=
117 kMaxObjectStoreIdSizeBytes
* 8 - 1; // 63
118 static const size_t kMaxIndexIdBits
= kMaxIndexIdSizeBytes
* 8 - 1; // 31
120 static const int64 kMaxDatabaseId
=
121 (1ULL << kMaxDatabaseIdBits
) - 1; // max signed int64
122 static const int64 kMaxObjectStoreId
=
123 (1ULL << kMaxObjectStoreIdBits
) - 1; // max signed int64
124 static const int64 kMaxIndexId
=
125 (1ULL << kMaxIndexIdBits
) - 1; // max signed int32
127 static bool IsValidDatabaseId(int64 database_id
);
128 static bool IsValidObjectStoreId(int64 index_id
);
129 static bool IsValidIndexId(int64 index_id
);
130 static bool ValidIds(int64 database_id
,
131 int64 object_store_id
,
133 return IsValidDatabaseId(database_id
) &&
134 IsValidObjectStoreId(object_store_id
) && IsValidIndexId(index_id
);
136 static bool ValidIds(int64 database_id
, int64 object_store_id
) {
137 return IsValidDatabaseId(database_id
) &&
138 IsValidObjectStoreId(object_store_id
);
144 int64 object_store_id_
;
147 static const int64 kInvalidId
= -1;
150 static std::string
EncodeInternal(int64 database_id
,
151 int64 object_store_id
,
153 // Special constructor for CreateWithSpecialIndex()
156 int64 object_store_id
,
160 class SchemaVersionKey
{
162 CONTENT_EXPORT
static std::string
Encode();
165 class MaxDatabaseIdKey
{
167 CONTENT_EXPORT
static std::string
Encode();
170 class DataVersionKey
{
172 static std::string
Encode();
175 class DatabaseFreeListKey
{
177 DatabaseFreeListKey();
178 static bool Decode(base::StringPiece
* slice
, DatabaseFreeListKey
* result
);
179 CONTENT_EXPORT
static std::string
Encode(int64 database_id
);
180 static CONTENT_EXPORT
std::string
EncodeMaxKey();
181 int64
DatabaseId() const;
182 int Compare(const DatabaseFreeListKey
& other
) const;
188 class DatabaseNameKey
{
190 static bool Decode(base::StringPiece
* slice
, DatabaseNameKey
* result
);
191 CONTENT_EXPORT
static std::string
Encode(const std::string
& origin_identifier
,
192 const base::string16
& database_name
);
193 static std::string
EncodeMinKeyForOrigin(
194 const std::string
& origin_identifier
);
195 static std::string
EncodeStopKeyForOrigin(
196 const std::string
& origin_identifier
);
197 base::string16
origin() const { return origin_
; }
198 base::string16
database_name() const { return database_name_
; }
199 int Compare(const DatabaseNameKey
& other
);
202 base::string16 origin_
; // TODO(jsbell): Store encoded strings, or just
204 base::string16 database_name_
;
207 class DatabaseMetaDataKey
{
213 MAX_OBJECT_STORE_ID
= 3,
214 USER_INT_VERSION
= 4,
215 MAX_SIMPLE_METADATA_TYPE
= 5
218 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
222 class ObjectStoreMetaDataKey
{
232 KEY_GENERATOR_CURRENT_NUMBER
= 7
235 ObjectStoreMetaDataKey();
236 static bool Decode(base::StringPiece
* slice
, ObjectStoreMetaDataKey
* result
);
237 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
238 int64 object_store_id
,
239 unsigned char meta_data_type
);
240 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
);
241 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
242 int64 object_store_id
);
243 int64
ObjectStoreId() const;
244 unsigned char MetaDataType() const;
245 int Compare(const ObjectStoreMetaDataKey
& other
);
248 int64 object_store_id_
;
249 unsigned char meta_data_type_
;
252 class IndexMetaDataKey
{
262 static bool Decode(base::StringPiece
* slice
, IndexMetaDataKey
* result
);
263 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
264 int64 object_store_id
,
266 unsigned char meta_data_type
);
267 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
268 int64 object_store_id
);
269 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
270 int64 object_store_id
,
272 int Compare(const IndexMetaDataKey
& other
);
273 int64
IndexId() const;
274 unsigned char meta_data_type() const { return meta_data_type_
; }
277 int64 object_store_id_
;
279 unsigned char meta_data_type_
;
282 class ObjectStoreFreeListKey
{
284 ObjectStoreFreeListKey();
285 static bool Decode(base::StringPiece
* slice
, ObjectStoreFreeListKey
* result
);
286 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
287 int64 object_store_id
);
288 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
);
289 int64
ObjectStoreId() const;
290 int Compare(const ObjectStoreFreeListKey
& other
);
293 int64 object_store_id_
;
296 class IndexFreeListKey
{
299 static bool Decode(base::StringPiece
* slice
, IndexFreeListKey
* result
);
300 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
301 int64 object_store_id
,
303 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
304 int64 object_store_id
);
305 int Compare(const IndexFreeListKey
& other
);
306 int64
ObjectStoreId() const;
307 int64
IndexId() const;
310 int64 object_store_id_
;
314 class ObjectStoreNamesKey
{
316 // TODO(jsbell): We never use this to look up object store ids,
317 // because a mapping is kept in the IndexedDBDatabase. Can the
318 // mapping become unreliable? Can we remove this?
319 static bool Decode(base::StringPiece
* slice
, ObjectStoreNamesKey
* result
);
320 CONTENT_EXPORT
static std::string
Encode(
322 const base::string16
& object_store_name
);
323 int Compare(const ObjectStoreNamesKey
& other
);
324 base::string16
object_store_name() const { return object_store_name_
; }
327 // TODO(jsbell): Store the encoded string, or just pointers to it.
328 base::string16 object_store_name_
;
331 class IndexNamesKey
{
334 // TODO(jsbell): We never use this to look up index ids, because a mapping
335 // is kept at a higher level.
336 static bool Decode(base::StringPiece
* slice
, IndexNamesKey
* result
);
337 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
338 int64 object_store_id
,
339 const base::string16
& index_name
);
340 int Compare(const IndexNamesKey
& other
);
341 base::string16
index_name() const { return index_name_
; }
344 int64 object_store_id_
;
345 base::string16 index_name_
;
348 class ObjectStoreDataKey
{
350 static bool Decode(base::StringPiece
* slice
, ObjectStoreDataKey
* result
);
351 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
352 int64 object_store_id
,
353 const std::string encoded_user_key
);
354 static std::string
Encode(int64 database_id
,
355 int64 object_store_id
,
356 const IndexedDBKey
& user_key
);
357 scoped_ptr
<IndexedDBKey
> user_key() const;
358 static const int64 kSpecialIndexNumber
;
359 ObjectStoreDataKey();
360 ~ObjectStoreDataKey();
363 std::string encoded_user_key_
;
366 class ExistsEntryKey
{
371 static bool Decode(base::StringPiece
* slice
, ExistsEntryKey
* result
);
372 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
373 int64 object_store_id
,
374 const std::string
& encoded_key
);
375 static std::string
Encode(int64 database_id
,
376 int64 object_store_id
,
377 const IndexedDBKey
& user_key
);
378 scoped_ptr
<IndexedDBKey
> user_key() const;
380 static const int64 kSpecialIndexNumber
;
383 std::string encoded_user_key_
;
384 DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey
);
391 static bool Decode(base::StringPiece
* slice
, IndexDataKey
* result
);
392 CONTENT_EXPORT
static std::string
Encode(
394 int64 object_store_id
,
396 const std::string
& encoded_user_key
,
397 const std::string
& encoded_primary_key
,
398 int64 sequence_number
);
399 static std::string
Encode(int64 database_id
,
400 int64 object_store_id
,
402 const IndexedDBKey
& user_key
);
403 static std::string
Encode(int64 database_id
,
404 int64 object_store_id
,
406 const IndexedDBKey
& user_key
,
407 const IndexedDBKey
& user_primary_key
);
408 static std::string
EncodeMinKey(int64 database_id
,
409 int64 object_store_id
,
411 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
412 int64 object_store_id
,
414 int64
DatabaseId() const;
415 int64
ObjectStoreId() const;
416 int64
IndexId() const;
417 scoped_ptr
<IndexedDBKey
> user_key() const;
418 scoped_ptr
<IndexedDBKey
> primary_key() const;
422 int64 object_store_id_
;
424 std::string encoded_user_key_
;
425 std::string encoded_primary_key_
;
426 int64 sequence_number_
;
428 DISALLOW_COPY_AND_ASSIGN(IndexDataKey
);
431 } // namespace content
433 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_