memmap.cr

Build Status Crystal Docs

Little lib to make using mmap() and related system calls relatively easy and hopefully fairly idiomatic.

Currently you can use either the << operator/push function to append Bytes to a mapped file, which calls ftruncate to expand te file size to fit the new data, advances the seek pointer to the old end of the file, writes whatever has been pushed, and calls mremap on Linux or simply munmap and then mmap elsewhere, or, if you prefer to copy the contents of the mapped file to a new mapped file with the new Bytes appended, you can call MapFile.write to ftruncate a file, create a new mapped buffer of the fixed size, and memcpy from the read buffer and the Bytes to be appended into the second buffer.

Calling the instance method value gets a Bytes/Slice(UInt8) that can be read from and manipulated in place safely.

Written with continual reference to the memmap crate for Rust.

Installation

Add this to your application's shard.yml:

dependencies:
  memmap:
    github: sourgrasses/memmap.cr

Usage

require "memmap"

# Maps a file named "test.txt" and prints it to stdout
file = Memmap::MapFile.new("test.txt")
file_string = String.new(file.value)
puts file_string

# Maps a file and replaces every character with 'j'
file2 = Memmap::MapFile.new("test.txt", mode = "r+")
file2.value.map! { |v| 106.to_u8 }
file2.flush()

# Maps a file and then appends a string to it
File.write("test.txt", "here are a bunch of bytes yet again")

file = Memmap::MapFile.new("test.txt", mode="r+")
appendix = " and more!".to_slice
file << appendix

Contributing

  1. Fork it (<https://github.com/sourgrasses/memmap.cr/fork>)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors