summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-05-15 09:12:49 -0600
committermo khan <mo.khan@gmail.com>2020-05-15 09:12:49 -0600
commite14f8086b5d64ac5e643e524279bb2ccc627ee7c (patch)
tree1f38016b7d29db07d6b52c27098ab175702ad279
parentf1a6736ed78c9fb48645f45505b57b61cc71c822 (diff)
Load from in memory index
-rw-r--r--lib/spandx/core/index_file.rb55
-rw-r--r--lib/spandx/core/relation.rb7
-rw-r--r--spec/unit/core/index_file_spec.rb28
3 files changed, 67 insertions, 23 deletions
diff --git a/lib/spandx/core/index_file.rb b/lib/spandx/core/index_file.rb
index a99a5dd..ba059ca 100644
--- a/lib/spandx/core/index_file.rb
+++ b/lib/spandx/core/index_file.rb
@@ -8,6 +8,7 @@ module Spandx
def initialize(data_file)
@data_file = data_file
@path = Pathname.new("#{data_file.absolute_path}.lines")
+ @entries = {}
end
def each
@@ -41,11 +42,23 @@ module Spandx
end
def size
+ #path.exist? ? path.size / 2 : (data&.size || 0)
data&.size || 0
end
def position_for(row_number)
- data[row_number]
+ #puts [row_number, data.size].inspect
+ data.fetch(row_number)
+
+ #@entries.fetch(row_number) do |key|
+ #offset = row_number * 2
+ #@entries[key] = IO.read(path, 2, offset, mode: 'rb').unpack1('v')
+
+ ##@entries[key] = File.open(path, mode: 'rb') do |io|
+ ##io.seek(row_number * 2)
+ ##io.read(2).unpack1('v')
+ ##end
+ #end
end
def scan
@@ -58,7 +71,7 @@ module Spandx
return unless data_file.exist?
sort(data_file)
- rebuild_index!
+ #rebuild_index!
end
private
@@ -73,36 +86,50 @@ module Spandx
def rebuild_index!
data_file.open_file do |data_io|
- File.open(path, mode: 'w') do |index_io|
- lines_in(data_io).each_with_index do |line, index|
- index_io.pwrite([line].pack('v'), index * 2)
+ File.open(path, mode: 'wb') do |index_io|
+ lines = lines_in(data_io)
+ lines.each do |pos|
+ #puts [pos].inspect
+ data_io.seek(pos)
+ x = data_io.gets
+ puts ['bidx', pos, x].inspect
+ index_io.write([pos].pack('v'))
end
end
end
end
def load
- return build_index_from_data_file unless path.exist?
-
- load_index_file
+ return build_index_from_data_file # unless path.exist?
+
+ #puts 'read index file'
+ #[].tap do |items|
+ #i = 0
+ #each_index do |position|
+ ##puts ['ridx', i, position].inspect
+ #i+=1
+ #items << position
+ #end
+ #end
end
def build_index_from_data_file
data_file.open_file { |io| lines_in(io) }
end
- def load_index_file
- [].tap do |items|
- File.open(path, mode: 'rb') do |io|
- items << io.read(2).unpack1('v') until io.eof?
- end
+ def each_index
+ File.open(path, mode: 'rb') do |io|
+ yield io.read(2).unpack1('v') until io.eof?
end
end
def lines_in(io)
lines = [0]
io.seek(0)
- lines << io.pos while io.gets
+ while (x = io.gets)
+ #puts ['widx', x, io.pos, x.size].inspect
+ lines << io.pos
+ end
lines.pop if lines.size > 1
lines
end
diff --git a/lib/spandx/core/relation.rb b/lib/spandx/core/relation.rb
index a8bd2fe..9f5ded1 100644
--- a/lib/spandx/core/relation.rb
+++ b/lib/spandx/core/relation.rb
@@ -21,8 +21,11 @@ module Spandx
end
def row(number)
- io.seek(index.position_for(number))
- parse_row(io.gets)
+ offset = index.position_for(number)
+ io.seek(offset)
+ x = parse_row(io.gets)
+ #puts ['row', number, offset, x].inspect
+ x
end
private
diff --git a/spec/unit/core/index_file_spec.rb b/spec/unit/core/index_file_spec.rb
index 8d82b15..280bee4 100644
--- a/spec/unit/core/index_file_spec.rb
+++ b/spec/unit/core/index_file_spec.rb
@@ -3,14 +3,10 @@
RSpec.describe Spandx::Core::IndexFile do
subject { described_class.new(data_file) }
- let(:data_file) { Spandx::Core::DataFile.new(tmp_file.path) }
- let(:tmp_file) { Tempfile.new }
-
- after do
- tmp_file.unlink
- end
-
describe '#scan' do
+ let(:data_file) { Spandx::Core::DataFile.new(tmp_file.path) }
+ let(:tmp_file) { Tempfile.new }
+
before do
data_file.insert('activemodel', '6.0.2.2', ['Apache-2.0'])
data_file.insert('spandx', '0.1.0', ['MIT'])
@@ -19,6 +15,10 @@ RSpec.describe Spandx::Core::IndexFile do
subject.update!
end
+ after do
+ tmp_file.unlink
+ end
+
specify do
subject.scan do |x|
expect(x.row(0)).to eql("\"activemodel\",\"6.0.2.2\",\"Apache-2.0\"\n")
@@ -37,4 +37,18 @@ RSpec.describe Spandx::Core::IndexFile do
end
end
end
+
+ describe "#update!" do
+ let(:data_file) { Spandx::Core::DataFile.new('/home/mokha/development/spandx-rubygems/.index/00/rubygems') }
+
+ before do
+ subject.update!
+ end
+
+ it 'rebuilds the index correctly' do
+ data_file.each do |item|
+ expect(item).not_to be_nil
+ end
+ end
+ end
end