Module: Hashish::ClassMethods

Defined in:
lib/acts_as_hashish/hashish.rb

Instance Method Summary collapse

Instance Method Details

#hashish_delete(key = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/acts_as_hashish/hashish.rb', line 40

def hashish_delete(key = nil)
  key ||= hashish_get_key(hashish_list.first)
  prefix = @options[:key_prefix]
  Hashish.redis_connection.zrem("#{prefix}*", "#{prefix}@#{key}")
  ("#{key}")
  Hashish.redis_connection.del("#{prefix}@#{key}")
end

#hashish_flush!Object



32
33
34
# File 'lib/acts_as_hashish/hashish.rb', line 32

def hashish_flush!
  hashish_redis_keys.each{|x| Hashish.redis_connection.del(x)}
end

#hashish_insert(data, t = Time.now.to_i) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/acts_as_hashish/hashish.rb', line 48

def hashish_insert(data, t = Time.now.to_i)
  if data.is_a?(Array)
    data.each do |d|
      hashish_single_insert(d, t)
    end
  elsif data.is_a?(Hash)
    hashish_single_insert(data, t)
  else
    raise "Data must be of type Hash or Array of Hash"
  end
end

#hashish_lengthObject



36
37
38
# File 'lib/acts_as_hashish/hashish.rb', line 36

def hashish_length
  Hashish.redis_connection.zcard("#{@options[:key_prefix]}*")
end

#hashish_list(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/acts_as_hashish/hashish.rb', line 60

def hashish_list(options = {})
  page_no = options[:page_no] || 1
  page_size = options[:page_size] || 10
  
  max_time = options[:to] || '+inf'
  min_time = options[:from] || '-inf'
  
  filters = options[:filters] || {}
  sort_by = options[:sort_by] || nil
  sort_order = options[:sort_order] || nil
  
  offset = (page_no - 1) * page_size
  limit = offset + page_size
  
  # get the next seq no for search operations for this search instance (perhaps this entire section shld be oops based stuff but what the heck :E mayb later)
  seq = Hashish.redis_connection.incr("#{@options[:key_prefix]}#SEQUENCER")
  
  # search
  inter = []
  unless filters.empty?
    # inter = filters.map{|k,v| "#{@options[:key_prefix]}:#{k}:#{v}"}
    
    filters.each do |key, value|
      if value.is_a?(Array)
        union = []
        union_key = "#{@options[:key_prefix]}#UNION##{key}##{seq}"
        value.each do |v|
          union << "#{@options[:key_prefix]}!#{key}=#{v}"
        end
        Hashish.redis_connection.zunionstore(union_key, union.uniq)
        Hashish.redis_connection.expire(union_key, Hashish.redis_search_keys_ttl)
        inter << union_key
      else
        inter << "#{@options[:key_prefix]}!#{key}=#{value}"
      end
    end
    
  end
  
  full_list = "#{@options[:key_prefix]}*"
  
  # is the user askin for a cropped set of data (min/max date/time of nQ)
  if min_time == '-inf' and max_time == '+inf'
    inter << full_list
  else
    # copy the full list to different temp set
    all_items_key = "#{@options[:key_prefix]}#ALL_ITEMS##{seq}"
    Hashish.redis_connection.zunionstore(all_items_key, [full_list])
    Hashish.redis_connection.expire(all_items_key, Hashish.redis_search_keys_ttl * 60)
    # crop the set based on min/max (wish we had a 1 step zcroprangebyscore)
    Hashish.redis_connection.zremrangebyscore(all_items_key, "-inf", "(#{min_time}") if min_time != '-inf'
    Hashish.redis_connection.zremrangebyscore(all_items_key, "(#{max_time}", "+inf") if max_time != '+inf'
    inter << all_items_key
  end
  
  result_key = "#{@options[:key_prefix]}#RESULT##{seq}"
  Hashish.redis_connection.zinterstore(result_key, inter, :aggregate => 'max')
  Hashish.redis_connection.expire(result_key, Hashish.redis_search_keys_ttl * 60)
  result = nil
  if sort_by
    custom_sort_key = "#{@options[:key_prefix]}#CUSTOM_SORT##{seq}"
    Hashish.redis_connection.sort(result_key, :by => "*$#{sort_by}",:get => '*', :store => custom_sort_key, :order => sort_order)
    Hashish.redis_connection.expire(custom_sort_key, Hashish.redis_search_keys_ttl * 60)
    result = Hashish.redis_connection.lrange(custom_sort_key, offset, limit -1)
  else
    res_keys = Hashish.redis_connection.send("z#{(sort_order == 'DESC' ? 'rev' : '')}range".to_sym, result_key, offset, limit - 1)
    if res_keys.empty?
      result = []
    else
      result = Hashish.redis_connection.mget(*res_keys)
    end
  end
  result.compact.map{|x| JSON.parse(x) rescue x}
end

#hashish_rebuild(data = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/acts_as_hashish/hashish.rb', line 20

def hashish_rebuild(data = nil)
  hashish_wait_on_lock do
    data ||= hashish_list(:page_size => 0)
    hashish_flush!
  end
  begin
    yield if block_given?
  ensure
    hashish_insert(data, Time.now.to_i)
  end
end