UNPKG

7.62 kBJavaScriptView Raw
1//- JavaScript source code
2
3//- defs-sqlite.js ~~
4//
5// These definitions need help from a SQLite guru.
6//
7// Known shortcomings:
8// - The API definition for `get_avar` does not update `exp_date`.
9// - There is no log storage definition.
10//
11// ~~ (c) SRW, 25 Sep 2012
12// ~~ last updated 10 Aug 2014
13
14
15(function () {
16 'use strict';
17
18 // Pragmas
19
20 /*jshint maxparams: 2, quotmark: single, strict: true */
21
22 /*jslint indent: 4, maxlen: 80, node: true */
23
24 /*properties
25 all, avar_ttl, body, '$body', '$box_key', '$box_status', cached, ceil,
26 collect_garbage, Database, errno, error, '$exp_date', exports, get,
27 get_avar, get_list, isWorker, join, key, '$key', length, log, map,
28 nextTick, now, '$now', run, set_avar, sqlite
29 */
30
31 // Declarations
32
33 var cluster, sqlite;
34
35 // Definitions
36
37 cluster = require('cluster');
38
39 sqlite = require('sqlite3');
40
41 // Out-of-scope definitions
42
43 module.exports = function (options) {
44 // This function needs documentation.
45
46 var collect_garbage, db, exp_date, get_avar, get_list, set_avar;
47
48 collect_garbage = function () {
49 // This function needs documentation.
50 var lines, values;
51 lines = 'DELETE FROM avars WHERE (exp_date < $now)';
52 values = {
53 $now: Math.ceil(Date.now() / 1000)
54 };
55 db.run(lines, values, function (err) {
56 // This function needs documentation.
57 if (err !== null) {
58 console.error('Error:', err);
59 return;
60 }
61 console.log('Finished collecting garbage.');
62 return;
63 });
64 return;
65 };
66
67 db = new sqlite.cached.Database(options.sqlite, function (err) {
68 // This function needs documentation.
69 if (err !== null) {
70 throw err;
71 }
72 if (cluster.isWorker) {
73 return;
74 }
75 var lines = [
76 'CREATE TABLE IF NOT EXISTS avars (',
77 ' body TEXT NOT NULL,',
78 ' box_key TEXT NOT NULL,',
79 ' box_status TEXT,',
80 ' exp_date INTEGER NOT NULL,',
81 ' key TEXT,', //- this doesn't need to be "NOT NULL"
82 ' PRIMARY KEY (box_key)',
83 ')'
84 ];
85 db.run(lines.join('\n'), function (err) {
86 // This function needs documentation.
87 if (err !== null) {
88 throw err;
89 }
90 var query;
91 query = 'CREATE INDEX IF NOT EXISTS idx_avars_box_status' +
92 ' ON avars (box_status)';
93 db.run(query, function (err) {
94 // This function needs documentation.
95 if (err !== null) {
96 throw err;
97 }
98 console.log('API: SQLite storage is ready.');
99 return;
100 });
101 return;
102 });
103 return;
104 });
105
106 exp_date = function () {
107 // This function needs documentation.
108 return Math.ceil((Date.now() / 1000) + options.avar_ttl);
109 };
110
111 get_avar = function (params, callback) {
112 // This function needs documentation.
113 var box_key, sql;
114 box_key = params[0] + '&' + params[1];
115 sql = 'SELECT body FROM avars WHERE box_key = $box_key';
116 db.get(sql, {$box_key: box_key}, function (err, row) {
117 // This function needs documentation.
118 if (err !== null) {
119 if (err.errno === 5) {
120 process.nextTick(function () {
121 // This function needs documentation.
122 return get_avar(params, callback);
123 });
124 return;
125 }
126 return callback(err, row);
127 }
128 return callback(null, (row === undefined) ? row : row.body);
129 });
130 return;
131 };
132
133 get_list = function (params, callback) {
134 // This function needs documentation.
135 var box_status, sql;
136 box_status = params[0] + '&' + params[1];
137 sql = 'SELECT key FROM avars WHERE box_status = $box_status';
138 db.all(sql, {$box_status: box_status}, function (err, rows) {
139 // This function needs documentation.
140 if (err !== null) {
141 if (err.errno === 5) {
142 process.nextTick(function () {
143 // This function needs documentation.
144 return get_list(params, callback);
145 });
146 return;
147 }
148 return callback(err, rows);
149 }
150 if (rows === undefined) {
151 return callback(null, rows);
152 }
153 return callback(null, rows.map(function (row) {
154 // This function needs documentation.
155 return row.key;
156 }));
157 });
158 return;
159 };
160
161 set_avar = function (params, callback) {
162 // This function needs documentation.
163 var lines, values;
164 if (params.length === 4) {
165 lines = [
166 'INSERT OR REPLACE INTO avars ' +
167 '(body, box_key, box_status, exp_date, key)',
168 ' VALUES ($body, ' +
169 ' $box_key,' +
170 ' $box_status,' +
171 ' $exp_date,' +
172 ' $key)'
173 ];
174 values = {
175 $body: params[3],
176 $box_key: params[0] + '&' + params[1],
177 $box_status: params[0] + '&' + params[2],
178 $exp_date: exp_date(),
179 $key: params[1]
180 };
181 } else {
182 lines = [
183 'INSERT OR REPLACE INTO avars ' +
184 '(body, box_key, exp_date)',
185 ' VALUES ($body, $box_key, $exp_date)'
186 ];
187 values = {
188 $body: params[2],
189 $box_key: params[0] + '&' + params[1],
190 $exp_date: exp_date()
191 };
192 }
193 db.run(lines.join('\n'), values, function (err) {
194 // This function needs documentation.
195 if (err !== null) {
196 if (err.errno === 5) {
197 process.nextTick(function () {
198 // This function needs documentation.
199 return set_avar(params, callback);
200 });
201 return;
202 }
203 return callback(err);
204 }
205 return callback(null);
206 });
207 return;
208 };
209
210 return {
211 collect_garbage: collect_garbage,
212 get_avar: get_avar,
213 get_list: get_list,
214 set_avar: set_avar
215 };
216 };
217
218 // That's all, folks!
219
220 return;
221
222}());
223
224//- vim:set syntax=javascript: