diff options
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | lib/spandx/core/index_file.rb | 31 | ||||
| -rw-r--r-- | spec/unit/core/index_file_spec.rb | 17 |
3 files changed, 21 insertions, 29 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index b589574..a6fe16d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,7 +93,7 @@ GEM unicode-display_width (>= 1.4.0, < 2.0) rubocop-rspec (1.39.0) rubocop (>= 0.68.1) - ruby-prof (1.4.0) + ruby-prof (1.4.1) ruby-progressbar (1.10.1) ruby-xxHash (0.4.0.1) rugged (0.99.0) diff --git a/lib/spandx/core/index_file.rb b/lib/spandx/core/index_file.rb index 31b63c7..bf5f886 100644 --- a/lib/spandx/core/index_file.rb +++ b/lib/spandx/core/index_file.rb @@ -21,24 +21,15 @@ module Spandx end end - def search - min = 0 - max = size - + def search(min: 0, max: size) scan do |reader| until min >= max - mid = (max - min) == 1 ? min : (((max - min) / 2) + min) + mid = mid_for(min, max) row = reader.row(mid) - return if row.nil? || row.empty? - comparison = yield row return row if comparison.zero? - if comparison.positive? - min = mid + 1 - else - max = mid - end + comparison.positive? ? (min = mid + 1) : (max = mid) end end end @@ -59,12 +50,6 @@ module Spandx entry end - def scan - data_file.open_file(mode: 'rb') do |io| - yield Relation.new(io, self) - end - end - def update! return unless data_file.exist? @@ -76,6 +61,12 @@ module Spandx attr_reader :entries + def scan + data_file.open_file(mode: 'rb') do |io| + yield Relation.new(io, self) + end + end + def offset_for(row_number) row_number * UINT_32_SIZE end @@ -101,6 +92,10 @@ module Spandx lines.pop if lines.size > 1 lines end + + def mid_for(min, max) + (max - min) == 1 ? min : (((max - min) / 2) + min) + end end end end diff --git a/spec/unit/core/index_file_spec.rb b/spec/unit/core/index_file_spec.rb index d3bf9a5..da2d672 100644 --- a/spec/unit/core/index_file_spec.rb +++ b/spec/unit/core/index_file_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Spandx::Core::IndexFile do subject { described_class.new(data_file) } - describe '#scan' do + describe '#search' do let(:data_file) { Spandx::Core::DataFile.new(tmp_file.path) } let(:tmp_file) { Tempfile.new } @@ -20,21 +20,18 @@ RSpec.describe Spandx::Core::IndexFile do end specify do - subject.scan do |x| - expect(x.row(0)).to eql(['activemodel', '6.0.2.2', 'Apache-2.0']) - end + result = subject.search { |row| 'activemodel-6.0.2.2' <=> "#{row[0]}-#{row[1]}" } + expect(result).to eql(['activemodel', '6.0.2.2', 'Apache-2.0']) end specify do - subject.scan do |x| - expect(x.row(1)).to eql(['spandx', '0.1.0', 'MIT']) - end + result = subject.search { |row| 'spandx-0.1.0' <=> "#{row[0]}-#{row[1]}" } + expect(result).to eql(['spandx', '0.1.0', 'MIT']) end specify do - subject.scan do |x| - expect(x.row(2)).to eql(['zlib', '1.1.0', '0BSD']) - end + result = subject.search { |row| 'zlib-1.1.0' <=> "#{row[0]}-#{row[1]}" } + expect(result).to eql(['zlib', '1.1.0', '0BSD']) end end |
