Class: HarvesterTools::Cache

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

Class Method Summary collapse

Class Method Details

.cacheMetaObject(meta, uri) ⇒ Object



65
66
67
68
69
# File 'lib/metadata_object.rb', line 65

def self.cacheMetaObject(meta, uri)
  filename = (Digest::MD5.hexdigest uri) + '_meta'
  warn "in cacheMetaObject Writing to cache for #{filename}"
  File.open("/tmp/#{filename}", 'wb') { |f| f.write(Marshal.dump(meta)) }
end

.checkCache(uri, headers) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/metadata_object.rb', line 105

def self.checkCache(uri, headers)
  filename = Digest::MD5.hexdigest uri + headers.to_s
  warn "Checking Error cache for #{filename}"
  if File.exist?("/tmp/#{filename}_error")
    warn 'Error file found in cache... returning'
    return ['ERROR', nil, nil]
  end
  if File.exist?("/tmp/#{filename}_head") and File.exist?("/tmp/#{filename}_body")
    warn 'FOUND data in cache'
    head = Marshal.load(File.read("/tmp/#{filename}_head"))
    body = Marshal.load(File.read("/tmp/#{filename}_body"))
    all_uris = ''
    all_uris = Marshal.load(File.read("/tmp/#{filename}_uri")) if File.exist?("/tmp/#{filename}_uri")
    warn 'Returning....'
    return [head, body, all_uris]
  end
  warn 'Not Found in Cache'
end

.checkRDFCache(body:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/metadata_object.rb', line 71

def self.checkRDFCache(body: )
  fs = File.join('/tmp/', '*_graphbody')
  bodies = Dir.glob(fs)
  g = RDF::Graph.new
  bodies.each do |bodyfile|
    next unless File.size(bodyfile) == body.bytesize # compare body size
    next unless bodyfile.match(/(.*)_graphbody$/) # continue if there's no match

    filename = Regexp.last_match(1)
    warn "Regexp match for #{filename} FOUND"
    next unless File.exist?("#{filename}_graph") # @ get the associated graph file

    warn "RDF Cache File #{filename} FOUND"
    graph = Marshal.load(File.read("#{filename}_graph")) # unmarshal it
    graph.each do |statement|
      g << statement # need to do this because the unmarshalled object isn't entirely functional as an RDF::Graph object
    end
    warn "returning a graph of #{g.size}"
    break
  end
  # return an empty graph otherwise
  g
end

.retrieveMetaObject(uri) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/metadata_object.rb', line 52

def self.retrieveMetaObject(uri)
  filename = (Digest::MD5.hexdigest uri) + '_meta'
  warn "Checking Meta cache for #{filename}"
  if File.exist?("/tmp/#{filename}")
    warn 'FOUND Meta object in cache'
    meta = Marshal.load(File.read("/tmp/#{filename}"))
    warn 'Returning....'
    return meta
  end
  warn 'Meta objectNot Found in Cache'
  false
end

.writeErrorToCache(uri, headers) ⇒ Object



135
136
137
138
139
# File 'lib/metadata_object.rb', line 135

def self.writeErrorToCache(uri, headers)
  filename = Digest::MD5.hexdigest uri + headers.to_s
  warn "in writeErrorToCache Writing error to cache for #{filename}"
  File.open("/tmp/#{filename}_error", 'wb') { |f| f.write('ERROR') }
end

.writeRDFCache(reader:, body:) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/metadata_object.rb', line 95

def self.writeRDFCache(reader:, body:)
  filename = Digest::MD5.hexdigest body
  graph = RDF::Graph.new
  reader.each_statement { |s| graph << s }
  warn "WRITING RDF TO CACHE #{filename}"
  File.open("/tmp/#{filename}_graph", 'wb') { |f| f.write(Marshal.dump(graph)) }
  File.open("/tmp/#{filename}_graphbody", 'wb') { |f| f.write(body) }
  warn "wrote RDF filename: #{filename}"
end

.writeToCache(uri, headers, head, body, all_uris) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/metadata_object.rb', line 124

def self.writeToCache(uri, headers, head, body, all_uris)
  filename = Digest::MD5.hexdigest uri + headers.to_s
  warn "in writeToCache Writing to cache for #{filename}"
  headfilename = filename + '_head'
  bodyfilename = filename + '_body'
  urifilename = filename + '_uri'
  File.open("/tmp/#{headfilename}", 'wb') { |f| f.write(Marshal.dump(head)) }
  File.open("/tmp/#{bodyfilename}", 'wb') { |f| f.write(Marshal.dump(body)) }
  File.open("/tmp/#{urifilename}", 'wb') { |f| f.write(Marshal.dump(all_uris)) }
end