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 // DatabaseId, BlobKey
27 typedef std::pair
<int64_t, int64_t> BlobJournalEntryType
;
28 typedef std::vector
<BlobJournalEntryType
> BlobJournalType
;
30 CONTENT_EXPORT
void EncodeByte(unsigned char value
, std::string
* into
);
31 CONTENT_EXPORT
void EncodeBool(bool value
, std::string
* into
);
32 CONTENT_EXPORT
void EncodeInt(int64 value
, std::string
* into
);
33 CONTENT_EXPORT
void EncodeVarInt(int64 value
, std::string
* into
);
34 CONTENT_EXPORT
void EncodeString(const base::string16
& value
,
36 CONTENT_EXPORT
void EncodeStringWithLength(const base::string16
& value
,
38 CONTENT_EXPORT
void EncodeBinary(const std::string
& value
, std::string
* into
);
39 CONTENT_EXPORT
void EncodeDouble(double value
, std::string
* into
);
40 CONTENT_EXPORT
void EncodeIDBKey(const IndexedDBKey
& value
, std::string
* into
);
41 CONTENT_EXPORT
void EncodeIDBKeyPath(const IndexedDBKeyPath
& value
,
43 CONTENT_EXPORT
void EncodeBlobJournal(const BlobJournalType
& journal
,
46 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeByte(base::StringPiece
* slice
,
47 unsigned char* value
);
48 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeBool(base::StringPiece
* slice
,
50 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeInt(base::StringPiece
* slice
,
52 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeVarInt(base::StringPiece
* slice
,
54 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeString(base::StringPiece
* slice
,
55 base::string16
* value
);
56 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeStringWithLength(
57 base::StringPiece
* slice
,
58 base::string16
* value
);
59 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeBinary(base::StringPiece
* slice
,
61 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeDouble(base::StringPiece
* slice
,
63 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeIDBKey(
64 base::StringPiece
* slice
,
65 scoped_ptr
<IndexedDBKey
>* value
);
66 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeIDBKeyPath(
67 base::StringPiece
* slice
,
68 IndexedDBKeyPath
* value
);
69 CONTENT_EXPORT WARN_UNUSED_RESULT
bool DecodeBlobJournal(
70 base::StringPiece
* slice
,
71 BlobJournalType
* journal
);
73 CONTENT_EXPORT
int CompareEncodedStringsWithLength(base::StringPiece
* slice1
,
74 base::StringPiece
* slice2
,
77 CONTENT_EXPORT WARN_UNUSED_RESULT
bool ExtractEncodedIDBKey(
78 base::StringPiece
* slice
,
81 CONTENT_EXPORT
int CompareEncodedIDBKeys(base::StringPiece
* slice1
,
82 base::StringPiece
* slice2
,
85 CONTENT_EXPORT
int Compare(const base::StringPiece
& a
,
86 const base::StringPiece
& b
,
92 explicit KeyPrefix(int64 database_id
);
93 KeyPrefix(int64 database_id
, int64 object_store_id
);
94 KeyPrefix(int64 database_id
, int64 object_store_id
, int64 index_id
);
95 static KeyPrefix
CreateWithSpecialIndex(int64 database_id
,
96 int64 object_store_id
,
99 static bool Decode(base::StringPiece
* slice
, KeyPrefix
* result
);
100 std::string
Encode() const;
101 static std::string
EncodeEmpty();
102 int Compare(const KeyPrefix
& other
) const;
104 // These are serialized to disk; any new items must be appended, and none can
116 static const size_t kMaxDatabaseIdSizeBits
= 3;
117 static const size_t kMaxObjectStoreIdSizeBits
= 3;
118 static const size_t kMaxIndexIdSizeBits
= 2;
120 static const size_t kMaxDatabaseIdSizeBytes
=
121 1ULL << kMaxDatabaseIdSizeBits
; // 8
122 static const size_t kMaxObjectStoreIdSizeBytes
=
123 1ULL << kMaxObjectStoreIdSizeBits
; // 8
124 static const size_t kMaxIndexIdSizeBytes
= 1ULL << kMaxIndexIdSizeBits
; // 4
126 static const size_t kMaxDatabaseIdBits
=
127 kMaxDatabaseIdSizeBytes
* 8 - 1; // 63
128 static const size_t kMaxObjectStoreIdBits
=
129 kMaxObjectStoreIdSizeBytes
* 8 - 1; // 63
130 static const size_t kMaxIndexIdBits
= kMaxIndexIdSizeBytes
* 8 - 1; // 31
132 static const int64 kMaxDatabaseId
=
133 (1ULL << kMaxDatabaseIdBits
) - 1; // max signed int64
134 static const int64 kMaxObjectStoreId
=
135 (1ULL << kMaxObjectStoreIdBits
) - 1; // max signed int64
136 static const int64 kMaxIndexId
=
137 (1ULL << kMaxIndexIdBits
) - 1; // max signed int32
139 static bool IsValidDatabaseId(int64 database_id
);
140 static bool IsValidObjectStoreId(int64 index_id
);
141 static bool IsValidIndexId(int64 index_id
);
142 static bool ValidIds(int64 database_id
,
143 int64 object_store_id
,
145 return IsValidDatabaseId(database_id
) &&
146 IsValidObjectStoreId(object_store_id
) && IsValidIndexId(index_id
);
148 static bool ValidIds(int64 database_id
, int64 object_store_id
) {
149 return IsValidDatabaseId(database_id
) &&
150 IsValidObjectStoreId(object_store_id
);
156 int64 object_store_id_
;
159 static const int64 kInvalidId
= -1;
162 static std::string
EncodeInternal(int64 database_id
,
163 int64 object_store_id
,
165 // Special constructor for CreateWithSpecialIndex()
168 int64 object_store_id
,
172 class SchemaVersionKey
{
174 CONTENT_EXPORT
static std::string
Encode();
177 class MaxDatabaseIdKey
{
179 CONTENT_EXPORT
static std::string
Encode();
182 class DataVersionKey
{
184 static std::string
Encode();
187 class BlobJournalKey
{
189 static std::string
Encode();
192 class LiveBlobJournalKey
{
194 static std::string
Encode();
197 class DatabaseFreeListKey
{
199 DatabaseFreeListKey();
200 static bool Decode(base::StringPiece
* slice
, DatabaseFreeListKey
* result
);
201 CONTENT_EXPORT
static std::string
Encode(int64 database_id
);
202 static CONTENT_EXPORT
std::string
EncodeMaxKey();
203 int64
DatabaseId() const;
204 int Compare(const DatabaseFreeListKey
& other
) const;
210 class DatabaseNameKey
{
212 static bool Decode(base::StringPiece
* slice
, DatabaseNameKey
* result
);
213 CONTENT_EXPORT
static std::string
Encode(const std::string
& origin_identifier
,
214 const base::string16
& database_name
);
215 static std::string
EncodeMinKeyForOrigin(
216 const std::string
& origin_identifier
);
217 static std::string
EncodeStopKeyForOrigin(
218 const std::string
& origin_identifier
);
219 base::string16
origin() const { return origin_
; }
220 base::string16
database_name() const { return database_name_
; }
221 int Compare(const DatabaseNameKey
& other
);
224 base::string16 origin_
; // TODO(jsbell): Store encoded strings, or just
226 base::string16 database_name_
;
229 class DatabaseMetaDataKey
{
235 MAX_OBJECT_STORE_ID
= 3,
236 USER_INT_VERSION
= 4,
237 BLOB_KEY_GENERATOR_CURRENT_NUMBER
= 5,
238 MAX_SIMPLE_METADATA_TYPE
= 6
241 CONTENT_EXPORT
static const int64 kAllBlobsKey
;
242 static const int64 kBlobKeyGeneratorInitialNumber
;
243 // All keys <= 0 are invalid. This one's just a convenient example.
244 static const int64 kInvalidBlobKey
;
246 static bool IsValidBlobKey(int64 blobKey
);
247 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
251 class ObjectStoreMetaDataKey
{
261 KEY_GENERATOR_CURRENT_NUMBER
= 7
264 ObjectStoreMetaDataKey();
265 static bool Decode(base::StringPiece
* slice
, ObjectStoreMetaDataKey
* result
);
266 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
267 int64 object_store_id
,
268 unsigned char meta_data_type
);
269 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
);
270 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
271 int64 object_store_id
);
272 int64
ObjectStoreId() const;
273 unsigned char MetaDataType() const;
274 int Compare(const ObjectStoreMetaDataKey
& other
);
277 int64 object_store_id_
;
278 unsigned char meta_data_type_
;
281 class IndexMetaDataKey
{
291 static bool Decode(base::StringPiece
* slice
, IndexMetaDataKey
* result
);
292 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
293 int64 object_store_id
,
295 unsigned char meta_data_type
);
296 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
297 int64 object_store_id
);
298 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
299 int64 object_store_id
,
301 int Compare(const IndexMetaDataKey
& other
);
302 int64
IndexId() const;
303 unsigned char meta_data_type() const { return meta_data_type_
; }
306 int64 object_store_id_
;
308 unsigned char meta_data_type_
;
311 class ObjectStoreFreeListKey
{
313 ObjectStoreFreeListKey();
314 static bool Decode(base::StringPiece
* slice
, ObjectStoreFreeListKey
* result
);
315 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
316 int64 object_store_id
);
317 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
);
318 int64
ObjectStoreId() const;
319 int Compare(const ObjectStoreFreeListKey
& other
);
322 int64 object_store_id_
;
325 class IndexFreeListKey
{
328 static bool Decode(base::StringPiece
* slice
, IndexFreeListKey
* result
);
329 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
330 int64 object_store_id
,
332 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
333 int64 object_store_id
);
334 int Compare(const IndexFreeListKey
& other
);
335 int64
ObjectStoreId() const;
336 int64
IndexId() const;
339 int64 object_store_id_
;
343 class ObjectStoreNamesKey
{
345 // TODO(jsbell): We never use this to look up object store ids,
346 // because a mapping is kept in the IndexedDBDatabase. Can the
347 // mapping become unreliable? Can we remove this?
348 static bool Decode(base::StringPiece
* slice
, ObjectStoreNamesKey
* result
);
349 CONTENT_EXPORT
static std::string
Encode(
351 const base::string16
& object_store_name
);
352 int Compare(const ObjectStoreNamesKey
& other
);
353 base::string16
object_store_name() const { return object_store_name_
; }
356 // TODO(jsbell): Store the encoded string, or just pointers to it.
357 base::string16 object_store_name_
;
360 class IndexNamesKey
{
363 // TODO(jsbell): We never use this to look up index ids, because a mapping
364 // is kept at a higher level.
365 static bool Decode(base::StringPiece
* slice
, IndexNamesKey
* result
);
366 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
367 int64 object_store_id
,
368 const base::string16
& index_name
);
369 int Compare(const IndexNamesKey
& other
);
370 base::string16
index_name() const { return index_name_
; }
373 int64 object_store_id_
;
374 base::string16 index_name_
;
377 class ObjectStoreDataKey
{
379 static bool Decode(base::StringPiece
* slice
, ObjectStoreDataKey
* result
);
380 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
381 int64 object_store_id
,
382 const std::string encoded_user_key
);
383 static std::string
Encode(int64 database_id
,
384 int64 object_store_id
,
385 const IndexedDBKey
& user_key
);
386 scoped_ptr
<IndexedDBKey
> user_key() const;
387 static const int64 kSpecialIndexNumber
;
388 ObjectStoreDataKey();
389 ~ObjectStoreDataKey();
392 std::string encoded_user_key_
;
395 class ExistsEntryKey
{
400 static bool Decode(base::StringPiece
* slice
, ExistsEntryKey
* result
);
401 CONTENT_EXPORT
static std::string
Encode(int64 database_id
,
402 int64 object_store_id
,
403 const std::string
& encoded_key
);
404 static std::string
Encode(int64 database_id
,
405 int64 object_store_id
,
406 const IndexedDBKey
& user_key
);
407 scoped_ptr
<IndexedDBKey
> user_key() const;
409 static const int64 kSpecialIndexNumber
;
412 std::string encoded_user_key_
;
413 DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey
);
418 BlobEntryKey() : database_id_(0), object_store_id_(0) {}
419 static bool Decode(base::StringPiece
* slice
, BlobEntryKey
* result
);
420 static bool FromObjectStoreDataKey(base::StringPiece
* slice
,
421 BlobEntryKey
* result
);
422 static std::string
ReencodeToObjectStoreDataKey(base::StringPiece
* slice
);
423 static std::string
EncodeMinKeyForObjectStore(int64 database_id
,
424 int64 object_store_id
);
425 static std::string
EncodeStopKeyForObjectStore(int64 database_id
,
426 int64 object_store_id
);
427 static std::string
Encode(int64 database_id
,
428 int64 object_store_id
,
429 const IndexedDBKey
& user_key
);
430 std::string
Encode() const;
431 int64
database_id() const { return database_id_
; }
432 int64
object_store_id() const { return object_store_id_
; }
434 static const int64 kSpecialIndexNumber
;
437 static std::string
Encode(int64 database_id
,
438 int64 object_store_id
,
439 const std::string
& encoded_user_key
);
441 int64 object_store_id_
;
442 // This is the user's ObjectStoreDataKey, not the BlobEntryKey itself.
443 std::string encoded_user_key_
;
450 static bool Decode(base::StringPiece
* slice
, IndexDataKey
* result
);
451 CONTENT_EXPORT
static std::string
Encode(
453 int64 object_store_id
,
455 const std::string
& encoded_user_key
,
456 const std::string
& encoded_primary_key
,
457 int64 sequence_number
);
458 static std::string
Encode(int64 database_id
,
459 int64 object_store_id
,
461 const IndexedDBKey
& user_key
);
462 static std::string
Encode(int64 database_id
,
463 int64 object_store_id
,
465 const IndexedDBKey
& user_key
,
466 const IndexedDBKey
& user_primary_key
);
467 static std::string
EncodeMinKey(int64 database_id
,
468 int64 object_store_id
,
470 CONTENT_EXPORT
static std::string
EncodeMaxKey(int64 database_id
,
471 int64 object_store_id
,
473 int64
DatabaseId() const;
474 int64
ObjectStoreId() const;
475 int64
IndexId() const;
476 scoped_ptr
<IndexedDBKey
> user_key() const;
477 scoped_ptr
<IndexedDBKey
> primary_key() const;
481 int64 object_store_id_
;
483 std::string encoded_user_key_
;
484 std::string encoded_primary_key_
;
485 int64 sequence_number_
;
487 DISALLOW_COPY_AND_ASSIGN(IndexDataKey
);
490 } // namespace content
492 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_