Class: LazyRecords::MemoryRecords

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_records.rb

Instance Method Summary collapse

Constructor Details

#initializeMemoryRecords

Returns a new instance of MemoryRecords.



5
6
7
# File 'lib/memory_records.rb', line 5

def initialize
  @store = empty
end

Instance Method Details

#add(definition, records) ⇒ Object

Raises:

  • (UnsupportedTypeException.new)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/memory_records.rb', line 9

def add(definition, records)
  raise(UnsupportedTypeException.new, "records must be of type: #{Sequence}<#{Record}> but was: #{records.class}") unless records.is_a?(Sequences::Sequence)
  table = @store.filter(where(key: equals(definition.name)))
  if table.empty?
    @store = @store.join(sequence(pair(definition.name, records)))
  else
    remaining_tables = @store.reject(where(key: equals(definition.name)))
    updated_table = table.update(value: table.head.value.join(records))
    @store = remaining_tables.join(updated_table)
  end
end

#as_lockObject



67
68
69
# File 'lib/memory_records.rb', line 67

def as_lock
  'lazy_store'
end

#get(definition, selection = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/memory_records.rb', line 21

def get(definition, selection=nil)
  if selection
    @store.filter(where(key: equals(definition.name))).send(:filter, selection).map { |row| row.value }.head_option.get_or_else(empty)
  else
    @store.filter(where(key: equals(definition.name))).map { |row| row.value }.head_option.get_or_else(empty)
  end
end

#inner_join(def1, def2, keyword_map) ⇒ Object



52
53
54
# File 'lib/memory_records.rb', line 52

def inner_join(def1, def2, keyword_map)
  get(def1).flat_map { |d1| get(def2).filter { |d2| process_keywords(keyword_map, d1, d2) }.map { |n| Record.new(process_columns(def1, def2, d1.get_hash, n.get_hash)) } }
end

#process_columns(d1, d2, h1, h2) ⇒ Object



60
61
62
63
64
# File 'lib/memory_records.rb', line 60

def process_columns(d1, d2, h1, h2)
  one = h1.map { |k, v| {"#{d1.name}_#{k}" => v} }.reduce({}) { |a, b| a.merge(b) }
  two = h2.map { |k, v| {"#{d2.name}_#{k}" => v} }.reduce({}) { |a, b| a.merge(b) }
  one.merge(two)
end

#process_keywords(keyword_map, d1, d2) ⇒ Object



56
57
58
# File 'lib/memory_records.rb', line 56

def process_keywords(keyword_map, d1, d2)
  eval(keyword_map.map { |k, v| "d1.send(:#{k.name}) == d2.send(:#{v.name})" }.to_a.join(' and '))
end

#read_data(location) ⇒ Object



71
72
73
74
75
76
# File 'lib/memory_records.rb', line 71

def read_data(location)
  if File.exists?(location)
    f = File.open(location)
    @store = deserialize(Marshal.load(f))
  end
end

#remove(definition, selection) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/memory_records.rb', line 44

def remove(definition, selection)
  table = @store.filter(where(key: equals(definition.name)))
  records = get(definition).send(:reject, selection)
  remaining_tables = @store.reject(where(key: equals(definition.name)))
  updated_table = table.update(value: records)
  @store = remaining_tables.join(updated_table)
end

#remove_definition(definition) ⇒ Object



39
40
41
42
# File 'lib/memory_records.rb', line 39

def remove_definition(definition)
  name = definition.is_a?(Symbol) ? definition : definition.name
  @store = @store.reject(where(key: equals(name)))
end

#set(definition, selection, *updates) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/memory_records.rb', line 29

def set(definition, selection, *updates)
  table = @store.filter(where(key: equals(definition.name)))
  updated = get(definition).send(:filter, selection).update(Maps.merge(sequence(updates).in_pairs.map { |e| {e.key.name.to_sym => e.value} }))
  remaining = get(definition).send(:reject, selection)
  records = remaining.join(updated)
  remaining_tables = @store.reject(where(key: equals(definition.name)))
  updated_table = table.update(value: records)
  @store = remaining_tables.join(updated_table)
end

#write_data(location) ⇒ Object



80
81
82
83
84
# File 'lib/memory_records.rb', line 80

def write_data(location)
  f = File.new(location, 'w')
  Marshal.dump(@store.serialize, f)
  f.close
end