OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" | 5 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | |
10 | 9 |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
13 #include "base/strings/string_piece.h" | |
14 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
15 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
16 #include "content/browser/indexed_db/leveldb/leveldb_slice.h" | |
17 #include "content/common/indexed_db/indexed_db_key.h" | 14 #include "content/common/indexed_db/indexed_db_key.h" |
18 #include "content/common/indexed_db/indexed_db_key_path.h" | 15 #include "content/common/indexed_db/indexed_db_key_path.h" |
19 #include "third_party/WebKit/public/platform/WebIDBKeyPath.h" | 16 #include "third_party/WebKit/public/platform/WebIDBKeyPath.h" |
20 | 17 |
21 // LevelDB stores key/value pairs. Keys and values are strings of bytes, | 18 // LevelDB stores key/value pairs. Keys and values are strings of bytes, |
22 // normally of type std::vector<char>. | 19 // normally of type std::string. |
23 // | 20 // |
24 // The keys in the backing store are variable-length tuples with different types | 21 // The keys in the backing store are variable-length tuples with different types |
25 // of fields. Each key in the backing store starts with a ternary prefix: | 22 // of fields. Each key in the backing store starts with a ternary prefix: |
26 // (database id, object store id, index id). For each, 0 is reserved for | 23 // (database id, object store id, index id). For each, 0 is reserved for |
27 // meta-data. | 24 // meta-data. |
28 // The prefix makes sure that data for a specific database, object store, and | 25 // The prefix makes sure that data for a specific database, object store, and |
29 // index are grouped together. The locality is important for performance: common | 26 // index are grouped together. The locality is important for performance: common |
30 // operations should only need a minimal number of seek operations. For example, | 27 // operations should only need a minimal number of seek operations. For example, |
31 // all the meta-data for a database is grouped together so that reading that | 28 // all the meta-data for a database is grouped together so that reading that |
32 // meta-data only requires one seek. | 29 // meta-data only requires one seek. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 static const unsigned char kObjectStoreFreeListTypeByte = 150; | 186 static const unsigned char kObjectStoreFreeListTypeByte = 150; |
190 static const unsigned char kIndexFreeListTypeByte = 151; | 187 static const unsigned char kIndexFreeListTypeByte = 151; |
191 static const unsigned char kObjectStoreNamesTypeByte = 200; | 188 static const unsigned char kObjectStoreNamesTypeByte = 200; |
192 static const unsigned char kIndexNamesKeyTypeByte = 201; | 189 static const unsigned char kIndexNamesKeyTypeByte = 201; |
193 | 190 |
194 static const unsigned char kObjectMetaDataTypeMaximum = 255; | 191 static const unsigned char kObjectMetaDataTypeMaximum = 255; |
195 static const unsigned char kIndexMetaDataTypeMaximum = 255; | 192 static const unsigned char kIndexMetaDataTypeMaximum = 255; |
196 | 193 |
197 const unsigned char kMinimumIndexId = 30; | 194 const unsigned char kMinimumIndexId = 30; |
198 | 195 |
199 inline void EncodeIntSafely(int64 nParam, int64 max, std::vector<char>* into) { | 196 inline void EncodeIntSafely(int64 nParam, int64 max, std::string* into) { |
200 DCHECK_LE(nParam, max); | 197 DCHECK_LE(nParam, max); |
201 return EncodeInt(nParam, into); | 198 return EncodeInt(nParam, into); |
202 } | 199 } |
203 | 200 |
204 std::vector<char> MaxIDBKey() { | 201 std::string MaxIDBKey() { |
205 std::vector<char> ret; | 202 std::string ret; |
206 EncodeByte(kIndexedDBKeyNullTypeByte, &ret); | 203 EncodeByte(kIndexedDBKeyNullTypeByte, &ret); |
207 return ret; | 204 return ret; |
208 } | 205 } |
209 | 206 |
210 std::vector<char> MinIDBKey() { | 207 std::string MinIDBKey() { |
211 std::vector<char> ret; | 208 std::string ret; |
212 EncodeByte(kIndexedDBKeyMinKeyTypeByte, &ret); | 209 EncodeByte(kIndexedDBKeyMinKeyTypeByte, &ret); |
213 return ret; | 210 return ret; |
214 } | 211 } |
215 | 212 |
216 void EncodeByte(unsigned char value, std::vector<char>* into) { | 213 void EncodeByte(unsigned char value, std::string* into) { |
217 into->push_back(value); | 214 into->push_back(value); |
218 } | 215 } |
219 | 216 |
220 void EncodeBool(bool value, std::vector<char>* into) { | 217 void EncodeBool(bool value, std::string* into) { |
221 into->push_back(value ? 1 : 0); | 218 into->push_back(value ? 1 : 0); |
222 } | 219 } |
223 | 220 |
224 void EncodeInt(int64 value, std::vector<char>* into) { | 221 void EncodeInt(int64 value, std::string* into) { |
225 #ifndef NDEBUG | 222 #ifndef NDEBUG |
226 // Exercised by unit tests in debug only. | 223 // Exercised by unit tests in debug only. |
227 DCHECK_GE(value, 0); | 224 DCHECK_GE(value, 0); |
228 #endif | 225 #endif |
229 uint64 n = static_cast<uint64>(value); | 226 uint64 n = static_cast<uint64>(value); |
230 | 227 |
231 do { | 228 do { |
232 unsigned char c = n; | 229 unsigned char c = n; |
233 into->push_back(c); | 230 into->push_back(c); |
234 n >>= 8; | 231 n >>= 8; |
235 } while (n); | 232 } while (n); |
236 } | 233 } |
237 | 234 |
238 void EncodeVarInt(int64 value, std::vector<char>* into) { | 235 void EncodeVarInt(int64 value, std::string* into) { |
239 #ifndef NDEBUG | 236 #ifndef NDEBUG |
240 // Exercised by unit tests in debug only. | 237 // Exercised by unit tests in debug only. |
241 DCHECK_GE(value, 0); | 238 DCHECK_GE(value, 0); |
242 #endif | 239 #endif |
243 uint64 n = static_cast<uint64>(value); | 240 uint64 n = static_cast<uint64>(value); |
244 | 241 |
245 do { | 242 do { |
246 unsigned char c = n & 0x7f; | 243 unsigned char c = n & 0x7f; |
247 n >>= 7; | 244 n >>= 7; |
248 if (n) | 245 if (n) |
249 c |= 0x80; | 246 c |= 0x80; |
250 into->push_back(c); | 247 into->push_back(c); |
251 } while (n); | 248 } while (n); |
252 } | 249 } |
253 | 250 |
254 void EncodeString(const string16& value, std::vector<char>* into) { | 251 void EncodeString(const string16& value, std::string* into) { |
255 if (value.empty()) | 252 if (value.empty()) |
256 return; | 253 return; |
257 // Backing store is UTF-16BE, convert from host endianness. | 254 // Backing store is UTF-16BE, convert from host endianness. |
258 size_t length = value.length(); | 255 size_t length = value.length(); |
259 size_t current = into->size(); | 256 size_t current = into->size(); |
260 into->resize(into->size() + length * sizeof(char16)); | 257 into->resize(into->size() + length * sizeof(char16)); |
261 | 258 |
262 const char16* src = value.c_str(); | 259 const char16* src = value.c_str(); |
263 char16* dst = reinterpret_cast<char16*>(&*into->begin() + current); | 260 char16* dst = reinterpret_cast<char16*>(&*into->begin() + current); |
264 for (unsigned i = 0; i < length; ++i) | 261 for (unsigned i = 0; i < length; ++i) |
265 *dst++ = htons(*src++); | 262 *dst++ = htons(*src++); |
266 } | 263 } |
267 | 264 |
268 void EncodeStringWithLength(const string16& value, std::vector<char>* into) { | 265 void EncodeStringWithLength(const string16& value, std::string* into) { |
269 EncodeVarInt(value.length(), into); | 266 EncodeVarInt(value.length(), into); |
270 EncodeString(value, into); | 267 EncodeString(value, into); |
271 } | 268 } |
272 | 269 |
273 void EncodeDouble(double value, std::vector<char>* into) { | 270 void EncodeDouble(double value, std::string* into) { |
274 // This always has host endianness. | 271 // This always has host endianness. |
275 const char* p = reinterpret_cast<char*>(&value); | 272 const char* p = reinterpret_cast<char*>(&value); |
276 into->insert(into->end(), p, p + sizeof(value)); | 273 into->insert(into->end(), p, p + sizeof(value)); |
277 } | 274 } |
278 | 275 |
279 void EncodeIDBKey(const IndexedDBKey& value, std::vector<char>* into) { | 276 void EncodeIDBKey(const IndexedDBKey& value, std::string* into) { |
280 size_t previous_size = into->size(); | 277 size_t previous_size = into->size(); |
281 DCHECK(value.IsValid()); | 278 DCHECK(value.IsValid()); |
282 switch (value.type()) { | 279 switch (value.type()) { |
283 case WebIDBKey::NullType: | 280 case WebIDBKey::NullType: |
284 case WebIDBKey::InvalidType: | 281 case WebIDBKey::InvalidType: |
285 case WebIDBKey::MinType: { | 282 case WebIDBKey::MinType: { |
286 NOTREACHED(); | 283 NOTREACHED(); |
287 EncodeByte(kIndexedDBKeyNullTypeByte, into); | 284 EncodeByte(kIndexedDBKeyNullTypeByte, into); |
288 return; | 285 return; |
289 } | 286 } |
(...skipping 24 matching lines...) Expand all Loading... | |
314 EncodeDouble(value.number(), into); | 311 EncodeDouble(value.number(), into); |
315 DCHECK_EQ(static_cast<size_t>(9), | 312 DCHECK_EQ(static_cast<size_t>(9), |
316 static_cast<size_t>(into->size() - previous_size)); | 313 static_cast<size_t>(into->size() - previous_size)); |
317 return; | 314 return; |
318 } | 315 } |
319 } | 316 } |
320 | 317 |
321 NOTREACHED(); | 318 NOTREACHED(); |
322 } | 319 } |
323 | 320 |
324 void EncodeIDBKeyPath(const IndexedDBKeyPath& value, std::vector<char>* into) { | 321 void EncodeIDBKeyPath(const IndexedDBKeyPath& value, std::string* into) { |
325 // May be typed, or may be a raw string. An invalid leading | 322 // May be typed, or may be a raw string. An invalid leading |
326 // byte is used to identify typed coding. New records are | 323 // byte is used to identify typed coding. New records are |
327 // always written as typed. | 324 // always written as typed. |
328 EncodeByte(kIndexedDBKeyPathTypeCodedByte1, into); | 325 EncodeByte(kIndexedDBKeyPathTypeCodedByte1, into); |
329 EncodeByte(kIndexedDBKeyPathTypeCodedByte2, into); | 326 EncodeByte(kIndexedDBKeyPathTypeCodedByte2, into); |
330 EncodeByte(static_cast<char>(value.type()), into); | 327 EncodeByte(static_cast<char>(value.type()), into); |
331 switch (value.type()) { | 328 switch (value.type()) { |
332 case WebIDBKeyPath::NullType: | 329 case WebIDBKeyPath::NullType: |
333 break; | 330 break; |
334 case WebIDBKeyPath::StringType: { | 331 case WebIDBKeyPath::StringType: { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
586 case kIndexedDBKeyNumberTypeByte: | 583 case kIndexedDBKeyNumberTypeByte: |
587 if (slice->size() < sizeof(double)) | 584 if (slice->size() < sizeof(double)) |
588 return false; | 585 return false; |
589 slice->remove_prefix(sizeof(double)); | 586 slice->remove_prefix(sizeof(double)); |
590 return true; | 587 return true; |
591 } | 588 } |
592 NOTREACHED(); | 589 NOTREACHED(); |
593 return false; | 590 return false; |
594 } | 591 } |
595 | 592 |
596 bool ExtractEncodedIDBKey(StringPiece* slice, std::vector<char>* result) { | 593 bool ExtractEncodedIDBKey(StringPiece* slice, std::string* result) { |
597 const char* start = slice->begin(); | 594 const char* start = slice->begin(); |
598 if (!ExtractEncodedIDBKey(slice)) | 595 if (!ExtractEncodedIDBKey(slice)) |
599 return 0; | 596 return 0; |
600 | 597 |
601 if (result) | 598 if (result) |
602 result->assign(start, slice->begin()); | 599 result->assign(start, slice->begin()); |
603 return true; | 600 return true; |
604 } | 601 } |
605 | 602 |
606 static WebIDBKey::Type KeyTypeByteToKeyType(unsigned char type) { | 603 static WebIDBKey::Type KeyTypeByteToKeyType(unsigned char type) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
717 if (d > e) | 714 if (d > e) |
718 return 1; | 715 return 1; |
719 return 0; | 716 return 0; |
720 } | 717 } |
721 } | 718 } |
722 | 719 |
723 NOTREACHED(); | 720 NOTREACHED(); |
724 return 0; | 721 return 0; |
725 } | 722 } |
726 | 723 |
727 int CompareEncodedIDBKeys(const std::vector<char>& key_a, | 724 int CompareEncodedIDBKeys(const std::string& key_a, |
728 const std::vector<char>& key_b, | 725 const std::string& key_b, |
729 bool* ok) { | 726 bool* ok) { |
730 DCHECK(!key_a.empty()); | 727 DCHECK(!key_a.empty()); |
731 DCHECK(!key_b.empty()); | 728 DCHECK(!key_b.empty()); |
732 | 729 |
733 StringPiece slice_a(&*key_a.begin(), key_a.size()); | 730 StringPiece slice_a(&*key_a.begin(), key_a.size()); |
734 StringPiece slice_b(&*key_b.begin(), key_b.size()); | 731 StringPiece slice_b(&*key_b.begin(), key_b.size()); |
735 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); | 732 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); |
736 } | 733 } |
737 | 734 |
738 namespace { | 735 namespace { |
739 | 736 |
740 template <typename KeyType> | 737 template <typename KeyType> |
741 int Compare(const LevelDBSlice& a, const LevelDBSlice& b, bool, bool* ok) { | 738 int Compare(const StringPiece& a, const StringPiece& b, bool, bool* ok) { |
742 KeyType key_a; | 739 KeyType key_a; |
743 KeyType key_b; | 740 KeyType key_b; |
744 | 741 |
745 const char* ptr_a = KeyType::Decode(a.begin(), a.end(), &key_a); | 742 const char* ptr_a = KeyType::Decode(a.begin(), a.end(), &key_a); |
746 DCHECK(ptr_a); | 743 DCHECK(ptr_a); |
747 if (!ptr_a) { | 744 if (!ptr_a) { |
748 *ok = false; | 745 *ok = false; |
749 return 0; | 746 return 0; |
750 } | 747 } |
751 const char* ptr_b = KeyType::Decode(b.begin(), b.end(), &key_b); | 748 const char* ptr_b = KeyType::Decode(b.begin(), b.end(), &key_b); |
752 DCHECK(ptr_b); | 749 DCHECK(ptr_b); |
753 if (!ptr_b) { | 750 if (!ptr_b) { |
754 *ok = false; | 751 *ok = false; |
755 return 0; | 752 return 0; |
756 } | 753 } |
757 | 754 |
758 *ok = true; | 755 *ok = true; |
759 return key_a.Compare(key_b); | 756 return key_a.Compare(key_b); |
760 } | 757 } |
761 | 758 |
762 template <> | 759 template <> |
763 int Compare<ExistsEntryKey>(const LevelDBSlice& a, | 760 int Compare<ExistsEntryKey>(const StringPiece& a, |
764 const LevelDBSlice& b, | 761 const StringPiece& b, |
765 bool, | 762 bool, |
766 bool* ok) { | 763 bool* ok) { |
767 KeyPrefix prefix_a; | 764 KeyPrefix prefix_a; |
768 KeyPrefix prefix_b; | 765 KeyPrefix prefix_b; |
769 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); | 766 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); |
770 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); | 767 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); |
771 DCHECK(ptr_a); | 768 DCHECK(ptr_a); |
772 DCHECK(ptr_b); | 769 DCHECK(ptr_b); |
773 DCHECK(prefix_a.database_id_); | 770 DCHECK(prefix_a.database_id_); |
774 DCHECK(prefix_a.object_store_id_); | 771 DCHECK(prefix_a.object_store_id_); |
775 DCHECK_EQ(prefix_a.index_id_, ExistsEntryKey::kSpecialIndexNumber); | 772 DCHECK_EQ(prefix_a.index_id_, ExistsEntryKey::kSpecialIndexNumber); |
776 DCHECK(prefix_b.database_id_); | 773 DCHECK(prefix_b.database_id_); |
777 DCHECK(prefix_b.object_store_id_); | 774 DCHECK(prefix_b.object_store_id_); |
778 DCHECK_EQ(prefix_b.index_id_, ExistsEntryKey::kSpecialIndexNumber); | 775 DCHECK_EQ(prefix_b.index_id_, ExistsEntryKey::kSpecialIndexNumber); |
779 DCHECK_NE(ptr_a, a.end()); | 776 DCHECK_NE(ptr_a, a.end()); |
780 DCHECK_NE(ptr_b, b.end()); | 777 DCHECK_NE(ptr_b, b.end()); |
781 // Prefixes are not compared - it is assumed this was already done. | 778 // Prefixes are not compared - it is assumed this was already done. |
782 DCHECK(!prefix_a.Compare(prefix_b)); | 779 DCHECK(!prefix_a.Compare(prefix_b)); |
783 | 780 |
784 StringPiece slice_a(ptr_a, a.end() - ptr_a); | 781 StringPiece slice_a(ptr_a, a.end() - ptr_a); |
785 StringPiece slice_b(ptr_b, b.end() - ptr_b); | 782 StringPiece slice_b(ptr_b, b.end() - ptr_b); |
786 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); | 783 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); |
787 } | 784 } |
788 | 785 |
789 template <> | 786 template <> |
790 int Compare<ObjectStoreDataKey>(const LevelDBSlice& a, | 787 int Compare<ObjectStoreDataKey>(const StringPiece& a, |
791 const LevelDBSlice& b, | 788 const StringPiece& b, |
792 bool, | 789 bool, |
793 bool* ok) { | 790 bool* ok) { |
794 KeyPrefix prefix_a; | 791 KeyPrefix prefix_a; |
795 KeyPrefix prefix_b; | 792 KeyPrefix prefix_b; |
796 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); | 793 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); |
797 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); | 794 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); |
798 DCHECK(ptr_a); | 795 DCHECK(ptr_a); |
799 DCHECK(ptr_b); | 796 DCHECK(ptr_b); |
800 DCHECK(prefix_a.database_id_); | 797 DCHECK(prefix_a.database_id_); |
801 DCHECK(prefix_a.object_store_id_); | 798 DCHECK(prefix_a.object_store_id_); |
802 DCHECK_EQ(prefix_a.index_id_, ObjectStoreDataKey::kSpecialIndexNumber); | 799 DCHECK_EQ(prefix_a.index_id_, ObjectStoreDataKey::kSpecialIndexNumber); |
803 DCHECK(prefix_b.database_id_); | 800 DCHECK(prefix_b.database_id_); |
804 DCHECK(prefix_b.object_store_id_); | 801 DCHECK(prefix_b.object_store_id_); |
805 DCHECK_EQ(prefix_b.index_id_, ObjectStoreDataKey::kSpecialIndexNumber); | 802 DCHECK_EQ(prefix_b.index_id_, ObjectStoreDataKey::kSpecialIndexNumber); |
806 DCHECK_NE(ptr_a, a.end()); | 803 DCHECK_NE(ptr_a, a.end()); |
807 DCHECK_NE(ptr_b, b.end()); | 804 DCHECK_NE(ptr_b, b.end()); |
808 // Prefixes are not compared - it is assumed this was already done. | 805 // Prefixes are not compared - it is assumed this was already done. |
809 DCHECK(!prefix_a.Compare(prefix_b)); | 806 DCHECK(!prefix_a.Compare(prefix_b)); |
810 | 807 |
811 StringPiece slice_a(ptr_a, a.end() - ptr_a); | 808 StringPiece slice_a(ptr_a, a.end() - ptr_a); |
812 StringPiece slice_b(ptr_b, b.end() - ptr_b); | 809 StringPiece slice_b(ptr_b, b.end() - ptr_b); |
813 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); | 810 return CompareEncodedIDBKeys(&slice_a, &slice_b, ok); |
814 } | 811 } |
815 | 812 |
816 template <> | 813 template <> |
817 int Compare<IndexDataKey>(const LevelDBSlice& a, | 814 int Compare<IndexDataKey>(const StringPiece& a, |
818 const LevelDBSlice& b, | 815 const StringPiece& b, |
819 bool ignore_duplicates, | 816 bool ignore_duplicates, |
820 bool* ok) { | 817 bool* ok) { |
821 KeyPrefix prefix_a; | 818 KeyPrefix prefix_a; |
822 KeyPrefix prefix_b; | 819 KeyPrefix prefix_b; |
823 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); | 820 const char* ptr_a = KeyPrefix::Decode(a.begin(), a.end(), &prefix_a); |
824 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); | 821 const char* ptr_b = KeyPrefix::Decode(b.begin(), b.end(), &prefix_b); |
825 DCHECK(ptr_a); | 822 DCHECK(ptr_a); |
826 DCHECK(ptr_b); | 823 DCHECK(ptr_b); |
827 DCHECK(prefix_a.database_id_); | 824 DCHECK(prefix_a.database_id_); |
828 DCHECK(prefix_a.object_store_id_); | 825 DCHECK(prefix_a.object_store_id_); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
865 if (slice_b.empty()) | 862 if (slice_b.empty()) |
866 return 1; | 863 return 1; |
867 | 864 |
868 result = CompareEncodedIDBKeys(&slice_a, &slice_b, ok); | 865 result = CompareEncodedIDBKeys(&slice_a, &slice_b, ok); |
869 if (!*ok || result) | 866 if (!*ok || result) |
870 return result; | 867 return result; |
871 | 868 |
872 return CompareInts(sequence_number_a, sequence_number_b); | 869 return CompareInts(sequence_number_a, sequence_number_b); |
873 } | 870 } |
874 | 871 |
875 int Compare(const LevelDBSlice& a, | 872 int Compare(const StringPiece& a, |
876 const LevelDBSlice& b, | 873 const StringPiece& b, |
877 bool index_keys, | 874 bool index_keys, |
878 bool* ok) { | 875 bool* ok) { |
879 const char* ptr_a = a.begin(); | 876 const char* ptr_a = a.begin(); |
880 const char* ptr_b = b.begin(); | 877 const char* ptr_b = b.begin(); |
881 const char* end_a = a.end(); | 878 const char* end_a = a.end(); |
882 const char* end_b = b.end(); | 879 const char* end_b = b.end(); |
883 | 880 |
884 KeyPrefix prefix_a; | 881 KeyPrefix prefix_a; |
885 KeyPrefix prefix_b; | 882 KeyPrefix prefix_b; |
886 | 883 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
992 break; | 989 break; |
993 } | 990 } |
994 | 991 |
995 NOTREACHED(); | 992 NOTREACHED(); |
996 *ok = false; | 993 *ok = false; |
997 return 0; | 994 return 0; |
998 } | 995 } |
999 | 996 |
1000 } // namespace | 997 } // namespace |
1001 | 998 |
1002 int Compare(const LevelDBSlice& a, const LevelDBSlice& b, bool index_keys) { | 999 int Compare(const StringPiece& a, const StringPiece& b, bool index_keys) { |
1003 bool ok; | 1000 bool ok; |
1004 int result = Compare(a, b, index_keys, &ok); | 1001 int result = Compare(a, b, index_keys, &ok); |
1005 DCHECK(ok); | 1002 DCHECK(ok); |
1006 if (!ok) | 1003 if (!ok) |
1007 return 0; | 1004 return 0; |
1008 return result; | 1005 return result; |
1009 } | 1006 } |
1010 | 1007 |
1011 KeyPrefix::KeyPrefix() | 1008 KeyPrefix::KeyPrefix() |
1012 : database_id_(INVALID_TYPE), | 1009 : database_id_(INVALID_TYPE), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1100 { | 1097 { |
1101 StringPiece slice(start, index_id_bytes); | 1098 StringPiece slice(start, index_id_bytes); |
1102 if (!DecodeInt(&slice, &result->index_id_)) | 1099 if (!DecodeInt(&slice, &result->index_id_)) |
1103 return 0; | 1100 return 0; |
1104 } | 1101 } |
1105 start += index_id_bytes; | 1102 start += index_id_bytes; |
1106 | 1103 |
1107 return start; | 1104 return start; |
1108 } | 1105 } |
1109 | 1106 |
1110 std::vector<char> KeyPrefix::EncodeEmpty() { | 1107 std::string KeyPrefix::EncodeEmpty() { |
1111 const std::vector<char> result(4, 0); | 1108 const std::string result(4, 0); |
1112 DCHECK(EncodeInternal(0, 0, 0) == std::vector<char>(4, 0)); | 1109 DCHECK(EncodeInternal(0, 0, 0) == std::string(4, 0)); |
1113 return result; | 1110 return result; |
1114 } | 1111 } |
1115 | 1112 |
1116 std::vector<char> KeyPrefix::Encode() const { | 1113 std::string KeyPrefix::Encode() const { |
1117 DCHECK(database_id_ != kInvalidId); | 1114 DCHECK(database_id_ != kInvalidId); |
1118 DCHECK(object_store_id_ != kInvalidId); | 1115 DCHECK(object_store_id_ != kInvalidId); |
1119 DCHECK(index_id_ != kInvalidId); | 1116 DCHECK(index_id_ != kInvalidId); |
1120 return EncodeInternal(database_id_, object_store_id_, index_id_); | 1117 return EncodeInternal(database_id_, object_store_id_, index_id_); |
1121 } | 1118 } |
1122 | 1119 |
1123 std::vector<char> KeyPrefix::EncodeInternal(int64 database_id, | 1120 std::string KeyPrefix::EncodeInternal(int64 database_id, |
1124 int64 object_store_id, | 1121 int64 object_store_id, |
1125 int64 index_id) { | 1122 int64 index_id) { |
1126 std::vector<char> database_id_string; | 1123 std::string database_id_string; |
1127 std::vector<char> object_store_id_string; | 1124 std::string object_store_id_string; |
1128 std::vector<char> index_id_string; | 1125 std::string index_id_string; |
1129 | 1126 |
1130 EncodeIntSafely(database_id, kMaxDatabaseId, &database_id_string); | 1127 EncodeIntSafely(database_id, kMaxDatabaseId, &database_id_string); |
1131 EncodeIntSafely(object_store_id, kMaxObjectStoreId, &object_store_id_string); | 1128 EncodeIntSafely(object_store_id, kMaxObjectStoreId, &object_store_id_string); |
1132 EncodeIntSafely(index_id, kMaxIndexId, &index_id_string); | 1129 EncodeIntSafely(index_id, kMaxIndexId, &index_id_string); |
1133 | 1130 |
1134 DCHECK(database_id_string.size() <= kMaxDatabaseIdSizeBytes); | 1131 DCHECK(database_id_string.size() <= kMaxDatabaseIdSizeBytes); |
1135 DCHECK(object_store_id_string.size() <= kMaxObjectStoreIdSizeBytes); | 1132 DCHECK(object_store_id_string.size() <= kMaxObjectStoreIdSizeBytes); |
1136 DCHECK(index_id_string.size() <= kMaxIndexIdSizeBytes); | 1133 DCHECK(index_id_string.size() <= kMaxIndexIdSizeBytes); |
1137 | 1134 |
1138 unsigned char first_byte = | 1135 unsigned char first_byte = |
1139 (database_id_string.size() - 1) | 1136 (database_id_string.size() - 1) |
1140 << (kMaxObjectStoreIdSizeBits + kMaxIndexIdSizeBits) | | 1137 << (kMaxObjectStoreIdSizeBits + kMaxIndexIdSizeBits) | |
1141 (object_store_id_string.size() - 1) << kMaxIndexIdSizeBits | | 1138 (object_store_id_string.size() - 1) << kMaxIndexIdSizeBits | |
1142 (index_id_string.size() - 1); | 1139 (index_id_string.size() - 1); |
1143 COMPILE_ASSERT(kMaxDatabaseIdSizeBits + kMaxObjectStoreIdSizeBits + | 1140 COMPILE_ASSERT(kMaxDatabaseIdSizeBits + kMaxObjectStoreIdSizeBits + |
1144 kMaxIndexIdSizeBits == | 1141 kMaxIndexIdSizeBits == |
1145 sizeof(first_byte) * 8, | 1142 sizeof(first_byte) * 8, |
1146 CANT_ENCODE_IDS); | 1143 CANT_ENCODE_IDS); |
1147 std::vector<char> ret; | 1144 std::string ret; |
1148 ret.reserve(kDefaultInlineBufferSize); | 1145 ret.reserve(kDefaultInlineBufferSize); |
1149 ret.push_back(first_byte); | 1146 ret.push_back(first_byte); |
1150 ret.insert(ret.end(), database_id_string.begin(), database_id_string.end()); | 1147 ret.insert(ret.end(), database_id_string.begin(), database_id_string.end()); |
alecflett
2013/07/09 18:26:30
these inserts can become appends now, no?
jsbell
2013/07/09 19:41:46
Done.
| |
1151 ret.insert( | 1148 ret.insert( |
1152 ret.end(), object_store_id_string.begin(), object_store_id_string.end()); | 1149 ret.end(), object_store_id_string.begin(), object_store_id_string.end()); |
1153 ret.insert(ret.end(), index_id_string.begin(), index_id_string.end()); | 1150 ret.insert(ret.end(), index_id_string.begin(), index_id_string.end()); |
1154 | 1151 |
1155 DCHECK_LE(ret.size(), kDefaultInlineBufferSize); | 1152 DCHECK_LE(ret.size(), kDefaultInlineBufferSize); |
1156 return ret; | 1153 return ret; |
1157 } | 1154 } |
1158 | 1155 |
1159 int KeyPrefix::Compare(const KeyPrefix& other) const { | 1156 int KeyPrefix::Compare(const KeyPrefix& other) const { |
1160 DCHECK(database_id_ != kInvalidId); | 1157 DCHECK(database_id_ != kInvalidId); |
(...skipping 22 matching lines...) Expand all Loading... | |
1183 return OBJECT_STORE_DATA; | 1180 return OBJECT_STORE_DATA; |
1184 if (index_id_ == kExistsEntryIndexId) | 1181 if (index_id_ == kExistsEntryIndexId) |
1185 return EXISTS_ENTRY; | 1182 return EXISTS_ENTRY; |
1186 if (index_id_ >= kMinimumIndexId) | 1183 if (index_id_ >= kMinimumIndexId) |
1187 return INDEX_DATA; | 1184 return INDEX_DATA; |
1188 | 1185 |
1189 NOTREACHED(); | 1186 NOTREACHED(); |
1190 return INVALID_TYPE; | 1187 return INVALID_TYPE; |
1191 } | 1188 } |
1192 | 1189 |
1193 std::vector<char> SchemaVersionKey::Encode() { | 1190 std::string SchemaVersionKey::Encode() { |
1194 std::vector<char> ret = KeyPrefix::EncodeEmpty(); | 1191 std::string ret = KeyPrefix::EncodeEmpty(); |
1195 ret.push_back(kSchemaVersionTypeByte); | 1192 ret.push_back(kSchemaVersionTypeByte); |
1196 return ret; | 1193 return ret; |
1197 } | 1194 } |
1198 | 1195 |
1199 std::vector<char> MaxDatabaseIdKey::Encode() { | 1196 std::string MaxDatabaseIdKey::Encode() { |
1200 std::vector<char> ret = KeyPrefix::EncodeEmpty(); | 1197 std::string ret = KeyPrefix::EncodeEmpty(); |
1201 ret.push_back(kMaxDatabaseIdTypeByte); | 1198 ret.push_back(kMaxDatabaseIdTypeByte); |
1202 return ret; | 1199 return ret; |
1203 } | 1200 } |
1204 | 1201 |
1205 std::vector<char> DataVersionKey::Encode() { | 1202 std::string DataVersionKey::Encode() { |
1206 std::vector<char> ret = KeyPrefix::EncodeEmpty(); | 1203 std::string ret = KeyPrefix::EncodeEmpty(); |
1207 ret.push_back(kDataVersionTypeByte); | 1204 ret.push_back(kDataVersionTypeByte); |
1208 return ret; | 1205 return ret; |
1209 } | 1206 } |
1210 | 1207 |
1211 DatabaseFreeListKey::DatabaseFreeListKey() : database_id_(-1) {} | 1208 DatabaseFreeListKey::DatabaseFreeListKey() : database_id_(-1) {} |
1212 | 1209 |
1213 const char* DatabaseFreeListKey::Decode(const char* start, | 1210 const char* DatabaseFreeListKey::Decode(const char* start, |
1214 const char* limit, | 1211 const char* limit, |
1215 DatabaseFreeListKey* result) { | 1212 DatabaseFreeListKey* result) { |
1216 KeyPrefix prefix; | 1213 KeyPrefix prefix; |
(...skipping 10 matching lines...) Expand all Loading... | |
1227 if (!DecodeByte(&slice, &type_byte)) | 1224 if (!DecodeByte(&slice, &type_byte)) |
1228 return 0; | 1225 return 0; |
1229 DCHECK_EQ(type_byte, kDatabaseFreeListTypeByte); | 1226 DCHECK_EQ(type_byte, kDatabaseFreeListTypeByte); |
1230 if (slice.empty()) | 1227 if (slice.empty()) |
1231 return 0; | 1228 return 0; |
1232 if (!DecodeVarInt(&slice, &result->database_id_)) | 1229 if (!DecodeVarInt(&slice, &result->database_id_)) |
1233 return 0; | 1230 return 0; |
1234 return slice.begin(); | 1231 return slice.begin(); |
1235 } | 1232 } |
1236 | 1233 |
1237 std::vector<char> DatabaseFreeListKey::Encode(int64 database_id) { | 1234 std::string DatabaseFreeListKey::Encode(int64 database_id) { |
1238 std::vector<char> ret = KeyPrefix::EncodeEmpty(); | 1235 std::string ret = KeyPrefix::EncodeEmpty(); |
1239 ret.push_back(kDatabaseFreeListTypeByte); | 1236 ret.push_back(kDatabaseFreeListTypeByte); |
1240 EncodeVarInt(database_id, &ret); | 1237 EncodeVarInt(database_id, &ret); |
1241 return ret; | 1238 return ret; |
1242 } | 1239 } |
1243 | 1240 |
1244 std::vector<char> DatabaseFreeListKey::EncodeMaxKey() { | 1241 std::string DatabaseFreeListKey::EncodeMaxKey() { |
1245 return Encode(std::numeric_limits<int64>::max()); | 1242 return Encode(std::numeric_limits<int64>::max()); |
1246 } | 1243 } |
1247 | 1244 |
1248 int64 DatabaseFreeListKey::DatabaseId() const { | 1245 int64 DatabaseFreeListKey::DatabaseId() const { |
1249 DCHECK_GE(database_id_, 0); | 1246 DCHECK_GE(database_id_, 0); |
1250 return database_id_; | 1247 return database_id_; |
1251 } | 1248 } |
1252 | 1249 |
1253 int DatabaseFreeListKey::Compare(const DatabaseFreeListKey& other) const { | 1250 int DatabaseFreeListKey::Compare(const DatabaseFreeListKey& other) const { |
1254 DCHECK_GE(database_id_, 0); | 1251 DCHECK_GE(database_id_, 0); |
(...skipping 17 matching lines...) Expand all Loading... | |
1272 if (!DecodeByte(&slice, &type_byte)) | 1269 if (!DecodeByte(&slice, &type_byte)) |
1273 return 0; | 1270 return 0; |
1274 DCHECK_EQ(type_byte, kDatabaseNameTypeByte); | 1271 DCHECK_EQ(type_byte, kDatabaseNameTypeByte); |
1275 if (!DecodeStringWithLength(&slice, &result->origin_)) | 1272 if (!DecodeStringWithLength(&slice, &result->origin_)) |
1276 return 0; | 1273 return 0; |
1277 if (!DecodeStringWithLength(&slice, &result->database_name_)) | 1274 if (!DecodeStringWithLength(&slice, &result->database_name_)) |
1278 return 0; | 1275 return 0; |
1279 return slice.begin(); | 1276 return slice.begin(); |
1280 } | 1277 } |
1281 | 1278 |
1282 std::vector<char> DatabaseNameKey::Encode(const std::string& origin_identifier, | 1279 std::string DatabaseNameKey::Encode(const std::string& origin_identifier, |
1283 const string16& database_name) { | 1280 const string16& database_name) { |
1284 std::vector<char> ret = KeyPrefix::EncodeEmpty(); | 1281 std::string ret = KeyPrefix::EncodeEmpty(); |
1285 ret.push_back(kDatabaseNameTypeByte); | 1282 ret.push_back(kDatabaseNameTypeByte); |
1286 EncodeStringWithLength(base::ASCIIToUTF16(origin_identifier), &ret); | 1283 EncodeStringWithLength(base::ASCIIToUTF16(origin_identifier), &ret); |
1287 EncodeStringWithLength(database_name, &ret); | 1284 EncodeStringWithLength(database_name, &ret); |
1288 return ret; | 1285 return ret; |
1289 } | 1286 } |
1290 | 1287 |
1291 std::vector<char> DatabaseNameKey::EncodeMinKeyForOrigin( | 1288 std::string DatabaseNameKey::EncodeMinKeyForOrigin( |
1292 const std::string& origin_identifier) { | 1289 const std::string& origin_identifier) { |
1293 return Encode(origin_identifier, string16()); | 1290 return Encode(origin_identifier, string16()); |
1294 } | 1291 } |
1295 | 1292 |
1296 std::vector<char> DatabaseNameKey::EncodeStopKeyForOrigin( | 1293 std::string DatabaseNameKey::EncodeStopKeyForOrigin( |
1297 const std::string& origin_identifier) { | 1294 const std::string& origin_identifier) { |
1298 // just after origin in collation order | 1295 // just after origin in collation order |
1299 return EncodeMinKeyForOrigin(origin_identifier + '\x01'); | 1296 return EncodeMinKeyForOrigin(origin_identifier + '\x01'); |
1300 } | 1297 } |
1301 | 1298 |
1302 int DatabaseNameKey::Compare(const DatabaseNameKey& other) { | 1299 int DatabaseNameKey::Compare(const DatabaseNameKey& other) { |
1303 if (int x = origin_.compare(other.origin_)) | 1300 if (int x = origin_.compare(other.origin_)) |
1304 return x; | 1301 return x; |
1305 return database_name_.compare(other.database_name_); | 1302 return database_name_.compare(other.database_name_); |
1306 } | 1303 } |
1307 | 1304 |
1308 std::vector<char> DatabaseMetaDataKey::Encode(int64 database_id, | 1305 std::string DatabaseMetaDataKey::Encode(int64 database_id, |
1309 MetaDataType meta_data_type) { | 1306 MetaDataType meta_data_type) { |
1310 KeyPrefix prefix(database_id); | 1307 KeyPrefix prefix(database_id); |
1311 std::vector<char> ret = prefix.Encode(); | 1308 std::string ret = prefix.Encode(); |
1312 ret.push_back(meta_data_type); | 1309 ret.push_back(meta_data_type); |
1313 return ret; | 1310 return ret; |
1314 } | 1311 } |
1315 | 1312 |
1316 ObjectStoreMetaDataKey::ObjectStoreMetaDataKey() | 1313 ObjectStoreMetaDataKey::ObjectStoreMetaDataKey() |
1317 : object_store_id_(-1), meta_data_type_(-1) {} | 1314 : object_store_id_(-1), meta_data_type_(-1) {} |
1318 | 1315 |
1319 const char* ObjectStoreMetaDataKey::Decode(const char* start, | 1316 const char* ObjectStoreMetaDataKey::Decode(const char* start, |
1320 const char* limit, | 1317 const char* limit, |
1321 ObjectStoreMetaDataKey* result) { | 1318 ObjectStoreMetaDataKey* result) { |
(...skipping 12 matching lines...) Expand all Loading... | |
1334 return 0; | 1331 return 0; |
1335 DCHECK_EQ(type_byte, kObjectStoreMetaDataTypeByte); | 1332 DCHECK_EQ(type_byte, kObjectStoreMetaDataTypeByte); |
1336 if (!DecodeVarInt(&slice, &result->object_store_id_)) | 1333 if (!DecodeVarInt(&slice, &result->object_store_id_)) |
1337 return 0; | 1334 return 0; |
1338 DCHECK(result->object_store_id_); | 1335 DCHECK(result->object_store_id_); |
1339 if (!DecodeByte(&slice, &result->meta_data_type_)) | 1336 if (!DecodeByte(&slice, &result->meta_data_type_)) |
1340 return 0; | 1337 return 0; |
1341 return slice.begin(); | 1338 return slice.begin(); |
1342 } | 1339 } |
1343 | 1340 |
1344 std::vector<char> ObjectStoreMetaDataKey::Encode(int64 database_id, | 1341 std::string ObjectStoreMetaDataKey::Encode(int64 database_id, |
1345 int64 object_store_id, | 1342 int64 object_store_id, |
1346 unsigned char meta_data_type) { | 1343 unsigned char meta_data_type) { |
1347 KeyPrefix prefix(database_id); | 1344 KeyPrefix prefix(database_id); |
1348 std::vector<char> ret = prefix.Encode(); | 1345 std::string ret = prefix.Encode(); |
1349 ret.push_back(kObjectStoreMetaDataTypeByte); | 1346 ret.push_back(kObjectStoreMetaDataTypeByte); |
1350 EncodeVarInt(object_store_id, &ret); | 1347 EncodeVarInt(object_store_id, &ret); |
1351 ret.push_back(meta_data_type); | 1348 ret.push_back(meta_data_type); |
1352 return ret; | 1349 return ret; |
1353 } | 1350 } |
1354 | 1351 |
1355 std::vector<char> ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id) { | 1352 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id) { |
1356 return Encode(database_id, | 1353 return Encode(database_id, |
1357 std::numeric_limits<int64>::max(), | 1354 std::numeric_limits<int64>::max(), |
1358 kObjectMetaDataTypeMaximum); | 1355 kObjectMetaDataTypeMaximum); |
1359 } | 1356 } |
1360 | 1357 |
1361 std::vector<char> ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id, | 1358 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id, |
1362 int64 object_store_id) { | 1359 int64 object_store_id) { |
1363 return Encode(database_id, object_store_id, kObjectMetaDataTypeMaximum); | 1360 return Encode(database_id, object_store_id, kObjectMetaDataTypeMaximum); |
1364 } | 1361 } |
1365 | 1362 |
1366 int64 ObjectStoreMetaDataKey::ObjectStoreId() const { | 1363 int64 ObjectStoreMetaDataKey::ObjectStoreId() const { |
1367 DCHECK_GE(object_store_id_, 0); | 1364 DCHECK_GE(object_store_id_, 0); |
1368 return object_store_id_; | 1365 return object_store_id_; |
1369 } | 1366 } |
1370 unsigned char ObjectStoreMetaDataKey::MetaDataType() const { | 1367 unsigned char ObjectStoreMetaDataKey::MetaDataType() const { |
1371 return meta_data_type_; | 1368 return meta_data_type_; |
1372 } | 1369 } |
(...skipping 30 matching lines...) Expand all Loading... | |
1403 DCHECK_EQ(type_byte, kIndexMetaDataTypeByte); | 1400 DCHECK_EQ(type_byte, kIndexMetaDataTypeByte); |
1404 if (!DecodeVarInt(&slice, &result->object_store_id_)) | 1401 if (!DecodeVarInt(&slice, &result->object_store_id_)) |
1405 return 0; | 1402 return 0; |
1406 if (!DecodeVarInt(&slice, &result->index_id_)) | 1403 if (!DecodeVarInt(&slice, &result->index_id_)) |
1407 return 0; | 1404 return 0; |
1408 if (!DecodeByte(&slice, &result->meta_data_type_)) | 1405 if (!DecodeByte(&slice, &result->meta_data_type_)) |
1409 return 0; | 1406 return 0; |
1410 return slice.begin(); | 1407 return slice.begin(); |
1411 } | 1408 } |
1412 | 1409 |
1413 std::vector<char> IndexMetaDataKey::Encode(int64 database_id, | 1410 std::string IndexMetaDataKey::Encode(int64 database_id, |
1414 int64 object_store_id, | 1411 int64 object_store_id, |
1415 int64 index_id, | 1412 int64 index_id, |
1416 unsigned char meta_data_type) { | 1413 unsigned char meta_data_type) { |
1417 KeyPrefix prefix(database_id); | 1414 KeyPrefix prefix(database_id); |
1418 std::vector<char> ret = prefix.Encode(); | 1415 std::string ret = prefix.Encode(); |
1419 ret.push_back(kIndexMetaDataTypeByte); | 1416 ret.push_back(kIndexMetaDataTypeByte); |
1420 EncodeVarInt(object_store_id, &ret); | 1417 EncodeVarInt(object_store_id, &ret); |
1421 EncodeVarInt(index_id, &ret); | 1418 EncodeVarInt(index_id, &ret); |
1422 EncodeByte(meta_data_type, &ret); | 1419 EncodeByte(meta_data_type, &ret); |
1423 return ret; | 1420 return ret; |
1424 } | 1421 } |
1425 | 1422 |
1426 std::vector<char> IndexMetaDataKey::EncodeMaxKey(int64 database_id, | 1423 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id, |
1427 int64 object_store_id) { | 1424 int64 object_store_id) { |
1428 return Encode(database_id, | 1425 return Encode(database_id, |
1429 object_store_id, | 1426 object_store_id, |
1430 std::numeric_limits<int64>::max(), | 1427 std::numeric_limits<int64>::max(), |
1431 kIndexMetaDataTypeMaximum); | 1428 kIndexMetaDataTypeMaximum); |
1432 } | 1429 } |
1433 | 1430 |
1434 std::vector<char> IndexMetaDataKey::EncodeMaxKey(int64 database_id, | 1431 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id, |
1435 int64 object_store_id, | 1432 int64 object_store_id, |
1436 int64 index_id) { | 1433 int64 index_id) { |
1437 return Encode( | 1434 return Encode( |
1438 database_id, object_store_id, index_id, kIndexMetaDataTypeMaximum); | 1435 database_id, object_store_id, index_id, kIndexMetaDataTypeMaximum); |
1439 } | 1436 } |
1440 | 1437 |
1441 int IndexMetaDataKey::Compare(const IndexMetaDataKey& other) { | 1438 int IndexMetaDataKey::Compare(const IndexMetaDataKey& other) { |
1442 DCHECK_GE(object_store_id_, 0); | 1439 DCHECK_GE(object_store_id_, 0); |
1443 DCHECK_GE(index_id_, 0); | 1440 DCHECK_GE(index_id_, 0); |
1444 | 1441 |
1445 if (int x = CompareInts(object_store_id_, other.object_store_id_)) | 1442 if (int x = CompareInts(object_store_id_, other.object_store_id_)) |
1446 return x; | 1443 return x; |
(...skipping 24 matching lines...) Expand all Loading... | |
1471 unsigned char type_byte = 0; | 1468 unsigned char type_byte = 0; |
1472 StringPiece slice(p, limit - p); | 1469 StringPiece slice(p, limit - p); |
1473 if (!DecodeByte(&slice, &type_byte)) | 1470 if (!DecodeByte(&slice, &type_byte)) |
1474 return 0; | 1471 return 0; |
1475 DCHECK_EQ(type_byte, kObjectStoreFreeListTypeByte); | 1472 DCHECK_EQ(type_byte, kObjectStoreFreeListTypeByte); |
1476 if (!DecodeVarInt(&slice, &result->object_store_id_)) | 1473 if (!DecodeVarInt(&slice, &result->object_store_id_)) |
1477 return 0; | 1474 return 0; |
1478 return slice.begin(); | 1475 return slice.begin(); |
1479 } | 1476 } |
1480 | 1477 |
1481 std::vector<char> ObjectStoreFreeListKey::Encode(int64 database_id, | 1478 std::string ObjectStoreFreeListKey::Encode(int64 database_id, |
1482 int64 object_store_id) { | 1479 int64 object_store_id) { |
1483 KeyPrefix prefix(database_id); | 1480 KeyPrefix prefix(database_id); |
1484 std::vector<char> ret = prefix.Encode(); | 1481 std::string ret = prefix.Encode(); |
1485 ret.push_back(kObjectStoreFreeListTypeByte); | 1482 ret.push_back(kObjectStoreFreeListTypeByte); |
1486 EncodeVarInt(object_store_id, &ret); | 1483 EncodeVarInt(object_store_id, &ret); |
1487 return ret; | 1484 return ret; |
1488 } | 1485 } |
1489 | 1486 |
1490 std::vector<char> ObjectStoreFreeListKey::EncodeMaxKey(int64 database_id) { | 1487 std::string ObjectStoreFreeListKey::EncodeMaxKey(int64 database_id) { |
1491 return Encode(database_id, std::numeric_limits<int64>::max()); | 1488 return Encode(database_id, std::numeric_limits<int64>::max()); |
1492 } | 1489 } |
1493 | 1490 |
1494 int64 ObjectStoreFreeListKey::ObjectStoreId() const { | 1491 int64 ObjectStoreFreeListKey::ObjectStoreId() const { |
1495 DCHECK_GE(object_store_id_, 0); | 1492 DCHECK_GE(object_store_id_, 0); |
1496 return object_store_id_; | 1493 return object_store_id_; |
1497 } | 1494 } |
1498 | 1495 |
1499 int ObjectStoreFreeListKey::Compare(const ObjectStoreFreeListKey& other) { | 1496 int ObjectStoreFreeListKey::Compare(const ObjectStoreFreeListKey& other) { |
1500 // TODO(jsbell): It may seem strange that we're not comparing database id's, | 1497 // TODO(jsbell): It may seem strange that we're not comparing database id's, |
(...skipping 22 matching lines...) Expand all Loading... | |
1523 if (!DecodeByte(&slice, &type_byte)) | 1520 if (!DecodeByte(&slice, &type_byte)) |
1524 return 0; | 1521 return 0; |
1525 DCHECK_EQ(type_byte, kIndexFreeListTypeByte); | 1522 DCHECK_EQ(type_byte, kIndexFreeListTypeByte); |
1526 if (!DecodeVarInt(&slice, &result->object_store_id_)) | 1523 if (!DecodeVarInt(&slice, &result->object_store_id_)) |
1527 return 0; | 1524 return 0; |
1528 if (!DecodeVarInt(&slice, &result->index_id_)) | 1525 if (!DecodeVarInt(&slice, &result->index_id_)) |
1529 return 0; | 1526 return 0; |
1530 return slice.begin(); | 1527 return slice.begin(); |
1531 } | 1528 } |
1532 | 1529 |
1533 std::vector<char> IndexFreeListKey::Encode(int64 database_id, | 1530 std::string IndexFreeListKey::Encode(int64 database_id, |
1534 int64 object_store_id, | 1531 int64 object_store_id, |
1535 int64 index_id) { | 1532 int64 index_id) { |
1536 KeyPrefix prefix(database_id); | 1533 KeyPrefix prefix(database_id); |
1537 std::vector<char> ret = prefix.Encode(); | 1534 std::string ret = prefix.Encode(); |
1538 ret.push_back(kIndexFreeListTypeByte); | 1535 ret.push_back(kIndexFreeListTypeByte); |
1539 EncodeVarInt(object_store_id, &ret); | 1536 EncodeVarInt(object_store_id, &ret); |
1540 EncodeVarInt(index_id, &ret); | 1537 EncodeVarInt(index_id, &ret); |
1541 return ret; | 1538 return ret; |
1542 } | 1539 } |
1543 | 1540 |
1544 std::vector<char> IndexFreeListKey::EncodeMaxKey(int64 database_id, | 1541 std::string IndexFreeListKey::EncodeMaxKey(int64 database_id, |
1545 int64 object_store_id) { | 1542 int64 object_store_id) { |
1546 return Encode( | 1543 return Encode( |
1547 database_id, object_store_id, std::numeric_limits<int64>::max()); | 1544 database_id, object_store_id, std::numeric_limits<int64>::max()); |
1548 } | 1545 } |
1549 | 1546 |
1550 int IndexFreeListKey::Compare(const IndexFreeListKey& other) { | 1547 int IndexFreeListKey::Compare(const IndexFreeListKey& other) { |
1551 DCHECK_GE(object_store_id_, 0); | 1548 DCHECK_GE(object_store_id_, 0); |
1552 DCHECK_GE(index_id_, 0); | 1549 DCHECK_GE(index_id_, 0); |
1553 if (int x = CompareInts(object_store_id_, other.object_store_id_)) | 1550 if (int x = CompareInts(object_store_id_, other.object_store_id_)) |
1554 return x; | 1551 return x; |
1555 return CompareInts(index_id_, other.index_id_); | 1552 return CompareInts(index_id_, other.index_id_); |
(...skipping 27 matching lines...) Expand all Loading... | |
1583 unsigned char type_byte = 0; | 1580 unsigned char type_byte = 0; |
1584 StringPiece slice(p, limit - p); | 1581 StringPiece slice(p, limit - p); |
1585 if (!DecodeByte(&slice, &type_byte)) | 1582 if (!DecodeByte(&slice, &type_byte)) |
1586 return 0; | 1583 return 0; |
1587 DCHECK_EQ(type_byte, kObjectStoreNamesTypeByte); | 1584 DCHECK_EQ(type_byte, kObjectStoreNamesTypeByte); |
1588 if (!DecodeStringWithLength(&slice, &result->object_store_name_)) | 1585 if (!DecodeStringWithLength(&slice, &result->object_store_name_)) |
1589 return 0; | 1586 return 0; |
1590 return slice.begin(); | 1587 return slice.begin(); |
1591 } | 1588 } |
1592 | 1589 |
1593 std::vector<char> ObjectStoreNamesKey::Encode( | 1590 std::string ObjectStoreNamesKey::Encode(int64 database_id, |
1594 int64 database_id, | 1591 const string16& object_store_name) { |
1595 const string16& object_store_name) { | |
1596 KeyPrefix prefix(database_id); | 1592 KeyPrefix prefix(database_id); |
1597 std::vector<char> ret = prefix.Encode(); | 1593 std::string ret = prefix.Encode(); |
1598 ret.push_back(kObjectStoreNamesTypeByte); | 1594 ret.push_back(kObjectStoreNamesTypeByte); |
1599 EncodeStringWithLength(object_store_name, &ret); | 1595 EncodeStringWithLength(object_store_name, &ret); |
1600 return ret; | 1596 return ret; |
1601 } | 1597 } |
1602 | 1598 |
1603 int ObjectStoreNamesKey::Compare(const ObjectStoreNamesKey& other) { | 1599 int ObjectStoreNamesKey::Compare(const ObjectStoreNamesKey& other) { |
1604 return object_store_name_.compare(other.object_store_name_); | 1600 return object_store_name_.compare(other.object_store_name_); |
1605 } | 1601 } |
1606 | 1602 |
1607 IndexNamesKey::IndexNamesKey() : object_store_id_(-1) {} | 1603 IndexNamesKey::IndexNamesKey() : object_store_id_(-1) {} |
(...skipping 17 matching lines...) Expand all Loading... | |
1625 if (!DecodeByte(&slice, &type_byte)) | 1621 if (!DecodeByte(&slice, &type_byte)) |
1626 return 0; | 1622 return 0; |
1627 DCHECK_EQ(type_byte, kIndexNamesKeyTypeByte); | 1623 DCHECK_EQ(type_byte, kIndexNamesKeyTypeByte); |
1628 if (!DecodeVarInt(&slice, &result->object_store_id_)) | 1624 if (!DecodeVarInt(&slice, &result->object_store_id_)) |
1629 return 0; | 1625 return 0; |
1630 if (!DecodeStringWithLength(&slice, &result->index_name_)) | 1626 if (!DecodeStringWithLength(&slice, &result->index_name_)) |
1631 return 0; | 1627 return 0; |
1632 return slice.begin(); | 1628 return slice.begin(); |
1633 } | 1629 } |
1634 | 1630 |
1635 std::vector<char> IndexNamesKey::Encode(int64 database_id, | 1631 std::string IndexNamesKey::Encode(int64 database_id, |
1636 int64 object_store_id, | 1632 int64 object_store_id, |
1637 const string16& index_name) { | 1633 const string16& index_name) { |
1638 KeyPrefix prefix(database_id); | 1634 KeyPrefix prefix(database_id); |
1639 std::vector<char> ret = prefix.Encode(); | 1635 std::string ret = prefix.Encode(); |
1640 ret.push_back(kIndexNamesKeyTypeByte); | 1636 ret.push_back(kIndexNamesKeyTypeByte); |
1641 EncodeVarInt(object_store_id, &ret); | 1637 EncodeVarInt(object_store_id, &ret); |
1642 EncodeStringWithLength(index_name, &ret); | 1638 EncodeStringWithLength(index_name, &ret); |
1643 return ret; | 1639 return ret; |
1644 } | 1640 } |
1645 | 1641 |
1646 int IndexNamesKey::Compare(const IndexNamesKey& other) { | 1642 int IndexNamesKey::Compare(const IndexNamesKey& other) { |
1647 DCHECK_GE(object_store_id_, 0); | 1643 DCHECK_GE(object_store_id_, 0); |
1648 if (int x = CompareInts(object_store_id_, other.object_store_id_)) | 1644 if (int x = CompareInts(object_store_id_, other.object_store_id_)) |
1649 return x; | 1645 return x; |
(...skipping 14 matching lines...) Expand all Loading... | |
1664 DCHECK(prefix.object_store_id_); | 1660 DCHECK(prefix.object_store_id_); |
1665 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); | 1661 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); |
1666 if (p == end) | 1662 if (p == end) |
1667 return 0; | 1663 return 0; |
1668 StringPiece slice(p, end - p); | 1664 StringPiece slice(p, end - p); |
1669 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) | 1665 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) |
1670 return 0; | 1666 return 0; |
1671 return slice.begin(); | 1667 return slice.begin(); |
1672 } | 1668 } |
1673 | 1669 |
1674 std::vector<char> ObjectStoreDataKey::Encode( | 1670 std::string ObjectStoreDataKey::Encode(int64 database_id, |
1675 int64 database_id, | 1671 int64 object_store_id, |
1676 int64 object_store_id, | 1672 const std::string encoded_user_key) { |
1677 const std::vector<char> encoded_user_key) { | |
1678 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( | 1673 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( |
1679 database_id, object_store_id, kSpecialIndexNumber)); | 1674 database_id, object_store_id, kSpecialIndexNumber)); |
1680 std::vector<char> ret = prefix.Encode(); | 1675 std::string ret = prefix.Encode(); |
1681 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); | 1676 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); |
1682 | 1677 |
1683 return ret; | 1678 return ret; |
1684 } | 1679 } |
1685 | 1680 |
1686 std::vector<char> ObjectStoreDataKey::Encode(int64 database_id, | 1681 std::string ObjectStoreDataKey::Encode(int64 database_id, |
1687 int64 object_store_id, | 1682 int64 object_store_id, |
1688 const IndexedDBKey& user_key) { | 1683 const IndexedDBKey& user_key) { |
1689 std::vector<char> encoded_key; | 1684 std::string encoded_key; |
1690 EncodeIDBKey(user_key, &encoded_key); | 1685 EncodeIDBKey(user_key, &encoded_key); |
1691 return Encode(database_id, object_store_id, encoded_key); | 1686 return Encode(database_id, object_store_id, encoded_key); |
1692 } | 1687 } |
1693 | 1688 |
1694 int ObjectStoreDataKey::Compare(const ObjectStoreDataKey& other, bool* ok) { | 1689 int ObjectStoreDataKey::Compare(const ObjectStoreDataKey& other, bool* ok) { |
1695 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); | 1690 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); |
1696 } | 1691 } |
1697 | 1692 |
1698 scoped_ptr<IndexedDBKey> ObjectStoreDataKey::user_key() const { | 1693 scoped_ptr<IndexedDBKey> ObjectStoreDataKey::user_key() const { |
1699 scoped_ptr<IndexedDBKey> key; | 1694 scoped_ptr<IndexedDBKey> key; |
(...skipping 20 matching lines...) Expand all Loading... | |
1720 DCHECK(prefix.object_store_id_); | 1715 DCHECK(prefix.object_store_id_); |
1721 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); | 1716 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); |
1722 if (p == end) | 1717 if (p == end) |
1723 return 0; | 1718 return 0; |
1724 StringPiece slice(p, end - p); | 1719 StringPiece slice(p, end - p); |
1725 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) | 1720 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) |
1726 return 0; | 1721 return 0; |
1727 return slice.begin(); | 1722 return slice.begin(); |
1728 } | 1723 } |
1729 | 1724 |
1730 std::vector<char> ExistsEntryKey::Encode(int64 database_id, | 1725 std::string ExistsEntryKey::Encode(int64 database_id, |
1731 int64 object_store_id, | 1726 int64 object_store_id, |
1732 const std::vector<char>& encoded_key) { | 1727 const std::string& encoded_key) { |
1733 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( | 1728 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( |
1734 database_id, object_store_id, kSpecialIndexNumber)); | 1729 database_id, object_store_id, kSpecialIndexNumber)); |
1735 std::vector<char> ret = prefix.Encode(); | 1730 std::string ret = prefix.Encode(); |
1736 ret.insert(ret.end(), encoded_key.begin(), encoded_key.end()); | 1731 ret.insert(ret.end(), encoded_key.begin(), encoded_key.end()); |
1737 return ret; | 1732 return ret; |
1738 } | 1733 } |
1739 | 1734 |
1740 std::vector<char> ExistsEntryKey::Encode(int64 database_id, | 1735 std::string ExistsEntryKey::Encode(int64 database_id, |
1741 int64 object_store_id, | 1736 int64 object_store_id, |
1742 const IndexedDBKey& user_key) { | 1737 const IndexedDBKey& user_key) { |
1743 std::vector<char> encoded_key; | 1738 std::string encoded_key; |
1744 EncodeIDBKey(user_key, &encoded_key); | 1739 EncodeIDBKey(user_key, &encoded_key); |
1745 return Encode(database_id, object_store_id, encoded_key); | 1740 return Encode(database_id, object_store_id, encoded_key); |
1746 } | 1741 } |
1747 | 1742 |
1748 int ExistsEntryKey::Compare(const ExistsEntryKey& other, bool* ok) { | 1743 int ExistsEntryKey::Compare(const ExistsEntryKey& other, bool* ok) { |
1749 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); | 1744 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); |
1750 } | 1745 } |
1751 | 1746 |
1752 scoped_ptr<IndexedDBKey> ExistsEntryKey::user_key() const { | 1747 scoped_ptr<IndexedDBKey> ExistsEntryKey::user_key() const { |
1753 scoped_ptr<IndexedDBKey> key; | 1748 scoped_ptr<IndexedDBKey> key; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1796 | 1791 |
1797 // [optional] primary key | 1792 // [optional] primary key |
1798 if (slice.empty()) | 1793 if (slice.empty()) |
1799 return slice.begin(); | 1794 return slice.begin(); |
1800 if (!ExtractEncodedIDBKey(&slice, &result->encoded_primary_key_)) | 1795 if (!ExtractEncodedIDBKey(&slice, &result->encoded_primary_key_)) |
1801 return 0; | 1796 return 0; |
1802 | 1797 |
1803 return slice.begin(); | 1798 return slice.begin(); |
1804 } | 1799 } |
1805 | 1800 |
1806 std::vector<char> IndexDataKey::Encode( | 1801 std::string IndexDataKey::Encode(int64 database_id, |
1807 int64 database_id, | 1802 int64 object_store_id, |
1808 int64 object_store_id, | 1803 int64 index_id, |
1809 int64 index_id, | 1804 const std::string& encoded_user_key, |
1810 const std::vector<char>& encoded_user_key, | 1805 const std::string& encoded_primary_key, |
1811 const std::vector<char>& encoded_primary_key, | 1806 int64 sequence_number) { |
1812 int64 sequence_number) { | |
1813 KeyPrefix prefix(database_id, object_store_id, index_id); | 1807 KeyPrefix prefix(database_id, object_store_id, index_id); |
1814 std::vector<char> ret = prefix.Encode(); | 1808 std::string ret = prefix.Encode(); |
1815 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); | 1809 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); |
1816 EncodeVarInt(sequence_number, &ret); | 1810 EncodeVarInt(sequence_number, &ret); |
1817 ret.insert(ret.end(), encoded_primary_key.begin(), encoded_primary_key.end()); | 1811 ret.insert(ret.end(), encoded_primary_key.begin(), encoded_primary_key.end()); |
1818 return ret; | 1812 return ret; |
1819 } | 1813 } |
1820 | 1814 |
1821 std::vector<char> IndexDataKey::Encode(int64 database_id, | 1815 std::string IndexDataKey::Encode(int64 database_id, |
1822 int64 object_store_id, | 1816 int64 object_store_id, |
1823 int64 index_id, | 1817 int64 index_id, |
1824 const IndexedDBKey& user_key) { | 1818 const IndexedDBKey& user_key) { |
1825 std::vector<char> encoded_key; | 1819 std::string encoded_key; |
1826 EncodeIDBKey(user_key, &encoded_key); | 1820 EncodeIDBKey(user_key, &encoded_key); |
1827 return Encode( | 1821 return Encode( |
1828 database_id, object_store_id, index_id, encoded_key, MinIDBKey(), 0); | 1822 database_id, object_store_id, index_id, encoded_key, MinIDBKey(), 0); |
1829 } | 1823 } |
1830 | 1824 |
1831 std::vector<char> IndexDataKey::EncodeMinKey(int64 database_id, | 1825 std::string IndexDataKey::EncodeMinKey(int64 database_id, |
1832 int64 object_store_id, | 1826 int64 object_store_id, |
1833 int64 index_id) { | 1827 int64 index_id) { |
1834 return Encode( | 1828 return Encode( |
1835 database_id, object_store_id, index_id, MinIDBKey(), MinIDBKey(), 0); | 1829 database_id, object_store_id, index_id, MinIDBKey(), MinIDBKey(), 0); |
1836 } | 1830 } |
1837 | 1831 |
1838 std::vector<char> IndexDataKey::EncodeMaxKey(int64 database_id, | 1832 std::string IndexDataKey::EncodeMaxKey(int64 database_id, |
1839 int64 object_store_id, | 1833 int64 object_store_id, |
1840 int64 index_id) { | 1834 int64 index_id) { |
1841 return Encode(database_id, | 1835 return Encode(database_id, |
1842 object_store_id, | 1836 object_store_id, |
1843 index_id, | 1837 index_id, |
1844 MaxIDBKey(), | 1838 MaxIDBKey(), |
1845 MaxIDBKey(), | 1839 MaxIDBKey(), |
1846 std::numeric_limits<int64>::max()); | 1840 std::numeric_limits<int64>::max()); |
1847 } | 1841 } |
1848 | 1842 |
1849 int IndexDataKey::Compare(const IndexDataKey& other, | 1843 int IndexDataKey::Compare(const IndexDataKey& other, |
1850 bool ignore_duplicates, | 1844 bool ignore_duplicates, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1892 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const { | 1886 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const { |
1893 scoped_ptr<IndexedDBKey> key; | 1887 scoped_ptr<IndexedDBKey> key; |
1894 StringPiece slice(&encoded_primary_key_[0], encoded_primary_key_.size()); | 1888 StringPiece slice(&encoded_primary_key_[0], encoded_primary_key_.size()); |
1895 if (!DecodeIDBKey(&slice, &key)) { | 1889 if (!DecodeIDBKey(&slice, &key)) { |
1896 // TODO(jsbell): Return error. | 1890 // TODO(jsbell): Return error. |
1897 } | 1891 } |
1898 return key.Pass(); | 1892 return key.Pass(); |
1899 } | 1893 } |
1900 | 1894 |
1901 } // namespace content | 1895 } // namespace content |
OLD | NEW |