-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresBackend.js
191 lines (175 loc) · 4.89 KB
/
postgresBackend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const Pg = require('pg'),
PostgresClient = (Pg.native || Pg).Client;
class PostgresBackend {
constructor(namespace) {
this.pg = new PostgresClient();
this.namespace = namespace;
this.pg.connect(function(err) {
if (err)
throw err;
});
}
// helper function
queryRow(query, args, cb) {
if (cb === undefined &&
typeof args == 'function')
{
cb = args;
args = [];
}
this.pg.query(query, args, function(err, result) {
return cb(
err,
result ? result.rows[0] : undefined
);
});
}
insertWord(key, direction, word, cb) {
this.insertWords(
[ {key, direction, word} ],
cb
);
}
insertWords(words, cb) {
if (!words.length)
return cb();
var i = 1;
this.pg.query(
`
INSERT INTO
markov (
namespace,
markov_order,
key,
direction,
word,
weight
)
VALUES
${words.map(() => `(
'${this.namespace}',
$${i++},
$${i++},
$${i++},
$${i++},
$${i++}
)`).join(', ')}
ON CONFLICT ON CONSTRAINT markov_pkey
DO UPDATE SET
weight = markov.weight + EXCLUDED.weight;
`,
words.reduce(
(carry, item) =>
carry.concat([
item.order,
item.key,
item.direction,
item.word,
item.weight
]),
[]
),
(err, result) => {
cb(err);
}
);
}
pick(key, direction, order, cb) {
if (typeof order == 'function') {
cb = order;
order = 1;
}
this.queryRow(
`
SELECT
word
FROM (
SELECT
word,
(
sum(weight) OVER (ORDER BY word)::decimal /
sum(weight) OVER (PARTITION BY key)
) AS cumm_weight
FROM
markov
WHERE
namespace = '${this.namespace}' AND
key = $1 AND
markov_order = $2 AND
direction = $3
) result
WHERE
$4 < cumm_weight
LIMIT 1;
`,
[ key, order, direction, this.getRandomDecimal() ],
(err, result) =>
cb(err, result ? result.word : undefined)
);
}
/**
* Picks a random key from the db
* TODO: maybe create a cache table
*/
pickRandomWord(opts, cb) {
if (typeof opts == 'function') {
cb = opts;
opts = {
order: 1
};
}
else if (typeof opts == 'number') {
opts = {
order: opts
};
}
var queryParams = [ opts.order, this.getRandomDecimal() ],
hasTokens = opts.tokens && opts.tokens.length;
if (hasTokens) {
queryParams.push(opts.tokens)
}
if (opts.match) {
queryParams.push(opts.match)
}
this.queryRow(
`
SELECT
key
FROM (
SELECT
key,
(
sum(weight) OVER (ORDER BY key)::decimal /
sum(weight) OVER ()
) AS cumm_weight
FROM
(
SELECT key, sum(weight) AS weight
FROM markov
WHERE
namespace = '${this.namespace}' AND
markov_order = $1
${hasTokens ? " AND key = ANY ( $3 ) " : ''}
${opts.match ? " AND to_tsvector('english', key) @@ to_tsquery('english', $3) " : ''}
GROUP BY
key
) a
) result
WHERE
$2 <= cumm_weight
LIMIT 1;
`,
queryParams,
function(err, result) {
cb(err, result ? result.key : undefined);
}
);
}
getRandomDecimal() {
return Math.random().toString() + Math.random().toString().substr(-2);
}
end(cb) {
this.pg.end(cb);
}
}
module.exports = PostgresBackend;