<commit_before>require 'sqlite3'

module SAAL
  class FileStore
    def initialize(opts = {:db => "sensor_reads.db"})
      @opts = opts
      @db = SQLite3::Database.new(@opts[:db])
      @db.type_translation = true
      @db.execute("create table if not exists sensor_reads
                    (sensor string, date integer, value real)")
      @mutex = Mutex.new
    end
    
    def write(sensor, date, value)
      @db.execute("insert into sensor_reads values (?,?,?)", 
                  sensor.to_s, date, value)
    end
    
    def average(sensor, from, to)
      r = @db.execute("select avg(value) as average from sensor_reads
                        where sensor = ? and date >= ? and date <= ?",
                       sensor.to_s, from, to)
      if r[0][0]
        Float(r[0][0])
      else
        nil
      end
    end
    
    def close
      @db.close
    end
    
    # Guard operations that access the database with a lock<commit_msg>Add indexes to the database to try and speedup chart creation.<commit_after>require 'sqlite3'

module SAAL
  class FileStore
    def initialize(opts = {:db => "sensor_reads.db"})
      @opts = opts
      @db = SQLite3::Database.new(@opts[:db])
      @db.type_translation = true
      @db.execute("create table if not exists sensor_reads
                    (sensor string, date integer, value real)")
      @db.execute("create index if not exists sensor_reads_sensor_index on
                    sensor_reads(sensor)")
      @db.execute("create index if not exists sensor_reads_date_index on
                    sensor_reads(date)")
      @mutex = Mutex.new
    end
    
    def write(sensor, date, value)
      @db.execute("insert into sensor_reads values (?,?,?)", 
                  sensor.to_s, date, value)
    end
    
    def average(sensor, from, to)
      r = @db.execute("select avg(value) as average from sensor_reads
                        where sensor = ? and date >= ? and date <= ?",
                       sensor.to_s, from, to)
      if r[0][0]
        Float(r[0][0])
      else
        nil
      end
    end
    
    def close
      @db.close
    end
    
    # Guard operations that access the database with a lock
{'fraction_non_alphanumeric': 0.102880658436214, 'fraction_numerical': 0.0054869684499314125, 'mean_word_length': 1.9173333333333333, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7010291', 'n_tokens_mistral': 661, 'n_tokens_neox': 631, 'n_words': 191}