Class: Alf::Engine::Materialize::Hash

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

Overview

Provides in-memory materialization through a ruby Hash.

This class acts as a Cog, that it, it is an enumerable of tuples. No particular ordering is guaranteed. In addition, the class provides indexed access through the `[]` method.

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"}
]
op = Materialize::Hash.new(rel, AttrList[:city])

op.to_a 
# => same as rel, no ordering guaranteed

op[:city => "London"].to_a
# => [
       {:name => "Jones", :city => "London"},
       {:name => "Blake", :city => "London"}
     ]

op[:city => "London"].to_a
# => [
       {:name => "Jones", :city => "London"},
       {:name => "Blake", :city => "London"}
     ]

op[:city => "Athens"].to_a
# => []

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, key = [], allbut = false, expr = nil, compiler = nil) ⇒ Hash

Creates a Materialize::Hash instance


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/alf/engine/materialize/hash.rb', line 61

def initialize(operand, key = [], allbut = false, expr = nil, compiler = nil)
  super(expr, compiler)
  @operand = operand
  @key = key
  @allbut = allbut
  @neutral = ->(key){
    []
  }
  @accumulate = ->(key, accumulator, tuple){
    accumulator << tuple
  }
  @materialized = nil
end

Instance Attribute Details

#accumulateProc

Returns a proc that accumulates tuples (key, accumulator, tuple)

Returns:

  • (Proc)

    a proc that accumulates tuples (key, accumulator, tuple)


58
59
60
# File 'lib/alf/engine/materialize/hash.rb', line 58

def accumulate
  @accumulate
end

#allbutBoolean

Returns Hash on all but specified attributes?

Returns:

  • (Boolean)

    Hash on all but specified attributes?


51
52
53
# File 'lib/alf/engine/materialize/hash.rb', line 51

def allbut
  @allbut
end

#keyAttrList|Proc

Returns Attributes for the hash key

Returns:

  • (AttrList|Proc)

    Attributes for the hash key


48
49
50
# File 'lib/alf/engine/materialize/hash.rb', line 48

def key
  @key
end

#neutralProc

keys

Returns:

  • (Proc)

    a proc that returns the neural value for newly found index


55
56
57
# File 'lib/alf/engine/materialize/hash.rb', line 55

def neutral
  @neutral
end

#operandEnumerable (readonly)

Returns The operand

Returns:

  • (Enumerable)

    The operand


45
46
47
# File 'lib/alf/engine/materialize/hash.rb', line 45

def operand
  @operand
end

Instance Method Details

#[](key_tuple, project = false) ⇒ Cog

Returns tuples that match a given key.

This method returns a Cog instance in all case. En empty Cog is returned if no tuples match the key.

Parameters:

  • key_tuple (Tuple)

    a key tuple

  • project (Boolean) (defaults to: false)

    project `key_tuple` on key first?

Returns:

  • (Cog)

    the tuples from operand that match `key_tuple`


95
96
97
98
# File 'lib/alf/engine/materialize/hash.rb', line 95

def [](key_tuple, project = false)
  key_tuple = key_for(key_tuple) if project
  materialized[key_tuple]
end

#_each(&block) ⇒ Object


76
77
78
79
80
# File 'lib/alf/engine/materialize/hash.rb', line 76

def _each(&block)
  materialized.each_value do |rel|
    rel.each(&block)
  end
end

#cleanObject

Frees the materizalied hash


118
119
120
# File 'lib/alf/engine/materialize/hash.rb', line 118

def clean
  @materialized = nil
end

#each_pair(&block) ⇒ Object

Yields indexed (key, tuples) pairs in turn.


83
84
85
# File 'lib/alf/engine/materialize/hash.rb', line 83

def each_pair(&block)
  materialized.each_pair(&block)
end

#prepareObject

Prepare through materialization of the operand as a hash


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/alf/engine/materialize/hash.rb', line 103

def prepare
  @materialized ||= begin
    h = ::Hash.new{|h,k| h[k] = @neutral.call(h) }
    operand.each do |tuple|
      index_key = key_for(tuple)
      h[index_key] = @accumulate.call(index_key, h[index_key], tuple)
    end
    h
  end
  self
end