<commit_before>/**
 * Copyright (C) 2009 - 2009 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.view.calc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.engine.view.View;
import com.opengamma.util.TerminatableJob;

/**
 * The primary job which will recalculate a {@link View}'s output data.
 */
public class ViewRecalculationJob extends TerminatableJob {
  private static final Logger s_logger = LoggerFactory.getLogger(ViewRecalculationJob.class);
  
  private static final long NANOS_PER_MILLISECOND = 1000000;
  
  private final View _view;
  private double _numExecutions;
  private SingleComputationCycle _previousCycle;
  
  /** Nanoseconds */
  private long _nextDeltaRecalculationTime = Long.MIN_VALUE;
  
  /** Nanoseconds */
  private long _nextFullRecalculationTime = Long.MIN_VALUE;
  
  /**
   * Nanoseconds
   */
  private double _totalTimeNanos;
  
  public ViewRecalculationJob(View view) {
    if (view == null) {
      throw new NullPointerException("Must provide a backing view.");
    }
    _view = view;
  }

  /**
   * @return the view
   */
  public View getView() {
    return _view;
  }
  
  private void setNextDeltaRecalculationTime(long currentNanos) {
    if (getView().getDefinition().getDeltaRecalculationPeriod() == null) {
      _nextDeltaRecalculationTime = Long.MAX_VALUE;
    } else {
      _nextDeltaRecalculationTime = currentNanos + NANOS_PER_MILLISECOND * getView().getDefinition().getDeltaRecalculationPeriod();
    }
  }
  
  private void setNextFullRecalculationTime(long currentNanos) {
    if (getView().getDefinition().getFullRecalculationPeriod() == null) {
      _nextFullRecalculationTime = Long.MAX_VALUE;
    } else {
      _nextFullRecalculationTime = currentNanos + NANOS_PER_MILLISECOND * getView().getDefinition().getFullRecalculationPeriod();
    }
  }
  
  @Override
  protected void runOneCycle() {
    
    boolean doFullRecalc = false;
    long currentTime = System.nanoTime();
    if (currentTime >= _nextFullRecalculationTime) {
      doFullRecalc = true;
      setNextFullRecalculationTime(currentTime);
      setNextDeltaRecalculationTime(currentTime); // make sure delta and full are in sync
    }
    
    boolean doDeltaRecalc = false;
    if (currentTime >= _nextDeltaRecalculationTime) {
      doDeltaRecalc = true;
      setNextDeltaRecalculationTime(currentTime);
    }
    
    if (!doFullRecalc && !doDeltaRecalc) {
      long nextTimeToCheck = Math.min(_nextDeltaRecalculationTime, _nextFullRecalculationTime);
      long delay = nextTimeToCheck - currentTime;
      delay = Math.max(0, delay);
      delay /= NANOS_PER_MILLISECOND;
      delay += 1; // round up a bit to make sure it'll be enough
      s_logger.info("Waiting for {} ms", delay);
      try {
        synchronized (this) {
          // This could wait until end of time if both minimum and full recalc periods are null.
          // In this case, you need to call liveDataChanged() to wake it up
          wait(delay);
        }
      } catch (InterruptedException e) {
        Thread.interrupted();<commit_msg>Lower the "waiting for" message from INFO to DEBUG<commit_after>/**
 * Copyright (C) 2009 - 2009 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.engine.view.calc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.engine.view.View;
import com.opengamma.util.TerminatableJob;

/**
 * The primary job which will recalculate a {@link View}'s output data.
 */
public class ViewRecalculationJob extends TerminatableJob {
  private static final Logger s_logger = LoggerFactory.getLogger(ViewRecalculationJob.class);
  
  private static final long NANOS_PER_MILLISECOND = 1000000;
  
  private final View _view;
  private double _numExecutions;
  private SingleComputationCycle _previousCycle;
  
  /** Nanoseconds */
  private long _nextDeltaRecalculationTime = Long.MIN_VALUE;
  
  /** Nanoseconds */
  private long _nextFullRecalculationTime = Long.MIN_VALUE;
  
  /**
   * Nanoseconds
   */
  private double _totalTimeNanos;
  
  public ViewRecalculationJob(View view) {
    if (view == null) {
      throw new NullPointerException("Must provide a backing view.");
    }
    _view = view;
  }

  /**
   * @return the view
   */
  public View getView() {
    return _view;
  }
  
  private void setNextDeltaRecalculationTime(long currentNanos) {
    if (getView().getDefinition().getDeltaRecalculationPeriod() == null) {
      _nextDeltaRecalculationTime = Long.MAX_VALUE;
    } else {
      _nextDeltaRecalculationTime = currentNanos + NANOS_PER_MILLISECOND * getView().getDefinition().getDeltaRecalculationPeriod();
    }
  }
  
  private void setNextFullRecalculationTime(long currentNanos) {
    if (getView().getDefinition().getFullRecalculationPeriod() == null) {
      _nextFullRecalculationTime = Long.MAX_VALUE;
    } else {
      _nextFullRecalculationTime = currentNanos + NANOS_PER_MILLISECOND * getView().getDefinition().getFullRecalculationPeriod();
    }
  }
  
  @Override
  protected void runOneCycle() {
    
    boolean doFullRecalc = false;
    long currentTime = System.nanoTime();
    if (currentTime >= _nextFullRecalculationTime) {
      doFullRecalc = true;
      setNextFullRecalculationTime(currentTime);
      setNextDeltaRecalculationTime(currentTime); // make sure delta and full are in sync
    }
    
    boolean doDeltaRecalc = false;
    if (currentTime >= _nextDeltaRecalculationTime) {
      doDeltaRecalc = true;
      setNextDeltaRecalculationTime(currentTime);
    }
    
    if (!doFullRecalc && !doDeltaRecalc) {
      long nextTimeToCheck = Math.min(_nextDeltaRecalculationTime, _nextFullRecalculationTime);
      long delay = nextTimeToCheck - currentTime;
      delay = Math.max(0, delay);
      delay /= NANOS_PER_MILLISECOND;
      delay += 1; // round up a bit to make sure it'll be enough
      s_logger.debug("Waiting for {} ms", delay);
      try {
        synchronized (this) {
          // This could wait until end of time if both minimum and full recalc periods are null.
          // In this case, you need to call liveDataChanged() to wake it up
          wait(delay);
        }
      } catch (InterruptedException e) {
        Thread.interrupted();
{'fraction_non_alphanumeric': 0.09111111111111111, 'fraction_numerical': 0.006031746031746032, 'mean_word_length': 3.384829505915101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141001', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1909, 'n_words': 463}