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
54
55
56
57
58
59
60
61
62
# File 'lib/cassandra_object/finder_methods.rb', line 51

def all(options = {})
  limit = options[: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

  results.map do |k, v|
    v.empty? ? nil : instantiate(k, v)
  end.compact
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



85
86
87
# File 'lib/cassandra_object/finder_methods.rb', line 85

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_with_ids(*ids) ⇒ Object



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

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 = {}) ⇒ Object



64
65
66
# File 'lib/cassandra_object/finder_methods.rb', line 64

def first(options = {})
  all(options.merge(:limit => 1)).first
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



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cassandra_object/finder_methods.rb', line 90

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