Module: Wordmap::FileAccess
- Defined in:
- lib/wordmap/file_access.rb
Class Method Summary collapse
- .each_cell(file, start = 0, meta:, count: Float::INFINITY, batch_size: meta[:batch_size], trace: nil) ⇒ Object
- .read_at(file, pos, bytes) ⇒ Object
- .read_cells(file, i, count, meta, trace) ⇒ Object
- .read_meta(file, spacer) ⇒ Object
Class Method Details
.each_cell(file, start = 0, meta:, count: Float::INFINITY, batch_size: meta[:batch_size], trace: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wordmap/file_access.rb', line 5 def each_cell file, start = 0, meta:, count: Float::INFINITY, batch_size: [:batch_size], trace: nil unless block_given? return enum_for(__method__, file, start, meta: , count: count, batch_size: batch_size, trace: trace ) end seen = 0 loop do batch_size = count if (count < batch_size) cells = read_cells(file, start + seen, batch_size, , trace) cells.each do |cell| yield(cell) end seen += cells.size count -= cells.size break if count < 1 break if cells.size < batch_size end end |
.read_at(file, pos, bytes) ⇒ Object
68 69 70 71 72 |
# File 'lib/wordmap/file_access.rb', line 68 def read_at(file, pos, bytes) # puts "Seeking in #{file.path.split('.wmap', 2)[1][1..-1]} to #{pos}, " # "and reading #{bytes} bytes" file.pread(bytes, pos) end |
.read_cells(file, i, count, meta, trace) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wordmap/file_access.rb', line 36 def read_cells(file, i, count, , trace) , cell_size, cell_count = .values_at(:offset, :cell_size, :cell_count) return [] if i + 1 > [:cell_count] if i + count + 1 > [:cell_count] count = ([:cell_count] - i) end pos = [:offset] + (i * [:cell_size]) bytes = [:cell_size] * count if trace parts = file.path.split('.wmap', 2) subpath = (File.basename(parts[0]) + '.wmap') + parts[1] trace << [:read_cells, subpath, i, count, pos, bytes] end read_at(file, pos, bytes).unpack("a#{meta[:cell_size]}" * count) end |
.read_meta(file, spacer) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wordmap/file_access.rb', line 57 def (file, spacer) = read_at(file, 0, 30).split(spacer, 2)[0] cell_size, cell_count = .split(',').map(&:to_i) { offset: .bytesize + 1, cell_size: cell_size, cell_count: cell_count, batch_size: [[10_000 / cell_size, 1].max, cell_count].min } end |