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

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

Issue 2276593002: Support renaming of IndexedDB indexes and object stores. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests for create rename in the same aborted transaction. Created 4 years, 3 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_database.h" 5 #include "content/browser/indexed_db/indexed_db_database.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <set> 10 #include <set>
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 } 396 }
397 metadata_.object_stores[object_store.id] = object_store; 397 metadata_.object_stores[object_store.id] = object_store;
398 } 398 }
399 399
400 void IndexedDBDatabase::RemoveObjectStore(int64_t object_store_id) { 400 void IndexedDBDatabase::RemoveObjectStore(int64_t object_store_id) {
401 DCHECK(metadata_.object_stores.find(object_store_id) != 401 DCHECK(metadata_.object_stores.find(object_store_id) !=
402 metadata_.object_stores.end()); 402 metadata_.object_stores.end());
403 metadata_.object_stores.erase(object_store_id); 403 metadata_.object_stores.erase(object_store_id);
404 } 404 }
405 405
406 void IndexedDBDatabase::SetObjectStoreName(
407 int64_t object_store_id, const base::string16& name) {
408 DCHECK(metadata_.object_stores.find(object_store_id) !=
409 metadata_.object_stores.end());
410 metadata_.object_stores[object_store_id].name = name;
411 }
412
406 void IndexedDBDatabase::AddIndex(int64_t object_store_id, 413 void IndexedDBDatabase::AddIndex(int64_t object_store_id,
407 const IndexedDBIndexMetadata& index, 414 const IndexedDBIndexMetadata& index,
408 int64_t new_max_index_id) { 415 int64_t new_max_index_id) {
409 DCHECK(metadata_.object_stores.find(object_store_id) != 416 DCHECK(metadata_.object_stores.find(object_store_id) !=
410 metadata_.object_stores.end()); 417 metadata_.object_stores.end());
411 IndexedDBObjectStoreMetadata object_store = 418 IndexedDBObjectStoreMetadata object_store =
412 metadata_.object_stores[object_store_id]; 419 metadata_.object_stores[object_store_id];
413 420
414 DCHECK(object_store.indexes.find(index.id) == object_store.indexes.end()); 421 DCHECK(object_store.indexes.find(index.id) == object_store.indexes.end());
415 object_store.indexes[index.id] = index; 422 object_store.indexes[index.id] = index;
416 if (new_max_index_id != IndexedDBIndexMetadata::kInvalidId) { 423 if (new_max_index_id != IndexedDBIndexMetadata::kInvalidId) {
417 DCHECK_LT(object_store.max_index_id, new_max_index_id); 424 DCHECK_LT(object_store.max_index_id, new_max_index_id);
418 object_store.max_index_id = new_max_index_id; 425 object_store.max_index_id = new_max_index_id;
419 } 426 }
420 metadata_.object_stores[object_store_id] = object_store; 427 metadata_.object_stores[object_store_id] = object_store;
421 } 428 }
422 429
423 void IndexedDBDatabase::RemoveIndex(int64_t object_store_id, int64_t index_id) { 430 void IndexedDBDatabase::RemoveIndex(int64_t object_store_id, int64_t index_id) {
424 DCHECK(metadata_.object_stores.find(object_store_id) != 431 DCHECK(metadata_.object_stores.find(object_store_id) !=
425 metadata_.object_stores.end()); 432 metadata_.object_stores.end());
426 IndexedDBObjectStoreMetadata object_store = 433 IndexedDBObjectStoreMetadata object_store =
427 metadata_.object_stores[object_store_id]; 434 metadata_.object_stores[object_store_id];
428 435
429 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end()); 436 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end());
430 object_store.indexes.erase(index_id); 437 object_store.indexes.erase(index_id);
431 metadata_.object_stores[object_store_id] = object_store; 438 metadata_.object_stores[object_store_id] = object_store;
432 } 439 }
433 440
441 void IndexedDBDatabase::SetIndexName(
442 int64_t object_store_id, int64_t index_id, const base::string16& name) {
443 DCHECK(metadata_.object_stores.find(object_store_id) !=
444 metadata_.object_stores.end());
445 IndexedDBObjectStoreMetadata object_store =
446 metadata_.object_stores[object_store_id];
447
448 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end());
449 object_store.indexes[index_id].name = name;
450 metadata_.object_stores[object_store_id] = object_store;
451 }
452
434 leveldb::Status IndexedDBDatabase::OpenInternal() { 453 leveldb::Status IndexedDBDatabase::OpenInternal() {
435 bool success = false; 454 bool success = false;
436 leveldb::Status s = backing_store_->GetIDBDatabaseMetaData( 455 leveldb::Status s = backing_store_->GetIDBDatabaseMetaData(
437 metadata_.name, &metadata_, &success); 456 metadata_.name, &metadata_, &success);
438 DCHECK(success == (metadata_.id != kInvalidId)) << "success = " << success 457 DCHECK(success == (metadata_.id != kInvalidId)) << "success = " << success
439 << " id = " << metadata_.id; 458 << " id = " << metadata_.id;
440 if (!s.ok()) 459 if (!s.ok())
441 return s; 460 return s;
442 if (success) 461 if (success)
443 return backing_store_->GetObjectStores(metadata_.id, 462 return backing_store_->GetObjectStores(metadata_.id,
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 611
593 if (!ValidateObjectStoreId(object_store_id)) 612 if (!ValidateObjectStoreId(object_store_id))
594 return; 613 return;
595 614
596 transaction->ScheduleTask( 615 transaction->ScheduleTask(
597 base::Bind(&IndexedDBDatabase::DeleteObjectStoreOperation, 616 base::Bind(&IndexedDBDatabase::DeleteObjectStoreOperation,
598 this, 617 this,
599 object_store_id)); 618 object_store_id));
600 } 619 }
601 620
621 void IndexedDBDatabase::RenameObjectStore(int64_t transaction_id,
622 int64_t object_store_id,
623 const base::string16& new_name) {
624 IDB_TRACE1("IndexedDBDatabase::RenameObjectStore", "txn.id", transaction_id);
625 IndexedDBTransaction* transaction = GetTransaction(transaction_id);
626 if (!transaction)
627 return;
628 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange);
629
630 if (!ValidateObjectStoreId(object_store_id))
631 return;
632
633 // Store creation is done synchronously, as it may be followed by
jsbell 2016/09/07 17:16:13 Update comment, e.g. "Store metadata changes are m
pwnall 2016/09/07 22:43:52 Done.
634 // index creation (also sync) since preemptive OpenCursor/SetIndexKeys
635 // may follow.
636 const IndexedDBObjectStoreMetadata object_store_metadata =
637 metadata_.object_stores[object_store_id];
638
639 leveldb::Status s =
640 backing_store_->RenameObjectStore(transaction->BackingStoreTransaction(),
641 transaction->database()->id(),
642 object_store_metadata.id, new_name);
643 if (!s.ok()) {
644 IndexedDBDatabaseError error(
645 blink::WebIDBDatabaseExceptionUnknownError,
646 ASCIIToUTF16("Internal error renaming object store '") +
647 object_store_metadata.name + ASCIIToUTF16("' to '") + new_name +
648 ASCIIToUTF16("'."));
649 transaction->Abort(error);
650 if (s.IsCorruption())
651 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error);
652 return;
653 }
654
655 transaction->ScheduleAbortTask(
656 base::Bind(&IndexedDBDatabase::RenameObjectStoreAbortOperation,
657 this,
658 object_store_id,
659 object_store_metadata.name));
660 SetObjectStoreName(object_store_id, new_name);
661 }
662
602 void IndexedDBDatabase::CreateIndex(int64_t transaction_id, 663 void IndexedDBDatabase::CreateIndex(int64_t transaction_id,
603 int64_t object_store_id, 664 int64_t object_store_id,
604 int64_t index_id, 665 int64_t index_id,
605 const base::string16& name, 666 const base::string16& name,
606 const IndexedDBKeyPath& key_path, 667 const IndexedDBKeyPath& key_path,
607 bool unique, 668 bool unique,
608 bool multi_entry) { 669 bool multi_entry) {
609 IDB_TRACE1("IndexedDBDatabase::CreateIndex", "txn.id", transaction_id); 670 IDB_TRACE1("IndexedDBDatabase::CreateIndex", "txn.id", transaction_id);
610 IndexedDBTransaction* transaction = GetTransaction(transaction_id); 671 IndexedDBTransaction* transaction = GetTransaction(transaction_id);
611 if (!transaction) 672 if (!transaction)
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 776
716 void IndexedDBDatabase::DeleteIndexAbortOperation( 777 void IndexedDBDatabase::DeleteIndexAbortOperation(
717 int64_t object_store_id, 778 int64_t object_store_id,
718 const IndexedDBIndexMetadata& index_metadata, 779 const IndexedDBIndexMetadata& index_metadata,
719 IndexedDBTransaction* transaction) { 780 IndexedDBTransaction* transaction) {
720 DCHECK(!transaction); 781 DCHECK(!transaction);
721 IDB_TRACE("IndexedDBDatabase::DeleteIndexAbortOperation"); 782 IDB_TRACE("IndexedDBDatabase::DeleteIndexAbortOperation");
722 AddIndex(object_store_id, index_metadata, IndexedDBIndexMetadata::kInvalidId); 783 AddIndex(object_store_id, index_metadata, IndexedDBIndexMetadata::kInvalidId);
723 } 784 }
724 785
786 void IndexedDBDatabase::RenameIndex(int64_t transaction_id,
787 int64_t object_store_id,
788 int64_t index_id,
789 const base::string16& new_name) {
790 IDB_TRACE1("IndexedDBDatabase::RenameIndex", "txn.id", transaction_id);
791 IndexedDBTransaction* transaction = GetTransaction(transaction_id);
792 if (!transaction)
793 return;
794 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange);
795
796 if (!ValidateObjectStoreIdAndIndexId(object_store_id, index_id))
797 return;
798
799 // Index creation is done synchronously since preemptive
jsbell 2016/09/07 17:16:13 Ditto
pwnall 2016/09/07 22:43:52 Done.
800 // OpenCursor/SetIndexKeys may follow.
801
802 const IndexedDBIndexMetadata index_metadata =
803 metadata_.object_stores[object_store_id].indexes[index_id];
804
805 leveldb::Status s =
806 backing_store_->RenameIndex(transaction->BackingStoreTransaction(),
807 transaction->database()->id(),
808 object_store_id,
809 index_id,
810 new_name);
811 if (!s.ok()) {
812 base::string16 error_string =
813 ASCIIToUTF16("Internal error creating index '") +
jsbell 2016/09/07 17:16:13 Update error message.
pwnall 2016/09/07 22:43:52 Done.
814 index_metadata.name + ASCIIToUTF16("'.");
815 transaction->Abort(IndexedDBDatabaseError(
816 blink::WebIDBDatabaseExceptionUnknownError, error_string));
817 return;
818 }
819
820 transaction->ScheduleAbortTask(
821 base::Bind(&IndexedDBDatabase::RenameIndexAbortOperation,
822 this,
823 object_store_id,
824 index_id,
825 index_metadata.name));
826 SetIndexName(object_store_id, index_id, new_name);
827 }
828
829 void IndexedDBDatabase::RenameIndexAbortOperation(
830 int64_t object_store_id,
831 int64_t index_id,
832 const base::string16 old_name,
833 IndexedDBTransaction* transaction) {
834 DCHECK(!transaction);
835 IDB_TRACE("IndexedDBDatabase::RenameIndexAbortOperation");
836 SetIndexName(object_store_id, index_id, old_name);
837 }
838
725 void IndexedDBDatabase::Commit(int64_t transaction_id) { 839 void IndexedDBDatabase::Commit(int64_t transaction_id) {
726 // The frontend suggests that we commit, but we may have previously initiated 840 // The frontend suggests that we commit, but we may have previously initiated
727 // an abort, and so have disposed of the transaction. on_abort has already 841 // an abort, and so have disposed of the transaction. on_abort has already
728 // been dispatched to the frontend, so it will find out about that 842 // been dispatched to the frontend, so it will find out about that
729 // asynchronously. 843 // asynchronously.
730 IndexedDBTransaction* transaction = GetTransaction(transaction_id); 844 IndexedDBTransaction* transaction = GetTransaction(transaction_id);
731 if (transaction) { 845 if (transaction) {
732 scoped_refptr<IndexedDBFactory> factory = factory_; 846 scoped_refptr<IndexedDBFactory> factory = factory_;
733 leveldb::Status s = transaction->Commit(); 847 leveldb::Status s = transaction->Commit();
734 if (s.IsCorruption()) { 848 if (s.IsCorruption()) {
(...skipping 1260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1995 2109
1996 void IndexedDBDatabase::DeleteObjectStoreAbortOperation( 2110 void IndexedDBDatabase::DeleteObjectStoreAbortOperation(
1997 const IndexedDBObjectStoreMetadata& object_store_metadata, 2111 const IndexedDBObjectStoreMetadata& object_store_metadata,
1998 IndexedDBTransaction* transaction) { 2112 IndexedDBTransaction* transaction) {
1999 DCHECK(!transaction); 2113 DCHECK(!transaction);
2000 IDB_TRACE("IndexedDBDatabase::DeleteObjectStoreAbortOperation"); 2114 IDB_TRACE("IndexedDBDatabase::DeleteObjectStoreAbortOperation");
2001 AddObjectStore(object_store_metadata, 2115 AddObjectStore(object_store_metadata,
2002 IndexedDBObjectStoreMetadata::kInvalidId); 2116 IndexedDBObjectStoreMetadata::kInvalidId);
2003 } 2117 }
2004 2118
2119 void IndexedDBDatabase::RenameObjectStoreAbortOperation(
2120 int64_t object_store_id,
2121 base::string16 old_name,
2122 IndexedDBTransaction* transaction) {
2123 DCHECK(!transaction);
2124 IDB_TRACE("IndexedDBDatabase::RenameObjectStoreAbortOperation");
2125 SetObjectStoreName(object_store_id, old_name);
2126 }
2127
2005 void IndexedDBDatabase::VersionChangeAbortOperation( 2128 void IndexedDBDatabase::VersionChangeAbortOperation(
2006 int64_t previous_version, 2129 int64_t previous_version,
2007 IndexedDBTransaction* transaction) { 2130 IndexedDBTransaction* transaction) {
2008 DCHECK(!transaction); 2131 DCHECK(!transaction);
2009 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); 2132 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation");
2010 metadata_.version = previous_version; 2133 metadata_.version = previous_version;
2011 } 2134 }
2012 2135
2013 } // namespace content 2136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698