class MFile
Overview
Memory mapped file
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:
lavinmq/mfile.crstdlib/copy_file_range.cr
Constructors
-
.new(path : String, capacity : Int | Nil = nil, writeonly : Bool = false)
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 Summary
-
.open(path, & : self -> _)
Opens an existing file in readonly mode
Instance Method Summary
- #advise(advice : Advice, addr = buffer, offset = 0, length = @capacity) : Nil
- #capacity : Int64
-
#close(truncate_to_size = true)
The file will be truncated to the current position unless readonly or deleted
- #copy_file_range(src : File, limit : Int)
- #copy_file_range(src : MFile, limit : Int)
-
#copy_to(output : IO, size = @size) : Int64
Copies the file to another IO Won't mmap the file if it's unmapped already
- #delete(*, raise_on_missing = true) : self
- #fd : Int32
- #finalize
-
#flush
Flushes buffered data, if any.
- #fsync : Nil
-
#inspect(io : IO)
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
- #msync
- #path : String
-
#pos : Int64
Returns the current position (in bytes) in this
IO
. -
#pos=(pos)
Sets the current position (in bytes) in this
IO
. -
#read(slice : Bytes)
Reads at most slice.size bytes from this
IO
into slice. -
#read_at(pos, bytes)
Read from a specific position in the file but without mapping the whole file, it uses
pread
- #resize(new_size : Int) : Nil
-
#rewind
Rewinds this
IO
. - #seek(offset : Int, whence : IO::Seek = IO::Seek::Set)
- #size : Int64
-
#skip(bytes_count : Int) : Int
Reads and discards exactly bytes_count bytes.
- #to_slice(pos, size)
- #to_slice
- #to_unsafe
-
#unmap : Nil
unload the memory mapping, will be remapped on demand
- #unmapped? : Bool
-
#write(slice : Bytes) : Nil
Writes the contents of slice into this
IO
.
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
The file will be truncated to the current position unless readonly or deleted
Copies the file to another IO Won't mmap the file if it's unmapped already
Flushes buffered data, if any.
IO
defines this is a no-op method, but including types may override.
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
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
Sets 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 = 3
file.gets_to_end # => "lo"
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
Read from a specific position in the file
but without mapping the whole file, it uses pread
Rewinds this IO
. By default this method raises, but including types
may implement it.
Reads and discards exactly bytes_count bytes.
Raises IO::EOFError
if there aren't at least bytes_count bytes.
io = IO::Memory.new "hello world"
io.skip(6)
io.gets # => "world"
io.skip(1) # raises IO::EOFError