From 20b1a87f8a5eba7ccdedb96b36b815f397e13ec8 Mon Sep 17 00:00:00 2001 From: Joe Z Date: Wed, 11 Jan 2012 17:15:57 -0500 Subject: [PATCH] Added remote host connect support --- limestone.js | 87 +++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/limestone.js b/limestone.js index 7c2cda5..3ad3458 100644 --- a/limestone.js +++ b/limestone.js @@ -2,7 +2,7 @@ var tcp = require('net'); exports.SphinxClient = function() { var self = { }; - + var buffer_extras = require('./buffer_extras'); var Sphinx = { @@ -86,7 +86,7 @@ exports.SphinxClient = function() { "RANGE" : 1, "FLOATRANGE" : 2 }; - + Sphinx.attribute = { "INTEGER": 1, "TIMESTAMP": 2, @@ -95,7 +95,7 @@ exports.SphinxClient = function() { "FLOAT": 5, "BIGINT": 6, "STRING": 7, - "MULTI": 0x40000000 + "MULTI": 0x40000000 }; var server_conn = null; @@ -104,17 +104,28 @@ exports.SphinxClient = function() { var _queue = []; var _persistent = false; - - + + // Connect to Sphinx server self.connect = function() { - // arguments: (port, [persistent], callback). + // arguments: ([host:port], [persistent], callback). var args = Array.prototype.slice.call(arguments); var callback = args.pop(); - var port = args.length ? args.shift(): Sphinx.port; + var hostport = args.length ? args.shift() + '' : ':'+Sphinx.port; + if(hostport.indexOf(':')==-1){ + hostport = isNaN(hostport) ? hostport + ':' + Sphinx.port : ':' + hostport; + } + hostport = hostport.split(':'); + + var host = hostport[0].trim().length ? hostport[0].trim(): 'localhost' ; + var port = hostport[1].trim().length ? hostport[1].trim() : Sphinx.port; + + + var persistent = _persistent = args.length ? args.shift() : false; - server_conn = tcp.createConnection(port); + console.log('connecting to : '+host+':'+port); + server_conn = tcp.createConnection(port, host); server_conn.on('error', function(x){ console.log('Error: '+x); server_conn.end(); @@ -123,7 +134,7 @@ exports.SphinxClient = function() { // disable Nagle algorithm server_conn.setNoDelay(true); - server_conn.addListener('connect', + server_conn.addListener('connect', function () { // Sending protocol version // Here we must send 4 bytes, '0x00000001' @@ -154,7 +165,7 @@ exports.SphinxClient = function() { if (data_unpacked[""] >= 1) { //all ok, send my version server_conn.write(version_number.toBuffer()); - + if(persistent){ var pers_req = Buffer.makeWriter(); pers_req.push.int16(Sphinx.command.PERSIST); @@ -166,15 +177,15 @@ exports.SphinxClient = function() { server_conn.on('data', readResponseData); _connected = true; server_conn.emit('sphinx.connected'); - + // Use callback callback(null); - + } else { callback(new Error('Wrong protocol version: ' + protocol_version)); server_conn.end(); } - + }); } else { callback(new Error('Connection is ' + server_conn.readyState + ' in OnConnect')); @@ -218,7 +229,7 @@ exports.SphinxClient = function() { error : "", // per-reply fields (for single-query case) warning : "", connerror : false, - + reqs : [], // requests storage (for multi-query case) mbenc : "", arrayresult : true, @@ -228,7 +239,7 @@ exports.SphinxClient = function() { if (query_raw.query) { for (x in query_parameters) { if (query_raw.hasOwnProperty(x)) { - query[x] = query_raw[x]; + query[x] = query_raw[x]; } else { query[x] = query_parameters[x]; } @@ -237,38 +248,38 @@ exports.SphinxClient = function() { query = query_raw.toString(); } - var request = Buffer.makeWriter(); + var request = Buffer.makeWriter(); request.push.int16(Sphinx.command.SEARCH); request.push.int16(Sphinx.clientCommand.SEARCH); - + request.push.int32(0); // This will be request length request.push.int32(0); request.push.int32(1); - + request.push.int32(query.offset); - + request.push.int32(query.limit); request.push.int32(query.mode); request.push.int32(query.ranker); - + request.push.int32(query.sort); - - request.push.lstring(query.sortby); + + request.push.lstring(query.sortby); request.push.lstring(query.query); // Query text - request.push.int32(query.weights.length); + request.push.int32(query.weights.length); for (var weight in query.weights) { request.push.int32(parseInt(weight)); } - request.push.lstring(query.indexes); // Indexes + request.push.lstring(query.indexes); // Indexes request.push.int32(1); // id64 range marker request.push.int64(0, query.min_id); // This is actually supposed to be two 64-bit numbers - request.push.int64(0, query.max_id); + request.push.int64(0, query.max_id); - request.push.int32(query.filters.length); + request.push.int32(query.filters.length); for (var filter in query.filters) { request.push.int32(filter.attr.length); request.push_lstring(filter.attr); @@ -290,7 +301,7 @@ exports.SphinxClient = function() { break; } } - + request.push.int32(query_parameters.groupfunc); request.push.lstring(query_parameters.groupby); // Groupby length @@ -320,7 +331,7 @@ exports.SphinxClient = function() { request.push.int32(query_parameters.indexweights[i]); } - request.push.int32(query_parameters.maxquerytime); + request.push.int32(query_parameters.maxquerytime); // per-field weights (preferred method) request.push.int32(Object.keys(query.fieldweights).length); for (var field_name in query.fieldweights) { @@ -328,11 +339,11 @@ exports.SphinxClient = function() { request.push.int32(query.fieldweights[field_name]); } - request.push.lstring(query_parameters.comment); + request.push.lstring(query_parameters.comment); request.push.int32(query_parameters.overrides.length); for (var i in query_parameters.overrides) { - request.push.lstring(query_parameters.overrides[i].attr); + request.push.lstring(query_parameters.overrides[i].attr); request.push.int32(query_parameters.overrides[i].type); request.push.int32(query_parameters.overrides[i].values.length); for (var id in query_parameters.overrides[i].values) { @@ -358,8 +369,8 @@ exports.SphinxClient = function() { req_length.push.int32(request_buf.length - 8); req_length.toBuffer().copy(request_buf, 4, 0); - console.log('Sending search request of ' + request_buf.length + ' bytes '); - _enqueue(request_buf, callback, Sphinx.clientCommand.SEARCH); + console.log('Sending search request of ' + request_buf.length + ' bytes '); + _enqueue(request_buf, callback, Sphinx.clientCommand.SEARCH); }; @@ -399,7 +410,7 @@ exports.SphinxClient = function() { 'allow_empty' : 256, 'emit_zones' : 256 }; - + for (x in flag_properties) { if (passage_opts_raw.hasOwnProperty(x)) { flags |= flag_properties[x]; @@ -412,7 +423,7 @@ exports.SphinxClient = function() { request.push.int16(Sphinx.command.EXCERPT); request.push.int16(Sphinx.clientCommand.EXCERPT); request.push.int32(0); // This will be request length - + // request 'body' (flags, options, docs) request.push.int32(0); @@ -422,7 +433,7 @@ exports.SphinxClient = function() { request.push.lstring(index); request.push.lstring(words); - + // options request.push.lstring(passage_opts.before_match); request.push.lstring(passage_opts.after_match); @@ -470,7 +481,7 @@ exports.SphinxClient = function() { server_conn.write(req_buf); }); } - } + } } function _dequeue() { @@ -544,7 +555,7 @@ exports.SphinxClient = function() { if (this.status == Sphinx.statusCode.RETRY){ errmsg += "Server issued RETRY: " + this.data; } - + return errmsg; }, runCallbackIfDone : function(search_command) { @@ -678,7 +689,7 @@ exports.SphinxClient = function() { output.words[word]["docs"] = response.int32(); output.words[word]["hits"] = response.int32(); } - + return output; };