Class: Rufio::NativeScannerZigCore

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/native_scanner_zig.rb

Overview

非同期スキャナークラス(ポーリングベース)

Constant Summary collapse

POLL_INTERVAL =

10ms

0.01

Instance Method Summary collapse

Constructor Details

#initializeNativeScannerZigCore

Returns a new instance of NativeScannerZigCore.

Raises:

  • (StandardError)


69
70
71
72
# File 'lib/rufio/native_scanner_zig.rb', line 69

def initialize
  @handle = NativeScannerZigFFI.core_async_create
  raise StandardError, "Failed to create scanner" if @handle.zero?
end

Instance Method Details

#cancelObject

キャンセル



150
151
152
# File 'lib/rufio/native_scanner_zig.rb', line 150

def cancel
  NativeScannerZigFFI.core_async_cancel(@handle)
end

#closeObject

スキャナーを明示的に破棄



163
164
165
166
167
168
# File 'lib/rufio/native_scanner_zig.rb', line 163

def close
  return if @handle.zero?

  NativeScannerZigFFI.core_async_destroy(@handle)
  @handle = 0
end

#get_progressObject

進捗取得



139
140
141
142
143
144
145
146
147
# File 'lib/rufio/native_scanner_zig.rb', line 139

def get_progress
  current = Fiddle::Pointer.malloc(8)
  total = Fiddle::Pointer.malloc(8)
  NativeScannerZigFFI.core_async_get_progress(@handle, current, total)
  {
    current: current[0, 8].unpack1('Q'),
    total: total[0, 8].unpack1('Q')
  }
end

#get_resultsObject

結果取得(完了後)



155
156
157
158
159
160
# File 'lib/rufio/native_scanner_zig.rb', line 155

def get_results
  count = NativeScannerZigFFI.core_async_get_count(@handle)
  entries = []
  count.times { |i| entries << get_entry(i) }
  entries
end

#get_stateObject

状態確認



133
134
135
136
# File 'lib/rufio/native_scanner_zig.rb', line 133

def get_state
  state_code = NativeScannerZigFFI.core_async_get_state(@handle)
  [:idle, :scanning, :done, :cancelled, :failed][state_code] || :failed
end

#scan_async(path) ⇒ Object

非同期スキャン開始

Raises:

  • (StandardError)


75
76
77
78
79
# File 'lib/rufio/native_scanner_zig.rb', line 75

def scan_async(path)
  result = NativeScannerZigFFI.core_async_scan(@handle, path)
  raise StandardError, "Failed to start scan" if result != 0
  self
end

#scan_fast_async(path, max_entries) ⇒ Object

高速スキャン(エントリ数制限付き)

Raises:

  • (StandardError)


82
83
84
85
86
# File 'lib/rufio/native_scanner_zig.rb', line 82

def scan_fast_async(path, max_entries)
  result = NativeScannerZigFFI.core_async_scan_fast(@handle, path, max_entries)
  raise StandardError, "Failed to start scan" if result != 0
  self
end

#wait(timeout: nil) ⇒ Object

ポーリングして完了待ち



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rufio/native_scanner_zig.rb', line 89

def wait(timeout: nil)
  start_time = Time.now
  loop do
    state = get_state

    case state
    when :done
      return get_results
    when :failed
      raise StandardError, "Scan failed"
    when :cancelled
      raise StandardError, "Scan cancelled"
    end

    if timeout && (Time.now - start_time) > timeout
      raise StandardError, "Timeout"
    end

    sleep POLL_INTERVAL
  end
end

#wait_with_progress(&block) ⇒ Object

進捗報告付きで完了待ち



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rufio/native_scanner_zig.rb', line 112

def wait_with_progress(&block)
  loop do
    state = get_state
    progress = get_progress

    yield(progress[:current], progress[:total]) if block_given?

    case state
    when :done
      return get_results
    when :failed
      raise StandardError, "Scan failed"
    when :cancelled
      raise StandardError, "Scan cancelled"
    end

    sleep POLL_INTERVAL
  end
end