Sha256: 2f68606ddae0267dd35b8a906fcaea6c0f61c1f8a9b123f3da49f7e5eaafae3b

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

module CassandraObject
  module FinderMethods
    extend ActiveSupport::Concern
    module ClassMethods
      def find(key)
        if parse_key(key) && attributes = connection.get(column_family, key)
          instantiate(key, attributes)
        else
          raise CassandraObject::RecordNotFound
        end
      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)
        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)
        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

      private
        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)
          end

          attribute_results.inject({}) do |memo, (key, attributes)|
            if attributes.empty?
              memo[key] = nil
            else
              memo[parse_key(key)] = instantiate(key, attributes)
            end
            memo
          end
        end

        def get(key, options={})
          multi_get([key], options).values.first
        end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.3.2 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.3.1 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.3.0 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.2.4 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.2.3 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.2.2 lib/cassandra_object/finder_methods.rb
gotime-cassandra_object-2.2.1 lib/cassandra_object/finder_methods.rb