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 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/sys_byteorder.h"
14 #include "content/common/indexed_db/indexed_db_key.h"
15 #include "content/common/indexed_db/indexed_db_key_path.h"
17 // LevelDB Coding Scheme
18 // =====================
20 // LevelDB stores key/value pairs. Keys and values are strings of bytes,
21 // normally of type std::string.
23 // The keys in the backing store are variable-length tuples with different
24 // types of fields. Each key in the backing store starts with a ternary
25 // prefix: (database id, object store id, index id). For each, 0 is reserved
26 // for metadata. See KeyPrefix::Decode() for details of the prefix coding.
28 // The prefix makes sure that data for a specific database, object store, and
29 // index are grouped together. The locality is important for performance:
30 // common operations should only need a minimal number of seek operations. For
31 // example, all the metadata for a database is grouped together so that
32 // reading that metadata only requires one seek.
34 // Each key type has a class (in square brackets below) which knows how to
35 // encode, decode, and compare that key type.
37 // Strings (origins, names, etc) are encoded as UTF-16BE.
42 // The prefix is <0, 0, 0>, followed by a metadata type byte:
44 // <0, 0, 0, 0> => backing store schema version [SchemaVersionKey]
45 // <0, 0, 0, 1> => maximum allocated database [MaxDatabaseIdKey]
46 // <0, 0, 0, 2> => SerializedScriptValue version [DataVersionKey]
49 // The format of the journal is: {database_id, blobKey}*.
50 // If the blobKey is kAllBlobsKey, the whole database should be deleted.
52 // <0, 0, 0, 4> => Live blob journal; same format. [LiveBlobJournalKey]
53 // <0, 0, 0, 100, database id>
54 // => Existence implies the database id is in the free list
55 // [DatabaseFreeListKey]
56 // <0, 0, 0, 201, origin, database name> => Database id [DatabaseNameKey]
59 // Database metadata: [DatabaseMetaDataKey]
60 // ----------------------------------------
61 // The prefix is <database id, 0, 0> followed by a metadata type byte:
63 // <database id, 0, 0, 0> => origin name
64 // <database id, 0, 0, 1> => database name
65 // <database id, 0, 0, 2> => IDB string version data (obsolete)
66 // <database id, 0, 0, 3> => maximum allocated object store id
67 // <database id, 0, 0, 4> => IDB integer version (var int)
68 // <database id, 0, 0, 5> => blob key generator current number
71 // Object store metadata: [ObjectStoreMetaDataKey]
72 // -----------------------------------------------
73 // The prefix is <database id, 0, 0>, followed by a type byte (50), then the
74 // object store id (var int), then a metadata type byte.
76 // <database id, 0, 0, 50, object store id, 0> => object store name
77 // <database id, 0, 0, 50, object store id, 1> => key path
78 // <database id, 0, 0, 50, object store id, 2> => auto increment flag
79 // <database id, 0, 0, 50, object store id, 3> => is evictable
80 // <database id, 0, 0, 50, object store id, 4> => last "version" number
81 // <database id, 0, 0, 50, object store id, 5> => maximum allocated index id
82 // <database id, 0, 0, 50, object store id, 6> => has key path flag (obsolete)
83 // <database id, 0, 0, 50, object store id, 7> => key generator current number
85 // The key path was originally just a string (#1) or null (identified by flag,
86 // #6). To support null, string, or array the coding is now identified by the
87 // leading bytes in #1 - see EncodeIDBKeyPath.
89 // The "version" field is used to weed out stale index data. Whenever new
90 // object store data is inserted, it gets a new "version" number, and new
91 // index data is written with this number. When the index is used for
92 // look-ups, entries are validated against the "exists" entries, and records
93 // with old "version" numbers are deleted when they are encountered in
94 // GetPrimaryKeyViaIndex, IndexCursorImpl::LoadCurrentRow and
95 // IndexKeyCursorImpl::LoadCurrentRow.
98 // Index metadata: [IndexMetaDataKey]
99 // ----------------------------------
100 // The prefix is <database id, 0, 0>, followed by a type byte (100), then the
101 // object store id (var int), then the index id (var int), then a metadata
104 // <database id, 0, 0, 100, object store id, index id, 0> => index name
105 // <database id, 0, 0, 100, object store id, index id, 1> => unique flag
106 // <database id, 0, 0, 100, object store id, index id, 2> => key path
107 // <database id, 0, 0, 100, object store id, index id, 3> => multi-entry flag
110 // Other object store and index metadata
111 // -------------------------------------
112 // The prefix is <database id, 0, 0> followed by a type byte. The object
113 // store and index id are variable length integers, the names are variable
116 // <database id, 0, 0, 150, object store id>
117 // => existence implies the object store id is in the free list
118 // [ObjectStoreFreeListKey]
119 // <database id, 0, 0, 151, object store id, index id>
120 // => existence implies the index id is in the free list [IndexFreeListKey]
121 // <database id, 0, 0, 200, object store name>
122 // => object store id [ObjectStoreNamesKey]
123 // <database id, 0, 0, 201, object store id, index name>
124 // => index id [IndexNamesKey]
127 // Object store data: [ObjectStoreDataKey]
128 // ---------------------------------------
129 // The prefix is followed by a type byte and the encoded IDB primary key. The
130 // data has a "version" prefix followed by the serialized script value.
132 // <database id, object store id, 1, user key>
133 // => "version", serialized script value
136 // "Exists" entry: [ExistsEntryKey]
137 // --------------------------------
138 // The prefix is followed by a type byte and the encoded IDB primary key.
140 // <database id, object store id, 2, user key> => "version"
143 // Blob entry table: [BlobEntryKey]
144 // --------------------------------
146 // The prefix is followed by a type byte and the encoded IDB primary key.
148 // <database id, object store id, 3, user key> => array of IndexedDBBlobInfo
153 // The prefix is followed by a type byte, the encoded IDB index key, a
154 // "sequence" number (obsolete; var int), and the encoded IDB primary key.
156 // <database id, object store id, index id, index key, sequence number,
157 // primary key> => "version", primary key [IndexDataKey]
159 // The sequence number is obsolete; it was used to allow two entries with the
160 // same user (index) key in non-unique indexes prior to the inclusion of the
161 // primary key in the data.
163 using base::StringPiece;
164 using blink::WebIDBKeyType;
165 using blink::WebIDBKeyTypeArray;
166 using blink::WebIDBKeyTypeBinary;
167 using blink::WebIDBKeyTypeDate;
168 using blink::WebIDBKeyTypeInvalid;
169 using blink::WebIDBKeyTypeMin;
170 using blink::WebIDBKeyTypeNull;
171 using blink::WebIDBKeyTypeNumber;
172 using blink::WebIDBKeyTypeString;
173 using blink::WebIDBKeyPathType;
174 using blink::WebIDBKeyPathTypeArray;
175 using blink::WebIDBKeyPathTypeNull;
176 using blink::WebIDBKeyPathTypeString;
180 // As most of the IndexedDBKeys and encoded values are short, we
181 // initialize some std::vectors with a default inline buffer size to reduce
182 // the memory re-allocations when the std::vectors are appended.
183 static const size_t kDefaultInlineBufferSize = 32;
185 static const unsigned char kIndexedDBKeyNullTypeByte = 0;
186 static const unsigned char kIndexedDBKeyStringTypeByte = 1;
187 static const unsigned char kIndexedDBKeyDateTypeByte = 2;
188 static const unsigned char kIndexedDBKeyNumberTypeByte = 3;
189 static const unsigned char kIndexedDBKeyArrayTypeByte = 4;
190 static const unsigned char kIndexedDBKeyMinKeyTypeByte = 5;
191 static const unsigned char kIndexedDBKeyBinaryTypeByte = 6;
193 static const unsigned char kIndexedDBKeyPathTypeCodedByte1 = 0;
194 static const unsigned char kIndexedDBKeyPathTypeCodedByte2 = 0;
196 static const unsigned char kObjectStoreDataIndexId = 1;
197 static const unsigned char kExistsEntryIndexId = 2;
198 static const unsigned char kBlobEntryIndexId = 3;
200 static const unsigned char kSchemaVersionTypeByte = 0;
201 static const unsigned char kMaxDatabaseIdTypeByte = 1;
202 static const unsigned char kDataVersionTypeByte = 2;
203 static const unsigned char kBlobJournalTypeByte = 3;
204 static const unsigned char kLiveBlobJournalTypeByte = 4;
205 static const unsigned char kMaxSimpleGlobalMetaDataTypeByte =
206 5; // Insert before this and increment.
207 static const unsigned char kDatabaseFreeListTypeByte = 100;
208 static const unsigned char kDatabaseNameTypeByte = 201;
210 static const unsigned char kObjectStoreMetaDataTypeByte = 50;
211 static const unsigned char kIndexMetaDataTypeByte = 100;
212 static const unsigned char kObjectStoreFreeListTypeByte = 150;
213 static const unsigned char kIndexFreeListTypeByte = 151;
214 static const unsigned char kObjectStoreNamesTypeByte = 200;
215 static const unsigned char kIndexNamesKeyTypeByte = 201;
217 static const unsigned char kObjectMetaDataTypeMaximum = 255;
218 static const unsigned char kIndexMetaDataTypeMaximum = 255;
220 const unsigned char kMinimumIndexId = 30;
222 inline void EncodeIntSafely(int64 nParam, int64 max, std::string* into) {
223 DCHECK_LE(nParam, max);
224 return EncodeInt(nParam, into);
227 std::string MaxIDBKey() {
229 EncodeByte(kIndexedDBKeyNullTypeByte, &ret);
233 std::string MinIDBKey() {
235 EncodeByte(kIndexedDBKeyMinKeyTypeByte, &ret);
239 void EncodeByte(unsigned char value, std::string* into) {
240 into->push_back(value);
243 void EncodeBool(bool value, std::string* into) {
244 into->push_back(value ? 1 : 0);
247 void EncodeInt(int64 value, std::string* into) {
249 // Exercised by unit tests in debug only.
252 uint64 n = static_cast<uint64>(value);
261 void EncodeVarInt(int64 value, std::string* into) {
263 // Exercised by unit tests in debug only.
266 uint64 n = static_cast<uint64>(value);
269 unsigned char c = n & 0x7f;
277 void EncodeString(const base::string16& value, std::string* into) {
280 // Backing store is UTF-16BE, convert from host endianness.
281 size_t length = value.length();
282 size_t current = into->size();
283 into->resize(into->size() + length * sizeof(base::char16));
285 const base::char16* src = value.c_str();
287 reinterpret_cast<base::char16*>(&*into->begin() + current);
288 for (unsigned i = 0; i < length; ++i)
289 *dst++ = htons(*src++);
292 void EncodeBinary(const std::string& value, std::string* into) {
293 EncodeVarInt(value.length(), into);
294 into->append(value.begin(), value.end());
295 DCHECK(into->size() >= value.size());
298 void EncodeStringWithLength(const base::string16& value, std::string* into) {
299 EncodeVarInt(value.length(), into);
300 EncodeString(value, into);
303 void EncodeDouble(double value, std::string* into) {
304 // This always has host endianness.
305 const char* p = reinterpret_cast<char*>(&value);
306 into->insert(into->end(), p, p + sizeof(value));
309 void EncodeIDBKey(const IndexedDBKey& value, std::string* into) {
310 size_t previous_size = into->size();
311 DCHECK(value.IsValid());
312 switch (value.type()) {
313 case WebIDBKeyTypeArray: {
314 EncodeByte(kIndexedDBKeyArrayTypeByte, into);
315 size_t length = value.array().size();
316 EncodeVarInt(length, into);
317 for (size_t i = 0; i < length; ++i)
318 EncodeIDBKey(value.array()[i], into);
319 DCHECK_GT(into->size(), previous_size);
322 case WebIDBKeyTypeBinary:
323 EncodeByte(kIndexedDBKeyBinaryTypeByte, into);
324 EncodeBinary(value.binary(), into);
325 DCHECK_GT(into->size(), previous_size);
327 case WebIDBKeyTypeString:
328 EncodeByte(kIndexedDBKeyStringTypeByte, into);
329 EncodeStringWithLength(value.string(), into);
330 DCHECK_GT(into->size(), previous_size);
332 case WebIDBKeyTypeDate:
333 EncodeByte(kIndexedDBKeyDateTypeByte, into);
334 EncodeDouble(value.date(), into);
335 DCHECK_EQ(9u, static_cast<size_t>(into->size() - previous_size));
337 case WebIDBKeyTypeNumber:
338 EncodeByte(kIndexedDBKeyNumberTypeByte, into);
339 EncodeDouble(value.number(), into);
340 DCHECK_EQ(9u, static_cast<size_t>(into->size() - previous_size));
342 case WebIDBKeyTypeNull:
343 case WebIDBKeyTypeInvalid:
344 case WebIDBKeyTypeMin:
347 EncodeByte(kIndexedDBKeyNullTypeByte, into);
352 void EncodeIDBKeyPath(const IndexedDBKeyPath& value, std::string* into) {
353 // May be typed, or may be a raw string. An invalid leading
354 // byte is used to identify typed coding. New records are
355 // always written as typed.
356 EncodeByte(kIndexedDBKeyPathTypeCodedByte1, into);
357 EncodeByte(kIndexedDBKeyPathTypeCodedByte2, into);
358 EncodeByte(static_cast<char>(value.type()), into);
359 switch (value.type()) {
360 case WebIDBKeyPathTypeNull:
362 case WebIDBKeyPathTypeString: {
363 EncodeStringWithLength(value.string(), into);
366 case WebIDBKeyPathTypeArray: {
367 const std::vector<base::string16>& array = value.array();
368 size_t count = array.size();
369 EncodeVarInt(count, into);
370 for (size_t i = 0; i < count; ++i) {
371 EncodeStringWithLength(array[i], into);
378 void EncodeBlobJournal(const BlobJournalType& journal, std::string* into) {
379 BlobJournalType::const_iterator iter;
380 for (iter = journal.begin(); iter != journal.end(); ++iter) {
381 EncodeVarInt(iter->first, into);
382 EncodeVarInt(iter->second, into);
386 bool DecodeByte(StringPiece* slice, unsigned char* value) {
390 *value = (*slice)[0];
391 slice->remove_prefix(1);
395 bool DecodeBool(StringPiece* slice, bool* value) {
399 *value = !!(*slice)[0];
400 slice->remove_prefix(1);
404 bool DecodeInt(StringPiece* slice, int64* value) {
408 StringPiece::const_iterator it = slice->begin();
411 while (it != slice->end()) {
412 unsigned char c = *it++;
413 ret |= static_cast<int64>(c) << shift;
417 slice->remove_prefix(it - slice->begin());
421 bool DecodeVarInt(StringPiece* slice, int64* value) {
425 StringPiece::const_iterator it = slice->begin();
429 if (it == slice->end())
432 unsigned char c = *it;
433 ret |= static_cast<int64>(c & 0x7f) << shift;
435 } while (*it++ & 0x80);
437 slice->remove_prefix(it - slice->begin());
441 bool DecodeString(StringPiece* slice, base::string16* value) {
442 if (slice->empty()) {
447 // Backing store is UTF-16BE, convert to host endianness.
448 DCHECK(!(slice->size() % sizeof(base::char16)));
449 size_t length = slice->size() / sizeof(base::char16);
450 base::string16 decoded;
451 decoded.reserve(length);
452 const base::char16* encoded =
453 reinterpret_cast<const base::char16*>(slice->begin());
454 for (unsigned i = 0; i < length; ++i)
455 decoded.push_back(ntohs(*encoded++));
458 slice->remove_prefix(length * sizeof(base::char16));
462 bool DecodeStringWithLength(StringPiece* slice, base::string16* value) {
467 if (!DecodeVarInt(slice, &length) || length < 0)
469 size_t bytes = length * sizeof(base::char16);
470 if (slice->size() < bytes)
473 StringPiece subpiece(slice->begin(), bytes);
474 slice->remove_prefix(bytes);
475 if (!DecodeString(&subpiece, value))
481 bool DecodeBinary(StringPiece* slice, std::string* value) {
486 if (!DecodeVarInt(slice, &length) || length < 0)
488 size_t size = length;
489 if (slice->size() < size)
492 value->assign(slice->begin(), size);
493 slice->remove_prefix(size);
497 bool DecodeIDBKey(StringPiece* slice, scoped_ptr<IndexedDBKey>* value) {
501 unsigned char type = (*slice)[0];
502 slice->remove_prefix(1);
505 case kIndexedDBKeyNullTypeByte:
506 *value = make_scoped_ptr(new IndexedDBKey());
509 case kIndexedDBKeyArrayTypeByte: {
511 if (!DecodeVarInt(slice, &length) || length < 0)
513 IndexedDBKey::KeyArray array;
515 scoped_ptr<IndexedDBKey> key;
516 if (!DecodeIDBKey(slice, &key))
518 array.push_back(*key);
520 *value = make_scoped_ptr(new IndexedDBKey(array));
523 case kIndexedDBKeyBinaryTypeByte: {
525 if (!DecodeBinary(slice, &binary))
527 *value = make_scoped_ptr(new IndexedDBKey(binary));
530 case kIndexedDBKeyStringTypeByte: {
532 if (!DecodeStringWithLength(slice, &s))
534 *value = make_scoped_ptr(new IndexedDBKey(s));
537 case kIndexedDBKeyDateTypeByte: {
539 if (!DecodeDouble(slice, &d))
541 *value = make_scoped_ptr(new IndexedDBKey(d, WebIDBKeyTypeDate));
544 case kIndexedDBKeyNumberTypeByte: {
546 if (!DecodeDouble(slice, &d))
548 *value = make_scoped_ptr(new IndexedDBKey(d, WebIDBKeyTypeNumber));
557 bool DecodeDouble(StringPiece* slice, double* value) {
558 if (slice->size() < sizeof(*value))
561 memcpy(value, slice->begin(), sizeof(*value));
562 slice->remove_prefix(sizeof(*value));
566 bool DecodeIDBKeyPath(StringPiece* slice, IndexedDBKeyPath* value) {
567 // May be typed, or may be a raw string. An invalid leading
568 // byte sequence is used to identify typed coding. New records are
569 // always written as typed.
570 if (slice->size() < 3 || (*slice)[0] != kIndexedDBKeyPathTypeCodedByte1 ||
571 (*slice)[1] != kIndexedDBKeyPathTypeCodedByte2) {
573 if (!DecodeString(slice, &s))
575 *value = IndexedDBKeyPath(s);
579 slice->remove_prefix(2);
580 DCHECK(!slice->empty());
581 WebIDBKeyPathType type = static_cast<WebIDBKeyPathType>((*slice)[0]);
582 slice->remove_prefix(1);
585 case WebIDBKeyPathTypeNull:
586 DCHECK(slice->empty());
587 *value = IndexedDBKeyPath();
589 case WebIDBKeyPathTypeString: {
590 base::string16 string;
591 if (!DecodeStringWithLength(slice, &string))
593 DCHECK(slice->empty());
594 *value = IndexedDBKeyPath(string);
597 case WebIDBKeyPathTypeArray: {
598 std::vector<base::string16> array;
600 if (!DecodeVarInt(slice, &count))
604 base::string16 string;
605 if (!DecodeStringWithLength(slice, &string))
607 array.push_back(string);
609 DCHECK(slice->empty());
610 *value = IndexedDBKeyPath(array);
618 bool DecodeBlobJournal(StringPiece* slice, BlobJournalType* journal) {
619 BlobJournalType output;
620 while (!slice->empty()) {
621 int64 database_id = -1;
623 if (!DecodeVarInt(slice, &database_id))
625 if (!KeyPrefix::IsValidDatabaseId(database_id))
627 if (!DecodeVarInt(slice, &blob_key))
629 if (!DatabaseMetaDataKey::IsValidBlobKey(blob_key) &&
630 (blob_key != DatabaseMetaDataKey::kAllBlobsKey)) {
633 output.push_back(std::make_pair(database_id, blob_key));
635 journal->swap(output);
639 bool ConsumeEncodedIDBKey(StringPiece* slice) {
640 unsigned char type = (*slice)[0];
641 slice->remove_prefix(1);
644 case kIndexedDBKeyNullTypeByte:
645 case kIndexedDBKeyMinKeyTypeByte:
647 case kIndexedDBKeyArrayTypeByte: {
649 if (!DecodeVarInt(slice, &length))
652 if (!ConsumeEncodedIDBKey(slice))
657 case kIndexedDBKeyBinaryTypeByte: {
659 if (!DecodeVarInt(slice, &length) || length < 0)
661 if (slice->size() < static_cast<size_t>(length))
663 slice->remove_prefix(length);
666 case kIndexedDBKeyStringTypeByte: {
668 if (!DecodeVarInt(slice, &length) || length < 0)
670 if (slice->size() < static_cast<size_t>(length) * sizeof(base::char16))
672 slice->remove_prefix(length * sizeof(base::char16));
675 case kIndexedDBKeyDateTypeByte:
676 case kIndexedDBKeyNumberTypeByte:
677 if (slice->size() < sizeof(double))
679 slice->remove_prefix(sizeof(double));
686 bool ExtractEncodedIDBKey(StringPiece* slice, std::string* result) {
687 const char* start = slice->begin();
688 if (!ConsumeEncodedIDBKey(slice))
692 result->assign(start, slice->begin());
696 static WebIDBKeyType KeyTypeByteToKeyType(unsigned char type) {
698 case kIndexedDBKeyNullTypeByte:
699 return WebIDBKeyTypeInvalid;
700 case kIndexedDBKeyArrayTypeByte:
701 return WebIDBKeyTypeArray;
702 case kIndexedDBKeyBinaryTypeByte:
703 return WebIDBKeyTypeBinary;
704 case kIndexedDBKeyStringTypeByte:
705 return WebIDBKeyTypeString;
706 case kIndexedDBKeyDateTypeByte:
707 return WebIDBKeyTypeDate;
708 case kIndexedDBKeyNumberTypeByte:
709 return WebIDBKeyTypeNumber;
710 case kIndexedDBKeyMinKeyTypeByte:
711 return WebIDBKeyTypeMin;
715 return WebIDBKeyTypeInvalid;
718 int CompareEncodedStringsWithLength(StringPiece* slice1,
722 if (!DecodeVarInt(slice1, &len1) || !DecodeVarInt(slice2, &len2)) {
728 if (len1 < 0 || len2 < 0) {
732 DCHECK_GE(slice1->size(), len1 * sizeof(base::char16));
733 DCHECK_GE(slice2->size(), len2 * sizeof(base::char16));
734 if (slice1->size() < len1 * sizeof(base::char16) ||
735 slice2->size() < len2 * sizeof(base::char16)) {
740 // Extract the string data, and advance the passed slices.
741 StringPiece string1(slice1->begin(), len1 * sizeof(base::char16));
742 StringPiece string2(slice2->begin(), len2 * sizeof(base::char16));
743 slice1->remove_prefix(len1 * sizeof(base::char16));
744 slice2->remove_prefix(len2 * sizeof(base::char16));
747 // Strings are UTF-16BE encoded, so a simple memcmp is sufficient.
748 return string1.compare(string2);
751 int CompareEncodedBinary(StringPiece* slice1,
755 if (!DecodeVarInt(slice1, &len1) || !DecodeVarInt(slice2, &len2)) {
761 if (len1 < 0 || len2 < 0) {
768 DCHECK_GE(slice1->size(), size1);
769 DCHECK_GE(slice2->size(), size2);
770 if (slice1->size() < size1 || slice2->size() < size2) {
775 // Extract the binary data, and advance the passed slices.
776 StringPiece binary1(slice1->begin(), size1);
777 StringPiece binary2(slice2->begin(), size2);
778 slice1->remove_prefix(size1);
779 slice2->remove_prefix(size2);
782 // This is the same as a memcmp()
783 return binary1.compare(binary2);
786 static int CompareInts(int64 a, int64 b) {
788 // Exercised by unit tests in debug only.
800 static inline int CompareSizes(size_t a, size_t b) {
808 static int CompareTypes(WebIDBKeyType a, WebIDBKeyType b) { return b - a; }
810 int CompareEncodedIDBKeys(StringPiece* slice_a,
811 StringPiece* slice_b,
813 DCHECK(!slice_a->empty());
814 DCHECK(!slice_b->empty());
816 unsigned char type_a = (*slice_a)[0];
817 unsigned char type_b = (*slice_b)[0];
818 slice_a->remove_prefix(1);
819 slice_b->remove_prefix(1);
821 if (int x = CompareTypes(KeyTypeByteToKeyType(type_a),
822 KeyTypeByteToKeyType(type_b)))
826 case kIndexedDBKeyNullTypeByte:
827 case kIndexedDBKeyMinKeyTypeByte:
828 // Null type or max type; no payload to compare.
830 case kIndexedDBKeyArrayTypeByte: {
831 int64 length_a, length_b;
832 if (!DecodeVarInt(slice_a, &length_a) ||
833 !DecodeVarInt(slice_b, &length_b)) {
837 for (int64 i = 0; i < length_a && i < length_b; ++i) {
838 int result = CompareEncodedIDBKeys(slice_a, slice_b, ok);
842 return length_a - length_b;
844 case kIndexedDBKeyBinaryTypeByte:
845 return CompareEncodedBinary(slice_a, slice_b, ok);
846 case kIndexedDBKeyStringTypeByte:
847 return CompareEncodedStringsWithLength(slice_a, slice_b, ok);
848 case kIndexedDBKeyDateTypeByte:
849 case kIndexedDBKeyNumberTypeByte: {
851 if (!DecodeDouble(slice_a, &d) || !DecodeDouble(slice_b, &e)) {
869 template <typename KeyType>
870 int Compare(const StringPiece& a,
871 const StringPiece& b,
872 bool only_compare_index_keys,
877 StringPiece slice_a(a);
878 if (!KeyType::Decode(&slice_a, &key_a)) {
882 StringPiece slice_b(b);
883 if (!KeyType::Decode(&slice_b, &key_b)) {
889 return key_a.Compare(key_b);
892 template <typename KeyType>
893 int CompareSuffix(StringPiece* a,
895 bool only_compare_index_keys,
902 int CompareSuffix<ExistsEntryKey>(StringPiece* slice_a,
903 StringPiece* slice_b,
904 bool only_compare_index_keys,
906 DCHECK(!slice_a->empty());
907 DCHECK(!slice_b->empty());
908 return CompareEncodedIDBKeys(slice_a, slice_b, ok);
912 int CompareSuffix<ObjectStoreDataKey>(StringPiece* slice_a,
913 StringPiece* slice_b,
914 bool only_compare_index_keys,
916 return CompareEncodedIDBKeys(slice_a, slice_b, ok);
920 int CompareSuffix<BlobEntryKey>(StringPiece* slice_a,
921 StringPiece* slice_b,
922 bool only_compare_index_keys,
924 return CompareEncodedIDBKeys(slice_a, slice_b, ok);
928 int CompareSuffix<IndexDataKey>(StringPiece* slice_a,
929 StringPiece* slice_b,
930 bool only_compare_index_keys,
933 int result = CompareEncodedIDBKeys(slice_a, slice_b, ok);
936 if (only_compare_index_keys)
939 // sequence number [optional]
940 int64 sequence_number_a = -1;
941 int64 sequence_number_b = -1;
942 if (!slice_a->empty() && !DecodeVarInt(slice_a, &sequence_number_a))
944 if (!slice_b->empty() && !DecodeVarInt(slice_b, &sequence_number_b))
947 if (slice_a->empty() || slice_b->empty())
948 return CompareSizes(slice_a->size(), slice_b->size());
950 // primary key [optional]
951 result = CompareEncodedIDBKeys(slice_a, slice_b, ok);
955 return CompareInts(sequence_number_a, sequence_number_b);
958 int Compare(const StringPiece& a,
959 const StringPiece& b,
960 bool only_compare_index_keys,
962 StringPiece slice_a(a);
963 StringPiece slice_b(b);
966 bool ok_a = KeyPrefix::Decode(&slice_a, &prefix_a);
967 bool ok_b = KeyPrefix::Decode(&slice_b, &prefix_b);
970 if (!ok_a || !ok_b) {
976 if (int x = prefix_a.Compare(prefix_b))
979 switch (prefix_a.type()) {
980 case KeyPrefix::GLOBAL_METADATA: {
981 DCHECK(!slice_a.empty());
982 DCHECK(!slice_b.empty());
984 unsigned char type_byte_a;
985 if (!DecodeByte(&slice_a, &type_byte_a)) {
990 unsigned char type_byte_b;
991 if (!DecodeByte(&slice_b, &type_byte_b)) {
996 if (int x = type_byte_a - type_byte_b)
998 if (type_byte_a < kMaxSimpleGlobalMetaDataTypeByte)
1001 // Compare<> is used (which re-decodes the prefix) rather than an
1002 // specialized CompareSuffix<> because metadata is relatively uncommon
1005 if (type_byte_a == kDatabaseFreeListTypeByte) {
1006 // TODO(jsbell): No need to pass only_compare_index_keys through here.
1007 return Compare<DatabaseFreeListKey>(a, b, only_compare_index_keys, ok);
1009 if (type_byte_a == kDatabaseNameTypeByte) {
1010 return Compare<DatabaseNameKey>(
1011 a, b, /*only_compare_index_keys*/ false, ok);
1016 case KeyPrefix::DATABASE_METADATA: {
1017 DCHECK(!slice_a.empty());
1018 DCHECK(!slice_b.empty());
1020 unsigned char type_byte_a;
1021 if (!DecodeByte(&slice_a, &type_byte_a)) {
1026 unsigned char type_byte_b;
1027 if (!DecodeByte(&slice_b, &type_byte_b)) {
1032 if (int x = type_byte_a - type_byte_b)
1034 if (type_byte_a < DatabaseMetaDataKey::MAX_SIMPLE_METADATA_TYPE)
1037 // Compare<> is used (which re-decodes the prefix) rather than an
1038 // specialized CompareSuffix<> because metadata is relatively uncommon
1041 if (type_byte_a == kObjectStoreMetaDataTypeByte) {
1042 // TODO(jsbell): No need to pass only_compare_index_keys through here.
1043 return Compare<ObjectStoreMetaDataKey>(
1044 a, b, only_compare_index_keys, ok);
1046 if (type_byte_a == kIndexMetaDataTypeByte) {
1047 return Compare<IndexMetaDataKey>(
1048 a, b, /*only_compare_index_keys*/ false, ok);
1050 if (type_byte_a == kObjectStoreFreeListTypeByte) {
1051 return Compare<ObjectStoreFreeListKey>(
1052 a, b, only_compare_index_keys, ok);
1054 if (type_byte_a == kIndexFreeListTypeByte) {
1055 return Compare<IndexFreeListKey>(
1056 a, b, /*only_compare_index_keys*/ false, ok);
1058 if (type_byte_a == kObjectStoreNamesTypeByte) {
1059 // TODO(jsbell): No need to pass only_compare_index_keys through here.
1060 return Compare<ObjectStoreNamesKey>(
1061 a, b, only_compare_index_keys, ok);
1063 if (type_byte_a == kIndexNamesKeyTypeByte) {
1064 return Compare<IndexNamesKey>(
1065 a, b, /*only_compare_index_keys*/ false, ok);
1070 case KeyPrefix::OBJECT_STORE_DATA: {
1071 // Provide a stable ordering for invalid data.
1072 if (slice_a.empty() || slice_b.empty())
1073 return CompareSizes(slice_a.size(), slice_b.size());
1075 return CompareSuffix<ObjectStoreDataKey>(
1076 &slice_a, &slice_b, /*only_compare_index_keys*/ false, ok);
1079 case KeyPrefix::EXISTS_ENTRY: {
1080 // Provide a stable ordering for invalid data.
1081 if (slice_a.empty() || slice_b.empty())
1082 return CompareSizes(slice_a.size(), slice_b.size());
1084 return CompareSuffix<ExistsEntryKey>(
1085 &slice_a, &slice_b, /*only_compare_index_keys*/ false, ok);
1088 case KeyPrefix::BLOB_ENTRY: {
1089 // Provide a stable ordering for invalid data.
1090 if (slice_a.empty() || slice_b.empty())
1091 return CompareSizes(slice_a.size(), slice_b.size());
1093 return CompareSuffix<BlobEntryKey>(
1094 &slice_a, &slice_b, /*only_compare_index_keys*/ false, ok);
1097 case KeyPrefix::INDEX_DATA: {
1098 // Provide a stable ordering for invalid data.
1099 if (slice_a.empty() || slice_b.empty())
1100 return CompareSizes(slice_a.size(), slice_b.size());
1102 return CompareSuffix<IndexDataKey>(
1103 &slice_a, &slice_b, only_compare_index_keys, ok);
1106 case KeyPrefix::INVALID_TYPE:
1117 int Compare(const StringPiece& a,
1118 const StringPiece& b,
1119 bool only_compare_index_keys) {
1121 int result = Compare(a, b, only_compare_index_keys, &ok);
1128 KeyPrefix::KeyPrefix()
1129 : database_id_(INVALID_TYPE),
1130 object_store_id_(INVALID_TYPE),
1131 index_id_(INVALID_TYPE) {}
1133 KeyPrefix::KeyPrefix(int64 database_id)
1134 : database_id_(database_id), object_store_id_(0), index_id_(0) {
1135 DCHECK(KeyPrefix::IsValidDatabaseId(database_id));
1138 KeyPrefix::KeyPrefix(int64 database_id, int64 object_store_id)
1139 : database_id_(database_id),
1140 object_store_id_(object_store_id),
1142 DCHECK(KeyPrefix::IsValidDatabaseId(database_id));
1143 DCHECK(KeyPrefix::IsValidObjectStoreId(object_store_id));
1146 KeyPrefix::KeyPrefix(int64 database_id, int64 object_store_id, int64 index_id)
1147 : database_id_(database_id),
1148 object_store_id_(object_store_id),
1149 index_id_(index_id) {
1150 DCHECK(KeyPrefix::IsValidDatabaseId(database_id));
1151 DCHECK(KeyPrefix::IsValidObjectStoreId(object_store_id));
1152 DCHECK(KeyPrefix::IsValidIndexId(index_id));
1155 KeyPrefix::KeyPrefix(enum Type type,
1157 int64 object_store_id,
1159 : database_id_(database_id),
1160 object_store_id_(object_store_id),
1161 index_id_(index_id) {
1162 DCHECK_EQ(type, INVALID_TYPE);
1163 DCHECK(KeyPrefix::IsValidDatabaseId(database_id));
1164 DCHECK(KeyPrefix::IsValidObjectStoreId(object_store_id));
1167 KeyPrefix KeyPrefix::CreateWithSpecialIndex(int64 database_id,
1168 int64 object_store_id,
1170 DCHECK(KeyPrefix::IsValidDatabaseId(database_id));
1171 DCHECK(KeyPrefix::IsValidObjectStoreId(object_store_id));
1173 return KeyPrefix(INVALID_TYPE, database_id, object_store_id, index_id);
1176 bool KeyPrefix::IsValidDatabaseId(int64 database_id) {
1177 return (database_id > 0) && (database_id < KeyPrefix::kMaxDatabaseId);
1180 bool KeyPrefix::IsValidObjectStoreId(int64 object_store_id) {
1181 return (object_store_id > 0) &&
1182 (object_store_id < KeyPrefix::kMaxObjectStoreId);
1185 bool KeyPrefix::IsValidIndexId(int64 index_id) {
1186 return (index_id >= kMinimumIndexId) && (index_id < KeyPrefix::kMaxIndexId);
1189 bool KeyPrefix::Decode(StringPiece* slice, KeyPrefix* result) {
1190 unsigned char first_byte;
1191 if (!DecodeByte(slice, &first_byte))
1194 size_t database_id_bytes = ((first_byte >> 5) & 0x7) + 1;
1195 size_t object_store_id_bytes = ((first_byte >> 2) & 0x7) + 1;
1196 size_t index_id_bytes = (first_byte & 0x3) + 1;
1198 if (database_id_bytes + object_store_id_bytes + index_id_bytes >
1203 StringPiece tmp(slice->begin(), database_id_bytes);
1204 if (!DecodeInt(&tmp, &result->database_id_))
1207 slice->remove_prefix(database_id_bytes);
1209 StringPiece tmp(slice->begin(), object_store_id_bytes);
1210 if (!DecodeInt(&tmp, &result->object_store_id_))
1213 slice->remove_prefix(object_store_id_bytes);
1215 StringPiece tmp(slice->begin(), index_id_bytes);
1216 if (!DecodeInt(&tmp, &result->index_id_))
1219 slice->remove_prefix(index_id_bytes);
1223 std::string KeyPrefix::EncodeEmpty() {
1224 const std::string result(4, 0);
1225 DCHECK(EncodeInternal(0, 0, 0) == std::string(4, 0));
1229 std::string KeyPrefix::Encode() const {
1230 DCHECK(database_id_ != kInvalidId);
1231 DCHECK(object_store_id_ != kInvalidId);
1232 DCHECK(index_id_ != kInvalidId);
1233 return EncodeInternal(database_id_, object_store_id_, index_id_);
1236 std::string KeyPrefix::EncodeInternal(int64 database_id,
1237 int64 object_store_id,
1239 std::string database_id_string;
1240 std::string object_store_id_string;
1241 std::string index_id_string;
1243 EncodeIntSafely(database_id, kMaxDatabaseId, &database_id_string);
1244 EncodeIntSafely(object_store_id, kMaxObjectStoreId, &object_store_id_string);
1245 EncodeIntSafely(index_id, kMaxIndexId, &index_id_string);
1247 DCHECK(database_id_string.size() <= kMaxDatabaseIdSizeBytes);
1248 DCHECK(object_store_id_string.size() <= kMaxObjectStoreIdSizeBytes);
1249 DCHECK(index_id_string.size() <= kMaxIndexIdSizeBytes);
1251 unsigned char first_byte =
1252 (database_id_string.size() - 1) << (kMaxObjectStoreIdSizeBits +
1253 kMaxIndexIdSizeBits) |
1254 (object_store_id_string.size() - 1) << kMaxIndexIdSizeBits |
1255 (index_id_string.size() - 1);
1256 COMPILE_ASSERT(kMaxDatabaseIdSizeBits + kMaxObjectStoreIdSizeBits +
1257 kMaxIndexIdSizeBits ==
1258 sizeof(first_byte) * 8,
1261 ret.reserve(kDefaultInlineBufferSize);
1262 ret.push_back(first_byte);
1263 ret.append(database_id_string);
1264 ret.append(object_store_id_string);
1265 ret.append(index_id_string);
1267 DCHECK_LE(ret.size(), kDefaultInlineBufferSize);
1271 int KeyPrefix::Compare(const KeyPrefix& other) const {
1272 DCHECK(database_id_ != kInvalidId);
1273 DCHECK(object_store_id_ != kInvalidId);
1274 DCHECK(index_id_ != kInvalidId);
1276 if (database_id_ != other.database_id_)
1277 return CompareInts(database_id_, other.database_id_);
1278 if (object_store_id_ != other.object_store_id_)
1279 return CompareInts(object_store_id_, other.object_store_id_);
1280 if (index_id_ != other.index_id_)
1281 return CompareInts(index_id_, other.index_id_);
1285 KeyPrefix::Type KeyPrefix::type() const {
1286 DCHECK(database_id_ != kInvalidId);
1287 DCHECK(object_store_id_ != kInvalidId);
1288 DCHECK(index_id_ != kInvalidId);
1291 return GLOBAL_METADATA;
1292 if (!object_store_id_)
1293 return DATABASE_METADATA;
1294 if (index_id_ == kObjectStoreDataIndexId)
1295 return OBJECT_STORE_DATA;
1296 if (index_id_ == kExistsEntryIndexId)
1297 return EXISTS_ENTRY;
1298 if (index_id_ == kBlobEntryIndexId)
1300 if (index_id_ >= kMinimumIndexId)
1304 return INVALID_TYPE;
1307 std::string SchemaVersionKey::Encode() {
1308 std::string ret = KeyPrefix::EncodeEmpty();
1309 ret.push_back(kSchemaVersionTypeByte);
1313 std::string MaxDatabaseIdKey::Encode() {
1314 std::string ret = KeyPrefix::EncodeEmpty();
1315 ret.push_back(kMaxDatabaseIdTypeByte);
1319 std::string DataVersionKey::Encode() {
1320 std::string ret = KeyPrefix::EncodeEmpty();
1321 ret.push_back(kDataVersionTypeByte);
1325 std::string BlobJournalKey::Encode() {
1326 std::string ret = KeyPrefix::EncodeEmpty();
1327 ret.push_back(kBlobJournalTypeByte);
1331 std::string LiveBlobJournalKey::Encode() {
1332 std::string ret = KeyPrefix::EncodeEmpty();
1333 ret.push_back(kLiveBlobJournalTypeByte);
1337 DatabaseFreeListKey::DatabaseFreeListKey() : database_id_(-1) {}
1339 bool DatabaseFreeListKey::Decode(StringPiece* slice,
1340 DatabaseFreeListKey* result) {
1342 if (!KeyPrefix::Decode(slice, &prefix))
1344 DCHECK(!prefix.database_id_);
1345 DCHECK(!prefix.object_store_id_);
1346 DCHECK(!prefix.index_id_);
1347 unsigned char type_byte = 0;
1348 if (!DecodeByte(slice, &type_byte))
1350 DCHECK_EQ(type_byte, kDatabaseFreeListTypeByte);
1351 if (!DecodeVarInt(slice, &result->database_id_))
1356 std::string DatabaseFreeListKey::Encode(int64 database_id) {
1357 std::string ret = KeyPrefix::EncodeEmpty();
1358 ret.push_back(kDatabaseFreeListTypeByte);
1359 EncodeVarInt(database_id, &ret);
1363 std::string DatabaseFreeListKey::EncodeMaxKey() {
1364 return Encode(std::numeric_limits<int64>::max());
1367 int64 DatabaseFreeListKey::DatabaseId() const {
1368 DCHECK_GE(database_id_, 0);
1369 return database_id_;
1372 int DatabaseFreeListKey::Compare(const DatabaseFreeListKey& other) const {
1373 DCHECK_GE(database_id_, 0);
1374 return CompareInts(database_id_, other.database_id_);
1377 bool DatabaseNameKey::Decode(StringPiece* slice, DatabaseNameKey* result) {
1379 if (!KeyPrefix::Decode(slice, &prefix))
1381 DCHECK(!prefix.database_id_);
1382 DCHECK(!prefix.object_store_id_);
1383 DCHECK(!prefix.index_id_);
1384 unsigned char type_byte = 0;
1385 if (!DecodeByte(slice, &type_byte))
1387 DCHECK_EQ(type_byte, kDatabaseNameTypeByte);
1388 if (!DecodeStringWithLength(slice, &result->origin_))
1390 if (!DecodeStringWithLength(slice, &result->database_name_))
1395 std::string DatabaseNameKey::Encode(const std::string& origin_identifier,
1396 const base::string16& database_name) {
1397 std::string ret = KeyPrefix::EncodeEmpty();
1398 ret.push_back(kDatabaseNameTypeByte);
1399 EncodeStringWithLength(base::ASCIIToUTF16(origin_identifier), &ret);
1400 EncodeStringWithLength(database_name, &ret);
1404 std::string DatabaseNameKey::EncodeMinKeyForOrigin(
1405 const std::string& origin_identifier) {
1406 return Encode(origin_identifier, base::string16());
1409 std::string DatabaseNameKey::EncodeStopKeyForOrigin(
1410 const std::string& origin_identifier) {
1411 // just after origin in collation order
1412 return EncodeMinKeyForOrigin(origin_identifier + '\x01');
1415 int DatabaseNameKey::Compare(const DatabaseNameKey& other) {
1416 if (int x = origin_.compare(other.origin_))
1418 return database_name_.compare(other.database_name_);
1421 bool DatabaseMetaDataKey::IsValidBlobKey(int64 blob_key) {
1422 return blob_key >= kBlobKeyGeneratorInitialNumber;
1425 const int64 DatabaseMetaDataKey::kAllBlobsKey = 1;
1426 const int64 DatabaseMetaDataKey::kBlobKeyGeneratorInitialNumber = 2;
1427 const int64 DatabaseMetaDataKey::kInvalidBlobKey = -1;
1429 std::string DatabaseMetaDataKey::Encode(int64 database_id,
1430 MetaDataType meta_data_type) {
1431 KeyPrefix prefix(database_id);
1432 std::string ret = prefix.Encode();
1433 ret.push_back(meta_data_type);
1437 ObjectStoreMetaDataKey::ObjectStoreMetaDataKey()
1438 : object_store_id_(-1), meta_data_type_(-1) {}
1440 bool ObjectStoreMetaDataKey::Decode(StringPiece* slice,
1441 ObjectStoreMetaDataKey* result) {
1443 if (!KeyPrefix::Decode(slice, &prefix))
1445 DCHECK(prefix.database_id_);
1446 DCHECK(!prefix.object_store_id_);
1447 DCHECK(!prefix.index_id_);
1448 unsigned char type_byte = 0;
1449 if (!DecodeByte(slice, &type_byte))
1451 DCHECK_EQ(type_byte, kObjectStoreMetaDataTypeByte);
1452 if (!DecodeVarInt(slice, &result->object_store_id_))
1454 DCHECK(result->object_store_id_);
1455 if (!DecodeByte(slice, &result->meta_data_type_))
1460 std::string ObjectStoreMetaDataKey::Encode(int64 database_id,
1461 int64 object_store_id,
1462 unsigned char meta_data_type) {
1463 KeyPrefix prefix(database_id);
1464 std::string ret = prefix.Encode();
1465 ret.push_back(kObjectStoreMetaDataTypeByte);
1466 EncodeVarInt(object_store_id, &ret);
1467 ret.push_back(meta_data_type);
1471 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id) {
1472 return Encode(database_id,
1473 std::numeric_limits<int64>::max(),
1474 kObjectMetaDataTypeMaximum);
1477 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id,
1478 int64 object_store_id) {
1479 return Encode(database_id, object_store_id, kObjectMetaDataTypeMaximum);
1482 int64 ObjectStoreMetaDataKey::ObjectStoreId() const {
1483 DCHECK_GE(object_store_id_, 0);
1484 return object_store_id_;
1486 unsigned char ObjectStoreMetaDataKey::MetaDataType() const {
1487 return meta_data_type_;
1490 int ObjectStoreMetaDataKey::Compare(const ObjectStoreMetaDataKey& other) {
1491 DCHECK_GE(object_store_id_, 0);
1492 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1494 return meta_data_type_ - other.meta_data_type_;
1497 IndexMetaDataKey::IndexMetaDataKey()
1498 : object_store_id_(-1), index_id_(-1), meta_data_type_(0) {}
1500 bool IndexMetaDataKey::Decode(StringPiece* slice, IndexMetaDataKey* result) {
1502 if (!KeyPrefix::Decode(slice, &prefix))
1504 DCHECK(prefix.database_id_);
1505 DCHECK(!prefix.object_store_id_);
1506 DCHECK(!prefix.index_id_);
1507 unsigned char type_byte = 0;
1508 if (!DecodeByte(slice, &type_byte))
1510 DCHECK_EQ(type_byte, kIndexMetaDataTypeByte);
1511 if (!DecodeVarInt(slice, &result->object_store_id_))
1513 if (!DecodeVarInt(slice, &result->index_id_))
1515 if (!DecodeByte(slice, &result->meta_data_type_))
1520 std::string IndexMetaDataKey::Encode(int64 database_id,
1521 int64 object_store_id,
1523 unsigned char meta_data_type) {
1524 KeyPrefix prefix(database_id);
1525 std::string ret = prefix.Encode();
1526 ret.push_back(kIndexMetaDataTypeByte);
1527 EncodeVarInt(object_store_id, &ret);
1528 EncodeVarInt(index_id, &ret);
1529 EncodeByte(meta_data_type, &ret);
1533 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id,
1534 int64 object_store_id) {
1535 return Encode(database_id,
1537 std::numeric_limits<int64>::max(),
1538 kIndexMetaDataTypeMaximum);
1541 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id,
1542 int64 object_store_id,
1545 database_id, object_store_id, index_id, kIndexMetaDataTypeMaximum);
1548 int IndexMetaDataKey::Compare(const IndexMetaDataKey& other) {
1549 DCHECK_GE(object_store_id_, 0);
1550 DCHECK_GE(index_id_, 0);
1552 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1554 if (int x = CompareInts(index_id_, other.index_id_))
1556 return meta_data_type_ - other.meta_data_type_;
1559 int64 IndexMetaDataKey::IndexId() const {
1560 DCHECK_GE(index_id_, 0);
1564 ObjectStoreFreeListKey::ObjectStoreFreeListKey() : object_store_id_(-1) {}
1566 bool ObjectStoreFreeListKey::Decode(StringPiece* slice,
1567 ObjectStoreFreeListKey* result) {
1569 if (!KeyPrefix::Decode(slice, &prefix))
1571 DCHECK(prefix.database_id_);
1572 DCHECK(!prefix.object_store_id_);
1573 DCHECK(!prefix.index_id_);
1574 unsigned char type_byte = 0;
1575 if (!DecodeByte(slice, &type_byte))
1577 DCHECK_EQ(type_byte, kObjectStoreFreeListTypeByte);
1578 if (!DecodeVarInt(slice, &result->object_store_id_))
1583 std::string ObjectStoreFreeListKey::Encode(int64 database_id,
1584 int64 object_store_id) {
1585 KeyPrefix prefix(database_id);
1586 std::string ret = prefix.Encode();
1587 ret.push_back(kObjectStoreFreeListTypeByte);
1588 EncodeVarInt(object_store_id, &ret);
1592 std::string ObjectStoreFreeListKey::EncodeMaxKey(int64 database_id) {
1593 return Encode(database_id, std::numeric_limits<int64>::max());
1596 int64 ObjectStoreFreeListKey::ObjectStoreId() const {
1597 DCHECK_GE(object_store_id_, 0);
1598 return object_store_id_;
1601 int ObjectStoreFreeListKey::Compare(const ObjectStoreFreeListKey& other) {
1602 // TODO(jsbell): It may seem strange that we're not comparing database id's,
1603 // but that comparison will have been made earlier.
1604 // We should probably make this more clear, though...
1605 DCHECK_GE(object_store_id_, 0);
1606 return CompareInts(object_store_id_, other.object_store_id_);
1609 IndexFreeListKey::IndexFreeListKey() : object_store_id_(-1), index_id_(-1) {}
1611 bool IndexFreeListKey::Decode(StringPiece* slice, IndexFreeListKey* result) {
1613 if (!KeyPrefix::Decode(slice, &prefix))
1615 DCHECK(prefix.database_id_);
1616 DCHECK(!prefix.object_store_id_);
1617 DCHECK(!prefix.index_id_);
1618 unsigned char type_byte = 0;
1619 if (!DecodeByte(slice, &type_byte))
1621 DCHECK_EQ(type_byte, kIndexFreeListTypeByte);
1622 if (!DecodeVarInt(slice, &result->object_store_id_))
1624 if (!DecodeVarInt(slice, &result->index_id_))
1629 std::string IndexFreeListKey::Encode(int64 database_id,
1630 int64 object_store_id,
1632 KeyPrefix prefix(database_id);
1633 std::string ret = prefix.Encode();
1634 ret.push_back(kIndexFreeListTypeByte);
1635 EncodeVarInt(object_store_id, &ret);
1636 EncodeVarInt(index_id, &ret);
1640 std::string IndexFreeListKey::EncodeMaxKey(int64 database_id,
1641 int64 object_store_id) {
1643 database_id, object_store_id, std::numeric_limits<int64>::max());
1646 int IndexFreeListKey::Compare(const IndexFreeListKey& other) {
1647 DCHECK_GE(object_store_id_, 0);
1648 DCHECK_GE(index_id_, 0);
1649 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1651 return CompareInts(index_id_, other.index_id_);
1654 int64 IndexFreeListKey::ObjectStoreId() const {
1655 DCHECK_GE(object_store_id_, 0);
1656 return object_store_id_;
1659 int64 IndexFreeListKey::IndexId() const {
1660 DCHECK_GE(index_id_, 0);
1664 // TODO(jsbell): We never use this to look up object store ids,
1665 // because a mapping is kept in the IndexedDBDatabase. Can the
1666 // mapping become unreliable? Can we remove this?
1667 bool ObjectStoreNamesKey::Decode(StringPiece* slice,
1668 ObjectStoreNamesKey* result) {
1670 if (!KeyPrefix::Decode(slice, &prefix))
1672 DCHECK(prefix.database_id_);
1673 DCHECK(!prefix.object_store_id_);
1674 DCHECK(!prefix.index_id_);
1675 unsigned char type_byte = 0;
1676 if (!DecodeByte(slice, &type_byte))
1678 DCHECK_EQ(type_byte, kObjectStoreNamesTypeByte);
1679 if (!DecodeStringWithLength(slice, &result->object_store_name_))
1684 std::string ObjectStoreNamesKey::Encode(
1686 const base::string16& object_store_name) {
1687 KeyPrefix prefix(database_id);
1688 std::string ret = prefix.Encode();
1689 ret.push_back(kObjectStoreNamesTypeByte);
1690 EncodeStringWithLength(object_store_name, &ret);
1694 int ObjectStoreNamesKey::Compare(const ObjectStoreNamesKey& other) {
1695 return object_store_name_.compare(other.object_store_name_);
1698 IndexNamesKey::IndexNamesKey() : object_store_id_(-1) {}
1700 // TODO(jsbell): We never use this to look up index ids, because a mapping
1701 // is kept at a higher level.
1702 bool IndexNamesKey::Decode(StringPiece* slice, IndexNamesKey* result) {
1704 if (!KeyPrefix::Decode(slice, &prefix))
1706 DCHECK(prefix.database_id_);
1707 DCHECK(!prefix.object_store_id_);
1708 DCHECK(!prefix.index_id_);
1709 unsigned char type_byte = 0;
1710 if (!DecodeByte(slice, &type_byte))
1712 DCHECK_EQ(type_byte, kIndexNamesKeyTypeByte);
1713 if (!DecodeVarInt(slice, &result->object_store_id_))
1715 if (!DecodeStringWithLength(slice, &result->index_name_))
1720 std::string IndexNamesKey::Encode(int64 database_id,
1721 int64 object_store_id,
1722 const base::string16& index_name) {
1723 KeyPrefix prefix(database_id);
1724 std::string ret = prefix.Encode();
1725 ret.push_back(kIndexNamesKeyTypeByte);
1726 EncodeVarInt(object_store_id, &ret);
1727 EncodeStringWithLength(index_name, &ret);
1731 int IndexNamesKey::Compare(const IndexNamesKey& other) {
1732 DCHECK_GE(object_store_id_, 0);
1733 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1735 return index_name_.compare(other.index_name_);
1738 ObjectStoreDataKey::ObjectStoreDataKey() {}
1739 ObjectStoreDataKey::~ObjectStoreDataKey() {}
1741 bool ObjectStoreDataKey::Decode(StringPiece* slice,
1742 ObjectStoreDataKey* result) {
1744 if (!KeyPrefix::Decode(slice, &prefix))
1746 DCHECK(prefix.database_id_);
1747 DCHECK(prefix.object_store_id_);
1748 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber);
1749 if (!ExtractEncodedIDBKey(slice, &result->encoded_user_key_))
1754 std::string ObjectStoreDataKey::Encode(int64 database_id,
1755 int64 object_store_id,
1756 const std::string encoded_user_key) {
1757 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1758 database_id, object_store_id, kSpecialIndexNumber));
1759 std::string ret = prefix.Encode();
1760 ret.append(encoded_user_key);
1765 std::string ObjectStoreDataKey::Encode(int64 database_id,
1766 int64 object_store_id,
1767 const IndexedDBKey& user_key) {
1768 std::string encoded_key;
1769 EncodeIDBKey(user_key, &encoded_key);
1770 return Encode(database_id, object_store_id, encoded_key);
1773 scoped_ptr<IndexedDBKey> ObjectStoreDataKey::user_key() const {
1774 scoped_ptr<IndexedDBKey> key;
1775 StringPiece slice(encoded_user_key_);
1776 if (!DecodeIDBKey(&slice, &key)) {
1777 // TODO(jsbell): Return error.
1782 const int64 ObjectStoreDataKey::kSpecialIndexNumber = kObjectStoreDataIndexId;
1784 ExistsEntryKey::ExistsEntryKey() {}
1785 ExistsEntryKey::~ExistsEntryKey() {}
1787 bool ExistsEntryKey::Decode(StringPiece* slice, ExistsEntryKey* result) {
1789 if (!KeyPrefix::Decode(slice, &prefix))
1791 DCHECK(prefix.database_id_);
1792 DCHECK(prefix.object_store_id_);
1793 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber);
1794 if (!ExtractEncodedIDBKey(slice, &result->encoded_user_key_))
1799 std::string ExistsEntryKey::Encode(int64 database_id,
1800 int64 object_store_id,
1801 const std::string& encoded_key) {
1802 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1803 database_id, object_store_id, kSpecialIndexNumber));
1804 std::string ret = prefix.Encode();
1805 ret.append(encoded_key);
1809 std::string ExistsEntryKey::Encode(int64 database_id,
1810 int64 object_store_id,
1811 const IndexedDBKey& user_key) {
1812 std::string encoded_key;
1813 EncodeIDBKey(user_key, &encoded_key);
1814 return Encode(database_id, object_store_id, encoded_key);
1817 scoped_ptr<IndexedDBKey> ExistsEntryKey::user_key() const {
1818 scoped_ptr<IndexedDBKey> key;
1819 StringPiece slice(encoded_user_key_);
1820 if (!DecodeIDBKey(&slice, &key)) {
1821 // TODO(jsbell): Return error.
1826 const int64 ExistsEntryKey::kSpecialIndexNumber = kExistsEntryIndexId;
1828 bool BlobEntryKey::Decode(StringPiece* slice, BlobEntryKey* result) {
1830 if (!KeyPrefix::Decode(slice, &prefix))
1832 DCHECK(prefix.database_id_);
1833 DCHECK(prefix.object_store_id_);
1834 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber);
1836 if (!ExtractEncodedIDBKey(slice, &result->encoded_user_key_))
1838 result->database_id_ = prefix.database_id_;
1839 result->object_store_id_ = prefix.object_store_id_;
1844 bool BlobEntryKey::FromObjectStoreDataKey(StringPiece* slice,
1845 BlobEntryKey* result) {
1847 if (!KeyPrefix::Decode(slice, &prefix))
1849 DCHECK(prefix.database_id_);
1850 DCHECK(prefix.object_store_id_);
1851 DCHECK_EQ(prefix.index_id_, ObjectStoreDataKey::kSpecialIndexNumber);
1853 if (!ExtractEncodedIDBKey(slice, &result->encoded_user_key_))
1855 result->database_id_ = prefix.database_id_;
1856 result->object_store_id_ = prefix.object_store_id_;
1860 std::string BlobEntryKey::ReencodeToObjectStoreDataKey(StringPiece* slice) {
1861 // TODO(ericu): We could be more efficient here, since the suffix is the same.
1863 if (!Decode(slice, &key))
1864 return std::string();
1866 return ObjectStoreDataKey::Encode(
1867 key.database_id_, key.object_store_id_, key.encoded_user_key_);
1870 std::string BlobEntryKey::EncodeMinKeyForObjectStore(int64 database_id,
1871 int64 object_store_id) {
1872 // Our implied encoded_user_key_ here is empty, the lowest possible key.
1873 return Encode(database_id, object_store_id, std::string());
1876 std::string BlobEntryKey::EncodeStopKeyForObjectStore(int64 database_id,
1877 int64 object_store_id) {
1878 DCHECK(KeyPrefix::ValidIds(database_id, object_store_id));
1879 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1880 database_id, object_store_id, kSpecialIndexNumber + 1));
1881 return prefix.Encode();
1884 std::string BlobEntryKey::Encode() const {
1885 DCHECK(!encoded_user_key_.empty());
1886 return Encode(database_id_, object_store_id_, encoded_user_key_);
1889 std::string BlobEntryKey::Encode(int64 database_id,
1890 int64 object_store_id,
1891 const IndexedDBKey& user_key) {
1892 std::string encoded_key;
1893 EncodeIDBKey(user_key, &encoded_key);
1894 return Encode(database_id, object_store_id, encoded_key);
1897 std::string BlobEntryKey::Encode(int64 database_id,
1898 int64 object_store_id,
1899 const std::string& encoded_user_key) {
1900 DCHECK(KeyPrefix::ValidIds(database_id, object_store_id));
1901 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1902 database_id, object_store_id, kSpecialIndexNumber));
1903 return prefix.Encode() + encoded_user_key;
1906 const int64 BlobEntryKey::kSpecialIndexNumber = kBlobEntryIndexId;
1908 IndexDataKey::IndexDataKey()
1910 object_store_id_(-1),
1912 sequence_number_(-1) {}
1914 IndexDataKey::~IndexDataKey() {}
1916 bool IndexDataKey::Decode(StringPiece* slice, IndexDataKey* result) {
1918 if (!KeyPrefix::Decode(slice, &prefix))
1920 DCHECK(prefix.database_id_);
1921 DCHECK(prefix.object_store_id_);
1922 DCHECK_GE(prefix.index_id_, kMinimumIndexId);
1923 result->database_id_ = prefix.database_id_;
1924 result->object_store_id_ = prefix.object_store_id_;
1925 result->index_id_ = prefix.index_id_;
1926 result->sequence_number_ = -1;
1927 result->encoded_primary_key_ = MinIDBKey();
1929 if (!ExtractEncodedIDBKey(slice, &result->encoded_user_key_))
1932 // [optional] sequence number
1935 if (!DecodeVarInt(slice, &result->sequence_number_))
1938 // [optional] primary key
1941 if (!ExtractEncodedIDBKey(slice, &result->encoded_primary_key_))
1946 std::string IndexDataKey::Encode(int64 database_id,
1947 int64 object_store_id,
1949 const std::string& encoded_user_key,
1950 const std::string& encoded_primary_key,
1951 int64 sequence_number) {
1952 KeyPrefix prefix(database_id, object_store_id, index_id);
1953 std::string ret = prefix.Encode();
1954 ret.append(encoded_user_key);
1955 EncodeVarInt(sequence_number, &ret);
1956 ret.append(encoded_primary_key);
1960 std::string IndexDataKey::Encode(int64 database_id,
1961 int64 object_store_id,
1963 const IndexedDBKey& user_key) {
1964 std::string encoded_key;
1965 EncodeIDBKey(user_key, &encoded_key);
1967 database_id, object_store_id, index_id, encoded_key, MinIDBKey(), 0);
1970 std::string IndexDataKey::Encode(int64 database_id,
1971 int64 object_store_id,
1973 const IndexedDBKey& user_key,
1974 const IndexedDBKey& user_primary_key) {
1975 std::string encoded_key;
1976 EncodeIDBKey(user_key, &encoded_key);
1977 std::string encoded_primary_key;
1978 EncodeIDBKey(user_primary_key, &encoded_primary_key);
1979 return Encode(database_id,
1983 encoded_primary_key,
1987 std::string IndexDataKey::EncodeMinKey(int64 database_id,
1988 int64 object_store_id,
1991 database_id, object_store_id, index_id, MinIDBKey(), MinIDBKey(), 0);
1994 std::string IndexDataKey::EncodeMaxKey(int64 database_id,
1995 int64 object_store_id,
1997 return Encode(database_id,
2002 std::numeric_limits<int64>::max());
2005 int64 IndexDataKey::DatabaseId() const {
2006 DCHECK_GE(database_id_, 0);
2007 return database_id_;
2010 int64 IndexDataKey::ObjectStoreId() const {
2011 DCHECK_GE(object_store_id_, 0);
2012 return object_store_id_;
2015 int64 IndexDataKey::IndexId() const {
2016 DCHECK_GE(index_id_, 0);
2020 scoped_ptr<IndexedDBKey> IndexDataKey::user_key() const {
2021 scoped_ptr<IndexedDBKey> key;
2022 StringPiece slice(encoded_user_key_);
2023 if (!DecodeIDBKey(&slice, &key)) {
2024 // TODO(jsbell): Return error.
2029 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const {
2030 scoped_ptr<IndexedDBKey> key;
2031 StringPiece slice(encoded_primary_key_);
2032 if (!DecodeIDBKey(&slice, &key)) {
2033 // TODO(jsbell): Return error.
2038 } // namespace content