Navigation Menu

Skip to content

Commit

Permalink
groonga: Degine "Loader" in a separeted file
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2014
1 parent 9b41a40 commit c385824
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 145 deletions.
146 changes: 1 addition & 145 deletions lib/adapter/api/groonga.js
@@ -1,149 +1,5 @@
var command = require('../command');
var jsonParser = require('jsonparse');

var statusCodes = {
SUCCESS: 0,
INVALID_ARGUMENT: -22
};
var httpStatusCodes = {
OK: 200,
BAD_REQUEST: 400
};

function Loader(request, response, connection, logger) {
this._request = request;
this._response = response;
this._connection = connection;
this._logger = logger
this._errorMessage = undefined;
}

Loader.prototype.run = function run() {
this._startTimeInMilliseconds = Date.now();
this._statusCode = statusCodes.SUCCESS;

try {
this._extractParameters();
} catch (error) {
this._statusCode = error.statusCode;
this._errorMessage = error.message;
this._sendResponse(0);
return;
}

this._jsonParser = new jsonParser();
this._nRecords = 0;
this._nResponses = 0;
this._nAdded = 0;
this._isEnd = false;

this._jsonParser.onValue = this._onValue.bind(this);
this._request.on('data', function(chunk) {
this._jsonParser.write(chunk);
}.bind(this));
this._request.once('end', function() {
this._isEnd = true;

if (this._nRecords == 0) {
this._sendResponse([0]);
}
}.bind(this));
};

Loader.prototype._extractParameters = function _extractParameters() {
var query = this._request.query;

this._table = query.table;
if (!this._table) {
throw {
statusCode: statusCodes.INVALID_ARGUMENT,
messasge: 'required parameter is missing: <table>',
httpStatusCode: httpStatusCodes.BAD_REQUEST
};
}

if (query.columns) {
this._columns = query.columns.split(/[,\s]+/);
} else {
this._columns = null;
}
};

Loader.prototype._onValue = function _onValue(value) {
if (this._jsonParser.stack.length != 1) {
return;
}

if (Array.isArray(value) && !this._columns) {
this._columns = value;
return;
}

var message = {
table: this._table,
values: {}
};

this._nRecords++;

if (Array.isArray(value)) {
this._columns.forEach(function(column, i) {
if (column == '_key') {
message.key = value[i];
} else {
message.values[column] = value[i];
}
});
} else {
Object.keys(value).forEach(function(key) {
if (key == '_key') {
message.key = value[key];
} else {
message.values[key] = value[key];
}
});
}

this._connection.emit('add', message, function(error, message) {
this._nResponses++;
if (error) {
this._logger.error("/d/load: failed to add a record", message);
} else {
var succeeded = message;
if (succeeded) {
this._nAdded++;
}
}
if (this._isEnd && this._nRecords == this._nResponses) {
this._sendResponse([this._nAdded]);
}
}.bind(this));
};

Loader.prototype._createResponse = function _createResponse(body) {
var elapsedTimeInMilliseconds = Date.now() - this._startTimeInMilliseconds;
var header = [
this._statusCode,
this._startTimeInMilliseconds / 1000,
elapsedTimeInMilliseconds / 1000,
];
if (this._errorMessage) {
header.push(this._errorMessage);
}
return [header, body];
};

Loader.prototype._sendResponse = function _sendResponse(body) {
var groongaResponse = this._createResponse(body);
var groongaResponseHeader = groongaResponse[0];
var httpStatusCode;
if (groongaResponseHeader[0] == statusCodes.SUCCESS) {
httpStatusCode = httpStatusCodes.OK;
} else {
httpStatusCode = httpStatusCodes.BAD_REQUEST;
}
this._response.jsonp(groongaResponse, httpStatusCode);
};
var Loader = require('./groonga/loader');

function handleHTTPRequest(request, connection) {
connection.emit(request.params.commandName, request.query);
Expand Down
147 changes: 147 additions & 0 deletions lib/adapter/api/groonga/loader.js
@@ -0,0 +1,147 @@
var jsonParser = require('jsonparse');

var statusCodes = {
SUCCESS: 0,
INVALID_ARGUMENT: -22
};
var httpStatusCodes = {
OK: 200,
BAD_REQUEST: 400
};

function Loader(request, response, connection, logger) {
this._request = request;
this._response = response;
this._connection = connection;
this._logger = logger
this._errorMessage = undefined;
}

Loader.prototype.run = function run() {
this._startTimeInMilliseconds = Date.now();
this._statusCode = statusCodes.SUCCESS;

try {
this._extractParameters();
} catch (error) {
this._statusCode = error.statusCode;
this._errorMessage = error.message;
this._sendResponse(0);
return;
}

this._jsonParser = new jsonParser();
this._nRecords = 0;
this._nResponses = 0;
this._nAdded = 0;
this._isEnd = false;

this._jsonParser.onValue = this._onValue.bind(this);
this._request.on('data', function(chunk) {
this._jsonParser.write(chunk);
}.bind(this));
this._request.once('end', function() {
this._isEnd = true;

if (this._nRecords == 0) {
this._sendResponse([0]);
}
}.bind(this));
};

Loader.prototype._extractParameters = function _extractParameters() {
var query = this._request.query;

this._table = query.table;
if (!this._table) {
throw {
statusCode: statusCodes.INVALID_ARGUMENT,
messasge: 'required parameter is missing: <table>',
httpStatusCode: httpStatusCodes.BAD_REQUEST
};
}

if (query.columns) {
this._columns = query.columns.split(/[,\s]+/);
} else {
this._columns = null;
}
};

Loader.prototype._onValue = function _onValue(value) {
if (this._jsonParser.stack.length != 1) {
return;
}

if (Array.isArray(value) && !this._columns) {
this._columns = value;
return;
}

var message = {
table: this._table,
values: {}
};

this._nRecords++;

if (Array.isArray(value)) {
this._columns.forEach(function(column, i) {
if (column == '_key') {
message.key = value[i];
} else {
message.values[column] = value[i];
}
});
} else {
Object.keys(value).forEach(function(key) {
if (key == '_key') {
message.key = value[key];
} else {
message.values[key] = value[key];
}
});
}

this._connection.emit('add', message, function(error, message) {
this._nResponses++;
if (error) {
this._logger.error("/d/load: failed to add a record", message);
} else {
var succeeded = message;
if (succeeded) {
this._nAdded++;
}
}
if (this._isEnd && this._nRecords == this._nResponses) {
this._sendResponse([this._nAdded]);
}
}.bind(this));
};

Loader.prototype._createResponse = function _createResponse(body) {
var elapsedTimeInMilliseconds = Date.now() - this._startTimeInMilliseconds;
var header = [
this._statusCode,
this._startTimeInMilliseconds / 1000,
elapsedTimeInMilliseconds / 1000,
];
if (this._errorMessage) {
header.push(this._errorMessage);
}
return [header, body];
};

Loader.prototype._sendResponse = function _sendResponse(body) {
var groongaResponse = this._createResponse(body);
var groongaResponseHeader = groongaResponse[0];
var httpStatusCode;
if (groongaResponseHeader[0] == statusCodes.SUCCESS) {
httpStatusCode = httpStatusCodes.OK;
} else {
httpStatusCode = httpStatusCodes.BAD_REQUEST;
}
this._response.jsonp(groongaResponse, httpStatusCode);
};

module.exports = Loader;

0 comments on commit c385824

Please sign in to comment.