summaryrefslogtreecommitdiffstats
path: root/sync/syncable/syncable_delete_journal.cc
blob: 923b065d333e07f3be082a9ad2d4a8fe52dd1f21 (plain)
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
// 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 "sync/syncable/syncable_delete_journal.h"

#include <stddef.h>
#include <stdint.h>

#include "base/stl_util.h"
#include "sync/internal_api/public/base/model_type.h"

namespace syncer {
namespace syncable {

DeleteJournal::DeleteJournal(JournalIndex* initial_journal) {
  CHECK(initial_journal);
  delete_journals_.swap(*initial_journal);
}

DeleteJournal::~DeleteJournal() {
  STLDeleteElements(&delete_journals_);
}

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.
      EntryKernel* t = new EntryKernel(entry);
      delete_journals_.insert(t);
      delete_journals_to_purge_.erase(t->ref(META_HANDLE));
    }
  } 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)->ref(META_HANDLE));
      delete *it;
      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 (JournalIndex::const_iterator it = delete_journals_.begin();
      it != delete_journals_.end(); ++it) {
    if ((*it)->GetServerModelType() == type ||
        GetModelTypeFromSpecifics((*it)->ref(SPECIFICS)) == type) {
      deleted_entries->insert(*it);
    }
  }
  passive_delete_journal_types_.Put(type);
}

void DeleteJournal::PurgeDeleteJournals(BaseTransaction* trans,
                                        const MetahandleSet& to_purge) {
  DCHECK(trans);
  JournalIndex::iterator it = delete_journals_.begin();
  while (it != delete_journals_.end()) {
    int64_t handle = (*it)->ref(META_HANDLE);
    if (to_purge.count(handle)) {
      delete *it;
      delete_journals_.erase(it++);
    } else {
      ++it;
    }
  }
  delete_journals_to_purge_.insert(to_purge.begin(), to_purge.end());
}

void DeleteJournal::TakeSnapshotAndClear(BaseTransaction* trans,
                                         EntryKernelSet* journal_entries,
                                         MetahandleSet* journals_to_purge) {
  DCHECK(trans);
  // Move passive delete journals to snapshot. Will copy back if snapshot fails
  // to save.
  JournalIndex::iterator it = delete_journals_.begin();
  while (it != delete_journals_.end()) {
    if (passive_delete_journal_types_.Has((*it)->GetServerModelType()) ||
        passive_delete_journal_types_.Has(GetModelTypeFromSpecifics(
            (*it)->ref(SPECIFICS)))) {
      journal_entries->insert(*it);
      delete_journals_.erase(it++);
    } else {
      ++it;
    }
  }
  *journals_to_purge = delete_journals_to_purge_;
  delete_journals_to_purge_.clear();
}

void DeleteJournal::AddJournalBatch(BaseTransaction* trans,
                                    const EntryKernelSet& entries) {
  DCHECK(trans);
  EntryKernel needle;
  for (EntryKernelSet::const_iterator i = entries.begin();
       i != entries.end(); ++i) {
    needle.put(ID, (*i)->ref(ID));
    if (delete_journals_.find(&needle) == delete_journals_.end()) {
      delete_journals_.insert(new EntryKernel(**i));
    }
    delete_journals_to_purge_.erase((*i)->ref(META_HANDLE));
  }
}

/* static */
bool DeleteJournal::IsDeleteJournalEnabled(ModelType type) {
  switch (type) {
    case BOOKMARKS:
      return true;
    default:
      return false;
  }
}

}  // namespace syncable
}  // namespace syncer