Class: Visor::Meta::Backends::MongoDB
- Includes:
- Common::Exception
- Defined in:
- lib/meta/backends/mongo_db.rb
Overview
The MongoDB Backend for the VISoR Meta.
Constant Summary collapse
- DEFAULT_DB =
Connection constants
Default MongoDB database
'visor'
- DEFAULT_HOST =
Default MongoDB host address
'127.0.0.1'
- DEFAULT_PORT =
Default MongoDB host port
27017
- DEFAULT_USER =
Default MongoDB user
nil
- DEFAULT_PASSWORD =
Default MongoDB password
nil
Constants inherited from Base
Base::ACCESS, Base::ALL, Base::ARCHITECTURE, Base::BRIEF, Base::DETAIL_EXC, Base::FILTERS, Base::FORMAT, Base::MANDATORY, Base::OPTIONAL, Base::READONLY, Base::STATUS, Base::STORE, Base::TYPE
Instance Attribute Summary
Attributes inherited from Base
#conn, #db, #host, #password, #port, #user
Class Method Summary collapse
-
.connect(opts = {}) ⇒ Object
Initializes a MongoDB Backend instance.
Instance Method Summary collapse
-
#connection ⇒ Mongo::Collection
Establishes and returns a MongoDB database connection.
-
#delete_all! ⇒ Object
Delete all images records.
-
#delete_image(id) ⇒ BSON::OrderedHash
Delete an image record.
-
#get_image(id, pass_timestamps = false) ⇒ BSON::OrderedHash
Returns the requested image metadata.
-
#get_public_images(brief = false, filters = {}) ⇒ Array
Returns an array with the public images metadata.
-
#initialize(opts) ⇒ MongoDB
constructor
A new instance of MongoDB.
-
#post_image(meta, opts = {}) ⇒ BSON::OrderedHash
Create a new image record for the given metadata.
-
#put_image(id, update) ⇒ BSON::OrderedHash
Update an image’s metadata.
Methods inherited from Base
#build_uri, #deserialize_others, #serialize_others, #set_protected_post, #set_protected_put, #string_time_or_hash?, #to_sql_insert, #to_sql_update, #to_sql_where, #validate_data_post, #validate_data_put, #validate_query_filters
Constructor Details
#initialize(opts) ⇒ MongoDB
Returns a new instance of MongoDB.
50 51 52 53 |
# File 'lib/meta/backends/mongo_db.rb', line 50 def initialize(opts) super opts @conn = connection end |
Class Method Details
.connect(opts = {}) ⇒ Object
Initializes a MongoDB Backend instance.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/meta/backends/mongo_db.rb', line 38 def self.connect(opts = {}) opts[:uri] ||= '' uri = URI.parse(opts[:uri]) opts[:db] = uri.path ? uri.path.gsub('/', '') : DEFAULT_DB opts[:host] = uri.host || DEFAULT_HOST opts[:port] = uri.port || DEFAULT_PORT opts[:user] = uri.user || DEFAULT_USER opts[:password] = uri.password || DEFAULT_PASSWORD self.new opts end |
Instance Method Details
#connection ⇒ Mongo::Collection
Establishes and returns a MongoDB database connection.
59 60 61 62 63 |
# File 'lib/meta/backends/mongo_db.rb', line 59 def connection db = Mongo::Connection.new(@host, @port, :pool_size => 10, :pool_timeout => 5).db(@db) db.authenticate(@user, @password) unless @user.empty? && @password.empty? db.collection('images') end |
#delete_all! ⇒ Object
Delete all images records.
128 129 130 |
# File 'lib/meta/backends/mongo_db.rb', line 128 def delete_all! @conn.remove end |
#delete_image(id) ⇒ BSON::OrderedHash
Delete an image record.
118 119 120 121 122 123 124 |
# File 'lib/meta/backends/mongo_db.rb', line 118 def delete_image(id) img = @conn.find_one({_id: id}) raise NotFound, "No image found with id '#{id}'." unless img @conn.remove({_id: id}) img end |
#get_image(id, pass_timestamps = false) ⇒ BSON::OrderedHash
Returns the requested image metadata.
74 75 76 77 78 79 |
# File 'lib/meta/backends/mongo_db.rb', line 74 def get_image(id, = false) = @conn.find_one({_id: id}, fields: exclude) raise NotFound, "No image found with id '#{id}'." if .nil? set_protected_get id unless end |
#get_public_images(brief = false, filters = {}) ⇒ Array
Returns an array with the public images metadata.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/meta/backends/mongo_db.rb', line 95 def get_public_images(brief = false, filters = {}) validate_query_filters filters unless filters.empty? sort = [(filters.delete(:sort) || '_id'), (filters.delete(:dir) || 'asc')] filters.merge!({access: 'public'}) unless filters[:owner] fields = brief ? BRIEF : exclude pub = @conn.find(filters, fields: fields, sort: sort).to_a raise NotFound, "No public images found." if pub.empty? && filters.empty? raise NotFound, "No public images found with given parameters." if pub.empty? pub end |
#post_image(meta, opts = {}) ⇒ BSON::OrderedHash
Create a new image record for the given metadata.
143 144 145 146 147 148 |
# File 'lib/meta/backends/mongo_db.rb', line 143 def post_image(, opts = {}) validate_data_post set_protected_post , opts id = @conn.insert() self.get_image(id, true) end |
#put_image(id, update) ⇒ BSON::OrderedHash
Update an image’s metadata.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/meta/backends/mongo_db.rb', line 159 def put_image(id, update) validate_data_put update img = @conn.find_one({_id: id}) raise NotFound, "No image found with id '#{id}'." unless img set_protected_put update @conn.update({_id: id}, :$set => update) self.get_image(id, true) end |