Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: * validation * unit tests
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,63 @@ | ||
| var command = require('../command'); | ||
| var jsonParser = require('jsonparse'); | ||
|
|
||
| var statusCodes = { | ||
| SUCCESS: 0 | ||
| }; | ||
|
|
||
| module.exports = { | ||
| 'groonga': new command.HTTPRequestResponse({ | ||
| path: '/d/:commandName', | ||
| onRequest: function(request, connection) { | ||
| connection.emit(request.params.commandName, request.query); | ||
| } | ||
| }), | ||
| 'groonga-load': new command.HTTPRequestResponse({ | ||
| method: 'POST', | ||
| path: '/d/load', | ||
| onRequest: function(request, connection, response) { | ||
| var parser = new jsonParser(); | ||
| var nRecords = 0; | ||
| var nResponses = 0; | ||
| var nAdded = 0; | ||
| var isEnd = false; | ||
| var startTimeInMilliseconds = Date.now(); | ||
|
|
||
| parser.onValue = function(value) { | ||
| if (parser.stack.length != 1) { | ||
| return; | ||
| } | ||
|
|
||
| nRecords++; | ||
| var message = { | ||
| table: request.query.table, // TODO: validate it | ||
| key: value._key, | ||
| values: {} | ||
| }; | ||
| connection.emit('add', message, function(error, message) { | ||
| nResponses++; | ||
| if (!error && message) { | ||
| nAdded++; | ||
| } | ||
| if (isEnd && nRecords == nResponses) { | ||
| var statusCode = statusCodes.SUCCESS; | ||
| var elapsedTimeInMilliseconds = | ||
| Date.now() - startTimeInMilliseconds; | ||
| var header = [ | ||
| statusCode, | ||
| startTimeInMilliseconds / 1000, | ||
| elapsedTimeInMilliseconds / 1000, | ||
| ]; | ||
| response.jsonp([header, [nAdded]]); | ||
| } | ||
| }); | ||
| }; | ||
| request.on('data', function(chunk) { | ||
| parser.write(chunk); | ||
| }); | ||
| request.once('end', function() { | ||
| isEnd = true; | ||
| }); | ||
| } | ||
| }) | ||
| }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters