OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 Vector<char> ret = encodeVarInt(s.length()); | 328 Vector<char> ret = encodeVarInt(s.length()); |
329 ret.append(encodeString(s)); | 329 ret.append(encodeString(s)); |
330 return ret; | 330 return ret; |
331 } | 331 } |
332 | 332 |
333 const char* decodeStringWithLength(const char* p, const char* limit, String& fou
ndString) | 333 const char* decodeStringWithLength(const char* p, const char* limit, String& fou
ndString) |
334 { | 334 { |
335 ASSERT(limit >= p); | 335 ASSERT(limit >= p); |
336 int64_t len; | 336 int64_t len; |
337 p = decodeVarInt(p, limit, len); | 337 p = decodeVarInt(p, limit, len); |
338 if (!p) | 338 if (!p || len < 0 || p + len * 2 > limit) |
339 return 0; | |
340 if (p + len * 2 > limit) | |
341 return 0; | 339 return 0; |
342 | 340 |
343 foundString = decodeString(p, p + len * 2); | 341 foundString = decodeString(p, p + len * 2); |
344 p += len * 2; | 342 p += len * 2; |
345 return p; | 343 return p; |
346 } | 344 } |
347 | 345 |
348 int compareEncodedStringsWithLength(const char*& p, const char* limitP, const ch
ar*& q, const char* limitQ) | 346 int compareEncodedStringsWithLength(const char*& p, const char* limitP, const ch
ar*& q, const char* limitQ, bool& ok) |
349 { | 347 { |
350 ASSERT(&p != &q); | 348 ASSERT(&p != &q); |
351 ASSERT(limitP >= p); | 349 ASSERT(limitP >= p); |
352 ASSERT(limitQ >= q); | 350 ASSERT(limitQ >= q); |
353 int64_t lenP, lenQ; | 351 int64_t lenP, lenQ; |
354 p = decodeVarInt(p, limitP, lenP); | 352 p = decodeVarInt(p, limitP, lenP); |
355 q = decodeVarInt(q, limitQ, lenQ); | 353 q = decodeVarInt(q, limitQ, lenQ); |
| 354 if (!p || !q || lenP < 0 || lenQ < 0) { |
| 355 ok = false; |
| 356 return 0; |
| 357 } |
356 ASSERT(p && q); | 358 ASSERT(p && q); |
357 ASSERT(lenP >= 0); | 359 ASSERT(lenP >= 0); |
358 ASSERT(lenQ >= 0); | 360 ASSERT(lenQ >= 0); |
359 ASSERT(p + lenP * 2 <= limitP); | 361 ASSERT(p + lenP * 2 <= limitP); |
360 ASSERT(q + lenQ * 2 <= limitQ); | 362 ASSERT(q + lenQ * 2 <= limitQ); |
361 | 363 |
362 const char* startP = p; | 364 const char* startP = p; |
363 const char* startQ = q; | 365 const char* startQ = q; |
364 p += lenP * 2; | 366 p += lenP * 2; |
365 q += lenQ * 2; | 367 q += lenQ * 2; |
366 | 368 |
367 if (p > limitP || q > limitQ) | 369 if (p > limitP || q > limitQ) { |
| 370 ok = false; |
368 return 0; | 371 return 0; |
| 372 } |
369 | 373 |
| 374 ok = true; |
370 const size_t lmin = static_cast<size_t>(lenP < lenQ ? lenP : lenQ); | 375 const size_t lmin = static_cast<size_t>(lenP < lenQ ? lenP : lenQ); |
371 if (int x = memcmp(startP, startQ, lmin * 2)) | 376 if (int x = memcmp(startP, startQ, lmin * 2)) |
372 return x; | 377 return x; |
373 | 378 |
374 if (lenP == lenQ) | 379 if (lenP == lenQ) |
375 return 0; | 380 return 0; |
376 | 381 |
377 return (lenP > lenQ) ? 1 : -1; | 382 return (lenP > lenQ) ? 1 : -1; |
378 } | 383 } |
379 | 384 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 unsigned char type = *p++; | 460 unsigned char type = *p++; |
456 | 461 |
457 switch (type) { | 462 switch (type) { |
458 case IDBKeyNullTypeByte: | 463 case IDBKeyNullTypeByte: |
459 foundKey = IDBKey::createInvalid(); | 464 foundKey = IDBKey::createInvalid(); |
460 return p; | 465 return p; |
461 | 466 |
462 case IDBKeyArrayTypeByte: { | 467 case IDBKeyArrayTypeByte: { |
463 int64_t length; | 468 int64_t length; |
464 p = decodeVarInt(p, limit, length); | 469 p = decodeVarInt(p, limit, length); |
465 if (!p) | 470 if (!p || length < 0) |
466 return 0; | |
467 if (length < 0) | |
468 return 0; | 471 return 0; |
469 IDBKey::KeyArray array; | 472 IDBKey::KeyArray array; |
470 while (length--) { | 473 while (length--) { |
471 RefPtr<IDBKey> key; | 474 RefPtr<IDBKey> key; |
472 p = decodeIDBKey(p, limit, key); | 475 p = decodeIDBKey(p, limit, key); |
473 if (!p) | 476 if (!p) |
474 return 0; | 477 return 0; |
475 array.append(key); | 478 array.append(key); |
476 } | 479 } |
477 foundKey = IDBKey::createArray(array); | 480 foundKey = IDBKey::createArray(array); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 518 |
516 unsigned char type = *p++; | 519 unsigned char type = *p++; |
517 | 520 |
518 switch (type) { | 521 switch (type) { |
519 case IDBKeyNullTypeByte: | 522 case IDBKeyNullTypeByte: |
520 case IDBKeyMinKeyTypeByte: | 523 case IDBKeyMinKeyTypeByte: |
521 break; | 524 break; |
522 case IDBKeyArrayTypeByte: { | 525 case IDBKeyArrayTypeByte: { |
523 int64_t length; | 526 int64_t length; |
524 p = decodeVarInt(p, limit, length); | 527 p = decodeVarInt(p, limit, length); |
525 if (!p) | 528 if (!p || length < 0) |
526 return 0; | |
527 if (length < 0) | |
528 return 0; | 529 return 0; |
529 while (length--) { | 530 while (length--) { |
530 p = extractEncodedIDBKey(p, limit); | 531 p = extractEncodedIDBKey(p, limit); |
531 if (!p) | 532 if (!p) |
532 return 0; | 533 return 0; |
533 } | 534 } |
534 break; | 535 break; |
535 } | 536 } |
536 case IDBKeyStringTypeByte: { | 537 case IDBKeyStringTypeByte: { |
537 int64_t length; | 538 int64_t length; |
538 p = decodeVarInt(p, limit, length); | 539 p = decodeVarInt(p, limit, length); |
539 if (!p) | 540 if (!p || length < 0 || p + length * 2 > limit) |
540 return 0; | |
541 if (p + length * 2 > limit) | |
542 return 0; | 541 return 0; |
543 p += length * 2; | 542 p += length * 2; |
544 break; | 543 break; |
545 } | 544 } |
546 case IDBKeyDateTypeByte: | 545 case IDBKeyDateTypeByte: |
547 case IDBKeyNumberTypeByte: | 546 case IDBKeyNumberTypeByte: |
548 if (p + sizeof(double) > limit) | 547 if (p + sizeof(double) > limit) |
549 return 0; | 548 return 0; |
550 p += sizeof(double); | 549 p += sizeof(double); |
551 break; | 550 break; |
(...skipping 23 matching lines...) Expand all Loading... |
575 case IDBKeyNumberTypeByte: | 574 case IDBKeyNumberTypeByte: |
576 return IDBKey::NumberType; | 575 return IDBKey::NumberType; |
577 case IDBKeyMinKeyTypeByte: | 576 case IDBKeyMinKeyTypeByte: |
578 return IDBKey::MinType; | 577 return IDBKey::MinType; |
579 } | 578 } |
580 | 579 |
581 ASSERT_NOT_REACHED(); | 580 ASSERT_NOT_REACHED(); |
582 return IDBKey::InvalidType; | 581 return IDBKey::InvalidType; |
583 } | 582 } |
584 | 583 |
585 int compareEncodedIDBKeys(const char*& ptrA, const char* limitA, const char*& pt
rB, const char* limitB) | 584 int compareEncodedIDBKeys(const char*& ptrA, const char* limitA, const char*& pt
rB, const char* limitB, bool& ok) |
586 { | 585 { |
| 586 ok = true; |
587 ASSERT(&ptrA != &ptrB); | 587 ASSERT(&ptrA != &ptrB); |
588 ASSERT(ptrA < limitA); | 588 ASSERT(ptrA < limitA); |
589 ASSERT(ptrB < limitB); | 589 ASSERT(ptrB < limitB); |
590 unsigned char typeA = *ptrA++; | 590 unsigned char typeA = *ptrA++; |
591 unsigned char typeB = *ptrB++; | 591 unsigned char typeB = *ptrB++; |
592 | 592 |
593 if (int x = IDBKey::compareTypes(keyTypeByteToKeyType(typeA), keyTypeByteToK
eyType(typeB))) | 593 if (int x = IDBKey::compareTypes(keyTypeByteToKeyType(typeA), keyTypeByteToK
eyType(typeB))) |
594 return x; | 594 return x; |
595 | 595 |
596 switch (typeA) { | 596 switch (typeA) { |
597 case IDBKeyNullTypeByte: | 597 case IDBKeyNullTypeByte: |
598 case IDBKeyMinKeyTypeByte: | 598 case IDBKeyMinKeyTypeByte: |
599 // Null type or max type; no payload to compare. | 599 // Null type or max type; no payload to compare. |
600 return 0; | 600 return 0; |
601 case IDBKeyArrayTypeByte: { | 601 case IDBKeyArrayTypeByte: { |
602 int64_t lengthA, lengthB; | 602 int64_t lengthA, lengthB; |
603 ptrA = decodeVarInt(ptrA, limitA, lengthA); | 603 ptrA = decodeVarInt(ptrA, limitA, lengthA); |
604 if (!ptrA) | 604 ptrB = decodeVarInt(ptrB, limitB, lengthB); |
| 605 if (!ptrA || !ptrB || lengthA < 0 || lengthB < 0) { |
| 606 ok = false; |
605 return 0; | 607 return 0; |
606 ptrB = decodeVarInt(ptrB, limitB, lengthB); | 608 } |
607 if (!ptrB) | |
608 return 0; | |
609 if (lengthA < 0 || lengthB < 0) | |
610 return 0; | |
611 for (int64_t i = 0; i < lengthA && i < lengthB; ++i) { | 609 for (int64_t i = 0; i < lengthA && i < lengthB; ++i) { |
612 if (int cmp = compareEncodedIDBKeys(ptrA, limitA, ptrB, limitB)) | 610 int result = compareEncodedIDBKeys(ptrA, limitA, ptrB, limitB, ok); |
613 return cmp; | 611 if (!ok || result) |
| 612 return result; |
614 } | 613 } |
615 if (lengthA < lengthB) | 614 if (lengthA < lengthB) |
616 return -1; | 615 return -1; |
617 if (lengthA > lengthB) | 616 if (lengthA > lengthB) |
618 return 1; | 617 return 1; |
619 return 0; | 618 return 0; |
620 } | 619 } |
621 case IDBKeyStringTypeByte: | 620 case IDBKeyStringTypeByte: |
622 return compareEncodedStringsWithLength(ptrA, limitA, ptrB, limitB); | 621 return compareEncodedStringsWithLength(ptrA, limitA, ptrB, limitB, ok); |
623 case IDBKeyDateTypeByte: | 622 case IDBKeyDateTypeByte: |
624 case IDBKeyNumberTypeByte: { | 623 case IDBKeyNumberTypeByte: { |
625 double d, e; | 624 double d, e; |
626 ptrA = decodeDouble(ptrA, limitA, &d); | 625 ptrA = decodeDouble(ptrA, limitA, &d); |
| 626 ptrB = decodeDouble(ptrB, limitB, &e); |
627 ASSERT(ptrA); | 627 ASSERT(ptrA); |
628 ptrB = decodeDouble(ptrB, limitB, &e); | |
629 ASSERT(ptrB); | 628 ASSERT(ptrB); |
| 629 if (!ptrA || !ptrB) { |
| 630 ok = false; |
| 631 return 0; |
| 632 } |
630 if (d < e) | 633 if (d < e) |
631 return -1; | 634 return -1; |
632 if (d > e) | 635 if (d > e) |
633 return 1; | 636 return 1; |
634 return 0; | 637 return 0; |
635 } | 638 } |
636 } | 639 } |
637 | 640 |
638 ASSERT_NOT_REACHED(); | 641 ASSERT_NOT_REACHED(); |
639 return 0; | 642 return 0; |
640 } | 643 } |
641 | 644 |
642 int compareEncodedIDBKeys(const Vector<char>& keyA, const Vector<char>& keyB) | 645 int compareEncodedIDBKeys(const Vector<char>& keyA, const Vector<char>& keyB, bo
ol& ok) |
643 { | 646 { |
644 ASSERT(keyA.size() >= 1); | 647 ASSERT(keyA.size() >= 1); |
645 ASSERT(keyB.size() >= 1); | 648 ASSERT(keyB.size() >= 1); |
646 | 649 |
647 const char* ptrA = keyA.data(); | 650 const char* ptrA = keyA.data(); |
648 const char* limitA = ptrA + keyA.size(); | 651 const char* limitA = ptrA + keyA.size(); |
649 const char* ptrB = keyB.data(); | 652 const char* ptrB = keyB.data(); |
650 const char* limitB = ptrB + keyB.size(); | 653 const char* limitB = ptrB + keyB.size(); |
651 | 654 |
652 return compareEncodedIDBKeys(ptrA, limitA, ptrB, limitB); | 655 return compareEncodedIDBKeys(ptrA, limitA, ptrB, limitB, ok); |
653 } | 656 } |
654 | 657 |
655 Vector<char> encodeIDBKeyPath(const IDBKeyPath& keyPath) | 658 Vector<char> encodeIDBKeyPath(const IDBKeyPath& keyPath) |
656 { | 659 { |
657 // May be typed, or may be a raw string. An invalid leading | 660 // May be typed, or may be a raw string. An invalid leading |
658 // byte is used to identify typed coding. New records are | 661 // byte is used to identify typed coding. New records are |
659 // always written as typed. | 662 // always written as typed. |
660 Vector<char, DefaultInlineBufferSize> ret; | 663 Vector<char, DefaultInlineBufferSize> ret; |
661 ret.append(IDBKeyPathTypeCodedByte1); | 664 ret.append(IDBKeyPathTypeCodedByte1); |
662 ret.append(IDBKeyPathTypeCodedByte2); | 665 ret.append(IDBKeyPathTypeCodedByte2); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 return IDBKeyPath(array); | 719 return IDBKeyPath(array); |
717 } | 720 } |
718 } | 721 } |
719 ASSERT_NOT_REACHED(); | 722 ASSERT_NOT_REACHED(); |
720 return IDBKeyPath(); | 723 return IDBKeyPath(); |
721 } | 724 } |
722 | 725 |
723 namespace { | 726 namespace { |
724 | 727 |
725 template<typename KeyType> | 728 template<typename KeyType> |
726 int compare(const LevelDBSlice& a, const LevelDBSlice& b, bool ignoreDuplicates
= false) | 729 int compare(const LevelDBSlice& a, const LevelDBSlice& b, bool ignoreDuplicates,
bool& ok) |
727 { | 730 { |
728 KeyType keyA; | 731 KeyType keyA; |
729 KeyType keyB; | 732 KeyType keyB; |
730 | 733 |
731 const char* ptrA = KeyType::decode(a.begin(), a.end(), &keyA); | 734 const char* ptrA = KeyType::decode(a.begin(), a.end(), &keyA); |
732 ASSERT_UNUSED(ptrA, ptrA); | 735 ASSERT(ptrA); |
| 736 if (!ptrA) { |
| 737 ok = false; |
| 738 return 0; |
| 739 } |
733 const char* ptrB = KeyType::decode(b.begin(), b.end(), &keyB); | 740 const char* ptrB = KeyType::decode(b.begin(), b.end(), &keyB); |
734 ASSERT_UNUSED(ptrB, ptrB); | 741 ASSERT(ptrB); |
| 742 if (!ptrB) { |
| 743 ok = false; |
| 744 return 0; |
| 745 } |
735 | 746 |
| 747 ok = true; |
736 return keyA.compare(keyB); | 748 return keyA.compare(keyB); |
737 } | 749 } |
738 | 750 |
739 template<> | 751 template<> |
740 int compare<ExistsEntryKey>(const LevelDBSlice& a, const LevelDBSlice& b, bool i
gnoreDuplicates) | 752 int compare<ExistsEntryKey>(const LevelDBSlice& a, const LevelDBSlice& b, bool i
gnoreDuplicates, bool& ok) |
741 { | 753 { |
742 KeyPrefix prefixA; | 754 KeyPrefix prefixA; |
743 KeyPrefix prefixB; | 755 KeyPrefix prefixB; |
744 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); | 756 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); |
745 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); | 757 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); |
746 ASSERT(ptrA); | 758 ASSERT(ptrA); |
747 ASSERT(ptrB); | 759 ASSERT(ptrB); |
748 ASSERT(prefixA.m_databaseId); | 760 ASSERT(prefixA.m_databaseId); |
749 ASSERT(prefixA.m_objectStoreId); | 761 ASSERT(prefixA.m_objectStoreId); |
750 ASSERT(prefixA.m_indexId == ExistsEntryKey::SpecialIndexNumber); | 762 ASSERT(prefixA.m_indexId == ExistsEntryKey::SpecialIndexNumber); |
751 ASSERT(prefixB.m_databaseId); | 763 ASSERT(prefixB.m_databaseId); |
752 ASSERT(prefixB.m_objectStoreId); | 764 ASSERT(prefixB.m_objectStoreId); |
753 ASSERT(prefixB.m_indexId == ExistsEntryKey::SpecialIndexNumber); | 765 ASSERT(prefixB.m_indexId == ExistsEntryKey::SpecialIndexNumber); |
754 ASSERT(ptrA != a.end()); | 766 ASSERT(ptrA != a.end()); |
755 ASSERT(ptrB != b.end()); | 767 ASSERT(ptrB != b.end()); |
756 // Prefixes are not compared - it is assumed this was already done. | 768 // Prefixes are not compared - it is assumed this was already done. |
757 ASSERT(!prefixA.compare(prefixB)); | 769 ASSERT(!prefixA.compare(prefixB)); |
758 | 770 |
759 return compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end()); | 771 return compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end(), ok); |
760 } | 772 } |
761 | 773 |
762 template<> | 774 template<> |
763 int compare<ObjectStoreDataKey>(const LevelDBSlice& a, const LevelDBSlice& b, bo
ol ignoreDuplicates) | 775 int compare<ObjectStoreDataKey>(const LevelDBSlice& a, const LevelDBSlice& b, bo
ol ignoreDuplicates, bool& ok) |
764 { | 776 { |
765 KeyPrefix prefixA; | 777 KeyPrefix prefixA; |
766 KeyPrefix prefixB; | 778 KeyPrefix prefixB; |
767 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); | 779 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); |
768 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); | 780 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); |
769 ASSERT(ptrA); | 781 ASSERT(ptrA); |
770 ASSERT(ptrB); | 782 ASSERT(ptrB); |
771 ASSERT(prefixA.m_databaseId); | 783 ASSERT(prefixA.m_databaseId); |
772 ASSERT(prefixA.m_objectStoreId); | 784 ASSERT(prefixA.m_objectStoreId); |
773 ASSERT(prefixA.m_indexId == ObjectStoreDataKey::SpecialIndexNumber); | 785 ASSERT(prefixA.m_indexId == ObjectStoreDataKey::SpecialIndexNumber); |
774 ASSERT(prefixB.m_databaseId); | 786 ASSERT(prefixB.m_databaseId); |
775 ASSERT(prefixB.m_objectStoreId); | 787 ASSERT(prefixB.m_objectStoreId); |
776 ASSERT(prefixB.m_indexId == ObjectStoreDataKey::SpecialIndexNumber); | 788 ASSERT(prefixB.m_indexId == ObjectStoreDataKey::SpecialIndexNumber); |
777 ASSERT(ptrA != a.end()); | 789 ASSERT(ptrA != a.end()); |
778 ASSERT(ptrB != b.end()); | 790 ASSERT(ptrB != b.end()); |
779 // Prefixes are not compared - it is assumed this was already done. | 791 // Prefixes are not compared - it is assumed this was already done. |
780 ASSERT(!prefixA.compare(prefixB)); | 792 ASSERT(!prefixA.compare(prefixB)); |
781 | 793 |
782 return compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end()); | 794 return compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end(), ok); |
783 } | 795 } |
784 | 796 |
785 template<> | 797 template<> |
786 int compare<IndexDataKey>(const LevelDBSlice& a, const LevelDBSlice& b, bool ign
oreDuplicates) | 798 int compare<IndexDataKey>(const LevelDBSlice& a, const LevelDBSlice& b, bool ign
oreDuplicates, bool& ok) |
787 { | 799 { |
788 KeyPrefix prefixA; | 800 KeyPrefix prefixA; |
789 KeyPrefix prefixB; | 801 KeyPrefix prefixB; |
790 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); | 802 const char* ptrA = KeyPrefix::decode(a.begin(), a.end(), &prefixA); |
791 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); | 803 const char* ptrB = KeyPrefix::decode(b.begin(), b.end(), &prefixB); |
792 ASSERT(ptrA); | 804 ASSERT(ptrA); |
793 ASSERT(ptrB); | 805 ASSERT(ptrB); |
794 ASSERT(prefixA.m_databaseId); | 806 ASSERT(prefixA.m_databaseId); |
795 ASSERT(prefixA.m_objectStoreId); | 807 ASSERT(prefixA.m_objectStoreId); |
796 ASSERT(prefixA.m_indexId >= MinimumIndexId); | 808 ASSERT(prefixA.m_indexId >= MinimumIndexId); |
797 ASSERT(prefixB.m_databaseId); | 809 ASSERT(prefixB.m_databaseId); |
798 ASSERT(prefixB.m_objectStoreId); | 810 ASSERT(prefixB.m_objectStoreId); |
799 ASSERT(prefixB.m_indexId >= MinimumIndexId); | 811 ASSERT(prefixB.m_indexId >= MinimumIndexId); |
800 ASSERT(ptrA != a.end()); | 812 ASSERT(ptrA != a.end()); |
801 ASSERT(ptrB != b.end()); | 813 ASSERT(ptrB != b.end()); |
802 // Prefixes are not compared - it is assumed this was already done. | 814 // Prefixes are not compared - it is assumed this was already done. |
803 ASSERT(!prefixA.compare(prefixB)); | 815 ASSERT(!prefixA.compare(prefixB)); |
804 | 816 |
805 // index key | 817 // index key |
806 if (int x = compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end())) | 818 int result = compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end(), ok); |
807 return x; | 819 if (!ok || result) |
| 820 return result; |
808 if (ignoreDuplicates) | 821 if (ignoreDuplicates) |
809 return 0; | 822 return 0; |
810 | 823 |
811 // sequence number [optional] | 824 // sequence number [optional] |
812 int64_t sequenceNumberA = -1; | 825 int64_t sequenceNumberA = -1; |
813 int64_t sequenceNumberB = -1; | 826 int64_t sequenceNumberB = -1; |
814 if (ptrA != a.end()) | 827 if (ptrA != a.end()) |
815 ptrA = decodeVarInt(ptrA, a.end(), sequenceNumberA); | 828 ptrA = decodeVarInt(ptrA, a.end(), sequenceNumberA); |
816 if (ptrB != b.end()) | 829 if (ptrB != b.end()) |
817 ptrB = decodeVarInt(ptrB, b.end(), sequenceNumberB); | 830 ptrB = decodeVarInt(ptrB, b.end(), sequenceNumberB); |
818 | 831 |
819 // primar key [optional] | 832 // primary key [optional] |
820 if (!ptrA || !ptrB) | 833 if (!ptrA || !ptrB) |
821 return 0; | 834 return 0; |
822 if (ptrA == a.end() && ptrB == b.end()) | 835 if (ptrA == a.end() && ptrB == b.end()) |
823 return 0; | 836 return 0; |
824 if (ptrA == a.end()) | 837 if (ptrA == a.end()) |
825 return -1; | 838 return -1; |
826 if (ptrB == b.end()) | 839 if (ptrB == b.end()) |
827 return 1; | 840 return 1; |
828 | 841 |
829 if (int x = compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end())) | 842 result = compareEncodedIDBKeys(ptrA, a.end(), ptrB, b.end(), ok); |
830 return x; | 843 if (!ok || result) |
| 844 return result; |
831 | 845 |
832 return compareInts(sequenceNumberA, sequenceNumberB); | 846 return compareInts(sequenceNumberA, sequenceNumberB); |
833 } | 847 } |
834 | 848 |
835 } | 849 int compare(const LevelDBSlice& a, const LevelDBSlice& b, bool indexKeys, bool&
ok) |
836 | |
837 int compare(const LevelDBSlice& a, const LevelDBSlice& b, bool indexKeys) | |
838 { | 850 { |
839 const char* ptrA = a.begin(); | 851 const char* ptrA = a.begin(); |
840 const char* ptrB = b.begin(); | 852 const char* ptrB = b.begin(); |
841 const char* endA = a.end(); | 853 const char* endA = a.end(); |
842 const char* endB = b.end(); | 854 const char* endB = b.end(); |
843 | 855 |
844 KeyPrefix prefixA; | 856 KeyPrefix prefixA; |
845 KeyPrefix prefixB; | 857 KeyPrefix prefixB; |
846 | 858 |
847 ptrA = KeyPrefix::decode(ptrA, endA, &prefixA); | 859 ptrA = KeyPrefix::decode(ptrA, endA, &prefixA); |
848 ptrB = KeyPrefix::decode(ptrB, endB, &prefixB); | 860 ptrB = KeyPrefix::decode(ptrB, endB, &prefixB); |
849 ASSERT(ptrA); | 861 ASSERT(ptrA); |
850 ASSERT(ptrB); | 862 ASSERT(ptrB); |
| 863 if (!ptrA || !ptrB) { |
| 864 ok = false; |
| 865 return 0; |
| 866 } |
851 | 867 |
| 868 ok = true; |
852 if (int x = prefixA.compare(prefixB)) | 869 if (int x = prefixA.compare(prefixB)) |
853 return x; | 870 return x; |
854 | 871 |
855 if (prefixA.type() == KeyPrefix::GlobalMetaData) { | 872 if (prefixA.type() == KeyPrefix::GlobalMetaData) { |
856 ASSERT(ptrA != endA); | 873 ASSERT(ptrA != endA); |
857 ASSERT(ptrB != endB); | 874 ASSERT(ptrB != endB); |
858 | 875 |
859 unsigned char typeByteA = *ptrA++; | 876 unsigned char typeByteA = *ptrA++; |
860 unsigned char typeByteB = *ptrB++; | 877 unsigned char typeByteB = *ptrB++; |
861 | 878 |
862 if (int x = typeByteA - typeByteB) | 879 if (int x = typeByteA - typeByteB) |
863 return x; | 880 return x; |
864 if (typeByteA < MaxSimpleGlobalMetaDataTypeByte) | 881 if (typeByteA < MaxSimpleGlobalMetaDataTypeByte) |
865 return 0; | 882 return 0; |
866 | 883 |
| 884 const bool ignoreDuplicates = false; |
867 if (typeByteA == DatabaseFreeListTypeByte) | 885 if (typeByteA == DatabaseFreeListTypeByte) |
868 return compare<DatabaseFreeListKey>(a, b); | 886 return compare<DatabaseFreeListKey>(a, b, ignoreDuplicates, ok); |
869 if (typeByteA == DatabaseNameTypeByte) | 887 if (typeByteA == DatabaseNameTypeByte) |
870 return compare<DatabaseNameKey>(a, b); | 888 return compare<DatabaseNameKey>(a, b, ignoreDuplicates, ok); |
871 } | 889 } |
872 | 890 |
873 if (prefixA.type() == KeyPrefix::DatabaseMetaData) { | 891 if (prefixA.type() == KeyPrefix::DatabaseMetaData) { |
874 ASSERT(ptrA != endA); | 892 ASSERT(ptrA != endA); |
875 ASSERT(ptrB != endB); | 893 ASSERT(ptrB != endB); |
876 | 894 |
877 unsigned char typeByteA = *ptrA++; | 895 unsigned char typeByteA = *ptrA++; |
878 unsigned char typeByteB = *ptrB++; | 896 unsigned char typeByteB = *ptrB++; |
879 | 897 |
880 if (int x = typeByteA - typeByteB) | 898 if (int x = typeByteA - typeByteB) |
881 return x; | 899 return x; |
882 if (typeByteA < DatabaseMetaDataKey::MaxSimpleMetaDataType) | 900 if (typeByteA < DatabaseMetaDataKey::MaxSimpleMetaDataType) |
883 return 0; | 901 return 0; |
884 | 902 |
| 903 const bool ignoreDuplicates = false; |
885 if (typeByteA == ObjectStoreMetaDataTypeByte) | 904 if (typeByteA == ObjectStoreMetaDataTypeByte) |
886 return compare<ObjectStoreMetaDataKey>(a, b); | 905 return compare<ObjectStoreMetaDataKey>(a, b, ignoreDuplicates, ok); |
887 if (typeByteA == IndexMetaDataTypeByte) | 906 if (typeByteA == IndexMetaDataTypeByte) |
888 return compare<IndexMetaDataKey>(a, b); | 907 return compare<IndexMetaDataKey>(a, b, ignoreDuplicates, ok); |
889 if (typeByteA == ObjectStoreFreeListTypeByte) | 908 if (typeByteA == ObjectStoreFreeListTypeByte) |
890 return compare<ObjectStoreFreeListKey>(a, b); | 909 return compare<ObjectStoreFreeListKey>(a, b, ignoreDuplicates, ok); |
891 if (typeByteA == IndexFreeListTypeByte) | 910 if (typeByteA == IndexFreeListTypeByte) |
892 return compare<IndexFreeListKey>(a, b); | 911 return compare<IndexFreeListKey>(a, b, ignoreDuplicates, ok); |
893 if (typeByteA == ObjectStoreNamesTypeByte) | 912 if (typeByteA == ObjectStoreNamesTypeByte) |
894 return compare<ObjectStoreNamesKey>(a, b); | 913 return compare<ObjectStoreNamesKey>(a, b, ignoreDuplicates, ok); |
895 if (typeByteA == IndexNamesKeyTypeByte) | 914 if (typeByteA == IndexNamesKeyTypeByte) |
896 return compare<IndexNamesKey>(a, b); | 915 return compare<IndexNamesKey>(a, b, ignoreDuplicates, ok); |
897 } | 916 } |
898 | 917 |
899 if (prefixA.type() == KeyPrefix::ObjectStoreData) { | 918 if (prefixA.type() == KeyPrefix::ObjectStoreData) { |
900 if (ptrA == endA && ptrB == endB) | 919 if (ptrA == endA && ptrB == endB) |
901 return 0; | 920 return 0; |
902 if (ptrA == endA) | 921 if (ptrA == endA) |
903 return -1; | 922 return -1; |
904 if (ptrB == endB) | 923 if (ptrB == endB) |
905 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. | 924 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. |
906 | 925 |
907 return compare<ObjectStoreDataKey>(a, b); | 926 const bool ignoreDuplicates = false; |
| 927 return compare<ObjectStoreDataKey>(a, b, ignoreDuplicates, ok); |
908 } | 928 } |
909 if (prefixA.type() == KeyPrefix::ExistsEntry) { | 929 if (prefixA.type() == KeyPrefix::ExistsEntry) { |
910 if (ptrA == endA && ptrB == endB) | 930 if (ptrA == endA && ptrB == endB) |
911 return 0; | 931 return 0; |
912 if (ptrA == endA) | 932 if (ptrA == endA) |
913 return -1; | 933 return -1; |
914 if (ptrB == endB) | 934 if (ptrB == endB) |
915 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. | 935 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. |
916 | 936 |
917 return compare<ExistsEntryKey>(a, b); | 937 const bool ignoreDuplicates = false; |
| 938 return compare<ExistsEntryKey>(a, b, ignoreDuplicates, ok); |
918 } | 939 } |
919 if (prefixA.type() == KeyPrefix::IndexData) { | 940 if (prefixA.type() == KeyPrefix::IndexData) { |
920 if (ptrA == endA && ptrB == endB) | 941 if (ptrA == endA && ptrB == endB) |
921 return 0; | 942 return 0; |
922 if (ptrA == endA) | 943 if (ptrA == endA) |
923 return -1; | 944 return -1; |
924 if (ptrB == endB) | 945 if (ptrB == endB) |
925 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. | 946 return 1; // FIXME: This case of non-existing user keys should not h
ave to be handled this way. |
926 | 947 |
927 bool ignoreDuplicates = indexKeys; | 948 bool ignoreDuplicates = indexKeys; |
928 return compare<IndexDataKey>(a, b, ignoreDuplicates); | 949 return compare<IndexDataKey>(a, b, ignoreDuplicates, ok); |
929 } | 950 } |
930 | 951 |
931 ASSERT_NOT_REACHED(); | 952 ASSERT_NOT_REACHED(); |
| 953 ok = false; |
932 return 0; | 954 return 0; |
933 } | 955 } |
934 | 956 |
| 957 } |
| 958 |
| 959 int compare(const LevelDBSlice& a, const LevelDBSlice& b, bool indexKeys) |
| 960 { |
| 961 bool ok; |
| 962 int result = compare(a, b, indexKeys, ok); |
| 963 ASSERT(ok); |
| 964 if (!ok) |
| 965 return 0; |
| 966 return result; |
| 967 } |
935 | 968 |
936 KeyPrefix::KeyPrefix() | 969 KeyPrefix::KeyPrefix() |
937 : m_databaseId(InvalidType) | 970 : m_databaseId(InvalidType) |
938 , m_objectStoreId(InvalidType) | 971 , m_objectStoreId(InvalidType) |
939 , m_indexId(InvalidType) | 972 , m_indexId(InvalidType) |
940 { | 973 { |
941 } | 974 } |
942 | 975 |
943 KeyPrefix::KeyPrefix(int64_t databaseId, int64_t objectStoreId, int64_t indexId) | 976 KeyPrefix::KeyPrefix(int64_t databaseId, int64_t objectStoreId, int64_t indexId) |
944 : m_databaseId(databaseId) | 977 : m_databaseId(databaseId) |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1529 ret.append(encodedUserKey); | 1562 ret.append(encodedUserKey); |
1530 | 1563 |
1531 return ret; | 1564 return ret; |
1532 } | 1565 } |
1533 | 1566 |
1534 Vector<char> ObjectStoreDataKey::encode(int64_t databaseId, int64_t objectStoreI
d, const IDBKey& userKey) | 1567 Vector<char> ObjectStoreDataKey::encode(int64_t databaseId, int64_t objectStoreI
d, const IDBKey& userKey) |
1535 { | 1568 { |
1536 return encode(databaseId, objectStoreId, encodeIDBKey(userKey)); | 1569 return encode(databaseId, objectStoreId, encodeIDBKey(userKey)); |
1537 } | 1570 } |
1538 | 1571 |
1539 int ObjectStoreDataKey::compare(const ObjectStoreDataKey& other) | 1572 int ObjectStoreDataKey::compare(const ObjectStoreDataKey& other, bool& ok) |
1540 { | 1573 { |
1541 return compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey); | 1574 return compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey, ok); |
1542 } | 1575 } |
1543 | 1576 |
1544 PassRefPtr<IDBKey> ObjectStoreDataKey::userKey() const | 1577 PassRefPtr<IDBKey> ObjectStoreDataKey::userKey() const |
1545 { | 1578 { |
1546 RefPtr<IDBKey> key; | 1579 RefPtr<IDBKey> key; |
1547 decodeIDBKey(m_encodedUserKey.begin(), m_encodedUserKey.end(), key); | 1580 decodeIDBKey(m_encodedUserKey.begin(), m_encodedUserKey.end(), key); |
1548 return key; | 1581 return key; |
1549 } | 1582 } |
1550 | 1583 |
1551 const int64_t ObjectStoreDataKey::SpecialIndexNumber = ObjectStoreDataIndexId; | 1584 const int64_t ObjectStoreDataKey::SpecialIndexNumber = ObjectStoreDataIndexId; |
(...skipping 18 matching lines...) Expand all Loading... |
1570 Vector<char> ret = prefix.encode(); | 1603 Vector<char> ret = prefix.encode(); |
1571 ret.append(encodedKey); | 1604 ret.append(encodedKey); |
1572 return ret; | 1605 return ret; |
1573 } | 1606 } |
1574 | 1607 |
1575 Vector<char> ExistsEntryKey::encode(int64_t databaseId, int64_t objectStoreId, c
onst IDBKey& userKey) | 1608 Vector<char> ExistsEntryKey::encode(int64_t databaseId, int64_t objectStoreId, c
onst IDBKey& userKey) |
1576 { | 1609 { |
1577 return encode(databaseId, objectStoreId, encodeIDBKey(userKey)); | 1610 return encode(databaseId, objectStoreId, encodeIDBKey(userKey)); |
1578 } | 1611 } |
1579 | 1612 |
1580 int ExistsEntryKey::compare(const ExistsEntryKey& other) | 1613 int ExistsEntryKey::compare(const ExistsEntryKey& other, bool& ok) |
1581 { | 1614 { |
1582 return compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey); | 1615 return compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey, ok); |
1583 } | 1616 } |
1584 | 1617 |
1585 PassRefPtr<IDBKey> ExistsEntryKey::userKey() const | 1618 PassRefPtr<IDBKey> ExistsEntryKey::userKey() const |
1586 { | 1619 { |
1587 RefPtr<IDBKey> key; | 1620 RefPtr<IDBKey> key; |
1588 decodeIDBKey(m_encodedUserKey.begin(), m_encodedUserKey.end(), key); | 1621 decodeIDBKey(m_encodedUserKey.begin(), m_encodedUserKey.end(), key); |
1589 return key; | 1622 return key; |
1590 } | 1623 } |
1591 | 1624 |
1592 const int64_t ExistsEntryKey::SpecialIndexNumber = ExistsEntryIndexId; | 1625 const int64_t ExistsEntryKey::SpecialIndexNumber = ExistsEntryIndexId; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1653 Vector<char> IndexDataKey::encodeMinKey(int64_t databaseId, int64_t objectStoreI
d, int64_t indexId) | 1686 Vector<char> IndexDataKey::encodeMinKey(int64_t databaseId, int64_t objectStoreI
d, int64_t indexId) |
1654 { | 1687 { |
1655 return encode(databaseId, objectStoreId, indexId, minIDBKey(), minIDBKey()); | 1688 return encode(databaseId, objectStoreId, indexId, minIDBKey(), minIDBKey()); |
1656 } | 1689 } |
1657 | 1690 |
1658 Vector<char> IndexDataKey::encodeMaxKey(int64_t databaseId, int64_t objectStoreI
d, int64_t indexId) | 1691 Vector<char> IndexDataKey::encodeMaxKey(int64_t databaseId, int64_t objectStoreI
d, int64_t indexId) |
1659 { | 1692 { |
1660 return encode(databaseId, objectStoreId, indexId, maxIDBKey(), maxIDBKey(),
INT64_MAX); | 1693 return encode(databaseId, objectStoreId, indexId, maxIDBKey(), maxIDBKey(),
INT64_MAX); |
1661 } | 1694 } |
1662 | 1695 |
1663 int IndexDataKey::compare(const IndexDataKey& other, bool ignoreDuplicates) | 1696 int IndexDataKey::compare(const IndexDataKey& other, bool ignoreDuplicates, bool
& ok) |
1664 { | 1697 { |
1665 ASSERT(m_databaseId >= 0); | 1698 ASSERT(m_databaseId >= 0); |
1666 ASSERT(m_objectStoreId >= 0); | 1699 ASSERT(m_objectStoreId >= 0); |
1667 ASSERT(m_indexId >= 0); | 1700 ASSERT(m_indexId >= 0); |
1668 if (int x = compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey)) | 1701 int result = compareEncodedIDBKeys(m_encodedUserKey, other.m_encodedUserKey,
ok); |
1669 return x; | 1702 if (!ok || result) |
| 1703 return result; |
1670 if (ignoreDuplicates) | 1704 if (ignoreDuplicates) |
1671 return 0; | 1705 return 0; |
1672 if (int x = compareEncodedIDBKeys(m_encodedPrimaryKey, other.m_encodedPrimar
yKey)) | 1706 result = compareEncodedIDBKeys(m_encodedPrimaryKey, other.m_encodedPrimaryKe
y, ok); |
1673 return x; | 1707 if (!ok || result) |
| 1708 return result; |
1674 return compareInts(m_sequenceNumber, other.m_sequenceNumber); | 1709 return compareInts(m_sequenceNumber, other.m_sequenceNumber); |
1675 } | 1710 } |
1676 | 1711 |
1677 int64_t IndexDataKey::databaseId() const | 1712 int64_t IndexDataKey::databaseId() const |
1678 { | 1713 { |
1679 ASSERT(m_databaseId >= 0); | 1714 ASSERT(m_databaseId >= 0); |
1680 return m_databaseId; | 1715 return m_databaseId; |
1681 } | 1716 } |
1682 | 1717 |
1683 int64_t IndexDataKey::objectStoreId() const | 1718 int64_t IndexDataKey::objectStoreId() const |
(...skipping 20 matching lines...) Expand all Loading... |
1704 RefPtr<IDBKey> key; | 1739 RefPtr<IDBKey> key; |
1705 decodeIDBKey(m_encodedPrimaryKey.begin(), m_encodedPrimaryKey.end(), key); | 1740 decodeIDBKey(m_encodedPrimaryKey.begin(), m_encodedPrimaryKey.end(), key); |
1706 return key; | 1741 return key; |
1707 } | 1742 } |
1708 | 1743 |
1709 } // namespace IDBLevelDBCoding | 1744 } // namespace IDBLevelDBCoding |
1710 } // namespace WebCore | 1745 } // namespace WebCore |
1711 | 1746 |
1712 #endif // USE(LEVELDB) | 1747 #endif // USE(LEVELDB) |
1713 #endif // ENABLE(INDEXED_DATABASE) | 1748 #endif // ENABLE(INDEXED_DATABASE) |
OLD | NEW |