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.cr
stdlib/copy_file_range.cr

Constructors

Class Method Summary

Instance Method Summary

Class methods inherited from class IO

copy(src, dst, limit : Int) : Int64
copy(src, dst) : Int64
copy

Constructor Detail

def self.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


[View source]

Class Method Detail

def self.open(path, & : self -> _) #

Opens an existing file in readonly mode


[View source]

Instance Method Detail

def advise(advice : Advice, addr = buffer, offset = 0, length = @capacity) : Nil #

[View source]
def capacity : Int64 #

[View source]
def close(truncate_to_size = true) #

Unmapping the file The file will be truncated to the current position unless readonly or deleted


[View source]
def closed? : Bool #
Description copied from class IO

Returns true if this IO is closed.

IO defines returns false, but including types may override.


[View source]
def copy_file_range(src : File, limit : Int) #

[View source]
def copy_file_range(src : MFile, limit : Int) #

[View source]
def copy_to(output : IO, size = @size) : Int64 #

Copies the file to another IO Won't mmap the file if it's unmapped already


[View source]
def delete : self #

[View source]
def fd : Int32 #

[View source]
def finalize #

[View source]
def flush #
Description copied from class IO

Flushes buffered data, if any.

IO defines this is a no-op method, but including types may override.


[View source]
def fsync : Nil #

[View source]
def inspect(io : IO) #
Description copied from class Reference

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>

[View source]
def msync #

[View source]
def path : String #

[View source]
def pos : Int64 #
Description copied from class IO

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

[View source]
def pos=(pos) #
Description copied from class IO

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"

[View source]
def read(slice : Bytes) #
Description copied from class IO

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

[View source]
def read_at(pos, bytes) #

Read from a specific position in the file but without mapping the whole file, it uses pread


[View source]
def resize(new_size : Int) : Nil #

[View source]
def rewind #
Description copied from class IO

Rewinds this IO. By default this method raises, but including types may implement it.


[View source]
def seek(offset : Int, whence : IO::Seek = IO::Seek::Set) #

[View source]
def size : Int64 #

[View source]
def skip(bytes_count : Int) : Int #
Description copied from class IO

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

[View source]
def to_slice(pos, size) #

[View source]
def to_slice #

[View source]
def to_unsafe #

[View source]
def unmap : Nil #

unload the memory mapping, will be remapped on demand


[View source]
def unmapped? : Bool #

[View source]
def write(slice : Bytes) : Nil #
Description copied from class IO

Writes the contents of slice into this IO.

io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"

[View source]