Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #100 from fengmk2/issue94-slow-search
Browse files Browse the repository at this point in the history
fix search too slow.
  • Loading branch information
fengmk2 committed Dec 18, 2013
2 parents b346139 + e1a7ca1 commit 1ab8832
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions proxy/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,26 @@ exports.listByAuthor = function (author, callback) {
});
};

var SEARCH_SQL = 'SELECT name, description FROM module WHERE id IN\
(SELECT module_id FROM tag WHERE name LIKE ? AND tag="latest")\
ORDER BY name LIMIT 20';
var LIST_BY_NAME_FROM_TAG_SQL = 'SELECT module_id FROM tag\
WHERE name LIKE ? AND tag="latest" ORDER BY name LIMIT 20;';
var LIST_DETAIL_FROM_MODULE_SQL = 'SELECT name, description FROM module\
WHERE id IN (?) ORDER BY name;';
exports.search = function (word, callback) {
mysql.query(SEARCH_SQL, [word + '%'], callback);
word = word.replace(/^%/, '') + '%'; //ignore prefix %
var ep = eventproxy.create();
ep.fail(callback);
mysql.query(LIST_BY_NAME_FROM_TAG_SQL, [word], ep.done(function (rows) {
if (!rows || !rows.length) {
return callback(null, []);
}
ep.emit('ids', rows.map(function (r) {
return r.module_id;
}));
}));

ep.on('ids', function (ids) {
mysql.query(LIST_DETAIL_FROM_MODULE_SQL, [ids], ep.done(function (modules) {
callback(null, modules);
}));
});
};

0 comments on commit 1ab8832

Please sign in to comment.