Class: Ankusa::FileSystemStorage

Inherits:
MemoryStorage show all
Defined in:
lib/ankusa/file_system_storage.rb

Instance Method Summary collapse

Methods inherited from MemoryStorage

#classnames, #close, #doc_count_totals, #get_doc_count, #get_total_word_count, #get_vocabulary_sizes, #get_word_counts, #incr_doc_count, #incr_total_word_count, #incr_word_count

Constructor Details

#initialize(file) ⇒ FileSystemStorage

Returns a new instance of FileSystemStorage.



7
8
9
10
# File 'lib/ankusa/file_system_storage.rb', line 7

def initialize(file)
  @file = file
  init_tables
end

Instance Method Details

#drop_tablesObject



20
21
22
23
# File 'lib/ankusa/file_system_storage.rb', line 20

def drop_tables
  File.delete(@file) rescue Errno::ENOENT
  reset
end

#init_tablesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ankusa/file_system_storage.rb', line 25

def init_tables
  data = {}
  begin
    File.open(@file) do |f|
      data = Marshal.load(f)
    end
    @freqs = data[:freqs]
    @total_word_counts = data[:total_word_counts]
    @total_doc_counts = data[:total_doc_counts]
    @klass_word_counts = data[:klass_word_counts]
    @klass_doc_counts = data[:klass_word_counts]
  rescue Errno::ENOENT
    reset
  end
end

#resetObject



12
13
14
15
16
17
18
# File 'lib/ankusa/file_system_storage.rb', line 12

def reset
  @freqs = {}
  @total_word_counts = Hash.new(0)
  @total_doc_counts = Hash.new(0)
  @klass_word_counts = {}
  @klass_doc_counts = {}
end

#save(file = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ankusa/file_system_storage.rb', line 41

def save(file = nil)
  file ||= @file
  data = { 	:freqs => @freqs,
		:total_word_counts => @total_word_counts,
		:total_doc_counts => @total_doc_counts,
		:klass_word_counts => @klass_word_counts,
		:klass_doc_counts => @klass_doc_counts }
  File.open(file, 'w+') do |f|
    Marshal.dump(data, f)
  end
end