summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-05-10 11:57:52 -0600
committermo khan <mo.khan@gmail.com>2020-05-10 11:57:52 -0600
commitc19558661b919522950a8f3f8006fd647f18de22 (patch)
treefaee04498e6786cd02faaee943189ace8e695795
parenta3b678d124621a3bfa80ea2f532040f020f3dc81 (diff)
Implement IndexFile#each and fix failing tests
-rw-r--r--lib/spandx/core/cache.rb10
-rw-r--r--lib/spandx/core/data_file.rb20
-rw-r--r--lib/spandx/core/index_file.rb43
-rw-r--r--spec/integration/core/cache_spec.rb17
4 files changed, 49 insertions, 41 deletions
diff --git a/lib/spandx/core/cache.rb b/lib/spandx/core/cache.rb
index bef39c7..fa7896b 100644
--- a/lib/spandx/core/cache.rb
+++ b/lib/spandx/core/cache.rb
@@ -34,9 +34,13 @@ module Spandx
datafile_for(name).insert(name, version, licenses)
end
+ def datafile_for(name)
+ datafiles.fetch(key_for(name))
+ end
+
def rebuild_index
datafiles.each do |_hex, datafile|
- datafile.index!
+ datafile.index.update!
end
end
@@ -50,10 +54,6 @@ module Spandx
digest_for(name)[0...2]
end
- def datafile_for(name)
- datafiles.fetch(key_for(name))
- end
-
def datafiles
@datafiles ||= candidate_keys.each_with_object({}) do |key, memo|
memo[key] = DataFile.new(File.join(root, key, package_manager))
diff --git a/lib/spandx/core/data_file.rb b/lib/spandx/core/data_file.rb
index 5daa65f..e958153 100644
--- a/lib/spandx/core/data_file.rb
+++ b/lib/spandx/core/data_file.rb
@@ -32,14 +32,8 @@ module Spandx
end
end
- def to_csv(array)
- array.to_csv(force_quotes: true)
- end
-
- def index!
- return unless exist?
-
- index.index!
+ def exist?
+ absolute_path.exist?
end
def open_file(mode: 'r')
@@ -52,15 +46,11 @@ module Spandx
nil
end
- private
-
def index
@index ||= IndexFile.new(self)
end
- def exist?
- absolute_path.exist?
- end
+ private
def search_for(term, io, lines)
return if lines.empty?
@@ -86,6 +76,10 @@ module Spandx
min, max = comparison.positive? ? [mid + 1, lines.size] : [0, mid]
lines.slice(min, max)
end
+
+ def to_csv(array)
+ array.to_csv(force_quotes: true)
+ end
end
end
end
diff --git a/lib/spandx/core/index_file.rb b/lib/spandx/core/index_file.rb
index f6112f0..f8833ff 100644
--- a/lib/spandx/core/index_file.rb
+++ b/lib/spandx/core/index_file.rb
@@ -10,6 +10,12 @@ module Spandx
@path = Pathname.new("#{data_file.absolute_path}.lines")
end
+ def each
+ data.each do |position|
+ yield position
+ end
+ end
+
def size
data.size
end
@@ -26,22 +32,37 @@ module Spandx
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
+ def update!
+ return unless data_file.exist?
+
+ sort(data_file)
+ rebuild_index!
end
private
+ def sort(data_file)
+ data_file.absolute_path.write(data_file.absolute_path.readlines.sort.uniq.join)
+ end
+
+ def rebuild_index!
+ data_file.open_file do |io|
+ lines = lines_in(io)
+ path.write(lines.map(&:to_s).join(','))
+ @data = lines
+ end
+ end
+
def data
- @data ||=
- if path.exist?
- FastestCSV.parse_line(path.read)
- else
- data_file.open_file { |io| lines_in(io) }
- end
+ @data ||= load
+ end
+
+ def load
+ if path.exist?
+ FastestCSV.parse_line(path.read).map(&:to_i)
+ else
+ data_file.open_file { |io| lines_in(io) }
+ end
end
def lines_in(io)
diff --git a/spec/integration/core/cache_spec.rb b/spec/integration/core/cache_spec.rb
index 3f947b2..3eb9de3 100644
--- a/spec/integration/core/cache_spec.rb
+++ b/spec/integration/core/cache_spec.rb
@@ -94,8 +94,6 @@ RSpec.describe Spandx::Core::Cache do
end
context 'when new items are added to the catalogue' do
- let(:index) { JSON.parse(IO.read(File.join(root_dir, 'cf', 'rubygems.lines'))) }
-
before do
subject.insert('spandx', '0.0.0', ['MIT'])
subject.insert('bolt', '0.2.0', ['Apache-2.0'])
@@ -105,20 +103,15 @@ RSpec.describe Spandx::Core::Cache do
end
it 'sorts each datafile' do
- lines = IO.readlines(File.join(root_dir, 'cf', 'rubygems'))
+ lines = subject.datafile_for('spandx').absolute_path.readlines
expect(lines).to eql(lines.sort)
end
- specify { expect(index).to be_instance_of(Array) }
-
- it 'stores an array that contains the position of the start of each line' do
- expect(index.count).to be(3)
- end
-
# rubocop:disable RSpec/MultipleExpectations
- it 'builds an index that contains the seek address for the start of each line' do
- File.open(File.join(root_dir, 'cf', 'rubygems')) do |io|
- index.each do |position|
+ it 'builds an index that contains the seek position for the start of each line' do
+ data_file = subject.datafile_for('spandx')
+ data_file.open_file do |io|
+ data_file.index.each do |position|
unless position.zero?
io.seek(position - 1)
expect(io.readchar).to eql("\n")