Skip to content

Commit

Permalink
improve image_finder speed by skipping mismatch label. Closes: #1896
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed May 10, 2023
1 parent c52b5fd commit c423909
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def ext
end

def imagedir
File.join(@basedir, config['imagedir'])
config['imagedir']
end

def image_types
Expand Down
43 changes: 29 additions & 14 deletions lib/review/book/image_finder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2014-2018 Minero Aoki, Kenshi Muto, Masayoshi Takahashi
# Copyright (c) 2014-2023 Minero Aoki, Kenshi Muto, Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand All @@ -13,35 +13,50 @@
module ReVIEW
module Book
class ImageFinder
def initialize(basedir, chapid, builder, exts)
@basedir = basedir
@chapid = chapid
@builder = builder
@exts = exts
@entries = dir_entries
def initialize(chapter)
@book = chapter.book
@basedir = @book.imagedir
@chapid = chapter.id
@builder = @book.config['builder']
@entries = dir_entries.map { |path| entry_object(path) }
end

def entry_object(path)
{ path: path, basename: path.sub(/\.[^.]+$/, ''), downcase: path.sub(/\.[^.]+$/, $&.downcase) }
end

def dir_entries
Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort
Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') }
end

def add_entry(path)
@entries << path unless @entries.include?(path)
path.sub!(%r{^\./}, '')
unless @entries.find { |entry| entry[:path] == path }
@entries << entry_object(path)
end
@entries
end

def find_path(id)
targets = target_list(id)
targets.each do |target|
@exts.each do |ext|
@entries.find do |entry|
downname = entry.sub(/\.[^.]+$/, File.extname(entry).downcase)
if downname == "#{target}#{ext}" || File.absolute_path(downname) == File.absolute_path("#{target}#{ext}")
return entry
@book.image_types.each do |ext|
entries = @entries.select do |entry|
entry[:basename] == target
end

unless entries
break
end

entries.find do |entry|
if entry[:downcase] == "#{target}#{ext}"
return entry[:path]
end
end
end
end

nil
end

Expand Down
9 changes: 1 addition & 8 deletions lib/review/book/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,7 @@ def self.item_type
def initialize(chapter)
super()
@chapter = chapter
book = @chapter.book

chapid = chapter.id
basedir = book.imagedir
builder = book.config['builder']
types = book.image_types

@image_finder = ReVIEW::Book::ImageFinder.new(basedir, chapid, builder, types)
@image_finder = ReVIEW::Book::ImageFinder.new(@chapter)
end

def find_path(id)
Expand Down
14 changes: 7 additions & 7 deletions test/test_image_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ def teardown
end
end

def finder
book = Book::Base.new(@dir, config: { 'builder' => 'builder', 'imagedir' => @dir })
book.image_types = ['.jpg']
ch = Book::Chapter.new(book, 1, 'ch01', nil)
ReVIEW::Book::ImageFinder.new(ch)
end

def test_find_path_pattern1
path = File.join(@dir, 'builder/ch01/foo.jpg')
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])

assert_equal(path, finder.find_path('foo'))
end

Expand All @@ -35,7 +40,6 @@ def test_find_path_pattern2
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])
assert_equal(path, finder.find_path('foo'))
end

Expand All @@ -44,7 +48,6 @@ def test_find_path_pattern3
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])
assert_equal(path, finder.find_path('foo'))
end

Expand All @@ -53,7 +56,6 @@ def test_find_path_pattern4
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])
assert_equal(path, finder.find_path('foo'))
end

Expand All @@ -62,7 +64,6 @@ def test_find_path_pattern5
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])
assert_equal(path, finder.find_path('foo'))
end

Expand All @@ -75,7 +76,6 @@ def test_find_path_dir_symlink
path_dstimg = File.join(path_dst, 'foo.jpg')
FileUtils.touch(path_srcimg)

finder = ReVIEW::Book::ImageFinder.new(@dir, 'ch01', 'builder', ['.jpg'])
assert_equal(path_dstimg, finder.find_path('foo'))
end
end

0 comments on commit c423909

Please sign in to comment.