UNPKG

5.9 kBJavaScriptView Raw
1//- JavaScript source code
2
3//- defs-mongo.js ~~
4//
5// These definitions are getting a lot more attention now :-)
6//
7// ~~ (c) SRW, 05 Nov 2012
8// ~~ last updated 03 Apr 2014
9
10(function () {
11 'use strict';
12
13 // Pragmas
14
15 /*jshint maxparams: 2, quotmark: single, strict: true */
16
17 /*jslint indent: 4, maxlen: 80, node: true, nomen: true */
18
19 /*properties
20 _id, api, avar_ttl, background, body, box_status, collect_garbage,
21 collection, connect, ensureIndex, error, exp_date, expireAfterSeconds,
22 find, findOne, get_avar, get_list, insert, isWorker, key, length, log,
23 mongo, MongoClient, on, push, safe, set_avar, sparse, stream, update,
24 upsert
25 */
26
27 // Declarations
28
29 var cluster, mongo;
30
31 // Definitions
32
33 cluster = require('cluster');
34
35 mongo = require('mongodb').MongoClient;
36
37 // Out-of-scope definitions
38
39 exports.api = function (options) {
40 // This function needs documentation, but as far as connection pooling is
41 // concerned, my strategy is justified by the post on Stack Overflow at
42 // http://stackoverflow.com/a/14464750.
43
44 var collect_garbage, db, get_avar, get_list, set_avar;
45
46 collect_garbage = function () {
47 // This function isn't even needed anymore, because these definitions
48 // are now taking advantage of TTL collections :-)
49 console.log('(fake garbage collection)');
50 return;
51 };
52
53 get_avar = function (params, callback) {
54 // This function needs documentation.
55 var pattern = {_id: params[0] + '&' + params[1]};
56 db.collection('avars').findOne(pattern, function (err, item) {
57 // This function needs documentation.
58 if (err !== null) {
59 return callback(err, undefined);
60 }
61 return callback(null, (item === null) ? undefined : item.body);
62 });
63 return;
64 };
65
66 get_list = function (params, callback) {
67 // This function needs documentation.
68 var items, pattern, stream;
69 items = [];
70 pattern = {box_status: params[0] + '&' + params[1]};
71 stream = db.collection('avars').find(pattern).stream();
72 stream.on('close', function () {
73 // This function needs documentation.
74 return callback(null, items);
75 });
76 stream.on('data', function (item) {
77 // This function needs documentation.
78 items.push(item.key);
79 return;
80 });
81 stream.on('error', callback);
82 return;
83 };
84
85 set_avar = function (params, callback) {
86 // This function needs documentation.
87 var obj, opts, spec;
88 if (params.length === 4) {
89 obj = {
90 _id: params[0] + '&' + params[1],
91 body: params[3],
92 box_status: params[0] + '&' + params[2],
93 exp_date: new Date(),
94 key: params[1]
95 };
96 } else {
97 obj = {
98 _id: params[0] + '&' + params[1],
99 body: params[2],
100 exp_date: new Date(),
101 key: params[1]
102 };
103 }
104 opts = {safe: true, upsert: true};
105 spec = {_id: params[0] + '&' + params[1]};
106 db.collection('avars').update(spec, obj, opts, callback);
107 return;
108 };
109
110 mongo.connect(options.mongo, function (err, db_handle) {
111 // This function is a LOT simpler than what I had before!
112 if (err !== null) {
113 throw err;
114 }
115 db = db_handle;
116 if (cluster.isWorker) {
117 return;
118 }
119 db.collection('avars').ensureIndex('exp_date', {
120 expireAfterSeconds: options.avar_ttl
121 }, function (err) {
122 // This function needs documentation.
123 if (err !== null) {
124 throw err;
125 }
126 var f, opts;
127 f = function (err) {
128 // This function needs documentation.
129 if (err !== null) {
130 console.error('Error:', err);
131 }
132 return;
133 };
134 opts = {background: true, sparse: true};
135 db.collection('avars').ensureIndex('box_status', opts, f);
136 console.log('API: MongoDB storage is ready.');
137 return;
138 });
139 return;
140 });
141
142 return {
143 collect_garbage: collect_garbage,
144 get_avar: get_avar,
145 get_list: get_list,
146 set_avar: set_avar
147 };
148 };
149
150 exports.log = function (options) {
151 // This function needs documentation.
152 var db;
153 mongo.connect(options.mongo, function (err, db_handle) {
154 // This function needs documentation.
155 if (err !== null) {
156 throw err;
157 }
158 db = db_handle;
159 return;
160 });
161 return function (obj) {
162 // This function needs documentation.
163 db.collection('traffic').insert(obj, {safe: false});
164 /*
165 db.collection('traffic').save(obj, function (err) {
166 // This function needs documentation.
167 if (err !== null) {
168 console.error('Error:', err);
169 }
170 return;
171 });
172 */
173 return;
174 };
175 };
176
177 // That's all, folks!
178
179 return;
180
181}());
182
183//- vim:set syntax=javascript: