Class: Kozeki::QueuedFilesystem

Inherits:
Object
  • Object
show all
Includes:
Filesystem
Defined in:
lib/kozeki/queued_filesystem.rb

Constant Summary collapse

Operation =
Data.define(:method, :args)

Constants included from Filesystem

Filesystem::Entry, Filesystem::Event

Instance Method Summary collapse

Constructor Details

#initialize(filesystem:, threads: 6) ⇒ QueuedFilesystem

Returns a new instance of QueuedFilesystem.



9
10
11
12
13
14
15
16
# File 'lib/kozeki/queued_filesystem.rb', line 9

def initialize(filesystem:, threads: 6)
  @backend = filesystem
  @lock = Mutex.new
  @threads_num = threads
  @threads = nil
  @queue = nil
  start
end

Instance Method Details

#delete(path) ⇒ Object



40
41
42
43
# File 'lib/kozeki/queued_filesystem.rb', line 40

def delete(path)
  @queue.push Operation.new(method: :delete, args: [path])
  nil
end

#flushObject



25
26
27
28
29
30
31
32
33
# File 'lib/kozeki/queued_filesystem.rb', line 25

def flush
  return unless @threads
  ths, q = @lock.synchronize do
    [@threads, @queue]
  end
  start
  q.close
  ths.each(&:value)
end

#listObject



60
# File 'lib/kozeki/queued_filesystem.rb', line 60

def list(...) = @backend.list(...)

#list_entriesObject



61
# File 'lib/kozeki/queued_filesystem.rb', line 61

def list_entries(...) = @backend.list_entries(...)

#readObject



58
# File 'lib/kozeki/queued_filesystem.rb', line 58

def read(...) = @backend.read(...)

#read_with_mtimeObject



59
# File 'lib/kozeki/queued_filesystem.rb', line 59

def read_with_mtime(...) = @backend.read_with_mtime(...)

#retain_onlyObject



63
# File 'lib/kozeki/queued_filesystem.rb', line 63

def retain_only(...) = @backend.retain_only(...)

#startObject



18
19
20
21
22
23
# File 'lib/kozeki/queued_filesystem.rb', line 18

def start
  @lock.synchronize do
    queue = @queue = Queue.new
    @threads = @threads_num.times.map { Thread.new(queue, &method(:do_worker)) }
  end
end

#watchObject



62
# File 'lib/kozeki/queued_filesystem.rb', line 62

def watch(...) = @backend.watch(...)

#write(path, string) ⇒ Object



35
36
37
38
# File 'lib/kozeki/queued_filesystem.rb', line 35

def write(path, string)
  @queue.push Operation.new(method: :write, args: [path, string])
  nil
end