Skip to content

Commit

Permalink
Merge pull request #50 from fengmk2/issue46-slow-sql
Browse files Browse the repository at this point in the history
fix sql, change test to fit my local database, fixed #46
  • Loading branch information
fengmk2 committed Dec 11, 2013
2 parents 92051a2 + ce8ef3a commit 2f7e62f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
9 changes: 4 additions & 5 deletions controllers/registry/module.js
Expand Up @@ -678,14 +678,13 @@ exports.listAllModulesSince = function (req, res, next) {
};

exports.listAllModuleNames = function (req, res, next) {
Module.listSince(new Date(0), function (err, mods) {
Module.listShort(function (err, mods) {
if (err) {
return next(err);
}
var results = [];
for (var i = 0; i < mods.length; i++) {
results.push(mods[i].name);
}
var results = mods.map(function (m) {
return m.name;
});
res.json(results);
});
};
2 changes: 1 addition & 1 deletion controllers/web/package.js
Expand Up @@ -41,7 +41,7 @@ exports.display = function (req, res, next) {
return next();
}
pkg = pkg.package;
pkg.readme = marked(pkg.readme);
pkg.readme = marked(pkg.readme || '');

setLicense(pkg);

Expand Down
21 changes: 12 additions & 9 deletions proxy/module.js
Expand Up @@ -200,15 +200,19 @@ exports.listByName = function (name, callback) {
});
};


var LIST_SINCE_SQL = 'SELECT name, package FROM module WHERE id IN \
(SELECT max(id) FROM module WHERE gmt_modified > ?\
GROUP BY name )\
ORDER BY name';
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';
exports.listSince = function (start, callback) {
mysql.query(LIST_SINCE_SQL, [start], callback);
};

var LIST_SHORT_SQL = 'SELECT distinct(name) FROM module ORDER BY name';
exports.listShort = function (callback) {
mysql.query(LIST_SHORT_SQL, callback);
};

var DELETE_MODULE_BY_NAME_SQL = 'DELETE FROM module WHERE name=?;';
exports.removeByName = function (name, callback) {
mysql.query(DELETE_MODULE_BY_NAME_SQL, [name], callback);
Expand All @@ -219,10 +223,9 @@ exports.removeByNameAndVersions = function (name, versions, callback) {
mysql.query(DELETE_MODULE_BY_NAME_AND_VERSIONS_SQL, [name, versions], callback);
};

var LIST_BY_AUTHOR_SQL = 'SELECT name, package FROM module WHERE id IN \
(SELECT max(id) FROM module WHERE author=?\
GROUP BY name )\
ORDER BY name';
var LIST_BY_AUTHOR_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 author = ?))';
exports.listByAuthor = function (author, callback) {
mysql.query(LIST_BY_AUTHOR_SQL, [author], callback);
};
20 changes: 9 additions & 11 deletions test/controllers/registry/module.test.js
Expand Up @@ -88,30 +88,28 @@ describe('controllers/registry/module.test.js', function () {
describe('GET /:name/:(version|tag)', function () {
it('should return module@version info', function (done) {
request(app)
.get('/cnpmjs.org/0.0.4')
.get('/cnpmjs.org/0.0.0')
.expect(200, function (err, res) {
should.not.exist(err);
var body = res.body;
body.name.should.equal('cnpmjs.org');
body.version.should.equal('0.0.4');
body._id.should.equal('cnpmjs.org@0.0.4');
body.dist.should.have.keys('tarball', 'shasum', 'size');
body.dist.tarball.should.include('/cnpmjs.org/download/cnpmjs.org-0.0.4.tgz');
body.version.should.equal('0.0.0');
body._id.should.equal('cnpmjs.org@0.0.0');
body.dist.tarball.should.include('cnpmjs.org-0.0.0.tgz');
done();
});
});

it('should return module@tag info', function (done) {
request(app)
.get('/cnpmjs.org/latest')
.get('/cutter/latest')
.expect(200, function (err, res) {
should.not.exist(err);
var body = res.body;
body.name.should.equal('cnpmjs.org');
body.version.should.equal('0.0.4');
body._id.should.equal('cnpmjs.org@0.0.4');
body.dist.should.have.keys('tarball', 'shasum', 'size');
body.dist.tarball.should.include('/cnpmjs.org/download/cnpmjs.org-0.0.4.tgz');
body.name.should.equal('cutter');
body.version.should.equal('0.0.3');
body._id.should.equal('cutter@0.0.3');
body.dist.tarball.should.include('/cutter/download/cutter-0.0.3.tgz');
done();
});
});
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/web/package.test.js
Expand Up @@ -29,7 +29,7 @@ describe('controllers/web/package.test.js', function () {
describe('GET /package/:name', function (done) {
it('should get 200', function (done) {
request(app)
.get('/package/moduletest')
.get('/package/cutter')
.expect(200)
.expect(/<div id="package">/)
.expect(/<th>Maintainers<\/th>/)
Expand All @@ -46,7 +46,7 @@ describe('controllers/web/package.test.js', function () {
describe('GET /package/:name/:version', function (done) {
it('should 200 when get by version', function (done) {
request(app)
.get('/package/moduletest/0.0.2')
.get('/package/cutter/0.0.2')
.expect(200)
.expect(/<div id="package">/)
.expect(/<th>Maintainers<\/th>/)
Expand All @@ -55,21 +55,21 @@ describe('controllers/web/package.test.js', function () {

it('should 200 when get by tag', function (done) {
request(app)
.get('/package/moduletest/latest')
.get('/package/cutter/latest')
.expect(200)
.expect(/<div id="package">/)
.expect(/<th>Maintainers<\/th>/)
.expect(/<th>Version<\/th>/, done);
});
it('should 404 when get by version not exist', function (done) {
request(app)
.get('/package/moduletest/1.1.2')
.get('/package/cutter/1.1.2')
.expect(404, done);
});

it('should 404 when get by tag', function (done) {
request(app)
.get('/package/moduletest/notexisttag')
.get('/package/cutter/notexisttag')
.expect(404, done);
});
});
Expand Down

0 comments on commit 2f7e62f

Please sign in to comment.