diff options
| author | mo khan <mo.khan@gmail.com> | 2020-05-10 11:14:01 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-05-10 11:14:01 -0600 |
| commit | db8925ab4111ddf342ce84286c554b85eb9bf63f (patch) | |
| tree | b7f23f81d9fa512316473f6c8d23af4a3d31f994 /lib | |
| parent | 2438ab1c4824f3381c9f62ec5015b63052e2c520 (diff) | |
Extract IndexFile class
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/spandx/core/datafile.rb | 45 | ||||
| -rw-r--r-- | lib/spandx/core/index_file.rb | 56 |
2 files changed, 70 insertions, 31 deletions
diff --git a/lib/spandx/core/datafile.rb b/lib/spandx/core/datafile.rb index 1f08f92..261ee4e 100644 --- a/lib/spandx/core/datafile.rb +++ b/lib/spandx/core/datafile.rb @@ -3,11 +3,10 @@ module Spandx module Core class Datafile - attr_reader :absolute_path, :index_path + attr_reader :absolute_path def initialize(absolute_path) @absolute_path = Pathname.new(absolute_path) - @index_path = Pathname.new("#{@absolute_path}.lines") FileUtils.mkdir_p(@absolute_path.dirname) end @@ -40,25 +39,7 @@ module Spandx def index! return unless exist? - absolute_path.write(absolute_path.readlines.sort.uniq.join) - absolute_path.open(mode: 'r') do |io| - index_path.write(JSON.generate(lines_in(io))) - end - end - - private - - def index - @index ||= - if index_path.exist? - JSON.parse(index_path.read) - else - open_file { |io| lines_in(io) } - end - end - - def exist? - absolute_path.exist? + index.index! end def open_file(mode: 'r') @@ -71,11 +52,21 @@ module Spandx nil end + private + + def index + @index ||= IndexFile.new(self) + end + + def exist? + absolute_path.exist? + end + def search_for(term, io, lines) return if lines.empty? mid = lines.size == 1 ? 0 : lines.size / 2 - io.seek(lines[mid]) + io.seek(lines[mid].to_i) comparison = matches?(term, parse_row(io)) do |row| return row end @@ -92,17 +83,9 @@ module Spandx end def partition(comparison, mid, lines) - min, max = comparison.positive? ? [mid + 1, lines.length] : [0, mid] + min, max = comparison.positive? ? [mid + 1, lines.size] : [0, mid] lines.slice(min, max) end - - def lines_in(io) - lines = [0] - io.seek(0) - lines << io.pos while io.gets - lines.pop if lines.size > 1 - lines - end end end end diff --git a/lib/spandx/core/index_file.rb b/lib/spandx/core/index_file.rb new file mode 100644 index 0000000..f6112f0 --- /dev/null +++ b/lib/spandx/core/index_file.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Spandx + module Core + class IndexFile + attr_reader :data_file, :path + + def initialize(data_file) + @data_file = data_file + @path = Pathname.new("#{data_file.absolute_path}.lines") + end + + def size + data.size + end + + def empty? + data.empty? + end + + def [](index) + data[index] + end + + def slice(min, max) + data.slice(min, max) + end + + def index! + data_file.absolute_path.write(data_file.absolute_path.readlines.sort.uniq.join) + data_file.absolute_path.open(mode: 'r') do |io| + path.write(JSON.generate(lines_in(io))) + end + end + + private + + def data + @data ||= + if path.exist? + FastestCSV.parse_line(path.read) + else + data_file.open_file { |io| lines_in(io) } + end + end + + def lines_in(io) + lines = [0] + io.seek(0) + lines << io.pos while io.gets + lines.pop if lines.size > 1 + lines + end + end + end +end |
