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

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

Issue 1996443003: Return number of values deleted by IDBObjectStore.delete(range) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delete Range Created 4 years, 6 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
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_backing_store.h" 5 #include "content/browser/indexed_db/indexed_db_backing_store.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 1345 matching lines...) Expand 10 before | Expand all | Expand 10 after
1356 DatabaseMetaDataKey::Encode(row_id, DatabaseMetaDataKey::USER_VERSION), 1356 DatabaseMetaDataKey::Encode(row_id, DatabaseMetaDataKey::USER_VERSION),
1357 version); 1357 version);
1358 return true; 1358 return true;
1359 } 1359 }
1360 1360
1361 // If you're deleting a range that contains user keys that have blob info, this 1361 // If you're deleting a range that contains user keys that have blob info, this
1362 // won't clean up the blobs. 1362 // won't clean up the blobs.
1363 static leveldb::Status DeleteRangeBasic(LevelDBTransaction* transaction, 1363 static leveldb::Status DeleteRangeBasic(LevelDBTransaction* transaction,
1364 const std::string& begin, 1364 const std::string& begin,
1365 const std::string& end, 1365 const std::string& end,
1366 bool upper_open) { 1366 bool upper_open,
1367 size_t* delete_count) {
1368 DCHECK(delete_count);
1367 std::unique_ptr<LevelDBIterator> it = transaction->CreateIterator(); 1369 std::unique_ptr<LevelDBIterator> it = transaction->CreateIterator();
1368 leveldb::Status s; 1370 leveldb::Status s;
1371 *delete_count = 0;
1369 for (s = it->Seek(begin); s.ok() && it->IsValid() && 1372 for (s = it->Seek(begin); s.ok() && it->IsValid() &&
1370 (upper_open ? CompareKeys(it->Key(), end) < 0 1373 (upper_open ? CompareKeys(it->Key(), end) < 0
1371 : CompareKeys(it->Key(), end) <= 0); 1374 : CompareKeys(it->Key(), end) <= 0);
1372 s = it->Next()) 1375 s = it->Next()) {
1373 transaction->Remove(it->Key()); 1376 if (transaction->Remove(it->Key()))
1377 (*delete_count)++;
1378 }
1374 return s; 1379 return s;
1375 } 1380 }
1376 1381
1377 static leveldb::Status DeleteBlobsInRange( 1382 static leveldb::Status DeleteBlobsInRange(
1378 IndexedDBBackingStore::Transaction* transaction, 1383 IndexedDBBackingStore::Transaction* transaction,
1379 int64_t database_id, 1384 int64_t database_id,
1380 int64_t object_store_id, 1385 int64_t object_store_id,
1381 const std::string& start_key, 1386 const std::string& start_key,
1382 const std::string& end_key, 1387 const std::string& end_key,
1383 bool upper_open) { 1388 bool upper_open) {
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
1760 leveldb::Status IndexedDBBackingStore::DeleteObjectStore( 1765 leveldb::Status IndexedDBBackingStore::DeleteObjectStore(
1761 IndexedDBBackingStore::Transaction* transaction, 1766 IndexedDBBackingStore::Transaction* transaction,
1762 int64_t database_id, 1767 int64_t database_id,
1763 int64_t object_store_id) { 1768 int64_t object_store_id) {
1764 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore"); 1769 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore");
1765 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 1770 if (!KeyPrefix::ValidIds(database_id, object_store_id))
1766 return InvalidDBKeyStatus(); 1771 return InvalidDBKeyStatus();
1767 LevelDBTransaction* leveldb_transaction = transaction->transaction(); 1772 LevelDBTransaction* leveldb_transaction = transaction->transaction();
1768 1773
1769 base::string16 object_store_name; 1774 base::string16 object_store_name;
1775 size_t delete_count = 0;
1770 bool found = false; 1776 bool found = false;
1771 leveldb::Status s = 1777 leveldb::Status s =
1772 GetString(leveldb_transaction, 1778 GetString(leveldb_transaction,
1773 ObjectStoreMetaDataKey::Encode( 1779 ObjectStoreMetaDataKey::Encode(
1774 database_id, object_store_id, ObjectStoreMetaDataKey::NAME), 1780 database_id, object_store_id, ObjectStoreMetaDataKey::NAME),
1775 &object_store_name, 1781 &object_store_name,
1776 &found); 1782 &found);
1777 if (!s.ok()) { 1783 if (!s.ok()) {
1778 INTERNAL_READ_ERROR_UNTESTED(DELETE_OBJECT_STORE); 1784 INTERNAL_READ_ERROR_UNTESTED(DELETE_OBJECT_STORE);
1779 return s; 1785 return s;
1780 } 1786 }
1781 if (!found) { 1787 if (!found) {
1782 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE); 1788 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE);
1783 return InternalInconsistencyStatus(); 1789 return InternalInconsistencyStatus();
1784 } 1790 }
1785 1791
1786 s = DeleteBlobsInObjectStore(transaction, database_id, object_store_id); 1792 s = DeleteBlobsInObjectStore(transaction, database_id, object_store_id);
1787 if (!s.ok()) { 1793 if (!s.ok()) {
1788 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE); 1794 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE);
1789 return s; 1795 return s;
1790 } 1796 }
1791 1797
1792 s = DeleteRangeBasic( 1798 s = DeleteRangeBasic(
1793 leveldb_transaction, 1799 leveldb_transaction,
1794 ObjectStoreMetaDataKey::Encode(database_id, object_store_id, 0), 1800 ObjectStoreMetaDataKey::Encode(database_id, object_store_id, 0),
1795 ObjectStoreMetaDataKey::EncodeMaxKey(database_id, object_store_id), 1801 ObjectStoreMetaDataKey::EncodeMaxKey(database_id, object_store_id), true,
1796 true); 1802 &delete_count);
1797 1803
1798 if (s.ok()) { 1804 if (s.ok()) {
1799 leveldb_transaction->Remove( 1805 leveldb_transaction->Remove(
1800 ObjectStoreNamesKey::Encode(database_id, object_store_name)); 1806 ObjectStoreNamesKey::Encode(database_id, object_store_name));
1801 1807
1802 s = DeleteRangeBasic( 1808 s = DeleteRangeBasic(
1803 leveldb_transaction, 1809 leveldb_transaction,
1804 IndexFreeListKey::Encode(database_id, object_store_id, 0), 1810 IndexFreeListKey::Encode(database_id, object_store_id, 0),
1805 IndexFreeListKey::EncodeMaxKey(database_id, object_store_id), 1811 IndexFreeListKey::EncodeMaxKey(database_id, object_store_id), true,
1806 true); 1812 &delete_count);
1807 } 1813 }
1808 1814
1809 if (s.ok()) { 1815 if (s.ok()) {
1810 s = DeleteRangeBasic( 1816 s = DeleteRangeBasic(
1811 leveldb_transaction, 1817 leveldb_transaction,
1812 IndexMetaDataKey::Encode(database_id, object_store_id, 0, 0), 1818 IndexMetaDataKey::Encode(database_id, object_store_id, 0, 0),
1813 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id), 1819 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id), true,
1814 true); 1820 &delete_count);
1815 } 1821 }
1816 1822
1817 if (!s.ok()) { 1823 if (!s.ok()) {
1818 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_OBJECT_STORE); 1824 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_OBJECT_STORE);
1819 return s; 1825 return s;
1820 } 1826 }
1821 1827
1822 return ClearObjectStore(transaction, database_id, object_store_id); 1828 return ClearObjectStore(transaction, database_id, object_store_id);
1823 } 1829 }
1824 1830
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 IndexedDBBackingStore::Transaction* transaction, 1954 IndexedDBBackingStore::Transaction* transaction,
1949 int64_t database_id, 1955 int64_t database_id,
1950 int64_t object_store_id) { 1956 int64_t object_store_id) {
1951 IDB_TRACE("IndexedDBBackingStore::ClearObjectStore"); 1957 IDB_TRACE("IndexedDBBackingStore::ClearObjectStore");
1952 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 1958 if (!KeyPrefix::ValidIds(database_id, object_store_id))
1953 return InvalidDBKeyStatus(); 1959 return InvalidDBKeyStatus();
1954 const std::string start_key = 1960 const std::string start_key =
1955 KeyPrefix(database_id, object_store_id).Encode(); 1961 KeyPrefix(database_id, object_store_id).Encode();
1956 const std::string stop_key = 1962 const std::string stop_key =
1957 KeyPrefix(database_id, object_store_id + 1).Encode(); 1963 KeyPrefix(database_id, object_store_id + 1).Encode();
1958 1964 size_t delete_count = 0;
1959 leveldb::Status s = 1965 leveldb::Status s = DeleteRangeBasic(transaction->transaction(), start_key,
1960 DeleteRangeBasic(transaction->transaction(), start_key, stop_key, true); 1966 stop_key, true, &delete_count);
1961 if (!s.ok()) { 1967 if (!s.ok()) {
1962 INTERNAL_WRITE_ERROR(CLEAR_OBJECT_STORE); 1968 INTERNAL_WRITE_ERROR(CLEAR_OBJECT_STORE);
1963 return s; 1969 return s;
1964 } 1970 }
1965 return DeleteBlobsInObjectStore(transaction, database_id, object_store_id); 1971 return DeleteBlobsInObjectStore(transaction, database_id, object_store_id);
1966 } 1972 }
1967 1973
1968 leveldb::Status IndexedDBBackingStore::DeleteRecord( 1974 leveldb::Status IndexedDBBackingStore::DeleteRecord(
1969 IndexedDBBackingStore::Transaction* transaction, 1975 IndexedDBBackingStore::Transaction* transaction,
1970 int64_t database_id, 1976 int64_t database_id,
(...skipping 15 matching lines...) Expand all
1986 const std::string exists_entry_key = ExistsEntryKey::Encode( 1992 const std::string exists_entry_key = ExistsEntryKey::Encode(
1987 database_id, object_store_id, record_identifier.primary_key()); 1993 database_id, object_store_id, record_identifier.primary_key());
1988 leveldb_transaction->Remove(exists_entry_key); 1994 leveldb_transaction->Remove(exists_entry_key);
1989 return leveldb::Status::OK(); 1995 return leveldb::Status::OK();
1990 } 1996 }
1991 1997
1992 leveldb::Status IndexedDBBackingStore::DeleteRange( 1998 leveldb::Status IndexedDBBackingStore::DeleteRange(
1993 IndexedDBBackingStore::Transaction* transaction, 1999 IndexedDBBackingStore::Transaction* transaction,
1994 int64_t database_id, 2000 int64_t database_id,
1995 int64_t object_store_id, 2001 int64_t object_store_id,
1996 const IndexedDBKeyRange& key_range) { 2002 const IndexedDBKeyRange& key_range,
2003 size_t* exists_delete_count) {
2004 DCHECK(exists_delete_count);
1997 leveldb::Status s; 2005 leveldb::Status s;
2006 *exists_delete_count = 0;
1998 std::unique_ptr<IndexedDBBackingStore::Cursor> start_cursor = 2007 std::unique_ptr<IndexedDBBackingStore::Cursor> start_cursor =
1999 OpenObjectStoreCursor(transaction, database_id, object_store_id, 2008 OpenObjectStoreCursor(transaction, database_id, object_store_id,
2000 key_range, blink::WebIDBCursorDirectionNext, &s); 2009 key_range, blink::WebIDBCursorDirectionNext, &s);
2001 if (!s.ok()) 2010 if (!s.ok())
2002 return s; 2011 return s;
2003 if (!start_cursor) 2012 if (!start_cursor)
2004 return leveldb::Status::OK(); // Empty range == delete success. 2013 return leveldb::Status::OK(); // Empty range == delete success.
2005
2006 std::unique_ptr<IndexedDBBackingStore::Cursor> end_cursor = 2014 std::unique_ptr<IndexedDBBackingStore::Cursor> end_cursor =
2007 OpenObjectStoreCursor(transaction, database_id, object_store_id, 2015 OpenObjectStoreCursor(transaction, database_id, object_store_id,
2008 key_range, blink::WebIDBCursorDirectionPrev, &s); 2016 key_range, blink::WebIDBCursorDirectionPrev, &s);
2009 2017
2010 if (!s.ok()) 2018 if (!s.ok())
2011 return s; 2019 return s;
2012 if (!end_cursor) 2020 if (!end_cursor)
2013 return leveldb::Status::OK(); // Empty range == delete success. 2021 return leveldb::Status::OK(); // Empty range == delete success.
2014 2022
2015 BlobEntryKey start_blob_key, end_blob_key; 2023 BlobEntryKey start_blob_key, end_blob_key;
2016
2017 std::string start_key = ObjectStoreDataKey::Encode( 2024 std::string start_key = ObjectStoreDataKey::Encode(
2018 database_id, object_store_id, start_cursor->key()); 2025 database_id, object_store_id, start_cursor->key());
2019 base::StringPiece start_key_piece(start_key); 2026 base::StringPiece start_key_piece(start_key);
2020 if (!BlobEntryKey::FromObjectStoreDataKey(&start_key_piece, &start_blob_key)) 2027 if (!BlobEntryKey::FromObjectStoreDataKey(&start_key_piece, &start_blob_key))
2021 return InternalInconsistencyStatus(); 2028 return InternalInconsistencyStatus();
2022 std::string stop_key = ObjectStoreDataKey::Encode( 2029 std::string stop_key = ObjectStoreDataKey::Encode(
2023 database_id, object_store_id, end_cursor->key()); 2030 database_id, object_store_id, end_cursor->key());
2024 base::StringPiece stop_key_piece(stop_key); 2031 base::StringPiece stop_key_piece(stop_key);
2025 if (!BlobEntryKey::FromObjectStoreDataKey(&stop_key_piece, &end_blob_key)) 2032 if (!BlobEntryKey::FromObjectStoreDataKey(&stop_key_piece, &end_blob_key))
2026 return InternalInconsistencyStatus(); 2033 return InternalInconsistencyStatus();
2027 2034
2028 s = DeleteBlobsInRange(transaction, 2035 s = DeleteBlobsInRange(transaction,
2029 database_id, 2036 database_id,
2030 object_store_id, 2037 object_store_id,
2031 start_blob_key.Encode(), 2038 start_blob_key.Encode(),
2032 end_blob_key.Encode(), 2039 end_blob_key.Encode(),
2033 false); 2040 false);
2034 if (!s.ok()) 2041 if (!s.ok())
2035 return s; 2042 return s;
2036 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false); 2043 size_t data_delete_count = 0;
2044 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false,
2045 &data_delete_count);
2037 if (!s.ok()) 2046 if (!s.ok())
2038 return s; 2047 return s;
2039 start_key = 2048 start_key =
2040 ExistsEntryKey::Encode(database_id, object_store_id, start_cursor->key()); 2049 ExistsEntryKey::Encode(database_id, object_store_id, start_cursor->key());
2041 stop_key = 2050 stop_key =
2042 ExistsEntryKey::Encode(database_id, object_store_id, end_cursor->key()); 2051 ExistsEntryKey::Encode(database_id, object_store_id, end_cursor->key());
2043 return DeleteRangeBasic( 2052
2044 transaction->transaction(), start_key, stop_key, false); 2053 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false,
2054 exists_delete_count);
2055 DCHECK_EQ(data_delete_count, *exists_delete_count);
2056 return s;
2045 } 2057 }
2046 2058
2047 leveldb::Status IndexedDBBackingStore::GetKeyGeneratorCurrentNumber( 2059 leveldb::Status IndexedDBBackingStore::GetKeyGeneratorCurrentNumber(
2048 IndexedDBBackingStore::Transaction* transaction, 2060 IndexedDBBackingStore::Transaction* transaction,
2049 int64_t database_id, 2061 int64_t database_id,
2050 int64_t object_store_id, 2062 int64_t object_store_id,
2051 int64_t* key_generator_current_number) { 2063 int64_t* key_generator_current_number) {
2052 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 2064 if (!KeyPrefix::ValidIds(database_id, object_store_id))
2053 return InvalidDBKeyStatus(); 2065 return InvalidDBKeyStatus();
2054 LevelDBTransaction* leveldb_transaction = transaction->transaction(); 2066 LevelDBTransaction* leveldb_transaction = transaction->transaction();
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
2828 leveldb::Status IndexedDBBackingStore::DeleteIndex( 2840 leveldb::Status IndexedDBBackingStore::DeleteIndex(
2829 IndexedDBBackingStore::Transaction* transaction, 2841 IndexedDBBackingStore::Transaction* transaction,
2830 int64_t database_id, 2842 int64_t database_id,
2831 int64_t object_store_id, 2843 int64_t object_store_id,
2832 int64_t index_id) { 2844 int64_t index_id) {
2833 IDB_TRACE("IndexedDBBackingStore::DeleteIndex"); 2845 IDB_TRACE("IndexedDBBackingStore::DeleteIndex");
2834 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id)) 2846 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id))
2835 return InvalidDBKeyStatus(); 2847 return InvalidDBKeyStatus();
2836 LevelDBTransaction* leveldb_transaction = transaction->transaction(); 2848 LevelDBTransaction* leveldb_transaction = transaction->transaction();
2837 2849
2850 size_t delete_count = 0;
2838 const std::string index_meta_data_start = 2851 const std::string index_meta_data_start =
2839 IndexMetaDataKey::Encode(database_id, object_store_id, index_id, 0); 2852 IndexMetaDataKey::Encode(database_id, object_store_id, index_id, 0);
2840 const std::string index_meta_data_end = 2853 const std::string index_meta_data_end =
2841 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id, index_id); 2854 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id, index_id);
2842 leveldb::Status s = DeleteRangeBasic( 2855 leveldb::Status s =
2843 leveldb_transaction, index_meta_data_start, index_meta_data_end, true); 2856 DeleteRangeBasic(leveldb_transaction, index_meta_data_start,
2857 index_meta_data_end, true, &delete_count);
2844 2858
2845 if (s.ok()) { 2859 if (s.ok()) {
2846 const std::string index_data_start = 2860 const std::string index_data_start =
2847 IndexDataKey::EncodeMinKey(database_id, object_store_id, index_id); 2861 IndexDataKey::EncodeMinKey(database_id, object_store_id, index_id);
2848 const std::string index_data_end = 2862 const std::string index_data_end =
2849 IndexDataKey::EncodeMaxKey(database_id, object_store_id, index_id); 2863 IndexDataKey::EncodeMaxKey(database_id, object_store_id, index_id);
2850 s = DeleteRangeBasic( 2864 s = DeleteRangeBasic(leveldb_transaction, index_data_start, index_data_end,
2851 leveldb_transaction, index_data_start, index_data_end, true); 2865 true, &delete_count);
2852 } 2866 }
2853 2867
2854 if (!s.ok()) 2868 if (!s.ok())
2855 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_INDEX); 2869 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_INDEX);
2856 2870
2857 return s; 2871 return s;
2858 } 2872 }
2859 2873
2860 leveldb::Status IndexedDBBackingStore::PutIndexDataForRecord( 2874 leveldb::Status IndexedDBBackingStore::PutIndexDataForRecord(
2861 IndexedDBBackingStore::Transaction* transaction, 2875 IndexedDBBackingStore::Transaction* transaction,
(...skipping 1597 matching lines...) Expand 10 before | Expand all | Expand 10 after
4459 4473
4460 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor( 4474 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor(
4461 const WriteDescriptor& other) = default; 4475 const WriteDescriptor& other) = default;
4462 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() = 4476 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() =
4463 default; 4477 default;
4464 IndexedDBBackingStore::Transaction::WriteDescriptor& 4478 IndexedDBBackingStore::Transaction::WriteDescriptor&
4465 IndexedDBBackingStore::Transaction::WriteDescriptor:: 4479 IndexedDBBackingStore::Transaction::WriteDescriptor::
4466 operator=(const WriteDescriptor& other) = default; 4480 operator=(const WriteDescriptor& other) = default;
4467 4481
4468 } // namespace content 4482 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_backing_store.h ('k') | content/browser/indexed_db/indexed_db_backing_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698