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.cr

Constant Summary

PAGESIZE = (LibC.sysconf(LibC::SC_PAGESIZE)).to_u32

Constructors

Class Method Summary

Instance Method Summary

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) : Int64
copy(src, dst) : Int64
copy

Constructor Detail

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


[View source]
def self.open(path) : MFile #

Opens an existing file in readonly mode


[View source]

Class Method Detail

def self.open(path, &) #

Opens an existing file in readonly mode


[View source]

Instance Method Detail

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

[View source]
def capacity : Int32 #

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

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 delete #

[View source]
def disk_usage : UInt64 #

[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 #

[View source]
def move(new_path : String) #

[View source]
def path : String #

[View source]
def pos : Int32 #
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 : Int32) #

[View source]
def punch_hole(size : UInt32, offset : UInt32 = 0_u32) : UInt32 #

[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 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, whence : IO::Seek = IO::Seek::Set) #
Description copied from class IO

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"

[View source]
def size : Int32 #

[View source]
def to_slice : Slice(UInt8) #

[View source]
def truncate(new_size : Int) : Int32 #

[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]