<commit_before>package org.multibit.hd.core.services;

import com.google.common.base.Optional;
import com.google.common.util.concurrent.Uninterruptibles;
import org.multibit.hd.core.dto.WalletId;
import org.multibit.hd.core.dto.WalletSummary;
import org.multibit.hd.core.events.ShutdownEvent;
import org.multibit.hd.core.managers.BackupManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * <p>Service to provide the following to application API:</p>
 * <ul>
 * <li>Access to rolling, local zip and cloud zip backups</li>
 * </ul>
 * <p/>
 * Wallet backup strategy:
 * <p/>
 * <p/>
 * Bitcoinj wallet writes
 * <p/>
 * this is a two stage write
 * wallet.saveNow is done instantly
 * wallet.saveLater - set to a 30 seconds period
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Rolling backups
 * <p/>
 * make saves every 2 minutes
 * make first save 1 minutes after MBHD start (most likely after initial sync)
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Local zip backups
 * <p/>
 * make saves every 10 minutes
 * make first save 1 minutes after MBHD start
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Cloud backups
 * <p/>
 * make saves every 30 minutes
 * make first save 1 minutes after MBHD start
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Do not bother tracking if the wallet is dirty, this only really affects the rolling backups and
 * is not worth the bother of writing a wallet extension to track it.
 *
 * @since 0.0.1
 */
public class BackupService extends AbstractService {

  /**
   * Initial delay in seconds after startup before making a backup
   * This delay is so that the wallet can sync
   */
  private static final int INITIAL_DELAY = 60;

  /**
   * This is the fastest tick in seconds used for backups
   * Everything else is done on a multiple of this
   */
  private static final int TICK_TIME_SECONDS = 120;
  /**
   * The slowdown rate for performing local zip backups
   */
  private static final int LOCAL_ZIP_BACKUP_MODULO = 5;
  /**
   * The slowdown rate for performing local zip backups
   */

  private static final int CLOUD_ZIP_BACKUP_MODULO = 15;

  private static final Logger log = LoggerFactory.getLogger(BackupService.class);

  /**
   * The number of times the backup main loop has incremented
   */
  private static int tickCount;

  /**
   * The wallet summary to use for the next rolling backup
   */
  private Optional<WalletSummary> rememberedWalletSummaryForRollingBackup = Optional.absent();

  /**
   * The credentials to use for the next rolling backup
   */
  private Optional<CharSequence> rememberedPasswordForRollingBackup = Optional.absent();

  /**<commit_msg>#323 Findbugs: Avoid instance writing to static (BackupService should be a singleton)<commit_after>package org.multibit.hd.core.services;

import com.google.common.base.Optional;
import com.google.common.util.concurrent.Uninterruptibles;
import org.multibit.hd.core.dto.WalletId;
import org.multibit.hd.core.dto.WalletSummary;
import org.multibit.hd.core.events.ShutdownEvent;
import org.multibit.hd.core.managers.BackupManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * <p>Service to provide the following to application API:</p>
 * <ul>
 * <li>Access to rolling, local zip and cloud zip backups</li>
 * </ul>
 * <p/>
 * Wallet backup strategy:
 * <p/>
 * <p/>
 * Bitcoinj wallet writes
 * <p/>
 * this is a two stage write
 * wallet.saveNow is done instantly
 * wallet.saveLater - set to a 30 seconds period
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Rolling backups
 * <p/>
 * make saves every 2 minutes
 * make first save 1 minutes after MBHD start (most likely after initial sync)
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Local zip backups
 * <p/>
 * make saves every 10 minutes
 * make first save 1 minutes after MBHD start
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Cloud backups
 * <p/>
 * make saves every 30 minutes
 * make first save 1 minutes after MBHD start
 * make a save at MBHD exit
 * <p/>
 * <p/>
 * Do not bother tracking if the wallet is dirty, this only really affects the rolling backups and
 * is not worth the bother of writing a wallet extension to track it.
 *
 * @since 0.0.1
 */
public class BackupService extends AbstractService {

  private static final Logger log = LoggerFactory.getLogger(BackupService.class);

  /**
   * Initial delay in seconds after startup before making a backup
   * This delay is so that the wallet can sync
   */
  private static final int INITIAL_DELAY = 60;

  /**
   * This is the fastest tick in seconds used for backups
   * Everything else is done on a multiple of this
   */
  private static final int TICK_TIME_SECONDS = 120;

  /**
   * The slowdown rate for performing local zip backups
   */
  private static final int LOCAL_ZIP_BACKUP_MODULO = 5;

  /**
   * The slowdown rate for performing local zip backups
   */
  private static final int CLOUD_ZIP_BACKUP_MODULO = 15;

  /**
   * The number of times the backup main loop has incremented
   * We expect this service to be a singleton
   */
  private int tickCount;

  /**
   * The wallet summary to use for the next rolling backup
   */
  private Optional<WalletSummary> rememberedWalletSummaryForRollingBackup = Optional.absent();

  /**
   * The credentials to use for the next rolling backup
   */
  private Optional<CharSequence> rememberedPasswordForRollingBackup = Optional.absent();

  /**
{'fraction_non_alphanumeric': 0.09194364161849711, 'fraction_numerical': 0.008851156069364163, 'mean_word_length': 3.443820224719101, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '4334542', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1659, 'n_words': 650}