Browse Source

added string attrs support to Java API, Ruby API, and MySQL listener


git-svn-id: svn://svn.sphinxsearch.com/sphinx/trunk@1857 406a0c4d-033a-0410-8de8-e80135713968
shodan 16 years ago
parent
commit
8a7d6c4abb
5 changed files with 66 additions and 3 deletions
  1. 10 1
      api/java/SphinxClient.java
  2. 2 1
      api/java/test.java
  3. 5 1
      api/ruby/lib/sphinx/client.rb
  4. 14 0
      api/ruby/test.rb
  5. 35 0
      src/searchd.cpp

+ 10 - 1
api/java/SphinxClient.java

@@ -71,6 +71,7 @@ public class SphinxClient
 	public final static int SPH_ATTR_BOOL			= 4;
 	public final static int SPH_ATTR_FLOAT			= 5;
 	public final static int SPH_ATTR_BIGINT			= 6;
+	public final static int SPH_ATTR_STRING			= 7;
 	public final static int SPH_ATTR_MULTI			= 0x40000000;
 
 	/* searchd commands */
@@ -82,7 +83,7 @@ public class SphinxClient
 
 	/* searchd command versions */
 	private final static int VER_MAJOR_PROTO		= 0x1;
-	private final static int VER_COMMAND_SEARCH		= 0x116;
+	private final static int VER_COMMAND_SEARCH		= 0x117;
 	private final static int VER_COMMAND_EXCERPT	= 0x100;
 	private final static int VER_COMMAND_UPDATE		= 0x101;
 	private final static int VER_COMMAND_KEYWORDS	= 0x100;
@@ -1026,6 +1027,14 @@ public class SphinxClient
 							continue;
 						}
 
+						/* handle strings */
+						if ( type==SPH_ATTR_STRING )
+						{
+							String s = readNetUTF8(in);
+							docInfo.attrValues.add ( attrNumber, s );
+							continue;
+						}
+
 						/* handle everything else as unsigned ints */
 						long val = readDword ( in );
 						if ( ( type & SPH_ATTR_MULTI )!=0 )

+ 2 - 1
api/java/test.java

@@ -137,7 +137,8 @@ public class test
 						case SphinxClient.SPH_ATTR_ORDINAL:
 						case SphinxClient.SPH_ATTR_FLOAT:
 						case SphinxClient.SPH_ATTR_BIGINT:
-							/* longs or floats; print as is */
+						case SphinxClient.SPH_ATTR_STRING:
+							/* ints, longs, floats, strings.. print as is */
 							System.out.print ( info.attrValues.get(a) );
 							break;
 

+ 5 - 1
api/ruby/lib/sphinx/client.rb

@@ -57,7 +57,7 @@ module Sphinx
     # Current client-side command implementation versions
     
     # search command version
-    VER_COMMAND_SEARCH   = 0x116
+    VER_COMMAND_SEARCH   = 0x117
     # excerpt command version
     VER_COMMAND_EXCERPT  = 0x100
     # update command version
@@ -147,6 +147,8 @@ module Sphinx
     SPH_ATTR_FLOAT     = 5
     # signed 64-bit integer
     SPH_ATTR_BIGINT    = 6
+    # string
+    SPH_ATTR_STRING    = 7
     # this attr has multiple values (0 or more)
     SPH_ATTR_MULTI     = 0x40000000
     
@@ -758,6 +760,8 @@ module Sphinx
                 when SPH_ATTR_FLOAT
                   # handle floats
                   r['attrs'][a] = response.get_float
+                when SPH_ATTR_STRING
+                  r['attrs'][a] = response.get_string
                 else
                   # handle everything else as unsigned ints
                   val = response.get_int

+ 14 - 0
api/ruby/test.rb

@@ -0,0 +1,14 @@
+require File.dirname(__FILE__) + "/lib/sphinx"
+
+sphinx = Sphinx::Client.new
+result = sphinx.Query("test", "test1")
+
+print "Found ", result["total_found"], " matches.\n\n"
+
+n = 1
+result['matches'].each do |m|
+	print "#{n}. id=#{m['id']}, weight=#{m['weight']}"
+	m['attrs'].each { |a| print ", #{a[0]}=#{a[1]}" }
+	print "\n"
+	n = n+1
+end

+ 35 - 0
src/searchd.cpp

@@ -5846,6 +5846,41 @@ void HandleClientMySQL ( int iSock, const char * sClientIP, int iPipeFD )
 							break;
 						}
 
+						case SPH_ATTR_STRING:
+						{
+							const BYTE * pStrings = pRes->m_dTag2Pools [ tMatch.m_iTag ].m_pStrings;
+
+							// get that string
+							const BYTE * pStr;
+							int iLen = 0;
+
+							DWORD uOffset = (DWORD) tMatch.GetAttr ( tLoc );
+							if ( uOffset )
+								iLen = sphUnpackStr ( pStrings+uOffset, &pStr );
+
+							// send length
+							BYTE * pLen = (BYTE*)p;
+							iLen = Min ( iLen, sRowMax-p-3 ); // clamp it, buffer size is limited
+							if ( iLen<251 )
+							{
+								pLen[0] = BYTE(iLen);
+								p++;
+							} else
+							{
+								assert ( iLen<=0xffff );
+								pLen[0] = 252;
+								pLen[1] = BYTE(iLen&0xff);
+								pLen[2] = BYTE(iLen>>8);
+								p += 3;
+							}
+
+							// send string data
+							if ( iLen )
+								memcpy ( p, pStr, iLen );
+							p += iLen;
+							break;
+						}
+
 						default:
 							p[0] = 1;
 							p[1] = '-';