Class: SimpleMmap::FileWindow
- Inherits:
-
Object
- Object
- SimpleMmap::FileWindow
- Defined in:
- lib/simple_mmap/file_window.rb
Overview
Represents a window into a mmap()‘ed file, using a familiar index syntax for getting single bytes or Ranges of bytes within that FileWindow
Instance Attribute Summary collapse
-
#offset ⇒ Object
(also: #pos)
The current offset.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#[](*index) ⇒ Object
Read data from the mmap’ed file.
-
#close ⇒ Object
(also: #unmap)
unmaps the mmap’ed file.
-
#initialize(path) ⇒ FileWindow
constructor
Create a mmap’ed window for file at
path
You are responsible for closing it when you’re done using #close. -
#read(length) ⇒ Object
Read
length
bytes starting from the current offset. -
#seek(to_pos) ⇒ Object
Move the current offset to
to_pos
. -
#size ⇒ Object
Return size of mapped file.
Constructor Details
#initialize(path) ⇒ FileWindow
Create a mmap’ed window for file at path
You are responsible for closing it when you’re done using #close
28 29 30 31 32 |
# File 'lib/simple_mmap/file_window.rb', line 28 def initialize(path) @path = path @offset = 0 @mmap = SimpleMmap::MappedFile.new(@path) end |
Instance Attribute Details
#offset ⇒ Object Also known as: pos
The current offset
43 44 45 |
# File 'lib/simple_mmap/file_window.rb', line 43 def offset @offset end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
33 34 35 |
# File 'lib/simple_mmap/file_window.rb', line 33 def path @path end |
Instance Method Details
#[](*index) ⇒ Object
Read data from the mmap’ed file. Takes the same arguments as Array/String#[] (eg [2], [2,5], [2..5] or [2…5]) The current offset will be tracked accordingly
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/simple_mmap/file_window.rb', line 62 def [](*index) if index.length == 1 index = index.first end case index when Array offset, length = index when Fixnum offset = index length = 0 when Range offset = index.begin length = index.end - index.begin unless index.exclude_end? length += 1 end end @offset = offset + length if length.zero? @mmap.read_window_data(offset, 1) else @mmap.read_window_data(offset, length) end end |
#close ⇒ Object Also known as: unmap
unmaps the mmap’ed file
36 37 38 39 |
# File 'lib/simple_mmap/file_window.rb', line 36 def close @mmap.close @mmap = nil end |
#read(length) ⇒ Object
Read length
bytes starting from the current offset
90 91 92 93 94 |
# File 'lib/simple_mmap/file_window.rb', line 90 def read(length) data = @mmap.read_window_data(@offset, length) @offset += length data end |
#seek(to_pos) ⇒ Object
Move the current offset to to_pos
55 56 57 |
# File 'lib/simple_mmap/file_window.rb', line 55 def seek(to_pos) self.pos = to_pos end |
#size ⇒ Object
Return size of mapped file
97 98 99 |
# File 'lib/simple_mmap/file_window.rb', line 97 def size @mmap.size end |