Sha256: cf29abf33816b37e32d2d11466d57a090f396c15fc42a4e0d34373304e6ca73c

Contents?: true

Size: 1.85 KB

Versions: 27

Compression:

Stored size: 1.85 KB

Contents

module CassandraObject
  module FinderMethods
    extend ActiveSupport::Concern

    module ClassMethods
      def find(key)
        key_string = key.try(:to_s)

        if key_string.blank?
          raise CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{key.inspect}"
        elsif attributes = connection.get(column_family, key_string, {:count => 500}).presence
          instantiate(key_string, attributes)
        else
          raise CassandraObject::RecordNotFound
        end
      end

      def find_by_id(key)
        find(key)
      rescue CassandraObject::RecordNotFound
        nil
      end

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

        results.map do |k, v|
          v.empty? ? nil : instantiate(k, v)
        end.compact
      end

      def first(options = {})
        all(options.merge(limit: 1)).first
      end

      def find_with_ids(*ids)
        ids = ids.flatten
        return ids if ids.empty?

        ids = ids.compact.map(&:to_s).uniq

        multi_get(ids).values.compact
      end

      def count
        connection.count_range(column_family)
      end

      def multi_get(keys, options={})
        attribute_results = ActiveSupport::Notifications.instrument("multi_get.cassandra_object", column_family: column_family, keys: keys) do
          connection.multi_get(column_family, keys.map(&:to_s), consistency: thrift_read_consistency, count: 500)
        end

        Hash[attribute_results.map do |key, attributes|
          [parse_key(key), attributes.present? ? instantiate(key, attributes) : nil]
        end]
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.11.9 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.8 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.7 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.6 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.5 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.4 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.3 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.2 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.1 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.11.0 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.11 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.10 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.9 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.8 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.7 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.6 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.5 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.4 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.3 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.10.2 lib/cassandra_object/finder_methods.rb