Module: CassandraObject::FinderMethods::ClassMethods

Defined in:
lib/cassandra_object/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



51
52
53
# File 'lib/cassandra_object/finder_methods.rb', line 51

def all(options={})
  CassandraObject::Relation.new(self, self.arel_table, options)
end

#find(key, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cassandra_object/finder_methods.rb', line 5

def find(key, opts={})
  # kludge to play nice ActiveRecord association
  opts.assert_valid_keys(:conditions, :consistency)
  opts[:consistency] ||= thrift_read_consistency
  raise(ArgumentError, "unexpected conditions") if opts[:conditions].present?
  raise(CassandraObject::InvalidKey, "invalid key: #{key}") if key.blank? || ! parse_key(key)

  attributes =
    begin
      CassandraObject::Base.with_connection(key, :read) do
        ActiveSupport::Notifications.instrument("get.cassandra_object", column_family: column_family, key: key) do
          connection.get column_family, key, opts.slice(:consistency)
        end
      end
    end

  if attributes && ! attributes.empty?
    instantiate(key, attributes)
  else
    raise CassandraObject::RecordNotFound
  end
end

#find_all_by_expression(expression, options = {}) ⇒ Object



110
111
112
# File 'lib/cassandra_object/finder_methods.rb', line 110

def find_all_by_expression(expression, options={})
  multi_get_by_expression(expression, options).values
end

#find_by_id(key, opts = {}) ⇒ Object



28
29
30
31
32
# File 'lib/cassandra_object/finder_methods.rb', line 28

def find_by_id(key, opts={})
  find(key, opts)
rescue CassandraObject::RecordNotFound
  nil
end

#find_by_sql(arel, bind_values) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cassandra_object/finder_methods.rb', line 65

def find_by_sql(arel, bind_values)
  if bind_values.size == 0
    limit = 100
    results = CassandraObject::Base.with_connection(nil, :read) do
      ActiveSupport::Notifications.instrument("get_range.cassandra_object", column_family: column_family, key_count: limit) do
        connection.get_range(column_family, key_count: limit, consistency: thrift_read_consistency)
      end
    end

    return results.map do |k, v|
      v.empty? ? nil : instantiate(k, v)
    end.compact
  elsif bind_values.size == 1
    if bind_values[0][0] == 'id' && bind_values[0][1].is_a?(String)
      return [find_by_id(bind_values[0][1])]
    elsif bind_values[0][:id]
      return [find_by_id(bind_values[0][:id])]
    end
  end

  raise "only supports lookups by id currently bind_values #{bind_values}"
end

#find_with_ids(*ids) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cassandra_object/finder_methods.rb', line 93

def find_with_ids(*ids)
  expects_array = ids.first.kind_of?(Array)
  return ids.first if expects_array && ids.first.empty?

  ids = ids.dup
  ids.flatten!
  ids.compact!
  ids.collect!(&:to_s)
  ids.uniq!

  #raise RecordNotFound, "Couldn't find #{record_klass.name} without an ID" if ids.empty?

  results = multi_get(ids).values.compact

  results.size <= 1 && !expects_array ? results.first : results
end

#first(options = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/cassandra_object/finder_methods.rb', line 55

def first(options=nil)
  result = CassandraObject::Base.with_connection(nil, :read) do
    ActiveSupport::Notifications.instrument("get_range.cassandra_object", column_family: column_family, key_count: 1) do
      connection.get_range(column_family, key_count: 1, consistency: thrift_read_consistency)
    end
  end.first

  result ? instantiate(result[0], result[1]) : nil
end

#get_counter(key, column, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cassandra_object/finder_methods.rb', line 34

def get_counter(key, column, opts={})
  opts.assert_valid_keys(:consistency)
  opts[:consistency] ||= thrift_read_consistency

  result = CassandraObject::Base.with_connection(key, :read) do
    ActiveSupport::Notifications.instrument("get_counter.cassandra_object", column_family: column_family, key: key, column: column) do
      connection.get(column_family, key, column, opts)
    end
  end

  if result
    result
  else
    raise CassandraObject::RecordNotFound
  end
end

#get_slice(key, start, finish, opts = {}) ⇒ Object

Selecting a slice of a super column



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cassandra_object/finder_methods.rb', line 115

def get_slice(key, start, finish, opts={})
  CassandraObject::Base.with_connection(key, :read) do
    connection.get_slice(column_family,
                         key, 
                         start,
                         finish,
                         opts[:count] || 100,
                         opts[:reversed] || false,
                         opts[:consistency] || thrift_read_consistency)
  end
end

#unscopedObject Also known as: current_scope



88
89
90
# File 'lib/cassandra_object/finder_methods.rb', line 88

def unscoped
  CassandraObject::Relation.new(self, self.arel_table, {})
end