Class: DpStmMap::ReferenceCounts

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

Instance Method Summary collapse

Constructor Details

#initialize(references_dir) ⇒ ReferenceCounts

Returns a new instance of ReferenceCounts.



153
154
155
156
157
# File 'lib/dp_stm_map/Manager.rb', line 153

def initialize references_dir
  @references_dir=references_dir
  FileUtils.mkdir_p(references_dir) unless File.exist?(references_dir)
  @cache=ThreadSafeLru::LruCache.new 9000
end

Instance Method Details

#add_reference(r) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/dp_stm_map/Manager.rb', line 163

def add_reference r
  count=0
  @cache.get(r) do |ref|
    count=File.open(file_name(ref),"r") {|f| f.read().to_i} if File.exist? file_name(ref)
  end
  File.open(file_name(r),"w") {|f| f.write(count+1)}
  @cache.drop(r)
end

#file_name(ref) ⇒ Object



159
160
161
# File 'lib/dp_stm_map/Manager.rb', line 159

def file_name ref
  "#{@references_dir}/#{ref}"
end

#has_references?(r) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
# File 'lib/dp_stm_map/Manager.rb', line 178

def has_references? r
  count=0
  @cache.get(r) do |ref|
    count=File.open(file_name(ref),"r") {|f| f.read().to_i} if File.exist? file_name(ref)
  end
  count > 0
end

#remove_reference(ref) ⇒ Object



172
173
174
175
176
# File 'lib/dp_stm_map/Manager.rb', line 172

def remove_reference ref
  count=File.open(file_name(ref),"r") {|f| f.read().to_i}
  File.open(file_name(ref),"w") {|f| f.write(count-1)}
  @cache.drop(ref)
end