OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/chromeos/drive/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 break; | 233 break; |
234 } | 234 } |
235 } | 235 } |
236 } | 236 } |
237 | 237 |
238 bool ResourceMetadataStorage::Iterator::HasError() const { | 238 bool ResourceMetadataStorage::Iterator::HasError() const { |
239 base::ThreadRestrictions::AssertIOAllowed(); | 239 base::ThreadRestrictions::AssertIOAllowed(); |
240 return !it_->status().ok(); | 240 return !it_->status().ok(); |
241 } | 241 } |
242 | 242 |
243 ResourceMetadataStorage::CacheEntryIterator::CacheEntryIterator( | |
244 scoped_ptr<leveldb::Iterator> it) : it_(it.Pass()) { | |
245 base::ThreadRestrictions::AssertIOAllowed(); | |
246 DCHECK(it_); | |
247 | |
248 it_->SeekToFirst(); | |
249 AdvanceInternal(); | |
250 } | |
251 | |
252 ResourceMetadataStorage::CacheEntryIterator::~CacheEntryIterator() { | |
253 base::ThreadRestrictions::AssertIOAllowed(); | |
254 } | |
255 | |
256 bool ResourceMetadataStorage::CacheEntryIterator::IsAtEnd() const { | |
257 base::ThreadRestrictions::AssertIOAllowed(); | |
258 return !it_->Valid(); | |
259 } | |
260 | |
261 const std::string& ResourceMetadataStorage::CacheEntryIterator::GetID() const { | |
262 base::ThreadRestrictions::AssertIOAllowed(); | |
263 DCHECK(!IsAtEnd()); | |
264 return id_; | |
265 } | |
266 | |
267 const FileCacheEntry& | |
268 ResourceMetadataStorage::CacheEntryIterator::GetValue() const { | |
269 base::ThreadRestrictions::AssertIOAllowed(); | |
270 DCHECK(!IsAtEnd()); | |
271 return entry_; | |
272 } | |
273 | |
274 void ResourceMetadataStorage::CacheEntryIterator::Advance() { | |
275 base::ThreadRestrictions::AssertIOAllowed(); | |
276 DCHECK(!IsAtEnd()); | |
277 | |
278 it_->Next(); | |
279 AdvanceInternal(); | |
280 } | |
281 | |
282 bool ResourceMetadataStorage::CacheEntryIterator::HasError() const { | |
283 base::ThreadRestrictions::AssertIOAllowed(); | |
284 return !it_->status().ok(); | |
285 } | |
286 | |
287 void ResourceMetadataStorage::CacheEntryIterator::AdvanceInternal() { | |
288 for (; it_->Valid(); it_->Next()) { | |
289 // Skip unparsable broken entries. | |
290 // TODO(hashimoto): Broken entries should be cleaned up at some point. | |
291 if (IsCacheEntryKey(it_->key()) && | |
292 entry_.ParseFromArray(it_->value().data(), it_->value().size())) { | |
293 id_ = GetIdFromCacheEntryKey(it_->key()); | |
294 break; | |
295 } | |
296 } | |
297 } | |
298 | |
299 // static | 243 // static |
300 bool ResourceMetadataStorage::UpgradeOldDB( | 244 bool ResourceMetadataStorage::UpgradeOldDB( |
301 const base::FilePath& directory_path, | 245 const base::FilePath& directory_path, |
302 const ResourceIdCanonicalizer& id_canonicalizer) { | 246 const ResourceIdCanonicalizer& id_canonicalizer) { |
303 base::ThreadRestrictions::AssertIOAllowed(); | 247 base::ThreadRestrictions::AssertIOAllowed(); |
304 COMPILE_ASSERT( | 248 COMPILE_ASSERT( |
305 kDBVersion == 12, | 249 kDBVersion == 12, |
306 db_version_and_this_function_should_be_updated_at_the_same_time); | 250 db_version_and_this_function_should_be_updated_at_the_same_time); |
307 | 251 |
308 const base::FilePath resource_map_path = | 252 const base::FilePath resource_map_path = |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
658 | 602 |
659 // Put the cache entry. | 603 // Put the cache entry. |
660 // TODO(hashimoto): Change DB layout and remove this. | 604 // TODO(hashimoto): Change DB layout and remove this. |
661 if (entry.file_specific_info().has_cache_state()) { | 605 if (entry.file_specific_info().has_cache_state()) { |
662 if (!entry.file_specific_info().cache_state().SerializeToString( | 606 if (!entry.file_specific_info().cache_state().SerializeToString( |
663 &serialized_entry)) { | 607 &serialized_entry)) { |
664 DLOG(ERROR) << "Failed to serialize the cache entry."; | 608 DLOG(ERROR) << "Failed to serialize the cache entry."; |
665 return FILE_ERROR_FAILED; | 609 return FILE_ERROR_FAILED; |
666 } | 610 } |
667 batch.Put(GetCacheEntryKey(id), serialized_entry); | 611 batch.Put(GetCacheEntryKey(id), serialized_entry); |
612 } else { | |
613 batch.Delete(GetCacheEntryKey(id)); | |
kinaba
2014/05/19 08:22:54
Wouldn't it be problematic when old_entry had cach
hashimoto
2014/05/20 02:46:33
You're right.
I should have addressed it first, bu
| |
668 } | 614 } |
669 | 615 |
670 // Put the entry itself. | 616 // Put the entry itself. |
671 { | 617 { |
672 // Clear cache state as it's stored separately. | 618 // Clear cache state as it's stored separately. |
673 // TODO(hashimoto): Change DB layout and remove this. | 619 // TODO(hashimoto): Change DB layout and remove this. |
674 ResourceEntry entry_copied(entry); | 620 ResourceEntry entry_copied(entry); |
675 entry_copied.mutable_file_specific_info()->clear_cache_state(); | 621 entry_copied.mutable_file_specific_info()->clear_cache_state(); |
676 if (!entry_copied.SerializeToString(&serialized_entry)) { | 622 if (!entry_copied.SerializeToString(&serialized_entry)) { |
677 DLOG(ERROR) << "Failed to serialize the entry: " << id; | 623 DLOG(ERROR) << "Failed to serialize the entry: " << id; |
(...skipping 23 matching lines...) Expand all Loading... | |
701 leveldb::Slice(id), | 647 leveldb::Slice(id), |
702 &serialized_entry); | 648 &serialized_entry); |
703 if (!status.ok()) | 649 if (!status.ok()) |
704 return LevelDBStatusToFileError(status); | 650 return LevelDBStatusToFileError(status); |
705 if (!out_entry->ParseFromString(serialized_entry)) | 651 if (!out_entry->ParseFromString(serialized_entry)) |
706 return FILE_ERROR_FAILED; | 652 return FILE_ERROR_FAILED; |
707 | 653 |
708 if (cache_error == FILE_ERROR_OK) { | 654 if (cache_error == FILE_ERROR_OK) { |
709 *out_entry->mutable_file_specific_info()->mutable_cache_state() = | 655 *out_entry->mutable_file_specific_info()->mutable_cache_state() = |
710 cache_entry; | 656 cache_entry; |
657 } else { | |
658 out_entry->mutable_file_specific_info()->clear_cache_state(); | |
711 } | 659 } |
712 return FILE_ERROR_OK; | 660 return FILE_ERROR_OK; |
713 } | 661 } |
714 | 662 |
715 FileError ResourceMetadataStorage::RemoveEntry(const std::string& id) { | 663 FileError ResourceMetadataStorage::RemoveEntry(const std::string& id) { |
716 base::ThreadRestrictions::AssertIOAllowed(); | 664 base::ThreadRestrictions::AssertIOAllowed(); |
717 DCHECK(!id.empty()); | 665 DCHECK(!id.empty()); |
718 | 666 |
719 ResourceEntry entry; | 667 ResourceEntry entry; |
720 FileError error = GetEntry(id, &entry); | 668 FileError error = GetEntry(id, &entry); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
778 resource_map_->NewIterator(leveldb::ReadOptions())); | 726 resource_map_->NewIterator(leveldb::ReadOptions())); |
779 for (it->Seek(parent_id); | 727 for (it->Seek(parent_id); |
780 it->Valid() && it->key().starts_with(leveldb::Slice(parent_id)); | 728 it->Valid() && it->key().starts_with(leveldb::Slice(parent_id)); |
781 it->Next()) { | 729 it->Next()) { |
782 if (IsChildEntryKey(it->key())) | 730 if (IsChildEntryKey(it->key())) |
783 children->push_back(it->value().ToString()); | 731 children->push_back(it->value().ToString()); |
784 } | 732 } |
785 return LevelDBStatusToFileError(it->status()); | 733 return LevelDBStatusToFileError(it->status()); |
786 } | 734 } |
787 | 735 |
788 FileError ResourceMetadataStorage::PutCacheEntry(const std::string& id, | |
789 const FileCacheEntry& entry) { | |
790 base::ThreadRestrictions::AssertIOAllowed(); | |
791 DCHECK(!id.empty()); | |
792 | |
793 std::string serialized_entry; | |
794 if (!entry.SerializeToString(&serialized_entry)) { | |
795 DLOG(ERROR) << "Failed to serialize the entry."; | |
796 return FILE_ERROR_FAILED; | |
797 } | |
798 | |
799 const leveldb::Status status = resource_map_->Put( | |
800 leveldb::WriteOptions(), | |
801 leveldb::Slice(GetCacheEntryKey(id)), | |
802 leveldb::Slice(serialized_entry)); | |
803 return LevelDBStatusToFileError(status); | |
804 } | |
805 | |
806 FileError ResourceMetadataStorage::GetCacheEntry(const std::string& id, | 736 FileError ResourceMetadataStorage::GetCacheEntry(const std::string& id, |
807 FileCacheEntry* out_entry) { | 737 FileCacheEntry* out_entry) { |
808 base::ThreadRestrictions::AssertIOAllowed(); | 738 base::ThreadRestrictions::AssertIOAllowed(); |
809 DCHECK(!id.empty()); | 739 DCHECK(!id.empty()); |
810 | 740 |
811 std::string serialized_entry; | 741 std::string serialized_entry; |
812 const leveldb::Status status = resource_map_->Get( | 742 const leveldb::Status status = resource_map_->Get( |
813 leveldb::ReadOptions(), | 743 leveldb::ReadOptions(), |
814 leveldb::Slice(GetCacheEntryKey(id)), | 744 leveldb::Slice(GetCacheEntryKey(id)), |
815 &serialized_entry); | 745 &serialized_entry); |
816 if (!status.ok()) | 746 if (!status.ok()) |
817 return LevelDBStatusToFileError(status); | 747 return LevelDBStatusToFileError(status); |
818 return out_entry->ParseFromString(serialized_entry) ? | 748 return out_entry->ParseFromString(serialized_entry) ? |
819 FILE_ERROR_OK : FILE_ERROR_FAILED; | 749 FILE_ERROR_OK : FILE_ERROR_FAILED; |
820 } | 750 } |
821 | 751 |
822 FileError ResourceMetadataStorage::RemoveCacheEntry(const std::string& id) { | |
823 base::ThreadRestrictions::AssertIOAllowed(); | |
824 DCHECK(!id.empty()); | |
825 | |
826 const leveldb::Status status = resource_map_->Delete( | |
827 leveldb::WriteOptions(), | |
828 leveldb::Slice(GetCacheEntryKey(id))); | |
829 return LevelDBStatusToFileError(status); | |
830 } | |
831 | |
832 scoped_ptr<ResourceMetadataStorage::CacheEntryIterator> | |
833 ResourceMetadataStorage::GetCacheEntryIterator() { | |
834 base::ThreadRestrictions::AssertIOAllowed(); | |
835 | |
836 scoped_ptr<leveldb::Iterator> it( | |
837 resource_map_->NewIterator(leveldb::ReadOptions())); | |
838 return make_scoped_ptr(new CacheEntryIterator(it.Pass())); | |
839 } | |
840 | |
841 ResourceMetadataStorage::RecoveredCacheInfo::RecoveredCacheInfo() | 752 ResourceMetadataStorage::RecoveredCacheInfo::RecoveredCacheInfo() |
842 : is_dirty(false) {} | 753 : is_dirty(false) {} |
843 | 754 |
844 ResourceMetadataStorage::RecoveredCacheInfo::~RecoveredCacheInfo() {} | 755 ResourceMetadataStorage::RecoveredCacheInfo::~RecoveredCacheInfo() {} |
845 | 756 |
846 FileError ResourceMetadataStorage::GetIdByResourceId( | 757 FileError ResourceMetadataStorage::GetIdByResourceId( |
847 const std::string& resource_id, | 758 const std::string& resource_id, |
848 std::string* out_id) { | 759 std::string* out_id) { |
849 base::ThreadRestrictions::AssertIOAllowed(); | 760 base::ThreadRestrictions::AssertIOAllowed(); |
850 DCHECK(!resource_id.empty()); | 761 DCHECK(!resource_id.empty()); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1015 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { | 926 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { |
1016 DLOG(ERROR) << "Error during checking resource map. status = " | 927 DLOG(ERROR) << "Error during checking resource map. status = " |
1017 << it->status().ToString(); | 928 << it->status().ToString(); |
1018 return false; | 929 return false; |
1019 } | 930 } |
1020 return true; | 931 return true; |
1021 } | 932 } |
1022 | 933 |
1023 } // namespace internal | 934 } // namespace internal |
1024 } // namespace drive | 935 } // namespace drive |
OLD | NEW |