Class: Ephem::IO::BinaryReader

Inherits:
Object
  • Object
show all
Defined in:
lib/ephem/io/binary_reader.rb

Constant Summary collapse

RECORD_SIZE =
1024

Instance Method Summary collapse

Constructor Details

#initialize(file_object) ⇒ BinaryReader

Returns a new instance of BinaryReader.



8
9
10
11
12
13
14
15
16
17
# File 'lib/ephem/io/binary_reader.rb', line 8

def initialize(file_object)
  validate_file_object(file_object)
  @file = file_object

  # Create a Mutex for thread-safe access to the file
  # Mutex ensures that only one thread can access the critical section at
  # a time. This prevents race conditions and data corruption when
  # multiple threads read from the file simultaneously
  @lock = Mutex.new
end

Instance Method Details

#closeObject



41
42
43
# File 'lib/ephem/io/binary_reader.rb', line 41

def close
  @file.close
end

#read_array(start_word:, end_word:, endianness:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ephem/io/binary_reader.rb', line 28

def read_array(start_word:, end_word:, endianness:)
  validate_array_bounds(start_word, end_word)
  length = end_word - start_word + 1

  # Use the Mutex to synchronize access to the file only one thread can
  # enter this critical section at a time. This ensures atomic reads of
  # the array data, preventing data corruption
  @lock.synchronize do
    seek_to_word(start_word)
    read_array_data(length, endianness)
  end
end

#read_record(record_number) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/ephem/io/binary_reader.rb', line 19

def read_record(record_number)
  validate_record_number(record_number)

  @lock.synchronize do
    seek_to_record(record_number)
    read_and_pad_record
  end
end