class MFile
Overview
Memory mapped file
Max 2GB files (Slice is currently limited to Int32)
If no #capacity
is given the file is open in read only mode and
#capacity
is set to the current file size.
If the process crashes the file will be #capacity
large,
not #size
large, only on graceful close is the file truncated to its #size
.
The file does not expand further than initial #capacity
, unless manually expanded.
Defined in:
avalanchemq/mfile.crConstant Summary
-
PAGESIZE =
(LibC.sysconf(LibC::SC_PAGESIZE)).to_u32
Constructors
-
.new(path : String, capacity : Int | Nil = nil)
Map a file, if no capacity is given the file must exists and the file will be mapped as readonly The file won't be truncated if the capacity is smaller than current size
-
.open(path) : MFile
Opens an existing file in readonly mode
Class Method Summary
-
.open(path, &)
Opens an existing file in readonly mode
Instance Method Summary
- #advise(advice : Advice, offset = 0, length = @capacity)
- #capacity : Int32
-
#close(truncate_to_size = true) : Nil
Unmapping the file The file will be truncated to the current position unless readonly or deleted
-
#closed? : Bool
Returns
true
if thisIO
is closed. - #delete
- #disk_usage : UInt64
- #finalize
-
#flush
Flushes buffered data, if any.
- #fsync
- #move(new_path : String)
- #path : String
-
#pos : Int32
Returns the current position (in bytes) in this
IO
. - #pos=(pos : Int32)
- #punch_hole(size : UInt32, offset : UInt32 = 0_u32) : UInt32
-
#read(slice : Bytes)
Reads at most slice.size bytes from this
IO
into slice. - #resize(new_size : Int) : Nil
-
#rewind
Rewinds this
IO
. -
#seek(offset, whence : IO::Seek = IO::Seek::Set)
Seeks to a given offset (in bytes) according to the whence argument.
- #size : Int32
- #to_slice : Slice(UInt8)
- #truncate(new_size : Int) : Int32
-
#write(slice : Bytes) : Nil
Writes the contents of slice into this
IO
.
Instance methods inherited from class IO
skip(bytes_count : Int) : Int64
skip,
skip_to_end : Int64
skip_to_end
Class methods inherited from class IO
copy(src, dst, limit : Int) : Int64copy(src, dst) : Int64 copy
Constructor Detail
Map a file, if no capacity is given the file must exists and the file will be mapped as readonly The file won't be truncated if the capacity is smaller than current size
Class Method Detail
Instance Method Detail
Unmapping the file The file will be truncated to the current position unless readonly or deleted
Returns true
if this IO
is closed.
IO
defines returns false
, but including types may override.
Flushes buffered data, if any.
IO
defines this is a no-op method, but including types may override.
Returns the current position (in bytes) in this IO
.
The IO
class raises on this method, but some subclasses, notable
IO::FileDescriptor
and IO::Memory
implement it.
File.write("testfile", "hello")
file = File.new("testfile")
file.pos # => 0
file.gets(2) # => "he"
file.pos # => 2
Reads at most slice.size bytes from this IO
into slice.
Returns the number of bytes read, which is 0 if and only if there is no
more data to read (so checking for 0 is the way to detect end of file).
io = IO::Memory.new "hello"
slice = Bytes.new(4)
io.read(slice) # => 4
slice # => Bytes[104, 101, 108, 108]
io.read(slice) # => 1
slice # => Bytes[111, 101, 108, 108]
io.read(slice) # => 0
Rewinds this IO
. By default this method raises, but including types
may implement it.
Seeks to a given offset (in bytes) according to the whence argument.
The IO
class raises on this method, but some subclasses, notable
IO::FileDescriptor
and IO::Memory
implement it.
Returns self
.
File.write("testfile", "abc")
file = File.new("testfile")
file.gets(3) # => "abc"
file.seek(1, IO::Seek::Set)
file.gets(2) # => "bc"
file.seek(-1, IO::Seek::Current)
file.gets(1) # => "c"