This source file includes following definitions.
- GetCommitIdsForType
- IsEntryInConflict
- HasAttachmentNotOnServer
- IsEntryReadyForCommit
- FilterUnreadyEntries
- trans_
- AddUncommittedParentsAndTheirPredecessors
- TryAddItem
- AddItemThenPredecessors
- AddPredecessorsThenItem
- IsFull
- HaveItem
- AppendManyToTraversal
- AppendToTraversal
- AddCreatesAndMoves
- AddDeletes
- OrderCommitIds
#include "sync/engine/get_commit_ids.h"
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "sync/engine/syncer_util.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/entry.h"
#include "sync/syncable/nigori_handler.h"
#include "sync/syncable/nigori_util.h"
#include "sync/syncable/syncable_base_transaction.h"
#include "sync/syncable/syncable_util.h"
#include "sync/util/cryptographer.h"
using std::set;
using std::vector;
namespace syncer {
namespace {
void FilterUnreadyEntries(
syncable::BaseTransaction* trans,
ModelTypeSet requested_types,
ModelTypeSet encrypted_types,
bool passphrase_missing,
const syncable::Directory::Metahandles& unsynced_handles,
std::set<int64>* ready_unsynced_set);
void OrderCommitIds(
syncable::BaseTransaction* trans,
size_t max_entries,
const std::set<int64>& ready_unsynced_set,
std::vector<int64>* out);
}
void GetCommitIdsForType(
syncable::BaseTransaction* trans,
ModelType type,
size_t max_entries,
syncable::Directory::Metahandles* out) {
syncable::Directory* dir = trans->directory();
std::set<int64> ready_unsynced_set;
syncable::Directory::Metahandles all_unsynced_handles;
GetUnsyncedEntries(trans, &all_unsynced_handles);
ModelTypeSet encrypted_types;
bool passphrase_missing = false;
Cryptographer* cryptographer = dir->GetCryptographer(trans);
if (cryptographer) {
encrypted_types = dir->GetNigoriHandler()->GetEncryptedTypes(trans);
passphrase_missing = cryptographer->has_pending_keys();
};
FilterUnreadyEntries(trans,
ModelTypeSet(type),
encrypted_types,
passphrase_missing,
all_unsynced_handles,
&ready_unsynced_set);
OrderCommitIds(trans, max_entries, ready_unsynced_set, out);
for (size_t i = 0; i < out->size(); i++) {
DVLOG(1) << "Debug commit batch result:" << (*out)[i];
}
}
namespace {
bool IsEntryInConflict(const syncable::Entry& entry) {
if (entry.GetIsUnsynced() &&
entry.GetServerVersion() > 0 &&
(entry.GetServerVersion() > entry.GetBaseVersion())) {
DCHECK(entry.GetIsUnappliedUpdate());
DVLOG(1) << "Excluding entry from commit due to version mismatch "
<< entry;
return true;
}
return false;
}
bool HasAttachmentNotOnServer(const syncable::Entry& entry) {
return false;
}
bool IsEntryReadyForCommit(ModelTypeSet requested_types,
ModelTypeSet encrypted_types,
bool passphrase_missing,
const syncable::Entry& entry) {
DCHECK(entry.GetIsUnsynced());
if (IsEntryInConflict(entry))
return false;
const ModelType type = entry.GetModelType();
if ((type != NIGORI) && encrypted_types.Has(type) &&
(passphrase_missing ||
syncable::EntryNeedsEncryption(encrypted_types, entry))) {
DVLOG(1) << "Excluding entry from commit due to lack of encryption "
<< entry;
return false;
}
if (!requested_types.Has(type))
return false;
if (entry.GetIsDel() && !entry.GetId().ServerKnows()) {
NOTREACHED() << "Found deleted and unsynced local item: " << entry;
return false;
}
syncable::Id id = entry.GetId();
if (id == entry.GetParentId()) {
CHECK(id.IsRoot()) << "Non-root item is self parenting." << entry;
NOTREACHED() << "Root item became unsynced " << entry;
return false;
}
if (entry.IsRoot()) {
NOTREACHED() << "Permanent item became unsynced " << entry;
return false;
}
if (HasAttachmentNotOnServer(entry)) {
return false;
}
DVLOG(2) << "Entry is ready for commit: " << entry;
return true;
}
void FilterUnreadyEntries(
syncable::BaseTransaction* trans,
ModelTypeSet requested_types,
ModelTypeSet encrypted_types,
bool passphrase_missing,
const syncable::Directory::Metahandles& unsynced_handles,
std::set<int64>* ready_unsynced_set) {
for (syncable::Directory::Metahandles::const_iterator iter =
unsynced_handles.begin(); iter != unsynced_handles.end(); ++iter) {
syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter);
if (IsEntryReadyForCommit(requested_types,
encrypted_types,
passphrase_missing,
entry)) {
ready_unsynced_set->insert(*iter);
}
}
}
class Traversal {
public:
Traversal(
syncable::BaseTransaction* trans,
int64 max_entries,
syncable::Directory::Metahandles* out);
~Traversal();
void AddCreatesAndMoves(const std::set<int64>& ready_unsynced_set);
void AddDeletes(const std::set<int64>& ready_unsynced_set);
private:
bool AddUncommittedParentsAndTheirPredecessors(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const;
void TryAddItem(const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const;
void AddItemThenPredecessors(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const;
void AddPredecessorsThenItem(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const;
bool IsFull() const;
bool HaveItem(int64 handle) const;
void AppendManyToTraversal(const syncable::Directory::Metahandles& handles);
void AppendToTraversal(int64 handle);
syncable::Directory::Metahandles* out_;
std::set<int64> added_handles_;
const size_t max_entries_;
syncable::BaseTransaction* trans_;
DISALLOW_COPY_AND_ASSIGN(Traversal);
};
Traversal::Traversal(
syncable::BaseTransaction* trans,
int64 max_entries,
syncable::Directory::Metahandles* out)
: out_(out),
max_entries_(max_entries),
trans_(trans) { }
Traversal::~Traversal() {}
bool Traversal::AddUncommittedParentsAndTheirPredecessors(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const {
syncable::Directory::Metahandles dependencies;
syncable::Id parent_id = item.GetParentId();
while (!parent_id.ServerKnows()) {
syncable::Entry parent(trans_, syncable::GET_BY_ID, parent_id);
CHECK(parent.good()) << "Bad user-only parent in item path.";
int64 handle = parent.GetMetahandle();
if (HaveItem(handle)) {
break;
}
if (IsEntryInConflict(parent)) {
DVLOG(1) << "Parent was in conflict, omitting " << item;
return false;
}
AddItemThenPredecessors(ready_unsynced_set,
parent,
&dependencies);
parent_id = parent.GetParentId();
}
result->insert(result->end(), dependencies.rbegin(), dependencies.rend());
return true;
}
void Traversal::TryAddItem(const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const {
DCHECK(item.GetIsUnsynced());
int64 item_handle = item.GetMetahandle();
if (ready_unsynced_set.count(item_handle) != 0) {
result->push_back(item_handle);
}
}
void Traversal::AddItemThenPredecessors(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const {
int64 item_handle = item.GetMetahandle();
if (HaveItem(item_handle)) {
return;
}
TryAddItem(ready_unsynced_set, item, result);
if (item.GetIsDel())
return;
syncable::Id prev_id = item.GetPredecessorId();
while (!prev_id.IsRoot()) {
syncable::Entry prev(trans_, syncable::GET_BY_ID, prev_id);
CHECK(prev.good()) << "Bad id when walking predecessors.";
if (!prev.GetIsUnsynced()) {
return;
}
int64 handle = prev.GetMetahandle();
if (HaveItem(handle)) {
return;
}
TryAddItem(ready_unsynced_set, prev, result);
prev_id = prev.GetPredecessorId();
}
}
void Traversal::AddPredecessorsThenItem(
const std::set<int64>& ready_unsynced_set,
const syncable::Entry& item,
syncable::Directory::Metahandles* result) const {
syncable::Directory::Metahandles dependencies;
AddItemThenPredecessors(ready_unsynced_set, item, &dependencies);
result->insert(result->end(), dependencies.rbegin(), dependencies.rend());
}
bool Traversal::IsFull() const {
return out_->size() >= max_entries_;
}
bool Traversal::HaveItem(int64 handle) const {
return added_handles_.find(handle) != added_handles_.end();
}
void Traversal::AppendManyToTraversal(
const syncable::Directory::Metahandles& handles) {
out_->insert(out_->end(), handles.begin(), handles.end());
added_handles_.insert(handles.begin(), handles.end());
}
void Traversal::AppendToTraversal(int64 metahandle) {
out_->push_back(metahandle);
added_handles_.insert(metahandle);
}
void Traversal::AddCreatesAndMoves(
const std::set<int64>& ready_unsynced_set) {
for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin();
!IsFull() && iter != ready_unsynced_set.end(); ++iter) {
int64 metahandle = *iter;
if (HaveItem(metahandle))
continue;
syncable::Entry entry(trans_,
syncable::GET_BY_HANDLE,
metahandle);
if (!entry.GetIsDel()) {
syncable::Directory::Metahandles item_dependencies;
if (AddUncommittedParentsAndTheirPredecessors(
ready_unsynced_set,
entry,
&item_dependencies)) {
AddPredecessorsThenItem(ready_unsynced_set,
entry,
&item_dependencies);
AppendManyToTraversal(item_dependencies);
}
}
}
if (out_->size() > max_entries_)
out_->resize(max_entries_);
}
void Traversal::AddDeletes(
const std::set<int64>& ready_unsynced_set) {
set<syncable::Id> legal_delete_parents;
for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin();
!IsFull() && iter != ready_unsynced_set.end(); ++iter) {
int64 metahandle = *iter;
if (HaveItem(metahandle))
continue;
syncable::Entry entry(trans_, syncable::GET_BY_HANDLE,
metahandle);
if (entry.GetIsDel()) {
syncable::Entry parent(trans_, syncable::GET_BY_ID,
entry.GetParentId());
if (parent.good() && parent.GetIsDel() && parent.GetIsUnsynced()) {
if (entry.GetId().ServerKnows() &&
entry.GetParentId() != entry.GetServerParentId()) {
DVLOG(1) << "Inserting moved and deleted entry, will be missed by "
<< "delete roll." << entry.GetId();
AppendToTraversal(metahandle);
}
continue;
}
legal_delete_parents.insert(entry.GetParentId());
}
}
for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin();
!IsFull() && iter != ready_unsynced_set.end(); ++iter) {
int64 metahandle = *iter;
if (HaveItem(metahandle))
continue;
syncable::Entry entry(trans_, syncable::GET_BY_HANDLE, metahandle);
if (entry.GetIsDel()) {
syncable::Id parent_id = entry.GetParentId();
if (legal_delete_parents.count(parent_id)) {
AppendToTraversal(metahandle);
}
}
}
}
void OrderCommitIds(
syncable::BaseTransaction* trans,
size_t max_entries,
const std::set<int64>& ready_unsynced_set,
syncable::Directory::Metahandles* out) {
Traversal traversal(trans, max_entries, out);
traversal.AddCreatesAndMoves(ready_unsynced_set);
traversal.AddDeletes(ready_unsynced_set);
}
}
}