drive: Split entry removal code from ChangeListProcessor::ApplyEntry

BUG=None
TEST=unit_tests
R=kinaba@chromium.org

Review URL: https://codereview.chromium.org/112713003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241105 0039d316-1c4b-4281-b951-d872f2087c98
parent 4d1e5ae5
...@@ -206,15 +206,13 @@ FileError ChangeListProcessor::ApplyEntryMap( ...@@ -206,15 +206,13 @@ FileError ChangeListProcessor::ApplyEntryMap(
} }
// Apply all entries except deleted ones to the metadata. // Apply all entries except deleted ones to the metadata.
std::vector<ResourceEntry> deleted_entries; std::vector<std::string> deleted_resource_ids;
deleted_entries.reserve(entry_map_.size());
while (!entry_map_.empty()) { while (!entry_map_.empty()) {
ResourceEntryMap::iterator it = entry_map_.begin(); ResourceEntryMap::iterator it = entry_map_.begin();
// Process deleted entries later to avoid deleting moved entries under it. // Process deleted entries later to avoid deleting moved entries under it.
if (it->second.deleted()) { if (it->second.deleted()) {
deleted_entries.push_back(ResourceEntry()); deleted_resource_ids.push_back(it->first);
deleted_entries.back().Swap(&it->second);
entry_map_.erase(it); entry_map_.erase(it);
continue; continue;
} }
...@@ -285,71 +283,63 @@ FileError ChangeListProcessor::ApplyEntryMap( ...@@ -285,71 +283,63 @@ FileError ChangeListProcessor::ApplyEntryMap(
} }
// Apply deleted entries. // Apply deleted entries.
for (size_t i = 0; i < deleted_entries.size(); ++i) { for (size_t i = 0; i < deleted_resource_ids.size(); ++i) {
// TODO(hashimoto): Handle ApplyEntry errors correctly. std::string local_id;
FileError error = ApplyEntry(deleted_entries[i]); FileError error = resource_metadata_->GetIdByResourceId(
DLOG_IF(WARNING, error != FILE_ERROR_OK) deleted_resource_ids[i], &local_id);
<< "ApplyEntry failed: " << FileErrorToString(error) if (error == FILE_ERROR_OK)
<< ", title = " << deleted_entries[i].title(); error = resource_metadata_->RemoveEntry(local_id);
DLOG_IF(WARNING, error != FILE_ERROR_OK && error != FILE_ERROR_NOT_FOUND)
<< "Failed to delete: " << FileErrorToString(error)
<< ", resource_id = " << deleted_resource_ids[i];
} }
return FILE_ERROR_OK; return FILE_ERROR_OK;
} }
FileError ChangeListProcessor::ApplyEntry(const ResourceEntry& entry) { FileError ChangeListProcessor::ApplyEntry(const ResourceEntry& entry) {
DCHECK(!entry.deleted());
const std::string& parent_resource_id =
parent_resource_id_map_[entry.resource_id()];
DCHECK(!parent_resource_id.empty()) << entry.resource_id();
std::string parent_local_id;
FileError error = resource_metadata_->GetIdByResourceId(
parent_resource_id, &parent_local_id);
if (error != FILE_ERROR_OK)
return error;
// Lookup the entry. // Lookup the entry.
std::string local_id; std::string local_id;
FileError error = resource_metadata_->GetIdByResourceId(entry.resource_id(), error = resource_metadata_->GetIdByResourceId(entry.resource_id(), &local_id);
&local_id);
ResourceEntry existing_entry; ResourceEntry existing_entry;
if (error == FILE_ERROR_OK) if (error == FILE_ERROR_OK)
error = resource_metadata_->GetResourceEntryById(local_id, &existing_entry); error = resource_metadata_->GetResourceEntryById(local_id, &existing_entry);
const FileError get_existing_entry_result = error; ResourceEntry new_entry(entry);
if (entry.deleted()) { new_entry.set_parent_local_id(parent_local_id);
// Deleted file/directory.
switch (get_existing_entry_result) {
case FILE_ERROR_OK:
error = resource_metadata_->RemoveEntry(local_id);
break;
case FILE_ERROR_NOT_FOUND: // Already deleted.
error = FILE_ERROR_OK;
break;
default:
error = get_existing_entry_result;
}
} else {
const std::string& parent_resource_id =
parent_resource_id_map_[entry.resource_id()];
DCHECK(!parent_resource_id.empty()) << entry.resource_id();
std::string parent_local_id;
error = resource_metadata_->GetIdByResourceId(
parent_resource_id, &parent_local_id);
if (error == FILE_ERROR_OK) {
ResourceEntry new_entry(entry);
new_entry.set_parent_local_id(parent_local_id);
switch (get_existing_entry_result) {
case FILE_ERROR_OK: // Entry exists and needs to be refreshed.
new_entry.set_local_id(local_id);
error = resource_metadata_->RefreshEntry(new_entry);
break;
case FILE_ERROR_NOT_FOUND: { // Adding a new entry.
std::string local_id;
error = resource_metadata_->AddEntry(new_entry, &local_id);
break;
}
default:
error = get_existing_entry_result;
}
if (error == FILE_ERROR_OK) switch (error) {
UpdateChangedDirs(entry); case FILE_ERROR_OK: // Entry exists and needs to be refreshed.
new_entry.set_local_id(local_id);
error = resource_metadata_->RefreshEntry(new_entry);
break;
case FILE_ERROR_NOT_FOUND: { // Adding a new entry.
std::string local_id;
error = resource_metadata_->AddEntry(new_entry, &local_id);
break;
} }
default:
return error;
} }
if (error != FILE_ERROR_OK)
return error;
return error; UpdateChangedDirs(entry);
return FILE_ERROR_OK;
} }
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment