Sha256: 94904bf83351c9a4623c55abd26e901e9140e7217a4cfde04d162f910e622546

Contents?: true

Size: 980 Bytes

Versions: 1

Compression:

Stored size: 980 Bytes

Contents

require 'java'
java_package 'jp.gr.java_conf.ogibayashi.norikra.udf'

class Lookup  
  FILE_UPDATE_CHECK_INTERVAL = 60
  @@lookup_table = Hash.new
  @@next_update_check = nil
  
  def self.read_tsv(path)
    info "Lookup#read_tsv Loading file : #{path}" if @logger
    @@next_update_check = Time.now + FILE_UPDATE_CHECK_INTERVAL
    data = File.open(path).readlines.map do |line|
      line.chomp.split("\t")
    end
    Hash[data]
  end

  def self.lookup_tsv(path,key,default_value)
    unless @@lookup_table[path]
      @@lookup_table[path] = self.read_tsv(path)
    end
    if Time.now > @@next_update_check
      # Check if file was updated in last FILE_UPDATE_CHECK_INTERVAL sec.
      if File::Stat.new(path).mtime > (@@next_update_check - FILE_UPDATE_CHECK_INTERVAL)
        @@lookup_table[path] = self.read_tsv(path)
      else
        @@next_update_check = Time.now + FILE_UPDATE_CHECK_INTERVAL
      end
    end
    @@lookup_table[path][key] || default_value
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
norikra-udf-lookup-0.0.2-java lib/esper_plugin/lookup.rb