Module: CassandraObject::FinderMethods::ClassMethods
- Defined in:
- lib/cassandra_object/finder_methods.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #find(key, opts = {}) ⇒ Object
- #find_all_by_expression(expression, options = {}) ⇒ Object
- #find_by_id(key, opts = {}) ⇒ Object
- #find_with_ids(*ids) ⇒ Object
- #first(options = {}) ⇒ Object
- #get_counter(key, column, opts = {}) ⇒ Object
-
#get_slice(key, start, finish, opts = {}) ⇒ Object
Selecting a slice of a super column is not supported by default with the cassandra gem TODO: move this to Cassandra gem.
Instance Method Details
#all(options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cassandra_object/finder_methods.rb', line 41 def all( = {}) limit = [:limit] || 100 results = 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 results.map do |k, v| v.empty? ? nil : instantiate(k, v) end.compact end |
#find(key, opts = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 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) if (attributes = ActiveSupport::Notifications.instrument("get.cassandra_object", column_family: column_family, key: key) { connection.get(column_family, key, opts.slice(:consistency)) }) && !attributes.empty? instantiate(key, attributes) else raise CassandraObject::RecordNotFound end end |
#find_all_by_expression(expression, options = {}) ⇒ Object
73 74 75 |
# File 'lib/cassandra_object/finder_methods.rb', line 73 def find_all_by_expression(expression, ={}) multi_get_by_expression(expression, ).values end |
#find_by_id(key, opts = {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/cassandra_object/finder_methods.rb', line 20 def find_by_id(key, opts={}) find(key, opts) rescue CassandraObject::RecordNotFound nil end |
#find_with_ids(*ids) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cassandra_object/finder_methods.rb', line 56 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
52 53 54 |
# File 'lib/cassandra_object/finder_methods.rb', line 52 def first( = {}) all(.merge(:limit => 1)).first end |
#get_counter(key, column, opts = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cassandra_object/finder_methods.rb', line 26 def get_counter(key, column, opts={}) opts.assert_valid_keys(:consistency) opts[:consistency] ||= thrift_read_consistency result = ActiveSupport::Notifications.instrument("get_counter.cassandra_object", column_family: column_family, key: key, column: column) do connection.get(column_family, key, column, opts) end if result result else raise CassandraObject::RecordNotFound end end |
#get_slice(key, start, finish, opts = {}) ⇒ Object
Selecting a slice of a super column is not supported by default with the cassandra gem TODO: move this to Cassandra gem.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cassandra_object/finder_methods.rb', line 79 def get_slice(key, start, finish, opts={}) parent = CassandraThrift::ColumnParent.new(:column_family => column_family) predicate = CassandraThrift::SlicePredicate. new(:slice_range => CassandraThrift::SliceRange.new(:start => start, :finish => finish, :count => opts[:count] || 100, :reversed => opts[:reversed] || false)) ActiveSupport::Notifications.instrument("get_slice.cassandra_object", column_family: column_family, key: key, start: start, finish: finish) do {}.tap do |result| connection.send(:client).get_slice(key, parent, predicate, opts[:consistency] || thrift_read_consistency).each do |column| result[column.counter_super_column.name] = _columns_to_hash(column.counter_super_column.columns) end end end end |