Module: LightStore::ClassMethods

Defined in:
lib/light_store/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_data(data) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/light_store/class_methods.rb', line 145

def add_data(data)
  added_records_count = 0
  data.each do |h|
    data_row_key = get_data_row_key(h)
    marshaled_h = marshal(h)
    new_member_check = add_member_id(h)
    if new_member_check
      datastore.set(data_row_key, marshaled_h)
      add_sorted_member_ids(h)
    else
      old_value = datastore.getset(data_row_key, marshaled_h)
      if old_value != marshaled_h
        add_sorted_member_ids(h)
      end
    end
    added_records_count += 1 if new_member_check
  end
  #plural_records = added_records_count == 1 ? "record" : "records"
  return added_records_count
end

#add_member_id(h) ⇒ Object



116
117
118
119
120
# File 'lib/light_store/class_methods.rb', line 116

def add_member_id(h)
  member_id_key = get_member_id_key(h)
  data_row_key = get_data_row_key(h)
  datastore.sadd(member_id_key, data_row_key)
end

#add_sorted_member_id(h, field_name, field_type) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/light_store/class_methods.rb', line 130

def add_sorted_member_id(h, field_name, field_type)
  primary_id = get_primary_id(h)
  sorted_member_id = get_sorted_member_id_key(primary_id, field_name)
  data_row_key = get_data_row_key(h)
  value = h[field_name]
  score = get_score(value, field_type)
  datastore.zadd(sorted_member_id, score, data_row_key)
end

#add_sorted_member_ids(h) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/light_store/class_methods.rb', line 122

def add_sorted_member_ids(h)
  sortable_fields.each do |s|
    field_name = s[:field_name]
    field_type = s[:field_type]
    add_sorted_member_id(h, field_name, field_type)
  end
end

#base_data_keyObject



64
65
66
# File 'lib/light_store/class_methods.rb', line 64

def base_data_key
  "#{base_key}:data"
end

#base_keyObject



60
61
62
# File 'lib/light_store/class_methods.rb', line 60

def base_key
  "#{namespace}:#{prefix}"
end

#base_member_ids_keyObject



68
69
70
# File 'lib/light_store/class_methods.rb', line 68

def base_member_ids_key
  "#{base_key}:member_ids"
end

#base_sorted_member_ids_keyObject



72
73
74
# File 'lib/light_store/class_methods.rb', line 72

def base_sorted_member_ids_key
  "#{base_key}:sorted_member_ids"
end

#clear(keys) ⇒ Object

make private.



200
201
202
# File 'lib/light_store/class_methods.rb', line 200

def clear(keys)
  deleted_count = datastore.del(keys) unless keys.empty?
end

#clear_allObject



194
195
196
197
# File 'lib/light_store/class_methods.rb', line 194

def clear_all
  all_keys = datastore.keys("#{base_key}*")
  clear(all_keys)
end

#clear_all_dataObject



187
188
189
190
191
192
# File 'lib/light_store/class_methods.rb', line 187

def clear_all_data
  data_keys = datastore.keys("#{base_member_ids_key}*")
  data_keys = data_keys.concat(datastore.keys("#{base_sorted_member_ids_key}*"))
  data_keys = data_keys.concat(datastore.keys("#{base_data_key}*"))
  clear(data_keys)
end

#datastoreObject



3
4
5
# File 'lib/light_store/class_methods.rb', line 3

def datastore
  LightStore.configuration.redis
end

#get_data(_primary_key = nil) ⇒ Object



166
167
168
169
170
171
# File 'lib/light_store/class_methods.rb', line 166

def get_data(_primary_key = nil)
  data_keys = _primary_key ? get_member_ids(_primary_key) : get_member_ids()
  return [] if data_keys.empty?
  marshaled_data = datastore.mget(data_keys)
  unmarshaled_data = marshaled_data.collect{ |d| unmarshal(d) }
end

#get_data_row_key(h) ⇒ Object



86
87
88
# File 'lib/light_store/class_methods.rb', line 86

def get_data_row_key(h)
  "#{base_data_key}:#{get_primary_id(h)}:#{get_secondary_id(h)}"
end

#get_member_id_key(h) ⇒ Object



90
91
92
# File 'lib/light_store/class_methods.rb', line 90

def get_member_id_key(h)
  "#{base_member_ids_key}:#{get_primary_id(h)}"
end

#get_member_ids(_primary_key = nil) ⇒ Object



139
140
141
142
143
# File 'lib/light_store/class_methods.rb', line 139

def get_member_ids(_primary_key = nil)
  key = _primary_key ? "#{base_member_ids_key}:#{_primary_key}" : base_member_ids_key
  member_keys = datastore.keys("#{key}*")
  member_keys.collect{ |k| datastore.smembers(k) }.flatten
end

#get_primary_id(h) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
# File 'lib/light_store/class_methods.rb', line 76

def get_primary_id(h)
  raise ArgumentError, 'primary_id must be set' unless h[primary_key]
  h[primary_key]
end

#get_score(value, sortable_field_type) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/light_store/class_methods.rb', line 98

def get_score(value, sortable_field_type)
  raise ArgumentError, "value for #{sortable_field_type} must be set" unless value
  case sortable_field_type
  when :integer
    value.to_i
  when :float
    value.to_f
  when :datetime
    if value.is_a? Time
      value.to_i
    else
      Time.parse(value).to_i
    end
  else
    raise ArgumentError, "score value for #{sortable_field_type} must be in proper format"
  end
end

#get_secondary_id(h) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
# File 'lib/light_store/class_methods.rb', line 81

def get_secondary_id(h)
  raise ArgumentError, 'secondary_key must be set' unless h[secondary_key]
  h[secondary_key]
end

#get_sorted_data(_primary_key, _sortable_field, min, max, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/light_store/class_methods.rb', line 173

def get_sorted_data(_primary_key, _sortable_field, min, max, options = {})
  grouped_sortable_fields = sortable_fields.group_by { |f| f[:field_name] }
  raise ArgumentError, "No sortable fields declared" if grouped_sortable_fields.empty?
  raise ArgumentError, "sortable field '#{_sortable_field}' not declared" unless grouped_sortable_fields.has_key?(_sortable_field)
  field_type = grouped_sortable_fields[_sortable_field].first[:field_type]
  min = get_score(min, field_type) unless min == '-inf'
  max = get_score(max, field_type) unless max == '+inf'
  sorted_member_id = get_sorted_member_id_key(_primary_key, _sortable_field)

  data_keys = datastore.zrangebyscore(sorted_member_id, min, max, options)
  marshaled_data = datastore.mget(data_keys)
  unmarshaled_data = marshaled_data.collect{ |d| unmarshal(d) }
end

#get_sorted_member_id_key(_primary_id, _sortable_field) ⇒ Object



94
95
96
# File 'lib/light_store/class_methods.rb', line 94

def get_sorted_member_id_key(_primary_id, _sortable_field)
  "#{base_sorted_member_ids_key}:#{_primary_id}:#{_sortable_field}"
end

#marshal(h) ⇒ Object



52
53
54
# File 'lib/light_store/class_methods.rb', line 52

def marshal(h)
  Marshal.dump(h)
end

#namespaceObject



11
12
13
# File 'lib/light_store/class_methods.rb', line 11

def namespace
  @namespace ||= 'light_store'
end

#prefixObject



19
20
21
# File 'lib/light_store/class_methods.rb', line 19

def prefix
  @prefix ||= self.name
end

#primary_keyObject



27
28
29
# File 'lib/light_store/class_methods.rb', line 27

def primary_key
  @primary_key ||= :id
end

#secondary_keyObject

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/light_store/class_methods.rb', line 35

def secondary_key
  raise ArgumentError, 'secondary_key must be set' unless @secondary_key
  @secondary_key
end

#set_namespace(x) ⇒ Object



7
8
9
# File 'lib/light_store/class_methods.rb', line 7

def set_namespace(x)
  @namespace = x
end

#set_prefix(x) ⇒ Object



15
16
17
# File 'lib/light_store/class_methods.rb', line 15

def set_prefix(x)
  @prefix = x
end

#set_primary_key(x) ⇒ Object



23
24
25
# File 'lib/light_store/class_methods.rb', line 23

def set_primary_key(x)
  @primary_key = x
end

#set_secondary_key(x) ⇒ Object



31
32
33
# File 'lib/light_store/class_methods.rb', line 31

def set_secondary_key(x)
  @secondary_key = x
end

#set_sortable_field(field_name, field_type = :integer) ⇒ Object

Raises:

  • (ArgumentError)


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

def set_sortable_field(field_name, field_type = :integer)
  allowed_field_types = [:integer, :float, :datetime]
  raise ArgumentError, 'field_type must be [:integer, :float, :datetime]' unless allowed_field_types.include?(field_type)
  h = {field_name: field_name, field_type: field_type}
  @sortable_fields = self.sortable_fields
  @sortable_fields.push(h) unless @sortable_fields.include?(h)
end

#sortable_fieldsObject



48
49
50
# File 'lib/light_store/class_methods.rb', line 48

def sortable_fields
  @sortable_fields ||= []
end

#unmarshal(h) ⇒ Object



56
57
58
# File 'lib/light_store/class_methods.rb', line 56

def unmarshal(h)
  Marshal.load(h)
end