Sha256: 8983ef08e001f3cb29c501ea829097f1a42d75287b6273aca943bff6adf1f8d8

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Hari
  class Node < Entity
    module Queries
      class Type

        attr_reader :relation, :name

        def initialize(relation, name)
          @relation, @name = relation, name
        end

        def intersect_count(type)
          intersect_key = interstore(type)

          Hari.redis.zcard(intersect_key).tap do
            Hari.redis.del intersect_key
          end
        end

        def intersect(type, start = nil, stop = nil)
          start ||= 0
          stop  ||= -1

          intersect_key = interstore(type)

          Hari.redis.zrevrange(intersect_key, start, stop).tap do
            Hari.redis.del intersect_key
          end
        end

        def sort_by(type, offset = nil, count = nil, order = nil)
          offset ||= 0
          count  ||= Hari.redis.zcard(key)
          order  = 'desc' unless order.to_s == 'asc'

          Hari.redis.sort key, by: type.sort_key, order: "#{order} alpha", limit: [offset, count]
        end

        def interstore(type)
          intersect_key(type).tap do |destination|
            Hari.redis.zinterstore destination, [key, type.key]
          end
        end

        def key
          start_key = Hari.node_key(relation.parent.node)
          "#{start_key}:#{relation.name}:#{relation.direction}:#{name}"
        end

        def sort_key
          start_key = Hari.node_key(relation.parent.node)
          "#{start_key}:#{relation.name}:#{name}#*"
        end

        def intersect_key(type)
          "inter:#{key}:#{type.key}"
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.4 lib/hari/node/queries/type.rb