Class: QM::MongoStore

Inherits:
Object
  • Object
show all
Defined in:
lib/qm/defs-mongo.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MongoStore

Returns a new instance of MongoStore.



103
104
105
106
# File 'lib/qm/defs-mongo.rb', line 103

def initialize(opts = {})
  # This constructor function needs documentation.
    @settings = opts
end

Instance Method Details

#closeObject



17
18
19
20
21
22
# File 'lib/qm/defs-mongo.rb', line 17

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

#connect_api_store(opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/qm/defs-mongo.rb', line 24

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

#connect_log_store(opts = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/qm/defs-mongo.rb', line 41

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

#get_avar(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qm/defs-mongo.rb', line 49

def get_avar(params)
  # 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
            }
        },
        upsert: false
    })
    return (x.nil?) ? '{}' : x['body']
end

#get_list(params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qm/defs-mongo.rb', line 76

def get_list(params)
  # 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 = []
    @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

#log(request) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/qm/defs-mongo.rb', line 108

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

#set_avar(params) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/qm/defs-mongo.rb', line 123

def set_avar(params)
  # 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,
        key: params[1]
    }
    doc['status'] = params[2] if params.length == 4
    opts = {
        multi: false,
        upsert: true,
        w: 1
    }
    query = {
        box: params[0],
        key: params[1]
    }
    @api_db.collection('avars').update(query, doc, opts)
    return
end