Code Monkey home page Code Monkey logo

jruby-mmap's Introduction

JRuby Mmap

This gem only supports JRuby.

JRuby Mmap is a Java JRuby extension wrapper over the Java NIO memory mapping.

See also

Installation

This gem only supports JRuby.

Add this line to your application's Gemfile:

gem 'jruby-mmap'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jruby-mmap

Building

Building uses Gradle. The build process compiles the Java classes, produces a jar file and copies the jar file in the project lib/jruby-mmap dir.

$ ./gradlew build

Usage

require "jruby-mmap"

BYTE_SIZE = 2048
FILE_PATH = "mmapfile.dat"

mmap = Mmap::ByteBuffer.new(FILE_PATH, BYTE_SIZE)
mmap.put_bytes("foobar")
mmap.close

Non String copying put_bytes

In Ruby there's no concept of byte arrays, for IO, data is ultimately carried as strings. The Mmap::ByteBuffer class exposes two methods for writing bytes to the mmap byte buffer which takes a String as argument:

  • Mmap::ByteBuffer#put_bytes
  • Mmap::ByteBuffer#put_bytes_copy

The former, put_bytes avoids copying the String content by directly passing the String underlying ByteList to the mmap byte buffer put method. This is obviously more efficient but also unsafe in the sense that further mutations of the String object will mutate that ByteList object and potentially create corruption. Invoking put_bytes is typically the last operation performed on that string so in most cases it should just be fine. If you are not sure or have doubts about the unsafe nature of put_bytes you can use put_bytes_copy which copies the string data into a new byte buffer and pass it to the mmap put method.

Tests

$ bundle install
$ bundle exec rspec

Author

Colin Surprenant on GitHub and Twitter.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/colinsurprenant/jruby-mmap.

License and Copyright

JRuby Mmap is released under the Apache License, Version 2.0. See LICENSE.

jruby-mmap's People

Contributors

colinsurprenant avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.