Class: Alf::Engine::Materialize::Array

Inherits:
Object
  • Object
show all
Includes:
Cog, Alf::Engine::Materialize
Defined in:
lib/alf/engine/materialize/array.rb

Overview

Provides in-memory materialization through a ruby Array.

This class acts as a Cog, that it, it is an enumerable of tuples. An optional ordering can be passed at construction.

Materialization occurs at prepare time, with auto-prepare on first access.

Example:

rel = [
  {:name => "Jones", :city => "London"},
  {:name => "Smith", :city => "Paris"},
  {:name => "Blake", :city => "London"}
]

Materialize::Array.new(rel).to_a
# => same as rel, in same order as the source

Materialize::Array.new(rel, Ordering[[:name, :asc]]).to_a
# => [
       {:name => "Blake", :city => "London"},
       {:name => "Jones", :city => "London"},
       {:name => "Smith", :city => "Paris"}
     ]

Constant Summary

Constants included from Cog

Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes included from Compiler::Cog

#compiler, #expr

Instance Method Summary collapse

Methods included from Cog

#arguments, #children, #each, #options, #to_s

Methods included from Compiler::Cog

#cog_orders, #orderedby?, #relation_type, #to_ascii_tree, #to_cog, #to_relation

Constructor Details

#initialize(operand, ordering = nil, expr = nil, compiler = nil) ⇒ Array

Creates a Materialize::Array instance


41
42
43
44
45
46
# File 'lib/alf/engine/materialize/array.rb', line 41

def initialize(operand, ordering = nil, expr = nil, compiler = nil)
  super(expr, compiler)
  @operand = operand
  @ordering = ordering
  @materialized = nil
end

Instance Attribute Details

#operandEnumerable (readonly)

Returns The operand

Returns:

  • (Enumerable)

    The operand


35
36
37
# File 'lib/alf/engine/materialize/array.rb', line 35

def operand
  @operand
end

#orderingOrdering (readonly)

Returns Ordering to ensure (optional)

Returns:

  • (Ordering)

    Ordering to ensure (optional)


38
39
40
# File 'lib/alf/engine/materialize/array.rb', line 38

def ordering
  @ordering
end

Instance Method Details

#_each(&block) ⇒ Object


49
50
51
# File 'lib/alf/engine/materialize/array.rb', line 49

def _each(&block)
  materialized.each(&block)
end

#cleanObject

Frees the materizalied hash


67
68
69
# File 'lib/alf/engine/materialize/array.rb', line 67

def clean
  @materialized = nil
end

#prepareObject

Prepare through materialization of the operand as an ordered array


56
57
58
59
60
61
62
# File 'lib/alf/engine/materialize/array.rb', line 56

def prepare
  @materialized ||= begin
    arr = operand.to_a
    arr.sort!(&ordering.sorter) if ordering
    arr
  end
end