Class: Vayacondios::Server::MongoDriver

Inherits:
Object
  • Object
show all
Includes:
Driver
Defined in:
lib/vayacondios/server/drivers/mongo.rb

Constant Summary

Constants included from Driver

Driver::Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Driver

#base_insert, #base_remove, #base_retrieve, #base_search, #base_update, #call, drivers, included, load_driver, retrieve, #set_location, #set_log, #unset_location

Constructor Details

#initialize(options = {}) ⇒ MongoDriver

Returns a new instance of MongoDriver.



9
10
11
12
13
# File 'lib/vayacondios/server/drivers/mongo.rb', line 9

def initialize(options = {})
  @log = options[:log]
  mongo = EM::Mongo::Connection.new(options[:host], options[:port], 1, reconnect_in: 1)
  @connection = mongo.db options[:name]
end

Class Method Details

.connect(options = {}) ⇒ Object



5
6
7
# File 'lib/vayacondios/server/drivers/mongo.rb', line 5

def self.connect(options = {})
  new(options)
end

Instance Method Details

#connectionObject



15
16
17
# File 'lib/vayacondios/server/drivers/mongo.rb', line 15

def connection
  @connection.collection location
end

#countObject



125
126
127
# File 'lib/vayacondios/server/drivers/mongo.rb', line 125

def count
  connection.count
end

#format_id(id) ⇒ BSON::ObjectId

Coerce objects into a BSON::ObjectId representation if possible.

Parameters:

  • id (BSON::ObjectId, Hash, #to_s)

    the object to be coerced

Returns:

  • (BSON::ObjectId)

    the canonical representation of the ID

Raises:

  • (Error)

    if ‘id` is a Hash and is missing the `$oid` parameter which is expected in this case

  • (Error)

    if the String representation of ‘id` is blank or empty



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vayacondios/server/drivers/mongo.rb', line 25

def format_id id
  case
  when id.is_a?(BSON::ObjectId)
    id
  when id.is_a?(Hash)
    raise Error.new("When settings the ID of a #{self.class} with a Hash, an '$oid' key is required") if id['$oid'].nil?
    format_id(id['$oid'])
  when !id.to_s.empty?
    id.to_s.match(/^[a-f0-9]{24}$/) ? BSON::ObjectId(id.to_s) : id.to_s
  else
    raise Error.new("A #{self} cannot have a blank or empty ID")
  end
end

#insert(request) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/vayacondios/server/drivers/mongo.rb', line 93

def insert request
  mongo_doc = mongo_prepare request
  log.debug "    Mongo doc: #{mongo_doc}"
  res = connection.save mongo_doc
  log.debug "      Result: #{res}"
  res = mongo_doc[:_id] if res == true
  { _id: format_id(res).to_s }
end

#mongo_prepare(doc) ⇒ Object



39
40
41
42
# File 'lib/vayacondios/server/drivers/mongo.rb', line 39

def mongo_prepare doc
  doc[:_id] = format_id(doc[:_id]) if doc[:_id]
  doc
end

#mongo_unprepare(doc) ⇒ Object



44
45
46
47
# File 'lib/vayacondios/server/drivers/mongo.rb', line 44

def mongo_unprepare doc
  doc['_id'] = doc['_id'].to_s
  doc.symbolize_keys
end

#projector(query) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vayacondios/server/drivers/mongo.rb', line 68

def projector query
  order = query.delete(:order)
  if order.to_s.match(/^(a|de)sc$/i)
    order = order.to_sym
  else
    raise Error.new("Search order must be 'asc' or 'desc'. Invalid search order: #{order}")
  end

  fields = query[:fields]
  fields.map!{|field| field.join('.') } if fields.is_a? Array

  query[:sort] = [query[:sort].join('.'), order]
  query
end

#remove(request, filter) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/vayacondios/server/drivers/mongo.rb', line 111

def remove(request, filter)
  mongo_doc = mongo_prepare(request)
  mongo_doc.merge! selector(filter)
  log.debug "    Mongo doc: #{mongo_doc}"
  res = connection.remove mongo_doc
  log.debug "      Result: #{res}"
  nil
end

#reset!Object

for testing only



121
122
123
# File 'lib/vayacondios/server/drivers/mongo.rb', line 121

def reset!
  connection.drop
end

#retrieve(request) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/vayacondios/server/drivers/mongo.rb', line 102

def retrieve request
  mongo_doc = mongo_prepare request
  log.debug "    Mongo doc: #{mongo_doc}"
  res = connection.find_one mongo_doc
  log.debug "      Result: #{res}"
  return nil if res.nil?
  mongo_unprepare res
end

#search(request, filter, opts) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/vayacondios/server/drivers/mongo.rb', line 83

def search(request, filter, opts)
  select = selector(filter)
  log.debug "    Selector doc: #{select}"
  project = projector(opts)
  log.debug "    Projector doc: #{project}"
  res = connection.find(select, project) || []
  log.debug "      Result: #{res}"
  res.map{ |res| mongo_unprepare res }
end

#selector(query) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/vayacondios/server/drivers/mongo.rb', line 49

def selector query
  sel = { }.tap do |sel|
    time = query.delete(:_t)
    sel[:_t] = time.inject({}){ |t, (k,v)| t[('$' + k.to_s).to_sym] = v ; t } if time
    sel.merge! to_dotted_hash(query)
  end.compact_blank
end

#to_dotted_hash(hsh, key_string = '') ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/vayacondios/server/drivers/mongo.rb', line 57

def to_dotted_hash(hsh, key_string = '')
  hsh.each_with_object({}) do |(k, v), ret|
    key = key_string + k.to_s
    if v.is_a? Hash
      ret.merge! to_dotted_hash(v, key + '.')
    else
      ret[key] = v
    end
  end
end