Class: ContentServer::QueueIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/content_server/queue_indexer.rb

Overview

Simple indexer, gets inputs events (files to index) and outputs content data updates into output queue.

Instance Method Summary collapse

Constructor Details

#initialize(input_queue) ⇒ QueueIndexer

Returns a new instance of QueueIndexer.



13
14
15
# File 'lib/content_server/queue_indexer.rb', line 13

def initialize(input_queue)
  @input_queue = input_queue
end

Instance Method Details

#calc_SHA1(path) ⇒ Object

Opens file and calculates SHA1 of it’s content, returns SHA1



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/content_server/queue_indexer.rb', line 60

def calc_SHA1(path)
  begin
    digest = Digest::SHA1.new
    File.open(path, 'rb') { |f|
      while buffer = f.read(65536) do
        digest << buffer
      end
    }
  rescue
   Log.warning("Monitored path'#{path}' does not exist. Probably file changed")
  end
  return digest.hexdigest.downcase
end

#runObject

index files and add to copy queue delete directory with it’s sub files delete file



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/content_server/queue_indexer.rb', line 20

def run
  # Start indexing on demand and write changes to queue
  thread = Thread.new do
    while true do
      (state, is_dir, path, mtime, size) = @input_queue.pop
      $process_vars.set('monitor to index queue size', @input_queue.size)
      Log.debug1("index event: state:%s  dir?%s  path:%s  time:%s  size:%s ",
                 state, is_dir, path, mtime, size)
      if state == FileMonitoring::FileStatEnum::STABLE && !is_dir
        # Calculating checksum
        checksum = calc_SHA1(path)
        $process_vars.inc('indexed_files')
        $indexed_file_count += 1
        Log.debug1("Index checksum:%s Adding index to content data. put in queue for dynamic update.",
                   checksum)
        $local_content_data_lock.synchronize{
          $local_content_data.add_instance(checksum, size, Params['local_server_name'], path, mtime)
        }
      elsif ((state == FileMonitoring::FileStatEnum::NON_EXISTING ||
          state == FileMonitoring::FileStatEnum::CHANGED) && !is_dir)
        # Remove file but only when non-existing.
        Log.debug1("File to remove: %s", path)
        $local_content_data_lock.synchronize{
          $local_content_data.remove_instance(Params['local_server_name'], path)
        }
      elsif state == FileMonitoring::FileStatEnum::NON_EXISTING && is_dir
        # Remove directory but only when non-existing.
        Log.debug1("Directory to remove: %s", path)
        $local_content_data_lock.synchronize{
          $local_content_data.remove_directory(Params['local_server_name'], path)
        }
      else
        Log.debug1("This case should not be handled: %s, %s, %s", state, is_dir, path)
      end
    end  # while true do
  end  # Thread.new do
  thread
end