Class: PacerXml::BuildGraphCached

Inherits:
BuildGraph show all
Defined in:
lib/pacer-xml/build_graph.rb

Instance Attribute Summary collapse

Attributes inherited from GraphVisitor

#depth, #documents, #graph, #html, #rename, #skip

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BuildGraph

#visit_many_rel, #visit_many_rels, #visit_one_rel

Methods inherited from GraphVisitor

build_rename, #level, #skip?, #tell, #visit_edge_fields

Constructor Details

#initialize(graph, opts = {}) ⇒ BuildGraphCached

Returns a new instance of BuildGraphCached.



150
151
152
153
154
155
156
157
# File 'lib/pacer-xml/build_graph.rb', line 150

def initialize(graph, opts = {})
  if opts[:cache]
    @cache = self.class.empty_cache.merge! opts[:cache]
  else
    @cache = self.class.empty_cache
  end
  super
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



147
148
149
# File 'lib/pacer-xml/build_graph.rb', line 147

def cache
  @cache
end

#fieldsObject

Returns the value of attribute fields.



148
149
150
# File 'lib/pacer-xml/build_graph.rb', line 148

def fields
  @fields
end

Class Method Details

.empty_cacheObject



137
138
139
140
141
142
143
144
# File 'lib/pacer-xml/build_graph.rb', line 137

def empty_cache
  cache = Hash.new { |h, k| h[k] = {} }
  cache[:hits] = Hash.new 0
  cache[:size] = 0
  cache[:kill] = nil
  cache[:skip] = Set[]
  cache
end

Instance Method Details

#build(doc) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pacer-xml/build_graph.rb', line 159

def build(doc)
  result = super
  #tell "CACHE size #{ cache[:size] },  hits:"
  if cache[:stats] and documents % 100 == 99
    tell '-----------------'
    cache.each do |k, adds|
      next unless k.is_a? String
      adds = adds.length
      hits = cache[:hits][k]
      tell("%40s: %6s / %6s = %5.4f" % [k, hits, adds, (hits/adds.to_f)])
    end
  end
  result
end

#cacheable?(e) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/pacer-xml/build_graph.rb', line 174

def cacheable?(e)
  not cache[:skip].include?(rename[e.name]) and not visit_vertex_fields(e).empty?
end

#get_cached(e) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/pacer-xml/build_graph.rb', line 178

def get_cached(e)
  if cacheable?(e)
    id = cache[rename[e.name]][visit_vertex_fields(e).hash]
    #tell "cache hit: #{ e.description }" if el
    if id
      cache[:hits][rename[e.name]] += 1
      graph.vertex(id)
    end
  end
end

#set_cached(e, el) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pacer-xml/build_graph.rb', line 189

def set_cached(e, el)
  return unless el
  if cacheable?(e)
    ct = cache[rename[e.name]]
    kill = cache[:kill]
    if kill and cache[:hits][rename[e.name]] == 0 and ct.length > kill
      tell "cache kill #{ e.description }"
      cache[:skip] << rename[e.name]
      cache[:size] -= ct.length
      cache[rename[e.name]] = []
    else
      ct[visit_vertex_fields(e).hash] = el.element_id
      cache[:size] += 1
    end
  end
  el
end

#visit_element(e) ⇒ Object



211
212
213
214
# File 'lib/pacer-xml/build_graph.rb', line 211

def visit_element(e)
  self.fields = nil
  get_cached(e) || set_cached(e, super)
end

#visit_vertex_fields(e) ⇒ Object



207
208
209
# File 'lib/pacer-xml/build_graph.rb', line 207

def visit_vertex_fields(e)
  self.fields ||= super
end