Module: Cequel::Record::ClassMethods

Defined in:
lib/octocore-cassandra/models.rb

Overview

Override Cequel::Record here

Instance Method Summary collapse

Instance Method Details

#fake_data_with(args, values, opts = {}) ⇒ Object

fakes up data



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/octocore-cassandra/models.rb', line 46

def fake_data_with(args, values, opts={})
  res = []
  ts = args.fetch(:ts, 7.days.ago..Time.now.floor)
  if ts.class == Range
    bod = opts.fetch(:bod, false)
    ts_begin = ts.begin
    ts_end = ts.end
    if bod
      ts_begin = ts_begin.beginning_of_day
      ts_end = ts_end.end_of_day
    end
    step = opts.fetch(:step, 1.minute)
    ts_begin.to(ts_end, step).each do |_ts|
      _args = args.merge({ ts: _ts })
      r = self.where(_args)
      if r.count == 0
        res << self.new(_args.merge(values)).save!
      else
        res << r
      end
    end
  elsif ts.class == Time
    _args = args.merge({ ts: ts }).merge(values)
    res << self.new(_args).save!
  end
  res.flatten
end

#findOrCreate(args, options = {}) ⇒ Object

Finds the record/recordset satisfying a ‘where` condition

or create a new record from the params passed

Parameters:

  • args (Hash)

    The args used to build ‘where` condition

  • options (Hash) (defaults to: {})

    The options used to construct record



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/octocore-cassandra/models.rb', line 132

def findOrCreate(args, options = {})
  # attempt to find the record
  res = get_cached(args)

  # on failure, do
  unless res
    args.merge!(options)
    res = self.new(args).save!

    # Update cache
    cache_key = gen_cache_key(args)
    Cequel::Record.redis.setex(cache_key, get_ttl, Octo::Utils.serialize(res))
  end
  res
end

#findOrCreateOrAdjust(args, options) ⇒ Object

If a record exists in a COUNTER TABLE, it will find

it and increment or decrement it's value with the
provided options. Else, will just create the
record with default value.


153
154
155
# File 'lib/octocore-cassandra/models.rb', line 153

def findOrCreateOrAdjust(args, options)
  self.where(args).data_set.increment(options)
end

#findOrCreateOrUpdate(args, options = {}) ⇒ Object

If a record exists, will find it and update it’s value with the

provided options. Else, will just create the record.


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
# File 'lib/octocore-cassandra/models.rb', line 92

def findOrCreateOrUpdate(args, options = {})
  cache_key = gen_cache_key(args)
  res = get_cached(args)
  if res
    dirty = false

    # handle price separately because of float issues
    if options.has_key?(:price)
      _v = options.delete(:price)
      dirty = _v.round(2) != res.price.round(2)
    end

    # remaining opts
    options.each do |k, v|
      if res.respond_to?(k)
        unless res.public_send(k) == v
          dirty = true
          res.public_send("#{ k }=", v)
        end
      end
    end

    if dirty
      res.save!
      Cequel::Record.redis.setex(cache_key, get_ttl,
                                 Octo::Utils.serialize(res))
    end
  else
    _args = args.merge(options)
    res = self.new(_args).save!
    Cequel::Record.redis.setex(cache_key, get_ttl,
                               Octo::Utils.serialize(res))
  end
  res
end

#get_cached(args) ⇒ Cequel::Record::RecordSet

Perform a cache backed get

Parameters:

  • args (Hash)

    The arguments hash for the record to be found

Returns:

  • (Cequel::Record::RecordSet)

    The record matching



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/octocore-cassandra/models.rb', line 162

def get_cached(args)
  cache_key = gen_cache_key(args)

  begin
    cached_val = Cequel::Record.redis.get(cache_key)
  rescue Exception
    cached_val = nil
  end

  unless cached_val
    res = where(args)
    result_count = res.count
    if result_count == 0
      return nil
    elsif result_count == 1
      cached_val = Octo::Utils.serialize(res.first)
      Cequel::Record.redis.setex(cache_key, get_ttl, cached_val)
    elsif result_count > 1
      cached_val = Octo::Utils.serialize(res)
      Cequel::Record.redis.setex(cache_key, get_ttl, cached_val)
    end
  end
  begin
    Octo::Utils.deserialize(cached_val)
  rescue Exception => e
    Octo.logger.error e
    nil
  end
end

#recreate_from(obj) ⇒ Object

Recreates this object from other object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/octocore-cassandra/models.rb', line 75

def recreate_from(obj)
  keys = self.key_column_names
  args = {}
  if obj.respond_to?(:enterprise_id) and obj.respond_to?(:uid)
    args[keys.delete(:enterprise_id)] = obj.enterprise_id
    if keys.length == 1
      args[keys.first] = obj.uid
      self.get_cached(args)
    else
      puts keys.to_a.to_s
      raise NotImplementedError, 'See octocore/models.rb'
    end
  end
end