Module: CorrectHorseBatteryStaple::Backend::Redis::InstanceMethods

Defined in:
lib/correct_horse_battery_staple/backend/redis.rb

Instance Method Summary collapse

Instance Method Details

#add_word(w, wid = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 25

def add_word(w, wid=nil)
  percentile = [0, w.percentile].max

  wid = get_new_word_id if wid.nil?

  db.zadd(@words_key, wid, w.word)
  db.zadd(@percentile_key, percentile, wid)
  # db.zadd(@frequency_key, w.frequency, wid)
  db.zadd(@lenprod_key, w.word.length + (percentile / 100.0), wid)
end

#close_databaseObject



80
81
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 80

def close_database
end

#create_databaseObject



56
57
58
59
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 56

def create_database
  db.del(@length_key, @percentile_key, @frequency_key, @lenprod_key,  @stats_key,
         @words_key, @id_key)
end

#dbObject



76
77
78
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 76

def db
  @db || open_database
end

#gensym_tempObject



87
88
89
90
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 87

def gensym_temp
  @_gensym_id ||= 0
  make_key("TEMP_#{Process.pid}_#{@gensym_id += 1}")
end

#get_new_word_idObject

Note that this does NOT work inside a multi/exec



39
40
41
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 39

def get_new_word_id
  db.incr(@id_key)
end

#get_word_by_id(wid) ⇒ Object



43
44
45
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 43

def get_word_by_id(wid)
  db.zrangebyscore(@words_key, wid, wid, :limit => [0,1])[0] rescue nil
end

#load_statsObject



47
48
49
50
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 47

def load_stats
  #noinspection RubyHashKeysTypesInspection
  load_stats_from_hash Hash[db.hgetall(@stats_key).map {|k,v| [k, v.to_f]}]
end

#make_key(name) ⇒ Object



83
84
85
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 83

def make_key(name)
  "chbs_#{options[:dbname]}_#{name}"
end

#open_databaseObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 61

def open_database
  @db ||= begin
            @gensym_id             = 0
            @length_key            = make_key("length_zset")
            @percentile_key        = make_key("percentile_zset")
            @frequency_key         = make_key("frequency_zset")
            # scores given by w.word.length + w.percentile/100.0
            @lenprod_key           = make_key("lenprod_zset")
            @stats_key             = make_key("stats_hash")
            @words_key             = make_key("words_zset")
            @id_key                = make_key("word_id_counter")
            ::Redis.new(:host => options[:host], :port => options[:port])
          end
end

#parse_uri(dest) ⇒ Object



18
19
20
21
22
23
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 18

def parse_uri(dest)
  (dbname, host, port)   = dest.gsub(/\.redis[0-9]?/, '').split(':')
  options[:dbname]     ||= (dbname || "chbs")
  options[:host]       ||= (host   || "127.0.0.1")
  options[:port]       ||= (port  || 6379).to_i
end

#save_stats(stats) ⇒ Object



52
53
54
# File 'lib/correct_horse_battery_staple/backend/redis.rb', line 52

def save_stats(stats)
  db.hmset @stats_key, *stats.to_a.flatten
end