lib/qm/defs-mongo.rb in qm-1.2.3 vs lib/qm/defs-mongo.rb in qm-1.2.4
- old
+ new
@@ -1,127 +1,150 @@
#- Ruby source code
#- defs-mongo.rb ~~
+#
+# See http://www.sinatrarb.com/extensions.html.
+#
# ~~ (c) SRW, 16 Jul 2014
-# ~~ last updated 22 Jan 2015
+# ~~ last updated 29 Jan 2015
require 'json'
require 'mongo'
-require 'sinatra/base'
-module Sinatra
+module QM
- module MongoConnect
+ class MongoStore
- def connect_api_store(opts = settings.persistent_storage)
- # This helper function needs documentation.
- db = Mongo::MongoClient.from_uri(opts[:mongo]).db
- db.collection('avars').ensure_index({
- box: Mongo::ASCENDING,
- key: Mongo::ASCENDING
- }, {
- unique: true
- })
- db.collection('avars').ensure_index('exp_date', {
- expireAfterSeconds: 0
- })
- return db
+ def close()
+ # This function needs documentation.
+ @api_db.connection.close if @api_db.respond_to?('connection')
+ @log_db.connection.close if @log_db.respond_to?('connection')
+ return
end
- def connect_log_store(opts = settings.trafficlog_storage)
- # This helper function needs documentation.
- if opts.has_key?(:mongo) then
- return Mongo::MongoClient.from_uri(opts[:mongo]).db
+ def connect_api_store(opts = {})
+ # This function needs documentation.
+ if (opts.has_key?(:mongo)) then
+ @api_db ||= Mongo::MongoClient.from_uri(opts[:mongo]).db
+ @api_db.collection('avars').ensure_index({
+ box: Mongo::ASCENDING,
+ key: Mongo::ASCENDING
+ }, {
+ unique: true
+ })
+ @api_db.collection('avars').ensure_index('exp_date', {
+ expireAfterSeconds: 0
+ })
end
+ return @api_db
end
- end
+ def connect_log_store(opts = {})
+ # This function needs documentation.
+ if (opts.has_key?(:mongo)) then
+ @log_db ||= Mongo::MongoClient.from_uri(opts[:mongo]).db
+ end
+ return @log_db
+ end
- module MongoAPIDefs
-
def get_avar(params)
- # This helper function needs documentation.
- x = settings.api_db.collection('avars').find_and_modify({
+ # This helper function retrieves an avar's representation if it
+ # exists, and it also updates the "expiration date" of the avar in
+ # the database so that data still being used for computations will
+ # not be removed.
+ x = @api_db.collection('avars').find_and_modify({
+ fields: {
+ _id: 0,
+ body: 1
+ },
query: {
box: params[0],
key: params[1]
},
update: {
+ # NOTE: The hash below must use `=>` (not `:`) in JRuby, as
+ # of version 1.7.18, but QM won't be supporting JRuby anyway
+ # until (a.) JRuby 9000 is stable and (b.) I understand Puma.
'$set': {
- exp_date: Time.now + settings.avar_ttl
+ exp_date: Time.now + @settings.avar_ttl
}
},
- fields: {
- _id: 0,
- body: 1
- },
upsert: false
})
return (x.nil?) ? '{}' : x['body']
end
def get_list(params)
- # This helper function needs documentation.
+ # This helper function retrieves a list of "key" properties for avars
+ # in the database that have a "status" property, because those are
+ # assumed to represent task descriptions. The function returns the
+ # list as a stringified JSON array in which order is not important.
opts = {
fields: {
_id: 0,
key: 1
}
}
query = {
box: params[0],
+ exp_date: {
+ '$gt': Time.now
+ },
status: params[1]
}
x = []
- settings.api_db.collection('avars').find(query, opts).each do |doc|
- # This block needs documentation.
+ @api_db.collection('avars').find(query, opts).each do |doc|
+ # This block appends each task's key to a running list, but the
+ # the order in which the keys are added is *not* sorted.
x.push(doc['key'])
end
return (x.length == 0) ? '[]' : x.to_json
end
+ def initialize(opts = {})
+ # This constructor function needs documentation.
+ @settings = opts
+ end
+
+ def log(request)
+ # This helper function inserts a new document into MongoDB after each
+ # request. Eventually, this function will be replaced by one that
+ # delegates to a custom `log` function like the Node.js version.
+ @log_db.collection('traffic').insert({
+ host: request.host,
+ method: request.request_method,
+ timestamp: Time.now,
+ url: request.fullpath
+ }, {
+ w: 0
+ })
+ return
+ end
+
def set_avar(params)
- # This helper function needs documentation.
+ # This helper function writes an avar to the database by "upserting"
+ # a Mongo document that represents it.
doc = {
body: params.last,
box: params[0],
- exp_date: Time.now + settings.avar_ttl,
+ exp_date: Time.now + @settings.avar_ttl,
key: params[1]
}
doc['status'] = params[2] if params.length == 4
opts = {
multi: false,
- upsert: true
+ upsert: true,
+ w: 1
}
query = {
box: params[0],
key: params[1]
}
- settings.api_db.collection('avars').update(query, doc, opts)
+ @api_db.collection('avars').update(query, doc, opts)
return
end
end
-
- module MongoLogDefs
-
- def log_to_db()
- # This helper function needs documentation.
- settings.log_db.collection('traffic').insert({
- host: request.host,
- ip: request.ip,
- method: request.request_method,
- status_code: response.status,
- timestamp: Time.now,
- url: request.fullpath
- })
- return
- end
-
- end
-
- helpers MongoAPIDefs, MongoLogDefs
- register MongoConnect
end
#- vim:set syntax=ruby: