1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/syncable/syncable_delete_journal.h"
#include <stdint.h>
#include <utility>
#include "base/memory/ptr_util.h"
#include "components/sync/base/model_type.h"
namespace syncer {
namespace syncable {
DeleteJournal::DeleteJournal(std::unique_ptr<JournalIndex> initial_journal) {
CHECK(initial_journal);
delete_journals_.swap(*initial_journal);
}
DeleteJournal::~DeleteJournal() {}
size_t DeleteJournal::GetDeleteJournalSize(BaseTransaction* trans) const {
DCHECK(trans);
return delete_journals_.size();
}
void DeleteJournal::UpdateDeleteJournalForServerDelete(
BaseTransaction* trans,
bool was_deleted,
const EntryKernel& entry) {
DCHECK(trans);
// Should be sufficient to check server type only but check for local
// type too because of incomplete test setup.
if (!(IsDeleteJournalEnabled(entry.GetServerModelType()) ||
IsDeleteJournalEnabled(
GetModelTypeFromSpecifics(entry.ref(SPECIFICS))))) {
return;
}
JournalIndex::iterator it = delete_journals_.find(&entry);
if (entry.ref(SERVER_IS_DEL)) {
if (it == delete_journals_.end()) {
// New delete.
std::unique_ptr<EntryKernel> t_ptr = base::MakeUnique<EntryKernel>(entry);
EntryKernel* t = t_ptr.get();
delete_journals_to_purge_.erase(t->ref(META_HANDLE));
delete_journals_[t] = std::move(t_ptr);
}
} else {
// Undelete. This could happen in two cases:
// * An entry was deleted then undeleted, i.e. server delete was
// overwritten because of entry has unsynced data locally.
// * A data type was broken, i.e. encountered unrecoverable error, in last
// sync session and all its entries were duplicated in delete journals.
// On restart, entries are recreated from downloads and recreation calls
// UpdateDeleteJournals() to remove live entries from delete journals,
// thus only deleted entries remain in journals.
if (it != delete_journals_.end()) {
delete_journals_to_purge_.insert((*it).first->ref(META_HANDLE));
delete_journals_.erase(it);
} else if (was_deleted) {
delete_journals_to_purge_.insert(entry.ref(META_HANDLE));
}
}
}
void DeleteJournal::GetDeleteJournals(BaseTransaction* trans,
ModelType type,
EntryKernelSet* deleted_entries) {
DCHECK(trans);
for (auto it = delete_journals_.begin(); it != delete_journals_.end(); ++it) {
if ((*it).first->GetServerModelType() == type ||
GetModelTypeFromSpecifics((*it).first->ref(SPECIFICS)) == type) {
deleted_entries->insert((*it).first);
}
}
passive_delete_journal_types_.Put(type);
}
void DeleteJournal::PurgeDeleteJournals(BaseTransaction* trans,
const MetahandleSet& to_purge) {
DCHECK(trans);
auto it = delete_journals_.begin();
while (it != delete_journals_.end()) {
int64_t handle = (*it).first->ref(META_HANDLE);
if (to_purge.count(handle)) {
delete_journals_.erase(it++);
} else {
++it;
}
}
delete_journals_to_purge_.insert(to_purge.begin(), to_purge.end());
}
void DeleteJournal::TakeSnapshotAndClear(BaseTransaction* trans,
OwnedEntryKernelSet* journal_entries,
MetahandleSet* journals_to_purge) {
DCHECK(trans);
// Move passive delete journals to snapshot. Will copy back if snapshot fails
// to save.
auto it = delete_journals_.begin();
while (it != delete_journals_.end()) {
if (passive_delete_journal_types_.Has((*it).first->GetServerModelType()) ||
passive_delete_journal_types_.Has(
GetModelTypeFromSpecifics((*it).first->ref(SPECIFICS)))) {
journal_entries->insert(std::move((*it).second));
delete_journals_.erase(it++);
} else {
++it;
}
}
*journals_to_purge = delete_journals_to_purge_;
delete_journals_to_purge_.clear();
}
void DeleteJournal::AddJournalBatch(BaseTransaction* trans,
const OwnedEntryKernelSet& entries) {
DCHECK(trans);
EntryKernel needle;
for (auto& entry : entries) {
needle.put(ID, entry->ref(ID));
if (delete_journals_.find(&needle) == delete_journals_.end()) {
std::unique_ptr<EntryKernel> t_ptr =
base::MakeUnique<EntryKernel>(*entry);
EntryKernel* t = t_ptr.get();
delete_journals_[t] = std::move(t_ptr);
}
delete_journals_to_purge_.erase(entry->ref(META_HANDLE));
}
}
/* static */
bool DeleteJournal::IsDeleteJournalEnabled(ModelType type) {
switch (type) {
case BOOKMARKS:
return true;
default:
return false;
}
}
} // namespace syncable
} // namespace syncer
|