Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: content/browser/indexed_db/indexed_db_leveldb_coding.cc

Issue 18075008: IndexedDB: Switch key/value handling from vector<char> to std::string (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove C++11ism Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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);
734 StringPiece slice_b(&*key_b.begin(), key_b.size()); 731 StringPiece slice_b(key_b);
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
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
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
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.append(database_id_string);
1151 ret.insert( 1148 ret.append(object_store_id_string);
1152 ret.end(), object_store_id_string.begin(), object_store_id_string.end()); 1149 ret.append(index_id_string);
1153 ret.insert(ret.end(), index_id_string.begin(), index_id_string.end());
1154 1150
1155 DCHECK_LE(ret.size(), kDefaultInlineBufferSize); 1151 DCHECK_LE(ret.size(), kDefaultInlineBufferSize);
1156 return ret; 1152 return ret;
1157 } 1153 }
1158 1154
1159 int KeyPrefix::Compare(const KeyPrefix& other) const { 1155 int KeyPrefix::Compare(const KeyPrefix& other) const {
1160 DCHECK(database_id_ != kInvalidId); 1156 DCHECK(database_id_ != kInvalidId);
1161 DCHECK(object_store_id_ != kInvalidId); 1157 DCHECK(object_store_id_ != kInvalidId);
1162 DCHECK(index_id_ != kInvalidId); 1158 DCHECK(index_id_ != kInvalidId);
1163 1159
(...skipping 19 matching lines...) Expand all
1183 return OBJECT_STORE_DATA; 1179 return OBJECT_STORE_DATA;
1184 if (index_id_ == kExistsEntryIndexId) 1180 if (index_id_ == kExistsEntryIndexId)
1185 return EXISTS_ENTRY; 1181 return EXISTS_ENTRY;
1186 if (index_id_ >= kMinimumIndexId) 1182 if (index_id_ >= kMinimumIndexId)
1187 return INDEX_DATA; 1183 return INDEX_DATA;
1188 1184
1189 NOTREACHED(); 1185 NOTREACHED();
1190 return INVALID_TYPE; 1186 return INVALID_TYPE;
1191 } 1187 }
1192 1188
1193 std::vector<char> SchemaVersionKey::Encode() { 1189 std::string SchemaVersionKey::Encode() {
1194 std::vector<char> ret = KeyPrefix::EncodeEmpty(); 1190 std::string ret = KeyPrefix::EncodeEmpty();
1195 ret.push_back(kSchemaVersionTypeByte); 1191 ret.push_back(kSchemaVersionTypeByte);
1196 return ret; 1192 return ret;
1197 } 1193 }
1198 1194
1199 std::vector<char> MaxDatabaseIdKey::Encode() { 1195 std::string MaxDatabaseIdKey::Encode() {
1200 std::vector<char> ret = KeyPrefix::EncodeEmpty(); 1196 std::string ret = KeyPrefix::EncodeEmpty();
1201 ret.push_back(kMaxDatabaseIdTypeByte); 1197 ret.push_back(kMaxDatabaseIdTypeByte);
1202 return ret; 1198 return ret;
1203 } 1199 }
1204 1200
1205 std::vector<char> DataVersionKey::Encode() { 1201 std::string DataVersionKey::Encode() {
1206 std::vector<char> ret = KeyPrefix::EncodeEmpty(); 1202 std::string ret = KeyPrefix::EncodeEmpty();
1207 ret.push_back(kDataVersionTypeByte); 1203 ret.push_back(kDataVersionTypeByte);
1208 return ret; 1204 return ret;
1209 } 1205 }
1210 1206
1211 DatabaseFreeListKey::DatabaseFreeListKey() : database_id_(-1) {} 1207 DatabaseFreeListKey::DatabaseFreeListKey() : database_id_(-1) {}
1212 1208
1213 const char* DatabaseFreeListKey::Decode(const char* start, 1209 const char* DatabaseFreeListKey::Decode(const char* start,
1214 const char* limit, 1210 const char* limit,
1215 DatabaseFreeListKey* result) { 1211 DatabaseFreeListKey* result) {
1216 KeyPrefix prefix; 1212 KeyPrefix prefix;
(...skipping 10 matching lines...) Expand all
1227 if (!DecodeByte(&slice, &type_byte)) 1223 if (!DecodeByte(&slice, &type_byte))
1228 return 0; 1224 return 0;
1229 DCHECK_EQ(type_byte, kDatabaseFreeListTypeByte); 1225 DCHECK_EQ(type_byte, kDatabaseFreeListTypeByte);
1230 if (slice.empty()) 1226 if (slice.empty())
1231 return 0; 1227 return 0;
1232 if (!DecodeVarInt(&slice, &result->database_id_)) 1228 if (!DecodeVarInt(&slice, &result->database_id_))
1233 return 0; 1229 return 0;
1234 return slice.begin(); 1230 return slice.begin();
1235 } 1231 }
1236 1232
1237 std::vector<char> DatabaseFreeListKey::Encode(int64 database_id) { 1233 std::string DatabaseFreeListKey::Encode(int64 database_id) {
1238 std::vector<char> ret = KeyPrefix::EncodeEmpty(); 1234 std::string ret = KeyPrefix::EncodeEmpty();
1239 ret.push_back(kDatabaseFreeListTypeByte); 1235 ret.push_back(kDatabaseFreeListTypeByte);
1240 EncodeVarInt(database_id, &ret); 1236 EncodeVarInt(database_id, &ret);
1241 return ret; 1237 return ret;
1242 } 1238 }
1243 1239
1244 std::vector<char> DatabaseFreeListKey::EncodeMaxKey() { 1240 std::string DatabaseFreeListKey::EncodeMaxKey() {
1245 return Encode(std::numeric_limits<int64>::max()); 1241 return Encode(std::numeric_limits<int64>::max());
1246 } 1242 }
1247 1243
1248 int64 DatabaseFreeListKey::DatabaseId() const { 1244 int64 DatabaseFreeListKey::DatabaseId() const {
1249 DCHECK_GE(database_id_, 0); 1245 DCHECK_GE(database_id_, 0);
1250 return database_id_; 1246 return database_id_;
1251 } 1247 }
1252 1248
1253 int DatabaseFreeListKey::Compare(const DatabaseFreeListKey& other) const { 1249 int DatabaseFreeListKey::Compare(const DatabaseFreeListKey& other) const {
1254 DCHECK_GE(database_id_, 0); 1250 DCHECK_GE(database_id_, 0);
(...skipping 17 matching lines...) Expand all
1272 if (!DecodeByte(&slice, &type_byte)) 1268 if (!DecodeByte(&slice, &type_byte))
1273 return 0; 1269 return 0;
1274 DCHECK_EQ(type_byte, kDatabaseNameTypeByte); 1270 DCHECK_EQ(type_byte, kDatabaseNameTypeByte);
1275 if (!DecodeStringWithLength(&slice, &result->origin_)) 1271 if (!DecodeStringWithLength(&slice, &result->origin_))
1276 return 0; 1272 return 0;
1277 if (!DecodeStringWithLength(&slice, &result->database_name_)) 1273 if (!DecodeStringWithLength(&slice, &result->database_name_))
1278 return 0; 1274 return 0;
1279 return slice.begin(); 1275 return slice.begin();
1280 } 1276 }
1281 1277
1282 std::vector<char> DatabaseNameKey::Encode(const std::string& origin_identifier, 1278 std::string DatabaseNameKey::Encode(const std::string& origin_identifier,
1283 const string16& database_name) { 1279 const string16& database_name) {
1284 std::vector<char> ret = KeyPrefix::EncodeEmpty(); 1280 std::string ret = KeyPrefix::EncodeEmpty();
1285 ret.push_back(kDatabaseNameTypeByte); 1281 ret.push_back(kDatabaseNameTypeByte);
1286 EncodeStringWithLength(base::ASCIIToUTF16(origin_identifier), &ret); 1282 EncodeStringWithLength(base::ASCIIToUTF16(origin_identifier), &ret);
1287 EncodeStringWithLength(database_name, &ret); 1283 EncodeStringWithLength(database_name, &ret);
1288 return ret; 1284 return ret;
1289 } 1285 }
1290 1286
1291 std::vector<char> DatabaseNameKey::EncodeMinKeyForOrigin( 1287 std::string DatabaseNameKey::EncodeMinKeyForOrigin(
1292 const std::string& origin_identifier) { 1288 const std::string& origin_identifier) {
1293 return Encode(origin_identifier, string16()); 1289 return Encode(origin_identifier, string16());
1294 } 1290 }
1295 1291
1296 std::vector<char> DatabaseNameKey::EncodeStopKeyForOrigin( 1292 std::string DatabaseNameKey::EncodeStopKeyForOrigin(
1297 const std::string& origin_identifier) { 1293 const std::string& origin_identifier) {
1298 // just after origin in collation order 1294 // just after origin in collation order
1299 return EncodeMinKeyForOrigin(origin_identifier + '\x01'); 1295 return EncodeMinKeyForOrigin(origin_identifier + '\x01');
1300 } 1296 }
1301 1297
1302 int DatabaseNameKey::Compare(const DatabaseNameKey& other) { 1298 int DatabaseNameKey::Compare(const DatabaseNameKey& other) {
1303 if (int x = origin_.compare(other.origin_)) 1299 if (int x = origin_.compare(other.origin_))
1304 return x; 1300 return x;
1305 return database_name_.compare(other.database_name_); 1301 return database_name_.compare(other.database_name_);
1306 } 1302 }
1307 1303
1308 std::vector<char> DatabaseMetaDataKey::Encode(int64 database_id, 1304 std::string DatabaseMetaDataKey::Encode(int64 database_id,
1309 MetaDataType meta_data_type) { 1305 MetaDataType meta_data_type) {
1310 KeyPrefix prefix(database_id); 1306 KeyPrefix prefix(database_id);
1311 std::vector<char> ret = prefix.Encode(); 1307 std::string ret = prefix.Encode();
1312 ret.push_back(meta_data_type); 1308 ret.push_back(meta_data_type);
1313 return ret; 1309 return ret;
1314 } 1310 }
1315 1311
1316 ObjectStoreMetaDataKey::ObjectStoreMetaDataKey() 1312 ObjectStoreMetaDataKey::ObjectStoreMetaDataKey()
1317 : object_store_id_(-1), meta_data_type_(-1) {} 1313 : object_store_id_(-1), meta_data_type_(-1) {}
1318 1314
1319 const char* ObjectStoreMetaDataKey::Decode(const char* start, 1315 const char* ObjectStoreMetaDataKey::Decode(const char* start,
1320 const char* limit, 1316 const char* limit,
1321 ObjectStoreMetaDataKey* result) { 1317 ObjectStoreMetaDataKey* result) {
(...skipping 12 matching lines...) Expand all
1334 return 0; 1330 return 0;
1335 DCHECK_EQ(type_byte, kObjectStoreMetaDataTypeByte); 1331 DCHECK_EQ(type_byte, kObjectStoreMetaDataTypeByte);
1336 if (!DecodeVarInt(&slice, &result->object_store_id_)) 1332 if (!DecodeVarInt(&slice, &result->object_store_id_))
1337 return 0; 1333 return 0;
1338 DCHECK(result->object_store_id_); 1334 DCHECK(result->object_store_id_);
1339 if (!DecodeByte(&slice, &result->meta_data_type_)) 1335 if (!DecodeByte(&slice, &result->meta_data_type_))
1340 return 0; 1336 return 0;
1341 return slice.begin(); 1337 return slice.begin();
1342 } 1338 }
1343 1339
1344 std::vector<char> ObjectStoreMetaDataKey::Encode(int64 database_id, 1340 std::string ObjectStoreMetaDataKey::Encode(int64 database_id,
1345 int64 object_store_id, 1341 int64 object_store_id,
1346 unsigned char meta_data_type) { 1342 unsigned char meta_data_type) {
1347 KeyPrefix prefix(database_id); 1343 KeyPrefix prefix(database_id);
1348 std::vector<char> ret = prefix.Encode(); 1344 std::string ret = prefix.Encode();
1349 ret.push_back(kObjectStoreMetaDataTypeByte); 1345 ret.push_back(kObjectStoreMetaDataTypeByte);
1350 EncodeVarInt(object_store_id, &ret); 1346 EncodeVarInt(object_store_id, &ret);
1351 ret.push_back(meta_data_type); 1347 ret.push_back(meta_data_type);
1352 return ret; 1348 return ret;
1353 } 1349 }
1354 1350
1355 std::vector<char> ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id) { 1351 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id) {
1356 return Encode(database_id, 1352 return Encode(database_id,
1357 std::numeric_limits<int64>::max(), 1353 std::numeric_limits<int64>::max(),
1358 kObjectMetaDataTypeMaximum); 1354 kObjectMetaDataTypeMaximum);
1359 } 1355 }
1360 1356
1361 std::vector<char> ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id, 1357 std::string ObjectStoreMetaDataKey::EncodeMaxKey(int64 database_id,
1362 int64 object_store_id) { 1358 int64 object_store_id) {
1363 return Encode(database_id, object_store_id, kObjectMetaDataTypeMaximum); 1359 return Encode(database_id, object_store_id, kObjectMetaDataTypeMaximum);
1364 } 1360 }
1365 1361
1366 int64 ObjectStoreMetaDataKey::ObjectStoreId() const { 1362 int64 ObjectStoreMetaDataKey::ObjectStoreId() const {
1367 DCHECK_GE(object_store_id_, 0); 1363 DCHECK_GE(object_store_id_, 0);
1368 return object_store_id_; 1364 return object_store_id_;
1369 } 1365 }
1370 unsigned char ObjectStoreMetaDataKey::MetaDataType() const { 1366 unsigned char ObjectStoreMetaDataKey::MetaDataType() const {
1371 return meta_data_type_; 1367 return meta_data_type_;
1372 } 1368 }
(...skipping 30 matching lines...) Expand all
1403 DCHECK_EQ(type_byte, kIndexMetaDataTypeByte); 1399 DCHECK_EQ(type_byte, kIndexMetaDataTypeByte);
1404 if (!DecodeVarInt(&slice, &result->object_store_id_)) 1400 if (!DecodeVarInt(&slice, &result->object_store_id_))
1405 return 0; 1401 return 0;
1406 if (!DecodeVarInt(&slice, &result->index_id_)) 1402 if (!DecodeVarInt(&slice, &result->index_id_))
1407 return 0; 1403 return 0;
1408 if (!DecodeByte(&slice, &result->meta_data_type_)) 1404 if (!DecodeByte(&slice, &result->meta_data_type_))
1409 return 0; 1405 return 0;
1410 return slice.begin(); 1406 return slice.begin();
1411 } 1407 }
1412 1408
1413 std::vector<char> IndexMetaDataKey::Encode(int64 database_id, 1409 std::string IndexMetaDataKey::Encode(int64 database_id,
1414 int64 object_store_id, 1410 int64 object_store_id,
1415 int64 index_id, 1411 int64 index_id,
1416 unsigned char meta_data_type) { 1412 unsigned char meta_data_type) {
1417 KeyPrefix prefix(database_id); 1413 KeyPrefix prefix(database_id);
1418 std::vector<char> ret = prefix.Encode(); 1414 std::string ret = prefix.Encode();
1419 ret.push_back(kIndexMetaDataTypeByte); 1415 ret.push_back(kIndexMetaDataTypeByte);
1420 EncodeVarInt(object_store_id, &ret); 1416 EncodeVarInt(object_store_id, &ret);
1421 EncodeVarInt(index_id, &ret); 1417 EncodeVarInt(index_id, &ret);
1422 EncodeByte(meta_data_type, &ret); 1418 EncodeByte(meta_data_type, &ret);
1423 return ret; 1419 return ret;
1424 } 1420 }
1425 1421
1426 std::vector<char> IndexMetaDataKey::EncodeMaxKey(int64 database_id, 1422 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id,
1427 int64 object_store_id) { 1423 int64 object_store_id) {
1428 return Encode(database_id, 1424 return Encode(database_id,
1429 object_store_id, 1425 object_store_id,
1430 std::numeric_limits<int64>::max(), 1426 std::numeric_limits<int64>::max(),
1431 kIndexMetaDataTypeMaximum); 1427 kIndexMetaDataTypeMaximum);
1432 } 1428 }
1433 1429
1434 std::vector<char> IndexMetaDataKey::EncodeMaxKey(int64 database_id, 1430 std::string IndexMetaDataKey::EncodeMaxKey(int64 database_id,
1435 int64 object_store_id, 1431 int64 object_store_id,
1436 int64 index_id) { 1432 int64 index_id) {
1437 return Encode( 1433 return Encode(
1438 database_id, object_store_id, index_id, kIndexMetaDataTypeMaximum); 1434 database_id, object_store_id, index_id, kIndexMetaDataTypeMaximum);
1439 } 1435 }
1440 1436
1441 int IndexMetaDataKey::Compare(const IndexMetaDataKey& other) { 1437 int IndexMetaDataKey::Compare(const IndexMetaDataKey& other) {
1442 DCHECK_GE(object_store_id_, 0); 1438 DCHECK_GE(object_store_id_, 0);
1443 DCHECK_GE(index_id_, 0); 1439 DCHECK_GE(index_id_, 0);
1444 1440
1445 if (int x = CompareInts(object_store_id_, other.object_store_id_)) 1441 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1446 return x; 1442 return x;
(...skipping 24 matching lines...) Expand all
1471 unsigned char type_byte = 0; 1467 unsigned char type_byte = 0;
1472 StringPiece slice(p, limit - p); 1468 StringPiece slice(p, limit - p);
1473 if (!DecodeByte(&slice, &type_byte)) 1469 if (!DecodeByte(&slice, &type_byte))
1474 return 0; 1470 return 0;
1475 DCHECK_EQ(type_byte, kObjectStoreFreeListTypeByte); 1471 DCHECK_EQ(type_byte, kObjectStoreFreeListTypeByte);
1476 if (!DecodeVarInt(&slice, &result->object_store_id_)) 1472 if (!DecodeVarInt(&slice, &result->object_store_id_))
1477 return 0; 1473 return 0;
1478 return slice.begin(); 1474 return slice.begin();
1479 } 1475 }
1480 1476
1481 std::vector<char> ObjectStoreFreeListKey::Encode(int64 database_id, 1477 std::string ObjectStoreFreeListKey::Encode(int64 database_id,
1482 int64 object_store_id) { 1478 int64 object_store_id) {
1483 KeyPrefix prefix(database_id); 1479 KeyPrefix prefix(database_id);
1484 std::vector<char> ret = prefix.Encode(); 1480 std::string ret = prefix.Encode();
1485 ret.push_back(kObjectStoreFreeListTypeByte); 1481 ret.push_back(kObjectStoreFreeListTypeByte);
1486 EncodeVarInt(object_store_id, &ret); 1482 EncodeVarInt(object_store_id, &ret);
1487 return ret; 1483 return ret;
1488 } 1484 }
1489 1485
1490 std::vector<char> ObjectStoreFreeListKey::EncodeMaxKey(int64 database_id) { 1486 std::string ObjectStoreFreeListKey::EncodeMaxKey(int64 database_id) {
1491 return Encode(database_id, std::numeric_limits<int64>::max()); 1487 return Encode(database_id, std::numeric_limits<int64>::max());
1492 } 1488 }
1493 1489
1494 int64 ObjectStoreFreeListKey::ObjectStoreId() const { 1490 int64 ObjectStoreFreeListKey::ObjectStoreId() const {
1495 DCHECK_GE(object_store_id_, 0); 1491 DCHECK_GE(object_store_id_, 0);
1496 return object_store_id_; 1492 return object_store_id_;
1497 } 1493 }
1498 1494
1499 int ObjectStoreFreeListKey::Compare(const ObjectStoreFreeListKey& other) { 1495 int ObjectStoreFreeListKey::Compare(const ObjectStoreFreeListKey& other) {
1500 // TODO(jsbell): It may seem strange that we're not comparing database id's, 1496 // TODO(jsbell): It may seem strange that we're not comparing database id's,
(...skipping 22 matching lines...) Expand all
1523 if (!DecodeByte(&slice, &type_byte)) 1519 if (!DecodeByte(&slice, &type_byte))
1524 return 0; 1520 return 0;
1525 DCHECK_EQ(type_byte, kIndexFreeListTypeByte); 1521 DCHECK_EQ(type_byte, kIndexFreeListTypeByte);
1526 if (!DecodeVarInt(&slice, &result->object_store_id_)) 1522 if (!DecodeVarInt(&slice, &result->object_store_id_))
1527 return 0; 1523 return 0;
1528 if (!DecodeVarInt(&slice, &result->index_id_)) 1524 if (!DecodeVarInt(&slice, &result->index_id_))
1529 return 0; 1525 return 0;
1530 return slice.begin(); 1526 return slice.begin();
1531 } 1527 }
1532 1528
1533 std::vector<char> IndexFreeListKey::Encode(int64 database_id, 1529 std::string IndexFreeListKey::Encode(int64 database_id,
1534 int64 object_store_id, 1530 int64 object_store_id,
1535 int64 index_id) { 1531 int64 index_id) {
1536 KeyPrefix prefix(database_id); 1532 KeyPrefix prefix(database_id);
1537 std::vector<char> ret = prefix.Encode(); 1533 std::string ret = prefix.Encode();
1538 ret.push_back(kIndexFreeListTypeByte); 1534 ret.push_back(kIndexFreeListTypeByte);
1539 EncodeVarInt(object_store_id, &ret); 1535 EncodeVarInt(object_store_id, &ret);
1540 EncodeVarInt(index_id, &ret); 1536 EncodeVarInt(index_id, &ret);
1541 return ret; 1537 return ret;
1542 } 1538 }
1543 1539
1544 std::vector<char> IndexFreeListKey::EncodeMaxKey(int64 database_id, 1540 std::string IndexFreeListKey::EncodeMaxKey(int64 database_id,
1545 int64 object_store_id) { 1541 int64 object_store_id) {
1546 return Encode( 1542 return Encode(
1547 database_id, object_store_id, std::numeric_limits<int64>::max()); 1543 database_id, object_store_id, std::numeric_limits<int64>::max());
1548 } 1544 }
1549 1545
1550 int IndexFreeListKey::Compare(const IndexFreeListKey& other) { 1546 int IndexFreeListKey::Compare(const IndexFreeListKey& other) {
1551 DCHECK_GE(object_store_id_, 0); 1547 DCHECK_GE(object_store_id_, 0);
1552 DCHECK_GE(index_id_, 0); 1548 DCHECK_GE(index_id_, 0);
1553 if (int x = CompareInts(object_store_id_, other.object_store_id_)) 1549 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1554 return x; 1550 return x;
1555 return CompareInts(index_id_, other.index_id_); 1551 return CompareInts(index_id_, other.index_id_);
(...skipping 27 matching lines...) Expand all
1583 unsigned char type_byte = 0; 1579 unsigned char type_byte = 0;
1584 StringPiece slice(p, limit - p); 1580 StringPiece slice(p, limit - p);
1585 if (!DecodeByte(&slice, &type_byte)) 1581 if (!DecodeByte(&slice, &type_byte))
1586 return 0; 1582 return 0;
1587 DCHECK_EQ(type_byte, kObjectStoreNamesTypeByte); 1583 DCHECK_EQ(type_byte, kObjectStoreNamesTypeByte);
1588 if (!DecodeStringWithLength(&slice, &result->object_store_name_)) 1584 if (!DecodeStringWithLength(&slice, &result->object_store_name_))
1589 return 0; 1585 return 0;
1590 return slice.begin(); 1586 return slice.begin();
1591 } 1587 }
1592 1588
1593 std::vector<char> ObjectStoreNamesKey::Encode( 1589 std::string ObjectStoreNamesKey::Encode(int64 database_id,
1594 int64 database_id, 1590 const string16& object_store_name) {
1595 const string16& object_store_name) {
1596 KeyPrefix prefix(database_id); 1591 KeyPrefix prefix(database_id);
1597 std::vector<char> ret = prefix.Encode(); 1592 std::string ret = prefix.Encode();
1598 ret.push_back(kObjectStoreNamesTypeByte); 1593 ret.push_back(kObjectStoreNamesTypeByte);
1599 EncodeStringWithLength(object_store_name, &ret); 1594 EncodeStringWithLength(object_store_name, &ret);
1600 return ret; 1595 return ret;
1601 } 1596 }
1602 1597
1603 int ObjectStoreNamesKey::Compare(const ObjectStoreNamesKey& other) { 1598 int ObjectStoreNamesKey::Compare(const ObjectStoreNamesKey& other) {
1604 return object_store_name_.compare(other.object_store_name_); 1599 return object_store_name_.compare(other.object_store_name_);
1605 } 1600 }
1606 1601
1607 IndexNamesKey::IndexNamesKey() : object_store_id_(-1) {} 1602 IndexNamesKey::IndexNamesKey() : object_store_id_(-1) {}
(...skipping 17 matching lines...) Expand all
1625 if (!DecodeByte(&slice, &type_byte)) 1620 if (!DecodeByte(&slice, &type_byte))
1626 return 0; 1621 return 0;
1627 DCHECK_EQ(type_byte, kIndexNamesKeyTypeByte); 1622 DCHECK_EQ(type_byte, kIndexNamesKeyTypeByte);
1628 if (!DecodeVarInt(&slice, &result->object_store_id_)) 1623 if (!DecodeVarInt(&slice, &result->object_store_id_))
1629 return 0; 1624 return 0;
1630 if (!DecodeStringWithLength(&slice, &result->index_name_)) 1625 if (!DecodeStringWithLength(&slice, &result->index_name_))
1631 return 0; 1626 return 0;
1632 return slice.begin(); 1627 return slice.begin();
1633 } 1628 }
1634 1629
1635 std::vector<char> IndexNamesKey::Encode(int64 database_id, 1630 std::string IndexNamesKey::Encode(int64 database_id,
1636 int64 object_store_id, 1631 int64 object_store_id,
1637 const string16& index_name) { 1632 const string16& index_name) {
1638 KeyPrefix prefix(database_id); 1633 KeyPrefix prefix(database_id);
1639 std::vector<char> ret = prefix.Encode(); 1634 std::string ret = prefix.Encode();
1640 ret.push_back(kIndexNamesKeyTypeByte); 1635 ret.push_back(kIndexNamesKeyTypeByte);
1641 EncodeVarInt(object_store_id, &ret); 1636 EncodeVarInt(object_store_id, &ret);
1642 EncodeStringWithLength(index_name, &ret); 1637 EncodeStringWithLength(index_name, &ret);
1643 return ret; 1638 return ret;
1644 } 1639 }
1645 1640
1646 int IndexNamesKey::Compare(const IndexNamesKey& other) { 1641 int IndexNamesKey::Compare(const IndexNamesKey& other) {
1647 DCHECK_GE(object_store_id_, 0); 1642 DCHECK_GE(object_store_id_, 0);
1648 if (int x = CompareInts(object_store_id_, other.object_store_id_)) 1643 if (int x = CompareInts(object_store_id_, other.object_store_id_))
1649 return x; 1644 return x;
(...skipping 14 matching lines...) Expand all
1664 DCHECK(prefix.object_store_id_); 1659 DCHECK(prefix.object_store_id_);
1665 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); 1660 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber);
1666 if (p == end) 1661 if (p == end)
1667 return 0; 1662 return 0;
1668 StringPiece slice(p, end - p); 1663 StringPiece slice(p, end - p);
1669 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) 1664 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_))
1670 return 0; 1665 return 0;
1671 return slice.begin(); 1666 return slice.begin();
1672 } 1667 }
1673 1668
1674 std::vector<char> ObjectStoreDataKey::Encode( 1669 std::string ObjectStoreDataKey::Encode(int64 database_id,
1675 int64 database_id, 1670 int64 object_store_id,
1676 int64 object_store_id, 1671 const std::string encoded_user_key) {
1677 const std::vector<char> encoded_user_key) {
1678 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( 1672 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1679 database_id, object_store_id, kSpecialIndexNumber)); 1673 database_id, object_store_id, kSpecialIndexNumber));
1680 std::vector<char> ret = prefix.Encode(); 1674 std::string ret = prefix.Encode();
1681 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); 1675 ret.append(encoded_user_key);
1682 1676
1683 return ret; 1677 return ret;
1684 } 1678 }
1685 1679
1686 std::vector<char> ObjectStoreDataKey::Encode(int64 database_id, 1680 std::string ObjectStoreDataKey::Encode(int64 database_id,
1687 int64 object_store_id, 1681 int64 object_store_id,
1688 const IndexedDBKey& user_key) { 1682 const IndexedDBKey& user_key) {
1689 std::vector<char> encoded_key; 1683 std::string encoded_key;
1690 EncodeIDBKey(user_key, &encoded_key); 1684 EncodeIDBKey(user_key, &encoded_key);
1691 return Encode(database_id, object_store_id, encoded_key); 1685 return Encode(database_id, object_store_id, encoded_key);
1692 } 1686 }
1693 1687
1694 int ObjectStoreDataKey::Compare(const ObjectStoreDataKey& other, bool* ok) { 1688 int ObjectStoreDataKey::Compare(const ObjectStoreDataKey& other, bool* ok) {
1695 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); 1689 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok);
1696 } 1690 }
1697 1691
1698 scoped_ptr<IndexedDBKey> ObjectStoreDataKey::user_key() const { 1692 scoped_ptr<IndexedDBKey> ObjectStoreDataKey::user_key() const {
1699 scoped_ptr<IndexedDBKey> key; 1693 scoped_ptr<IndexedDBKey> key;
1700 StringPiece slice(&encoded_user_key_[0], encoded_user_key_.size()); 1694 StringPiece slice(encoded_user_key_);
1701 if (!DecodeIDBKey(&slice, &key)) { 1695 if (!DecodeIDBKey(&slice, &key)) {
1702 // TODO(jsbell): Return error. 1696 // TODO(jsbell): Return error.
1703 } 1697 }
1704 return key.Pass(); 1698 return key.Pass();
1705 } 1699 }
1706 1700
1707 const int64 ObjectStoreDataKey::kSpecialIndexNumber = kObjectStoreDataIndexId; 1701 const int64 ObjectStoreDataKey::kSpecialIndexNumber = kObjectStoreDataIndexId;
1708 1702
1709 ExistsEntryKey::ExistsEntryKey() {} 1703 ExistsEntryKey::ExistsEntryKey() {}
1710 ExistsEntryKey::~ExistsEntryKey() {} 1704 ExistsEntryKey::~ExistsEntryKey() {}
1711 1705
1712 const char* ExistsEntryKey::Decode(const char* start, 1706 const char* ExistsEntryKey::Decode(const char* start,
1713 const char* end, 1707 const char* end,
1714 ExistsEntryKey* result) { 1708 ExistsEntryKey* result) {
1715 KeyPrefix prefix; 1709 KeyPrefix prefix;
1716 const char* p = KeyPrefix::Decode(start, end, &prefix); 1710 const char* p = KeyPrefix::Decode(start, end, &prefix);
1717 if (!p) 1711 if (!p)
1718 return 0; 1712 return 0;
1719 DCHECK(prefix.database_id_); 1713 DCHECK(prefix.database_id_);
1720 DCHECK(prefix.object_store_id_); 1714 DCHECK(prefix.object_store_id_);
1721 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber); 1715 DCHECK_EQ(prefix.index_id_, kSpecialIndexNumber);
1722 if (p == end) 1716 if (p == end)
1723 return 0; 1717 return 0;
1724 StringPiece slice(p, end - p); 1718 StringPiece slice(p, end - p);
1725 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_)) 1719 if (!ExtractEncodedIDBKey(&slice, &result->encoded_user_key_))
1726 return 0; 1720 return 0;
1727 return slice.begin(); 1721 return slice.begin();
1728 } 1722 }
1729 1723
1730 std::vector<char> ExistsEntryKey::Encode(int64 database_id, 1724 std::string ExistsEntryKey::Encode(int64 database_id,
1731 int64 object_store_id, 1725 int64 object_store_id,
1732 const std::vector<char>& encoded_key) { 1726 const std::string& encoded_key) {
1733 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex( 1727 KeyPrefix prefix(KeyPrefix::CreateWithSpecialIndex(
1734 database_id, object_store_id, kSpecialIndexNumber)); 1728 database_id, object_store_id, kSpecialIndexNumber));
1735 std::vector<char> ret = prefix.Encode(); 1729 std::string ret = prefix.Encode();
1736 ret.insert(ret.end(), encoded_key.begin(), encoded_key.end()); 1730 ret.append(encoded_key);
1737 return ret; 1731 return ret;
1738 } 1732 }
1739 1733
1740 std::vector<char> ExistsEntryKey::Encode(int64 database_id, 1734 std::string ExistsEntryKey::Encode(int64 database_id,
1741 int64 object_store_id, 1735 int64 object_store_id,
1742 const IndexedDBKey& user_key) { 1736 const IndexedDBKey& user_key) {
1743 std::vector<char> encoded_key; 1737 std::string encoded_key;
1744 EncodeIDBKey(user_key, &encoded_key); 1738 EncodeIDBKey(user_key, &encoded_key);
1745 return Encode(database_id, object_store_id, encoded_key); 1739 return Encode(database_id, object_store_id, encoded_key);
1746 } 1740 }
1747 1741
1748 int ExistsEntryKey::Compare(const ExistsEntryKey& other, bool* ok) { 1742 int ExistsEntryKey::Compare(const ExistsEntryKey& other, bool* ok) {
1749 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok); 1743 return CompareEncodedIDBKeys(encoded_user_key_, other.encoded_user_key_, ok);
1750 } 1744 }
1751 1745
1752 scoped_ptr<IndexedDBKey> ExistsEntryKey::user_key() const { 1746 scoped_ptr<IndexedDBKey> ExistsEntryKey::user_key() const {
1753 scoped_ptr<IndexedDBKey> key; 1747 scoped_ptr<IndexedDBKey> key;
1754 StringPiece slice(&encoded_user_key_[0], encoded_user_key_.size()); 1748 StringPiece slice(encoded_user_key_);
1755 if (!DecodeIDBKey(&slice, &key)) { 1749 if (!DecodeIDBKey(&slice, &key)) {
1756 // TODO(jsbell): Return error. 1750 // TODO(jsbell): Return error.
1757 } 1751 }
1758 return key.Pass(); 1752 return key.Pass();
1759 } 1753 }
1760 1754
1761 const int64 ExistsEntryKey::kSpecialIndexNumber = kExistsEntryIndexId; 1755 const int64 ExistsEntryKey::kSpecialIndexNumber = kExistsEntryIndexId;
1762 1756
1763 IndexDataKey::IndexDataKey() 1757 IndexDataKey::IndexDataKey()
1764 : database_id_(-1), 1758 : database_id_(-1),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1796 1790
1797 // [optional] primary key 1791 // [optional] primary key
1798 if (slice.empty()) 1792 if (slice.empty())
1799 return slice.begin(); 1793 return slice.begin();
1800 if (!ExtractEncodedIDBKey(&slice, &result->encoded_primary_key_)) 1794 if (!ExtractEncodedIDBKey(&slice, &result->encoded_primary_key_))
1801 return 0; 1795 return 0;
1802 1796
1803 return slice.begin(); 1797 return slice.begin();
1804 } 1798 }
1805 1799
1806 std::vector<char> IndexDataKey::Encode( 1800 std::string IndexDataKey::Encode(int64 database_id,
1807 int64 database_id, 1801 int64 object_store_id,
1808 int64 object_store_id, 1802 int64 index_id,
1809 int64 index_id, 1803 const std::string& encoded_user_key,
1810 const std::vector<char>& encoded_user_key, 1804 const std::string& encoded_primary_key,
1811 const std::vector<char>& encoded_primary_key, 1805 int64 sequence_number) {
1812 int64 sequence_number) {
1813 KeyPrefix prefix(database_id, object_store_id, index_id); 1806 KeyPrefix prefix(database_id, object_store_id, index_id);
1814 std::vector<char> ret = prefix.Encode(); 1807 std::string ret = prefix.Encode();
1815 ret.insert(ret.end(), encoded_user_key.begin(), encoded_user_key.end()); 1808 ret.append(encoded_user_key);
1816 EncodeVarInt(sequence_number, &ret); 1809 EncodeVarInt(sequence_number, &ret);
1817 ret.insert(ret.end(), encoded_primary_key.begin(), encoded_primary_key.end()); 1810 ret.append(encoded_primary_key);
1818 return ret; 1811 return ret;
1819 } 1812 }
1820 1813
1821 std::vector<char> IndexDataKey::Encode(int64 database_id, 1814 std::string IndexDataKey::Encode(int64 database_id,
1822 int64 object_store_id, 1815 int64 object_store_id,
1823 int64 index_id, 1816 int64 index_id,
1824 const IndexedDBKey& user_key) { 1817 const IndexedDBKey& user_key) {
1825 std::vector<char> encoded_key; 1818 std::string encoded_key;
1826 EncodeIDBKey(user_key, &encoded_key); 1819 EncodeIDBKey(user_key, &encoded_key);
1827 return Encode( 1820 return Encode(
1828 database_id, object_store_id, index_id, encoded_key, MinIDBKey(), 0); 1821 database_id, object_store_id, index_id, encoded_key, MinIDBKey(), 0);
1829 } 1822 }
1830 1823
1831 std::vector<char> IndexDataKey::EncodeMinKey(int64 database_id, 1824 std::string IndexDataKey::EncodeMinKey(int64 database_id,
1832 int64 object_store_id, 1825 int64 object_store_id,
1833 int64 index_id) { 1826 int64 index_id) {
1834 return Encode( 1827 return Encode(
1835 database_id, object_store_id, index_id, MinIDBKey(), MinIDBKey(), 0); 1828 database_id, object_store_id, index_id, MinIDBKey(), MinIDBKey(), 0);
1836 } 1829 }
1837 1830
1838 std::vector<char> IndexDataKey::EncodeMaxKey(int64 database_id, 1831 std::string IndexDataKey::EncodeMaxKey(int64 database_id,
1839 int64 object_store_id, 1832 int64 object_store_id,
1840 int64 index_id) { 1833 int64 index_id) {
1841 return Encode(database_id, 1834 return Encode(database_id,
1842 object_store_id, 1835 object_store_id,
1843 index_id, 1836 index_id,
1844 MaxIDBKey(), 1837 MaxIDBKey(),
1845 MaxIDBKey(), 1838 MaxIDBKey(),
1846 std::numeric_limits<int64>::max()); 1839 std::numeric_limits<int64>::max());
1847 } 1840 }
1848 1841
1849 int IndexDataKey::Compare(const IndexDataKey& other, 1842 int IndexDataKey::Compare(const IndexDataKey& other,
1850 bool ignore_duplicates, 1843 bool ignore_duplicates,
(...skipping 24 matching lines...) Expand all
1875 return object_store_id_; 1868 return object_store_id_;
1876 } 1869 }
1877 1870
1878 int64 IndexDataKey::IndexId() const { 1871 int64 IndexDataKey::IndexId() const {
1879 DCHECK_GE(index_id_, 0); 1872 DCHECK_GE(index_id_, 0);
1880 return index_id_; 1873 return index_id_;
1881 } 1874 }
1882 1875
1883 scoped_ptr<IndexedDBKey> IndexDataKey::user_key() const { 1876 scoped_ptr<IndexedDBKey> IndexDataKey::user_key() const {
1884 scoped_ptr<IndexedDBKey> key; 1877 scoped_ptr<IndexedDBKey> key;
1885 StringPiece slice(&encoded_user_key_[0], encoded_user_key_.size()); 1878 StringPiece slice(encoded_user_key_);
1886 if (!DecodeIDBKey(&slice, &key)) { 1879 if (!DecodeIDBKey(&slice, &key)) {
1887 // TODO(jsbell): Return error. 1880 // TODO(jsbell): Return error.
1888 } 1881 }
1889 return key.Pass(); 1882 return key.Pass();
1890 } 1883 }
1891 1884
1892 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const { 1885 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const {
1893 scoped_ptr<IndexedDBKey> key; 1886 scoped_ptr<IndexedDBKey> key;
1894 StringPiece slice(&encoded_primary_key_[0], encoded_primary_key_.size()); 1887 StringPiece slice(encoded_primary_key_);
1895 if (!DecodeIDBKey(&slice, &key)) { 1888 if (!DecodeIDBKey(&slice, &key)) {
1896 // TODO(jsbell): Return error. 1889 // TODO(jsbell): Return error.
1897 } 1890 }
1898 return key.Pass(); 1891 return key.Pass();
1899 } 1892 }
1900 1893
1901 } // namespace content 1894 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698