<commit_before>// Copyright (c) 2009 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 "chrome/browser/sync/engine/update_applicator.h"

#include <vector>

#include "base/logging.h"
#include "chrome/browser/sync/engine/syncer_util.h"
#include "chrome/browser/sync/sessions/session_state.h"
#include "chrome/browser/sync/syncable/syncable.h"
#include "chrome/browser/sync/syncable/syncable_id.h"

using std::vector;

namespace browser_sync {

UpdateApplicator::UpdateApplicator(ConflictResolver* resolver,
                                   const UpdateIterator& begin,
                                   const UpdateIterator& end,
                                   const ModelSafeRoutingInfo& routes,
                                   ModelSafeGroup group_filter)
    : resolver_(resolver),
      begin_(begin),
      end_(end),
      pointer_(begin),
      group_filter_(group_filter),
      progress_(false),
      routing_info_(routes) {
    size_t item_count = end - begin;
    LOG(INFO) << "UpdateApplicator created for " << item_count << " items.";
    successful_ids_.reserve(item_count);
  }

// Returns true if there's more to do.
bool UpdateApplicator::AttemptOneApplication(
    syncable::WriteTransaction* trans) {
  // If there are no updates left to consider, we're done.
  if (end_ == begin_)
    return false;
  if (pointer_ == end_) {
    if (!progress_)
      return false;

    LOG(INFO) << "UpdateApplicator doing additional pass.";
    pointer_ = begin_;
    progress_ = false;

    // Clear the tracked failures to avoid double-counting.
    conflicting_ids_.clear();
  }
  syncable::MutableEntry entry(trans, syncable::GET_BY_HANDLE, *pointer_);

  if (SkipUpdate(entry)) {
    Advance();
    return true;
  }

  UpdateAttemptResponse updateResponse =
      SyncerUtil::AttemptToUpdateEntry(trans, &entry, resolver_);
  switch (updateResponse) {
    case SUCCESS:
      Advance();
      progress_ = true;
      successful_ids_.push_back(entry.Get(syncable::ID));
      break;
    case CONFLICT:
      pointer_++;
      conflicting_ids_.push_back(entry.Get(syncable::ID));
      break;
    default:
      NOTREACHED();
      break;
  }
  LOG(INFO) << "Apply Status for " << entry.Get(syncable::META_HANDLE)
            << " is " << updateResponse;

  return true;
}

void UpdateApplicator::Advance() {
  --end_;
  *pointer_ = *end_;
}

bool UpdateApplicator::SkipUpdate(const syncable::MutableEntry& entry) {
  ModelSafeGroup g = GetGroupForModelType(entry.GetModelType(), routing_info_);
  if (g != group_filter_)
    return true;
  return false;
}

bool UpdateApplicator::AllUpdatesApplied() const {
  return conflicting_ids_.empty() && begin_ == end_;
}
<commit_msg>When applying updates, the UpdateApplicator should check ServerModelType and not the local version, as it may not have been set yet.  I messed this up as part of my multi-pass syncer command change yesterday.<commit_after>// Copyright (c) 2009 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 "chrome/browser/sync/engine/update_applicator.h"

#include <vector>

#include "base/logging.h"
#include "chrome/browser/sync/engine/syncer_util.h"
#include "chrome/browser/sync/sessions/session_state.h"
#include "chrome/browser/sync/syncable/syncable.h"
#include "chrome/browser/sync/syncable/syncable_id.h"

using std::vector;

namespace browser_sync {

UpdateApplicator::UpdateApplicator(ConflictResolver* resolver,
                                   const UpdateIterator& begin,
                                   const UpdateIterator& end,
                                   const ModelSafeRoutingInfo& routes,
                                   ModelSafeGroup group_filter)
    : resolver_(resolver),
      begin_(begin),
      end_(end),
      pointer_(begin),
      group_filter_(group_filter),
      progress_(false),
      routing_info_(routes) {
    size_t item_count = end - begin;
    LOG(INFO) << "UpdateApplicator created for " << item_count << " items.";
    successful_ids_.reserve(item_count);
  }

// Returns true if there's more to do.
bool UpdateApplicator::AttemptOneApplication(
    syncable::WriteTransaction* trans) {
  // If there are no updates left to consider, we're done.
  if (end_ == begin_)
    return false;
  if (pointer_ == end_) {
    if (!progress_)
      return false;

    LOG(INFO) << "UpdateApplicator doing additional pass.";
    pointer_ = begin_;
    progress_ = false;

    // Clear the tracked failures to avoid double-counting.
    conflicting_ids_.clear();
  }
  syncable::MutableEntry entry(trans, syncable::GET_BY_HANDLE, *pointer_);

  if (SkipUpdate(entry)) {
    Advance();
    return true;
  }

  UpdateAttemptResponse updateResponse =
      SyncerUtil::AttemptToUpdateEntry(trans, &entry, resolver_);
  switch (updateResponse) {
    case SUCCESS:
      Advance();
      progress_ = true;
      successful_ids_.push_back(entry.Get(syncable::ID));
      break;
    case CONFLICT:
      pointer_++;
      conflicting_ids_.push_back(entry.Get(syncable::ID));
      break;
    default:
      NOTREACHED();
      break;
  }
  LOG(INFO) << "Apply Status for " << entry.Get(syncable::META_HANDLE)
            << " is " << updateResponse;

  return true;
}

void UpdateApplicator::Advance() {
  --end_;
  *pointer_ = *end_;
}

bool UpdateApplicator::SkipUpdate(const syncable::MutableEntry& entry) {
  ModelSafeGroup g =
      GetGroupForModelType(entry.GetServerModelType(), routing_info_);
  if (g != group_filter_)
    return true;
  return false;
}

bool UpdateApplicator::AllUpdatesApplied() const {
  return conflicting_ids_.empty() && begin_ == end_;
}

{'fraction_non_alphanumeric': 0.10935078353711039, 'fraction_numerical': 0.0013776476666092646, 'mean_word_length': 3.1133144475920678, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4324709', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1710, 'n_words': 423}