Sha256: 59756391772c36ee9a6871bb4eb8e9ee8e8f1fe92128a74287f01e21cdd5b889

Contents?: true

Size: 1.84 KB

Versions: 13

Compression:

Stored size: 1.84 KB

Contents

module Bmg
  module Operator
    #
    # Project operator.
    #
    # Projects operand's tuples on given attributes, that is, keep those attributes
    # only. The operator takes care of removing duplicates.
    #
    # Example:
    #
    #   [{ a: 1, b: 2 }] project [:b] => [{ b: 2 }]
    #
    # All attributes in the attrlist SHOULD be existing attributes of the
    # input tuples.
    #
    class Project
      include Operator::Unary

      def initialize(type, operand, attrlist)
        @type = type
        @operand = operand
        @attrlist = attrlist
      end

    private

      attr_reader :attrlist

    public

      def each
        seen = {}
        @operand.each do |tuple|
          projected = project(tuple)
          unless seen.has_key?(projected)
            yield(projected)
            seen[projected] = true
          end
        end
      end

      def insert(arg)
        case arg
        when Hash       then operand.insert(valid_tuple!(arg))
        when Enumerable then operand.insert(arg.map{|t| valid_tuple!(t) })
        else
          super
        end
      end

      def update(tuple)
        operand.update(valid_tuple!(tuple))
      end

      def delete
        operand.delete
      end

      def to_ast
        [ :project, operand.to_ast, attrlist ]
      end

    protected ### optimization

      def _restrict(type, predicate)
        operand.restrict(predicate).project(attrlist)
      end

    protected ### inspect

      def args
        [ attrlist ]
      end

    private

      def project(tuple)
        tuple.delete_if{|k,_| !@attrlist.include?(k) }
      end

      def valid_tuple!(tuple)
        offending = tuple.keys - attrlist
        raise InvalidUpdateError, "#{offending.inspect} cannot be updated" unless offending.empty?
        tuple
      end

    end # class Project
  end # module Operator
end # module Bmg

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bmg-0.14.0 lib/bmg/operator/project.rb
bmg-0.13.0 lib/bmg/operator/project.rb
bmg-0.12.0 lib/bmg/operator/project.rb
bmg-0.11.0 lib/bmg/operator/project.rb
bmg-0.10.1 lib/bmg/operator/project.rb
bmg-0.10.0 lib/bmg/operator/project.rb
bmg-0.9.1 lib/bmg/operator/project.rb
bmg-0.9.0 lib/bmg/operator/project.rb
bmg-0.8.0 lib/bmg/operator/project.rb
bmg-0.7.1 lib/bmg/operator/project.rb
bmg-0.7.0 lib/bmg/operator/project.rb
bmg-0.6.1 lib/bmg/operator/project.rb
bmg-0.6.0 lib/bmg/operator/project.rb