Giter Site home page Giter Site logo

moped-gridfs's Introduction

moped-gridfs

Add GridFS support to the Moped driver.

Moped is a fast MongoDB driver for Ruby, but it does not implement the GridFS specifications (while mongo-ruby-driver does).

Bucket

GridFS places the collections in a common bucket by prefixing each with the bucket name. By default, GridFS uses two collections with names prefixed by "fs" bucket: fs.files and fs.chunks.

You can choose a different bucket name than "fs", and create multiple buckets in a single database.
Access the default bucket (named "fs") this way:

  require 'moped'
  require 'moped/gridfs'

  session = Moped::Session.new(["127.0.0.1:27017"])
  session.use("test")
  bucket = session.bucket #<Moped::GridFS::Bucket:7ffbdbd4e160 name=fs>

or

  bucket = Moped::GridFS::Bucket.new(session) #<Moped::GridFS::Bucket:7fc06db72c00 name=fs>

A list of all the buckets can be retrieved with Session#buckets. For example, you can access the photos bucket with session.buckets['photos'].

To open a file call Bucket#open, with the filename (or the _id) and the open mode. A more generic selector can be also given instead of the filename.

File

The GridFS::File class exposes an API similar to the ruby File class.

  file = bucket.open("myfile", "w+") #<Moped::GridFS::File:7f88599a58b0 bucket=fs _id=539c532ddb13a973ed000001 mode=w+ filename=myfile length=0>
  file.write("foobar") # 6
  file.seek(3) # 3
  file.read # "bar"

All the open modes are supported: r, r+, w, w+, a and a+.

GridFS::File attributes are: _id, length, chunk_size, filename, content_type, md5, aliases, metadata and uploadDate. Some of them may be changed if the file is opened in write/append mode.

  file.content_type # "application/octet-stream"
  file.md5 # "3858f62230ac3c915f300c664312c63f"
  file.filename = "test"
  file.filename # "test"

Thread safe?

It depends on what you're doing.
You may face race conditions if many threads are writing on the same file a buffer that have to be splitted onto multiple chunks. This is due to how the GridFS specs have been designed: read this.

Performance

Are pretty much the same of mongo-ruby-driver (run the script perf/compare.rb).

Installation

Add this line to your application's Gemfile:

gem 'moped-gridfs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install moped-gridfs

Contributing

  1. Fork it
  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

moped-gridfs's People

Contributors

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