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

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

Issue 2708223002: IndexedDB: Optimize range deletion operations (e.g. clearing a store) (Closed)
Patch Set: Created 3 years, 10 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 681 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 PutInt(transaction, MaxDatabaseIdKey::Encode(), database_id); 692 PutInt(transaction, MaxDatabaseIdKey::Encode(), database_id);
693 *new_id = database_id; 693 *new_id = database_id;
694 return Status::OK(); 694 return Status::OK();
695 } 695 }
696 696
697 // If you're deleting a range that contains user keys that have blob info, this 697 // If you're deleting a range that contains user keys that have blob info, this
698 // won't clean up the blobs. 698 // won't clean up the blobs.
699 Status DeleteRangeBasic(LevelDBTransaction* transaction, 699 Status DeleteRangeBasic(LevelDBTransaction* transaction,
700 const std::string& begin, 700 const std::string& begin,
701 const std::string& end, 701 const std::string& end,
702 bool upper_open, 702 bool upper_open) {
703 size_t* delete_count) { 703 return transaction->RemoveRange(begin, end, upper_open);
704 DCHECK(delete_count);
705 std::unique_ptr<LevelDBIterator> it = transaction->CreateIterator();
706 Status s;
707 *delete_count = 0;
708 for (s = it->Seek(begin); s.ok() && it->IsValid() &&
709 (upper_open ? CompareKeys(it->Key(), end) < 0
710 : CompareKeys(it->Key(), end) <= 0);
711 s = it->Next()) {
712 if (transaction->Remove(it->Key()))
713 (*delete_count)++;
714 }
715 return s;
716 } 704 }
717 705
718 Status DeleteBlobsInRange(IndexedDBBackingStore::Transaction* transaction, 706 Status DeleteBlobsInRange(IndexedDBBackingStore::Transaction* transaction,
719 int64_t database_id, 707 int64_t database_id,
720 int64_t object_store_id, 708 int64_t object_store_id,
721 const std::string& start_key, 709 const std::string& start_key,
722 const std::string& end_key, 710 const std::string& end_key,
723 bool upper_open) { 711 bool upper_open) {
724 std::unique_ptr<LevelDBIterator> it = 712 std::unique_ptr<LevelDBIterator> it =
725 transaction->transaction()->CreateIterator(); 713 transaction->transaction()->CreateIterator();
(...skipping 1267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1993 Status IndexedDBBackingStore::DeleteObjectStore( 1981 Status IndexedDBBackingStore::DeleteObjectStore(
1994 IndexedDBBackingStore::Transaction* transaction, 1982 IndexedDBBackingStore::Transaction* transaction,
1995 int64_t database_id, 1983 int64_t database_id,
1996 int64_t object_store_id) { 1984 int64_t object_store_id) {
1997 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore"); 1985 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore");
1998 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 1986 if (!KeyPrefix::ValidIds(database_id, object_store_id))
1999 return InvalidDBKeyStatus(); 1987 return InvalidDBKeyStatus();
2000 LevelDBTransaction* leveldb_transaction = transaction->transaction(); 1988 LevelDBTransaction* leveldb_transaction = transaction->transaction();
2001 1989
2002 base::string16 object_store_name; 1990 base::string16 object_store_name;
2003 size_t delete_count = 0;
2004 bool found = false; 1991 bool found = false;
2005 Status s = GetString(leveldb_transaction, ObjectStoreMetaDataKey::Encode( 1992 Status s = GetString(leveldb_transaction, ObjectStoreMetaDataKey::Encode(
2006 database_id, object_store_id, 1993 database_id, object_store_id,
2007 ObjectStoreMetaDataKey::NAME), 1994 ObjectStoreMetaDataKey::NAME),
2008 &object_store_name, &found); 1995 &object_store_name, &found);
2009 if (!s.ok()) { 1996 if (!s.ok()) {
2010 INTERNAL_READ_ERROR_UNTESTED(DELETE_OBJECT_STORE); 1997 INTERNAL_READ_ERROR_UNTESTED(DELETE_OBJECT_STORE);
2011 return s; 1998 return s;
2012 } 1999 }
2013 if (!found) { 2000 if (!found) {
2014 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE); 2001 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE);
2015 return InternalInconsistencyStatus(); 2002 return InternalInconsistencyStatus();
2016 } 2003 }
2017 2004
2018 s = DeleteBlobsInObjectStore(transaction, database_id, object_store_id); 2005 s = DeleteBlobsInObjectStore(transaction, database_id, object_store_id);
2019 if (!s.ok()) { 2006 if (!s.ok()) {
2020 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE); 2007 INTERNAL_CONSISTENCY_ERROR_UNTESTED(DELETE_OBJECT_STORE);
2021 return s; 2008 return s;
2022 } 2009 }
2023 2010
2024 s = DeleteRangeBasic( 2011 s = DeleteRangeBasic(
2025 leveldb_transaction, 2012 leveldb_transaction,
2026 ObjectStoreMetaDataKey::Encode(database_id, object_store_id, 0), 2013 ObjectStoreMetaDataKey::Encode(database_id, object_store_id, 0),
2027 ObjectStoreMetaDataKey::EncodeMaxKey(database_id, object_store_id), true, 2014 ObjectStoreMetaDataKey::EncodeMaxKey(database_id, object_store_id), true);
2028 &delete_count);
2029 2015
2030 if (s.ok()) { 2016 if (s.ok()) {
2031 leveldb_transaction->Remove( 2017 leveldb_transaction->Remove(
2032 ObjectStoreNamesKey::Encode(database_id, object_store_name)); 2018 ObjectStoreNamesKey::Encode(database_id, object_store_name));
2033 2019
2034 s = DeleteRangeBasic( 2020 s = DeleteRangeBasic(
2035 leveldb_transaction, 2021 leveldb_transaction,
2036 IndexFreeListKey::Encode(database_id, object_store_id, 0), 2022 IndexFreeListKey::Encode(database_id, object_store_id, 0),
2037 IndexFreeListKey::EncodeMaxKey(database_id, object_store_id), true, 2023 IndexFreeListKey::EncodeMaxKey(database_id, object_store_id), true);
2038 &delete_count);
2039 } 2024 }
2040 2025
2041 if (s.ok()) { 2026 if (s.ok()) {
2042 s = DeleteRangeBasic( 2027 s = DeleteRangeBasic(
2043 leveldb_transaction, 2028 leveldb_transaction,
2044 IndexMetaDataKey::Encode(database_id, object_store_id, 0, 0), 2029 IndexMetaDataKey::Encode(database_id, object_store_id, 0, 0),
2045 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id), true, 2030 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id), true);
2046 &delete_count);
2047 } 2031 }
2048 2032
2049 if (!s.ok()) { 2033 if (!s.ok()) {
2050 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_OBJECT_STORE); 2034 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_OBJECT_STORE);
2051 return s; 2035 return s;
2052 } 2036 }
2053 2037
2054 return ClearObjectStore(transaction, database_id, object_store_id); 2038 return ClearObjectStore(transaction, database_id, object_store_id);
2055 } 2039 }
2056 2040
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
2183 IndexedDBBackingStore::Transaction* transaction, 2167 IndexedDBBackingStore::Transaction* transaction,
2184 int64_t database_id, 2168 int64_t database_id,
2185 int64_t object_store_id) { 2169 int64_t object_store_id) {
2186 IDB_TRACE("IndexedDBBackingStore::ClearObjectStore"); 2170 IDB_TRACE("IndexedDBBackingStore::ClearObjectStore");
2187 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 2171 if (!KeyPrefix::ValidIds(database_id, object_store_id))
2188 return InvalidDBKeyStatus(); 2172 return InvalidDBKeyStatus();
2189 const std::string start_key = 2173 const std::string start_key =
2190 KeyPrefix(database_id, object_store_id).Encode(); 2174 KeyPrefix(database_id, object_store_id).Encode();
2191 const std::string stop_key = 2175 const std::string stop_key =
2192 KeyPrefix(database_id, object_store_id + 1).Encode(); 2176 KeyPrefix(database_id, object_store_id + 1).Encode();
2193 size_t delete_count = 0; 2177 Status s =
2194 Status s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, 2178 DeleteRangeBasic(transaction->transaction(), start_key, stop_key, true);
2195 true, &delete_count);
2196 if (!s.ok()) { 2179 if (!s.ok()) {
2197 INTERNAL_WRITE_ERROR(CLEAR_OBJECT_STORE); 2180 INTERNAL_WRITE_ERROR(CLEAR_OBJECT_STORE);
2198 return s; 2181 return s;
2199 } 2182 }
2200 return DeleteBlobsInObjectStore(transaction, database_id, object_store_id); 2183 return DeleteBlobsInObjectStore(transaction, database_id, object_store_id);
2201 } 2184 }
2202 2185
2203 Status IndexedDBBackingStore::DeleteRecord( 2186 Status IndexedDBBackingStore::DeleteRecord(
2204 IndexedDBBackingStore::Transaction* transaction, 2187 IndexedDBBackingStore::Transaction* transaction,
2205 int64_t database_id, 2188 int64_t database_id,
(...skipping 15 matching lines...) Expand all
2221 const std::string exists_entry_key = ExistsEntryKey::Encode( 2204 const std::string exists_entry_key = ExistsEntryKey::Encode(
2222 database_id, object_store_id, record_identifier.primary_key()); 2205 database_id, object_store_id, record_identifier.primary_key());
2223 leveldb_transaction->Remove(exists_entry_key); 2206 leveldb_transaction->Remove(exists_entry_key);
2224 return Status::OK(); 2207 return Status::OK();
2225 } 2208 }
2226 2209
2227 Status IndexedDBBackingStore::DeleteRange( 2210 Status IndexedDBBackingStore::DeleteRange(
2228 IndexedDBBackingStore::Transaction* transaction, 2211 IndexedDBBackingStore::Transaction* transaction,
2229 int64_t database_id, 2212 int64_t database_id,
2230 int64_t object_store_id, 2213 int64_t object_store_id,
2231 const IndexedDBKeyRange& key_range, 2214 const IndexedDBKeyRange& key_range) {
2232 size_t* exists_delete_count) {
2233 DCHECK(exists_delete_count);
2234 Status s; 2215 Status s;
2235 *exists_delete_count = 0;
2236 std::unique_ptr<IndexedDBBackingStore::Cursor> start_cursor = 2216 std::unique_ptr<IndexedDBBackingStore::Cursor> start_cursor =
2237 OpenObjectStoreCursor(transaction, database_id, object_store_id, 2217 OpenObjectStoreCursor(transaction, database_id, object_store_id,
2238 key_range, blink::WebIDBCursorDirectionNext, &s); 2218 key_range, blink::WebIDBCursorDirectionNext, &s);
2239 if (!s.ok()) 2219 if (!s.ok())
2240 return s; 2220 return s;
2241 if (!start_cursor) 2221 if (!start_cursor)
2242 return Status::OK(); // Empty range == delete success. 2222 return Status::OK(); // Empty range == delete success.
2243 std::unique_ptr<IndexedDBBackingStore::Cursor> end_cursor = 2223 std::unique_ptr<IndexedDBBackingStore::Cursor> end_cursor =
2244 OpenObjectStoreCursor(transaction, database_id, object_store_id, 2224 OpenObjectStoreCursor(transaction, database_id, object_store_id,
2245 key_range, blink::WebIDBCursorDirectionPrev, &s); 2225 key_range, blink::WebIDBCursorDirectionPrev, &s);
(...skipping 16 matching lines...) Expand all
2262 return InternalInconsistencyStatus(); 2242 return InternalInconsistencyStatus();
2263 2243
2264 s = DeleteBlobsInRange(transaction, 2244 s = DeleteBlobsInRange(transaction,
2265 database_id, 2245 database_id,
2266 object_store_id, 2246 object_store_id,
2267 start_blob_key.Encode(), 2247 start_blob_key.Encode(),
2268 end_blob_key.Encode(), 2248 end_blob_key.Encode(),
2269 false); 2249 false);
2270 if (!s.ok()) 2250 if (!s.ok())
2271 return s; 2251 return s;
2272 size_t data_delete_count = 0; 2252 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false);
2273 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false,
2274 &data_delete_count);
2275 if (!s.ok()) 2253 if (!s.ok())
2276 return s; 2254 return s;
2277 start_key = 2255 start_key =
2278 ExistsEntryKey::Encode(database_id, object_store_id, start_cursor->key()); 2256 ExistsEntryKey::Encode(database_id, object_store_id, start_cursor->key());
2279 stop_key = 2257 stop_key =
2280 ExistsEntryKey::Encode(database_id, object_store_id, end_cursor->key()); 2258 ExistsEntryKey::Encode(database_id, object_store_id, end_cursor->key());
2281 2259
2282 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false, 2260 s = DeleteRangeBasic(transaction->transaction(), start_key, stop_key, false);
2283 exists_delete_count);
2284 DCHECK_EQ(data_delete_count, *exists_delete_count);
2285 return s; 2261 return s;
2286 } 2262 }
2287 2263
2288 Status IndexedDBBackingStore::GetKeyGeneratorCurrentNumber( 2264 Status IndexedDBBackingStore::GetKeyGeneratorCurrentNumber(
2289 IndexedDBBackingStore::Transaction* transaction, 2265 IndexedDBBackingStore::Transaction* transaction,
2290 int64_t database_id, 2266 int64_t database_id,
2291 int64_t object_store_id, 2267 int64_t object_store_id,
2292 int64_t* key_generator_current_number) { 2268 int64_t* key_generator_current_number) {
2293 if (!KeyPrefix::ValidIds(database_id, object_store_id)) 2269 if (!KeyPrefix::ValidIds(database_id, object_store_id))
2294 return InvalidDBKeyStatus(); 2270 return InvalidDBKeyStatus();
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
3022 Status IndexedDBBackingStore::DeleteIndex( 2998 Status IndexedDBBackingStore::DeleteIndex(
3023 IndexedDBBackingStore::Transaction* transaction, 2999 IndexedDBBackingStore::Transaction* transaction,
3024 int64_t database_id, 3000 int64_t database_id,
3025 int64_t object_store_id, 3001 int64_t object_store_id,
3026 int64_t index_id) { 3002 int64_t index_id) {
3027 IDB_TRACE("IndexedDBBackingStore::DeleteIndex"); 3003 IDB_TRACE("IndexedDBBackingStore::DeleteIndex");
3028 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id)) 3004 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id))
3029 return InvalidDBKeyStatus(); 3005 return InvalidDBKeyStatus();
3030 LevelDBTransaction* leveldb_transaction = transaction->transaction(); 3006 LevelDBTransaction* leveldb_transaction = transaction->transaction();
3031 3007
3032 size_t delete_count = 0;
3033 const std::string index_meta_data_start = 3008 const std::string index_meta_data_start =
3034 IndexMetaDataKey::Encode(database_id, object_store_id, index_id, 0); 3009 IndexMetaDataKey::Encode(database_id, object_store_id, index_id, 0);
3035 const std::string index_meta_data_end = 3010 const std::string index_meta_data_end =
3036 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id, index_id); 3011 IndexMetaDataKey::EncodeMaxKey(database_id, object_store_id, index_id);
3037 Status s = DeleteRangeBasic(leveldb_transaction, index_meta_data_start, 3012 Status s = DeleteRangeBasic(leveldb_transaction, index_meta_data_start,
3038 index_meta_data_end, true, &delete_count); 3013 index_meta_data_end, true);
3039 3014
3040 if (s.ok()) { 3015 if (s.ok()) {
3041 const std::string index_data_start = 3016 const std::string index_data_start =
3042 IndexDataKey::EncodeMinKey(database_id, object_store_id, index_id); 3017 IndexDataKey::EncodeMinKey(database_id, object_store_id, index_id);
3043 const std::string index_data_end = 3018 const std::string index_data_end =
3044 IndexDataKey::EncodeMaxKey(database_id, object_store_id, index_id); 3019 IndexDataKey::EncodeMaxKey(database_id, object_store_id, index_id);
3045 s = DeleteRangeBasic(leveldb_transaction, index_data_start, index_data_end, 3020 s = DeleteRangeBasic(leveldb_transaction, index_data_start, index_data_end,
3046 true, &delete_count); 3021 true);
3047 } 3022 }
3048 3023
3049 if (!s.ok()) 3024 if (!s.ok())
3050 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_INDEX); 3025 INTERNAL_WRITE_ERROR_UNTESTED(DELETE_INDEX);
3051 3026
3052 return s; 3027 return s;
3053 } 3028 }
3054 3029
3055 Status IndexedDBBackingStore::RenameIndex( 3030 Status IndexedDBBackingStore::RenameIndex(
3056 IndexedDBBackingStore::Transaction* transaction, 3031 IndexedDBBackingStore::Transaction* transaction,
(...skipping 1418 matching lines...) Expand 10 before | Expand all | Expand 10 after
4475 4450
4476 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor( 4451 IndexedDBBackingStore::Transaction::WriteDescriptor::WriteDescriptor(
4477 const WriteDescriptor& other) = default; 4452 const WriteDescriptor& other) = default;
4478 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() = 4453 IndexedDBBackingStore::Transaction::WriteDescriptor::~WriteDescriptor() =
4479 default; 4454 default;
4480 IndexedDBBackingStore::Transaction::WriteDescriptor& 4455 IndexedDBBackingStore::Transaction::WriteDescriptor&
4481 IndexedDBBackingStore::Transaction::WriteDescriptor:: 4456 IndexedDBBackingStore::Transaction::WriteDescriptor::
4482 operator=(const WriteDescriptor& other) = default; 4457 operator=(const WriteDescriptor& other) = default;
4483 4458
4484 } // namespace content 4459 } // 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