Class: Visor::Meta::Backends::MongoDB

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • [Hash] (Hash)

    a customizable set of options

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :uri (String)

    The connection uri, if provided, no other option needs to be setted.

  • :db (String) — default: DEFAULT_DB

    The wanted database.

  • :host (String) — default: DEFAULT_HOST

    The host address.

  • :port (Integer) — default: DEFAULT_PORT

    The port to be used.

  • :user (String) — default: DEFAULT_USER

    The user to be used.

  • :password (String) — default: DEFAULT_PASSWORD

    The password to be used.

  • :conn (Object)

    The connection pool to access database.



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

#connectionMongo::Collection

Establishes and returns a MongoDB database connection.

Returns:

  • (Mongo::Collection)

    A MongoDB collection object.



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.

Parameters:

  • id (Integer)

    The image’s _id to remove.

Returns:

  • (BSON::OrderedHash)

    The deleted image metadata.

Raises:

  • (NotFound)

    If image not found.



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.

Parameters:

  • id (Integer)

    The requested image’s _id.

  • pass_timestamps (true, false) (defaults to: false)

    If we want to pass timestamps setting step.

Returns:

  • (BSON::OrderedHash)

    The requested image metadata.

Raises:

  • (NotFound)

    If image not found.



74
75
76
77
78
79
# File 'lib/meta/backends/mongo_db.rb', line 74

def get_image(id, pass_timestamps = false)
  meta = @conn.find_one({_id: id}, fields: exclude)
  raise NotFound, "No image found with id '#{id}'." if meta.nil?
  set_protected_get id unless pass_timestamps
  meta
end

#get_public_images(brief = false, filters = {}) ⇒ Array

Returns an array with the public images metadata.

Parameters:

  • brief (true, false) (defaults to: false)

    (false) If true, the returned images will only contain BRIEF attributes.

  • [Hash] (Hash)

    a customizable set of options

  • opts (Hash)

    a customizable set of options

Returns:

  • (Array)

    The public images metadata.

Raises:

  • (NotFound)

    If there is no public images.



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.

Parameters:

  • meta (Hash)

    The image metadata.

  • [Hash] (Hash)

    a customizable set of options

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :owner (String) — default: Nil

    The owner of the image.

  • :size (Integer) — default: Nil

    The image file size.

Returns:

  • (BSON::OrderedHash)

    The already added image metadata.

Raises:

  • (Invalid)

    If image meta validation fails.



143
144
145
146
147
148
# File 'lib/meta/backends/mongo_db.rb', line 143

def post_image(meta, opts = {})
  validate_data_post meta
  set_protected_post meta, opts
  id = @conn.insert(meta)
  self.get_image(id, true)
end

#put_image(id, update) ⇒ BSON::OrderedHash

Update an image’s metadata.

Parameters:

  • id (Integer)

    The image _id to update.

  • update (Hash)

    The image metadata to update.

Returns:

  • (BSON::OrderedHash)

    The updated image metadata.

Raises:

  • (Invalid)

    If update metadata validation fails.

  • (NotFound)

    If image not found.



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