<commit_before>package hm.edu.pulsebuddy.data.perst;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.garret.perst.Aggregator;
import org.garret.perst.Aggregator.Aggregate;
import org.garret.perst.IterableIterator;
import org.garret.perst.Storage;
import org.garret.perst.StorageFactory;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class PerstStorage
{
  private static final String TAG = "perst.storage";

  /* Stored on the external storage (SD-Card) */
  private static final String DB_PATH = "/perst/pulsebuddy.dbs";

  /* The database object. */
  private Storage db;

  /* Perst root class. */
  private PerstRootClass root;

  private static final int pagePoolSize = 32 * 1024 * 1024;

  private static final String[] months = { "Jan", "Feb", "Mar", "Apr", "May",
      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

  public PerstStorage( Context context )
  {
    String databasePath = Environment.getExternalStorageDirectory()
        .getAbsolutePath() + DB_PATH;

    db = StorageFactory.getInstance().createStorage();
    db.open( databasePath, pagePoolSize );

    root = (PerstRootClass) db.getRoot();
    if ( root == null )
    {
      root = new PerstRootClass( db );
      db.setRoot( root );
    }
  }

  public Boolean addPulseValue( int aPulseValue )
  {
    Pulse pulse = new Pulse();
    pulse.date = new Date().getTime();
    pulse.value = aPulseValue;

    Boolean success = root.pulses.add( pulse );
    db.commit();
    Log.d( TAG, "Saved pulse " + success );

    return success;
  }

  public void printPulseStatistics()
  {
    int i;
    Iterator<Pulse> iterator = root.pulses.iterator();
    for ( i = 0; iterator.hasNext(); i++ )<commit_msg>Database folders are now created on SD-Card if they not exist.<commit_after>package hm.edu.pulsebuddy.data.perst;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;

import org.garret.perst.Storage;
import org.garret.perst.StorageFactory;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class PerstStorage
{
  private static final String TAG = "perst.storage";

  /* Stored on the external storage (SD-Card) */
  private static final String DB_PATH = "/perst/";
  private static final String DB_FILE = "pulsebuddy.dbs";

  /* The database object. */
  private Storage db;

  /* Perst root class. */
  private PerstRootClass root;

  /* TODO-tof: Investigate */
  private static final int pagePoolSize = 32 * 1024 * 1024;

  public PerstStorage( Context context )
  {
    /* Create the path on the SD-Card if it not exists. */
    File dbPath = new File( Environment.getExternalStorageDirectory()
        .getAbsolutePath() + DB_PATH );
    if ( !( dbPath.exists() && dbPath.isDirectory() ) )
    {
      dbPath.mkdirs();
    }

    File dbFile = new File( dbPath, DB_FILE );
    try
    {
      dbFile.createNewFile();
    }
    catch ( IOException e )
    {
      Log.e( TAG, "Error creating DB file." );
    }

    String databasePath = Environment.getExternalStorageDirectory()
        .getAbsolutePath() + DB_PATH + DB_FILE;

    db = StorageFactory.getInstance().createStorage();
    db.open( databasePath, pagePoolSize );

    root = (PerstRootClass) db.getRoot();
    if ( root == null )
    {
      root = new PerstRootClass( db );
      db.setRoot( root );
    }
  }

  public synchronized Boolean addPulseValue( int aPulseValue )
  {
    Pulse pulse = new Pulse();
    pulse.date = new Date().getTime();
    pulse.value = aPulseValue;

    Boolean success = root.pulses.add( pulse );
    db.commit();
    Log.d( TAG, "Saved pulse " + success );

    return success;
  }

  /**
   * Get some pulse related statistics.
   */
  public void printPulseStatistics()
  {
    int i;
    Iterator<Pulse> iterator = root.pulses.iterator();
    for ( i = 0; iterator.hasNext(); i++ )
{'fraction_non_alphanumeric': 0.12408204608761712, 'fraction_numerical': 0.005571030640668524, 'mean_word_length': 3.1274817136886104, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5041394', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1231, 'n_words': 336}