This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
382 lines (339 loc) · 11.1 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* Module dependencies.
*/
var path = require('path'),
mkdirp = require('mkdirp'),
_ = require('lodash'),
Knex = require('knex'),
Promise = require('bluebird');
exports = module.exports = init;
/**
* Module initialization function.
*
* @public
*/
function init(config) {
return new TiqDB(config);
}
/**
* Main module object.
*
* @param {Object} config
* @param {string} [config.client="sqlite3"] - The client for the chosen RDBMS.
* This can be one of "sqlite3", "pg" or "mysql".
* @param {Object} [config.connection={}]
* @param {string} [config.connection.host="localhost"]
* @param {string} [config.connection.user=null]
* @param {string} [config.connection.password=null]
* @param {string} [config.connection.database="tiq"]
* @param {string} [config.connection.filename="$XDG_DATA_HOME/tiq/store.db"] -
* The storage file to use. Only applicable to SQLite.
* @constructor
* @private
*/
function TiqDB(config) {
var defaultConfig = {
client: 'sqlite3',
connection: {
host: 'localhost',
user: null,
password: null,
database: 'tiq',
filename: path.join(process.env.XDG_DATA_HOME ||
path.join(process.env.HOME, '.local', 'share'), 'tiq', 'store.db')
}
},
config = _.merge(defaultConfig, config || {});
// Setup the DB connection
Knex.knex = Knex.initialize(config);
this.config = config;
return this;
}
/**
* Connect to the database and create the schema if unavailable.
*/
TiqDB.prototype.enter = function() {
// Make sure the storage directory exists if dealing with sqlite
if (this.config.client == 'sqlite3') {
mkdirp.sync(path.dirname(this.config.connection.filename));
}
}
/**
* Cleanup and close DB connection.
*/
TiqDB.prototype.exit = function() {
Promise.all(this.pendingOperations).then(function() {
Knex.knex.client.pool.destroy();
});
}
/**
* Create the DB schema.
*/
TiqDB.prototype.createSchema = function() {
var tiq = this,
knex = Knex.knex;
if (typeof tiq.schemaCreated !== 'undefined') {
return tiq.schemaCreated;
}
var promise = Promise.join(
knex.schema.hasTable('tags').then(function(exists) {
if (!exists) {
return knex.schema.createTable('tags', function(t) {
t.increments().primary();
t.string('text', 1024).notNullable();
t.string('namespace').notNullable();
t.integer('count', 100).defaultTo(0);
t.timestamps();
t.unique(['text', 'namespace']);
});
}
}),
knex.schema.hasTable('tags_associations').then(function(exists) {
if (!exists) {
return knex.schema.createTable('tags_associations', function(t) {
t.integer('tag_id1').notNullable();
t.integer('tag_id2').notNullable();
t.unique(['tag_id1', 'tag_id2']);
});
}
})
);
this.schemaCreated = promise;
return promise;
}
/**
* Keep a record of a pending operation for process cleanup purposes.
*
* This is used to wait for any operations in process before tearing down
* the connection. Mainly for using the plugin in the CLI tool, and making
* sure the sync call to `.exit()` waits for everything to finish.
*/
TiqDB.prototype.holdOperation = function(promise) {
// To avoid memory leaks and still make this plugin usable in the CLI,
// let's just set a new array. To deal with multiple operations, we would
// need to add some sort of cleanup operation for stale (fulfilled) promises.
// This is assuming the CLI tool won't have any concurrent operations.
// This will probably need to be rethought in the near future.
this.pendingOperations = [promise];
}
/**
* Check if two arrays are either equal or palindromes, i.e. their elements
* match even if one is reversed.
*
* @param {Array} arr1
* @param {Array} arr2
* @returns {Boolean} - Whether the arrays are equal or not.
*/
TiqDB.prototype.equalArrays = function(arr1, arr2) {
return _.isEqual(arr1, arr2) || _.isEqual(arr1, arr2.reverse());
}
/**
* Associate a collection of tokens with a collection of tags.
*
* Practically, tags and tokens are the same thing (arbitrary text). We just
* treat them as different concepts to be able to associate them correctly.
*
* Each "tag" is associated with each "token", and each "token" is associated
* with each "tag".
*
* @param {Array.<string>} tokens
* @param {Array.<string>} tags
* @param {string} [ns='public'] - Namespace used for all tags and tokens.
*/
TiqDB.prototype.associate = function(tokens, tags, ns) {
if (!tokens.length || !tags.length) {
return;
}
ns = ns || 'public';
var tiq = this,
knex = Knex.knex,
allTags = _.uniq(tokens.concat(tags));
var promise = this.createSchema().then(function() {
// Couldn't figure out a way to use Bluebird's `bind()` to maintain scope
// within the promise handler functions, so we use this poor-man's version.
var scope = {};
return knex.transaction(function(trans) {
// Fetch existing tags
knex('tags').transacting(trans)
.select('id', 'text').whereIn('text', allTags).andWhere('namespace', ns)
.then(function(existingTags) {
scope.existingTags = existingTags;
// Create missing tags
scope.missingTagsRaw = _.xor(allTags, _.pluck(existingTags, 'text'));
if (scope.missingTagsRaw.length) {
var now = new Date();
var tagObjs = _.map(
scope.missingTagsRaw, function(t) {
return {text: t, namespace: ns,
created_at: now, updated_at: now}
}
);
return knex('tags').transacting(trans).insert(tagObjs);
}
}).then(function(inserted) {
// `inserted` is of not much use here, since it returns different things
// depending on the client (see http://knexjs.org/#Builder-insert),
// so we avoid using it and just run another query to get the newly
// created tags.
if (!scope.missingTagsRaw.length) {
return [];
}
return knex('tags').transacting(trans)
.select('id', 'text')
.whereIn('text', scope.missingTagsRaw).andWhere('namespace', ns);
}).then(function(missingTags) {
scope.fetchedTags = scope.existingTags.concat(missingTags);
// Fetch existing associations
var tagIds = _.pluck(scope.fetchedTags, 'id');
return knex('tags_associations').transacting(trans)
.select('tag_id1', 'tag_id2').whereIn('tag_id1', tagIds);
}).then(function(existingAssocs) {
var associations = [],
tokenIds = [],
tagIds = [];
// Extract all tag IDs
_.forOwn(scope.fetchedTags, function(tag) {
if (_.contains(tags, tag.text)) {
tagIds.push(tag.id);
} else {
tokenIds.push(tag.id);
}
});
// Build the association maps
// We only need them one-way, as they'll be fetched backwards as well.
_.each(tagIds, function(tid) {
associations = associations.concat(
_.map(tokenIds, function(t) {return {tag_id1: tid, tag_id2: t}})
);
});
// Include only missing associations, to not trip the unique DB constraint
scope.missingAssocs = _.filter(associations, function(assoc) {
var notExists = true,
assocValues = _.values(assoc);
for (var i=0; i < existingAssocs.length; i++) {
if (tiq.equalArrays(assocValues, _.values(existingAssocs[i]))) {
notExists = false;
break;
}
}
return notExists;
});
if (scope.missingAssocs.length) {
// Create the missing associations
return knex('tags_associations').transacting(trans)
.insert(scope.missingAssocs);
}
}).then(function(inserted) {
// Increment the association count for each tag
var tagIds = _.uniq(
_.pluck(scope.missingAssocs, 'tag_id1').concat(
_.pluck(scope.missingAssocs, 'tag_id2')
)
);
if (!tagIds.length) {
return;
}
return knex('tags').transacting(trans)
.whereIn('id', tagIds)
.update({
'updated_at': new Date(),
'count': knex.raw('count + 1')
});
}).then(function() {
if (trans.connection) {
trans.commit();
}
}, trans.rollback);
});
});
this.holdOperation(promise);
return promise;
}
/**
* Get the tags associated with the given tokens.
*
* @param {Array.<string>} tokens
* @param {string} [ns='public'] - Namespace used for all tags and tokens.
* @returns {Array.<string>} - Collection of associated tags.
*/
TiqDB.prototype.describe = function(tokens, ns) {
if (!tokens.length) {
return;
}
ns = ns || 'public';
var tiq = this,
knex = Knex.knex,
tokens = _.uniq(tokens);
var promise = this.createSchema().then(function() {
/*
We need individual selects for each token passed, so we can use intersect
on them later. This could be simplified a lot and use a single query if
Knex supported standard SQL 'intersect'. Basically, intersect these
individual queries and wrap them up in another select to fetch the final
text. We could accomplish this with raw queries, but we lose the abstraction
and would have to deal with protecting against SQL injection ourselves.
*/
var queries = [],
// Select either forwards or backwards relation
rawCase = knex.raw(
'(case '
+ 'when tag_id1 = id then tag_id2 '
+ 'when tag_id2 = id then tag_id1 '
+ 'end) id'
);
_.each(tokens, function(tok) {
queries.push(
knex('tags_associations')
.select(rawCase)
.join('tags', function() {
this.on('tag_id1', '=', 'id')
.orOn('tag_id2', '=', 'id');
}).where('text', tok).andWhere('namespace', ns)
);
});
return Promise.all(queries);
}).spread(function() {
var args = arguments,
allIds = [];
// Extract all IDs from queries results
for (var i=0; i<args.length; i++) {
var ids = [];
for (var j=0; j<args[i].length; j++) {
ids.push(args[i][j].id);
}
allIds.push(ids);
}
// Only IDs that exist in *all* results
var ids = _.intersection.apply(this, allIds);
if (!ids.length) {
return ids;
}
return knex('tags')
.select('text')
.whereIn('id', ids).pluck('text');
});
this.holdOperation(promise);
return promise;
}
/**
* Search for tags matching the text.
*
* @param {string} text
* @param {string} [ns='public'] - Namespace to search in.
* @returns {Array.<string>} - Collection of matching tags.
*/
TiqDB.prototype.search = function(text, ns) {
if (!text) {
return;
}
ns = ns || 'public';
var tiq = this,
knex = Knex.knex;
var promise = this.createSchema().then(function() {
return knex('tags').select('text')
.where('text', 'like', '%' + text + '%').andWhere('namespace', ns)
.pluck('text');
});
this.holdOperation(promise);
return promise;
}