Class: Wukong::Store::FlatFileStore

Inherits:
Base show all
Defined in:
lib/wukong/store/flat_file_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#each_as, #log_line

Constructor Details

#initialize(options = {}) ⇒ FlatFileStore

filename_root : first part of name for files



12
13
14
15
16
17
# File 'lib/wukong/store/flat_file_store.rb', line 12

def initialize options={}
  super options
  self.filename = options[:filename] or raise "Missing filename in #{self.class}"
  self.filemode = options[:filemode] || 'r'
  skip!(options[:skip]) if options[:skip]
end

Instance Attribute Details

#filemodeObject

Returns the value of attribute filemode.



7
8
9
# File 'lib/wukong/store/flat_file_store.rb', line 7

def filemode
  @filemode
end

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/wukong/store/flat_file_store.rb', line 7

def filename
  @filename
end

Instance Method Details

#<<(obj) ⇒ Object

delegates to #save – writes the object to the file. Returns self for chaining on the stream.



81
82
83
84
# File 'lib/wukong/store/flat_file_store.rb', line 81

def <<(obj)
  save obj
  self
end

#closeObject

Close the dump file



51
52
53
54
# File 'lib/wukong/store/flat_file_store.rb', line 51

def close
  @file.close if @file
  @file = nil
end

#each(&block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/wukong/store/flat_file_store.rb', line 22

def each &block
  file.each do |line|
    attrs = line.chomp.split("\t")
    next if attrs.blank?
    yield *attrs
  end
end

#fileObject

Open the timestamped file, ensuring its directory exists



44
45
46
47
48
# File 'lib/wukong/store/flat_file_store.rb', line 44

def file
  return @file if @file
  Log.info "Opening file #{filename} with mode #{filemode}"
  @file = File.open(filename, filemode)
end

#flushObject



56
57
58
# File 'lib/wukong/store/flat_file_store.rb', line 56

def flush
  @file.flush if @file
end

#mkdir!Object

Ensure the file’s directory exists



61
62
63
64
65
66
# File 'lib/wukong/store/flat_file_store.rb', line 61

def mkdir!
  dir = File.dirname(filename)
  return if File.directory?(dir)
  Log.info "Making directory #{dir}"
  FileUtils.mkdir_p dir
end

#save(obj) ⇒ Object

write to the file



69
70
71
72
# File 'lib/wukong/store/flat_file_store.rb', line 69

def save obj
  file.puts obj
  obj
end

#sizeObject

returns the size of the current file



75
76
77
78
# File 'lib/wukong/store/flat_file_store.rb', line 75

def size
  return 0 if !@file
  File.size(filename)
end

#skip!(n_lines) ⇒ Object

Read ahead n_lines lines in the file



33
34
35
36
37
38
# File 'lib/wukong/store/flat_file_store.rb', line 33

def skip! n_lines
  Log.info "Skipping #{n_lines} in #{self.class}:#{filename}"
  n_lines.times do
    file.readline
  end
end