项目作者: topac

项目描述 :
mongoDB GridFS implementation for Moped
高级语言: Ruby
项目地址: git://github.com/topac/moped-gridfs.git
创建时间: 2014-06-15T19:16:34Z
项目社区:https://github.com/topac/moped-gridfs

开源协议:MIT License

下载


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:

  1. require 'moped'
  2. require 'moped/gridfs'
  3. session = Moped::Session.new(["127.0.0.1:27017"])
  4. session.use("test")
  5. bucket = session.bucket #<Moped::GridFS::Bucket:7ffbdbd4e160 name=fs>

or

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

  1. file = bucket.open("myfile", "w+") #<Moped::GridFS::File:7f88599a58b0 bucket=fs _id=539c532ddb13a973ed000001 mode=w+ filename=myfile length=0>
  2. file.write("foobar") # 6
  3. file.seek(3) # 3
  4. 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.

  1. file.content_type # "application/octet-stream"
  2. file.md5 # "3858f62230ac3c915f300c664312c63f"
  3. file.filename = "test"
  4. 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:

  1. gem 'moped-gridfs'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ 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