Class: Taggata::FilesystemScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/taggata/filesystem_scanner.rb

Instance Method Summary collapse

Constructor Details

#initializeFilesystemScanner

Initialize scanner’s internal objects and default tag



4
5
6
7
8
# File 'lib/taggata/filesystem_scanner.rb', line 4

def initialize
  @jobs = []
  @done_files = 0
  @done_directories = 0
end

Instance Method Details

#add_directory_jobs(dirs, parent_id) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/taggata/filesystem_scanner.rb', line 40

def add_directory_jobs(dirs, parent_id)
  ids = find_in_db ::Taggata::Directory,
                   parent_id,
                   dirs.map { |d| ::File.basename d },
                   :id
  ids.zip(dirs).each { |job| @jobs << job }
end

#do_job(dir_id, path) ⇒ Object

Process directory at full path

Parameters:

  • dir

    String name of the directory

  • path

    String full path of the directory



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/taggata/filesystem_scanner.rb', line 21

def do_job(dir_id, path)
  contents = Dir.glob("#{path}/*")
             .reduce(::Taggata::File => [],
                     ::Taggata::Directory => []) do |acc, cur|
    key = ::File.file?(cur) ? ::Taggata::File : ::Taggata::Directory
    acc.merge(key => acc[key].push(cur))
  end

  contents.each_pair do |klass, files|
    save_missing files.map { |f| ::File.basename f },
                 dir_id,
                 klass unless files.empty?
  end
  @done_files += contents[::Taggata::File].length
  add_directory_jobs contents[::Taggata::Directory],
                     dir_id unless contents[::Taggata::Directory].empty?
  @done_directories += 1
end

#find_in_db(klass, parent_id, names, param) ⇒ Object



56
57
58
59
60
61
# File 'lib/taggata/filesystem_scanner.rb', line 56

def find_in_db(klass, parent_id, names, param)
  klass
    .where(:parent_id => parent_id)
    .where(:name => names)
    .map(param)
end

#process(dir) ⇒ Object

Breadth first search traversal through the filesystem tree



64
65
66
67
68
69
70
# File 'lib/taggata/filesystem_scanner.rb', line 64

def process(dir)
  @jobs << [dir.id, dir.name]
  until @jobs.empty?
    do_job(*@jobs.shift)
    report
  end
end

#reportObject

Report progress



11
12
13
14
15
# File 'lib/taggata/filesystem_scanner.rb', line 11

def report
  print 'Done files/dirs - Queued: ',
        "#{@done_files}/#{@done_directories} ",
        "- #{@jobs.length}\n"
end

#save_missing(files, parent_id, klass) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/taggata/filesystem_scanner.rb', line 48

def save_missing(files, parent_id, klass)
  in_db = find_in_db(klass, parent_id, files, :name)
  to_save = (files - in_db).map do |basename|
    { :name => basename, :parent_id => parent_id }
  end
  klass.dataset.multi_insert(to_save)
end