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, #with_body

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, #with_body?

Constructor Details

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

Returns a new instance of BuildGraphCached.



159
160
161
162
163
164
165
166
# File 'lib/pacer-xml/build_graph.rb', line 159

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.



156
157
158
# File 'lib/pacer-xml/build_graph.rb', line 156

def cache
  @cache
end

#fieldsObject

Returns the value of attribute fields.



157
158
159
# File 'lib/pacer-xml/build_graph.rb', line 157

def fields
  @fields
end

Class Method Details

.empty_cacheObject



146
147
148
149
150
151
152
153
# File 'lib/pacer-xml/build_graph.rb', line 146

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pacer-xml/build_graph.rb', line 168

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)


183
184
185
# File 'lib/pacer-xml/build_graph.rb', line 183

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

#get_cached(e) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/pacer-xml/build_graph.rb', line 187

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/pacer-xml/build_graph.rb', line 198

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



220
221
222
223
# File 'lib/pacer-xml/build_graph.rb', line 220

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

#visit_vertex_fields(e) ⇒ Object



216
217
218
# File 'lib/pacer-xml/build_graph.rb', line 216

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