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 #106 from fengmk2/issue104-user
Browse files Browse the repository at this point in the history
split all sub queries, fixed #104
  • Loading branch information
fengmk2 committed Dec 19, 2013
2 parents 7e9f91a + 44c7593 commit 6d32a5b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
14 changes: 8 additions & 6 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ exports.removeAll = function (req, res, next) {
});
};

function parseModsForList(mods, req) {
function parseModsForList(updated, mods, req) {
var results = {
_updated: Date.now()
_updated: updated
};

for (var i = 0; i < mods.length; i++) {
Expand All @@ -594,11 +594,12 @@ function parseModsForList(mods, req) {
}

exports.listAllModules = function (req, res, next) {
Module.listSince(new Date(0), function (err, mods) {
var updated = Date.now();
Module.listSince(0, function (err, mods) {
if (err) {
return next(err);
}
return res.json(parseModsForList(mods, req));
return res.json(parseModsForList(updated, mods, req));
});
};

Expand All @@ -612,11 +613,12 @@ exports.listAllModulesSince = function (req, res, next) {
}
debug('list all modules from %s', req.startkey);
var startkey = Number(query.startkey) || 0;
Module.listSince(new Date(startkey), function (err, mods) {
var updated = Date.now();
Module.listSince(startkey, function (err, mods) {
if (err) {
return next(err);
}
res.json(parseModsForList(mods, req));
res.json(parseModsForList(updated, mods, req));
});
};

Expand Down
76 changes: 54 additions & 22 deletions proxy/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,40 @@ exports.listByName = function (name, callback) {
});
};

var LIST_SINCE_SQL = 'SELECT name, package FROM module WHERE id IN\
(SELECT module_id FROM tag WHERE tag="latest" AND name IN\
(SELECT distinct(name) FROM module WHERE gmt_modified > ?))\
ORDER BY name';
var LIST_SINCE_SQLS = [
'SELECT distinct(name) AS name FROM module WHERE publish_time > ?;',
'SELECT module_id FROM tag WHERE tag="latest" AND name IN (?);',
'SELECT name, package FROM module WHERE id IN (?);'
];
exports.listSince = function (start, callback) {
mysql.query(LIST_SINCE_SQL, [start], callback);
var ep = eventproxy.create();
ep.fail(callback);
mysql.query(LIST_SINCE_SQLS[0], [start], ep.done(function (rows) {
if (!rows || rows.length === 0) {
return callback(null, []);
}
ep.emit('names', rows.map(function (r) {
return r.name;
}));
}));

ep.once('names', function (names) {
mysql.query(LIST_SINCE_SQLS[1], [names], ep.done(function (rows) {
if (!rows || rows.length === 0) {
return callback(null, []);
}
ep.emit('ids', rows.map(function (r) {
return r.module_id;
}));
}));
});

ep.once('ids', function (ids) {
mysql.query(LIST_SINCE_SQLS[2], [ids], callback);
});
};

var LIST_SHORT_SQL = 'SELECT distinct(name) FROM module ORDER BY name';
var LIST_SHORT_SQL = 'SELECT distinct(name) FROM tag ORDER BY name';
exports.listShort = function (callback) {
mysql.query(LIST_SHORT_SQL, callback);
};
Expand All @@ -249,15 +274,15 @@ exports.removeByNameAndVersions = function (name, versions, callback) {
mysql.query(DELETE_MODULE_BY_NAME_AND_VERSIONS_SQL, [name, versions], callback);
};

var LIST_RECENTLY_NAMES_SQL = 'SELECT distinct(name) AS name FROM module WHERE author = ? ORDER BY publish_time DESC LIMIT 100;';
var LIST_BY_NAMES_SQL = 'SELECT name, description FROM module WHERE id IN \
( \
SELECT module_id FROM tag WHERE tag="latest" AND name IN (?) \
) ORDER BY publish_time DESC;';
var LIST_BY_AUTH_SQLS = [
'SELECT distinct(name) AS name FROM module WHERE author = ? ORDER BY publish_time DESC LIMIT 100;',
'SELECT module_id FROM tag WHERE tag="latest" AND name IN (?)',
'SELECT name, description FROM module WHERE id IN (?) ORDER BY publish_time DESC'
];
exports.listByAuthor = function (author, callback) {
var ep = eventproxy.create();
ep.fail(callback);
mysql.query(LIST_RECENTLY_NAMES_SQL, [author], ep.done(function (rows) {
mysql.query(LIST_BY_AUTH_SQLS[0], [author], ep.done(function (rows) {
if (!rows || rows.length === 0) {
return callback(null, []);
}
Expand All @@ -266,23 +291,30 @@ exports.listByAuthor = function (author, callback) {
}));
}));
ep.on('names', function (names) {
mysql.query(LIST_BY_NAMES_SQL, [names], ep.done('modules'));
mysql.query(LIST_BY_AUTH_SQLS[1], [names], ep.done(function (rows) {
if (!rows || rows.length === 0) {
return callback(null, []);
}
ep.emit('ids', rows.map(function (r) {
return r.module_id;
}));
}));
});
ep.on('modules', function (modules) {
callback(null, modules);
ep.on('ids', function (ids) {
mysql.query(LIST_BY_AUTH_SQLS[2], [ids], callback);
});
};

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;';
var SEARCH_SQLS = [
'SELECT module_id FROM tag WHERE name LIKE ? AND tag="latest" ORDER BY name LIMIT 100;',
'SELECT name, description FROM module WHERE id IN (?) ORDER BY name;'
];
exports.search = function (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) {
mysql.query(SEARCH_SQLS[0], [word], ep.done(function (rows) {
if (!rows || rows.length === 0) {
return callback(null, []);
}
ep.emit('ids', rows.map(function (r) {
Expand All @@ -291,7 +323,7 @@ exports.search = function (word, callback) {
}));

ep.on('ids', function (ids) {
mysql.query(LIST_DETAIL_FROM_MODULE_SQL, [ids], ep.done(function (modules) {
mysql.query(SEARCH_SQLS[1], [ids], ep.done(function (modules) {
callback(null, modules);
}));
});
Expand Down
6 changes: 3 additions & 3 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ function routes(app) {

//before /:name/:version
//get all modules, for npm search
// app.get('/-/all', mod.listAllModules);
// app.get('/-/all/since', mod.listAllModulesSince);
app.get('/-/all', mod.listAllModules);
app.get('/-/all/since', mod.listAllModulesSince);
//get all module names, for auto completion
// app.get('/-/short', mod.listAllModuleNames);
app.get('/-/short', mod.listAllModuleNames);

// module
app.get('/:name', [syncByInstall], mod.show);
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/registry/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('controllers/registry/module.test.js', function () {
});
});

describe.skip('GET /-/all', function () {
describe('GET /-/all', function () {
it('should get 200', function (done) {
request(app)
.get('/-/all')
Expand All @@ -361,7 +361,7 @@ describe('controllers/registry/module.test.js', function () {
});
});

describe.skip('GET /-/all/since', function () {
describe('GET /-/all/since', function () {
it('should get 200', function (done) {
request(app)
.get('/-/all/since?stale=update_after&startkey=0')
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('controllers/registry/module.test.js', function () {
});
});

describe.skip('GET /-/short', function () {
describe('GET /-/short', function () {
it('should get 200', function (done) {
request(app)
.get('/-/short')
Expand Down

0 comments on commit 6d32a5b

Please sign in to comment.