From: Uwe Steinmann Date: Fri, 27 Oct 2000 09:29:40 +0000 (+0000) Subject: - added four functions to do fulltext query X-Git-Tag: php-4.0.4RC3~509 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e5772157362dc8e1474270f789dda26217aa0660;p=php - added four functions to do fulltext query --- diff --git a/ext/hyperwave/hg_comm.c b/ext/hyperwave/hg_comm.c index e5868883ec..5469446a18 100644 --- a/ext/hyperwave/hg_comm.c +++ b/ext/hyperwave/hg_comm.c @@ -4268,6 +4268,421 @@ int send_getobjbyquerycollobj(int sockfd, hw_objectID collID, char *query, int m return(0); } +int send_getobjbyftquery(int sockfd, char *query, int maxhits, hw_objectID **childIDs, float **weights, int *count) +{ + hg_msg msg, *retmsg; + int length, error; + char *tmp; + int *ptr, i, *ptr1; + float *ptr2; + + length = HEADER_LENGTH + strlen(query) + 1; + + build_msg_header(&msg, length, msgid++, GETOBJBYFTQUERY_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { + lowerror = LE_MALLOC; + return(-1); + } + + tmp = build_msg_str(msg.buf, query); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + return(-1); + } + efree(msg.buf); + retmsg = recv_hg_msg(sockfd); + if ( retmsg == NULL ) + return(-1); + + ptr = (int *) retmsg->buf; + if(ptr == NULL) { + if(retmsg) efree(retmsg); + return -1; + } + if(*ptr++ == 0) { + *count = (*ptr < maxhits) ? *ptr : maxhits; + ptr++; + if(NULL != (*childIDs = emalloc(*count * sizeof(hw_objectID)))) { + ptr1 = *childIDs; + if(NULL != (*weights = emalloc(*count * sizeof(float)))) { + ptr2 = *weights; + for(i=0; i<*count; i++) { + ptr1[i] = *ptr++; + ptr2[i] = (float) *ptr++; + } + efree(retmsg->buf); + efree(retmsg); + } else { + efree(*childIDs); + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + } else { + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + } else { + error = *((int *) retmsg->buf); + efree(retmsg->buf); + efree(retmsg); + return error; + } + return(0); +} + +int send_getobjbyftqueryobj(int sockfd, char *query, int maxhits, char ***childrec, float **weights, int *count) +{ + hg_msg msg, *retmsg; + int length, i, error; + char *tmp; + int *childIDs = NULL; + char **objptr; + int *ptr, *ptr1; + float *ptr2; + + length = HEADER_LENGTH + strlen(query) + 1; + + build_msg_header(&msg, length, msgid++, GETOBJBYFTQUERY_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { +/* perror("send_command"); */ + lowerror = LE_MALLOC; + return(-1); + } + + tmp = build_msg_str(msg.buf, query); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + return(-2); + } + + efree(msg.buf); + retmsg = recv_hg_msg(sockfd); + if ( retmsg == NULL ) + return(-3); + + ptr = (int *) retmsg->buf; + if(ptr == NULL) { + if(retmsg) efree(retmsg); + return -4; + } + if(*ptr++ == 0) { + *count = (*ptr < maxhits) ? *ptr : maxhits; + ptr++; + if(NULL != (childIDs = emalloc(*count * sizeof(hw_objectID)))) { + ptr1 = childIDs; + if(NULL != (*weights = emalloc(*count * sizeof(float)))) { + ptr2 = *weights; + for(i=0; i<*count; i++) { + ptr1[i] = *ptr++; + ptr2[i] = (float) *ptr++; + } + efree(retmsg->buf); + efree(retmsg); + } else { + efree(childIDs); + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-5); + } + } else { + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-5); + } + } else { + error = *((int *) retmsg->buf); + efree(retmsg->buf); + efree(retmsg); + return error; + } + + /* Now get for each child collection the object record */ +#ifdef hw_less_server_stress + if(0 != send_objectbyidquery(sockfd, childIDs, count, NULL, childrec)) { + efree(childIDs); + efree(*weights); + return -2; + } + efree(childIDs); +#else + for(i=0; i<*count; i++) { + length = HEADER_LENGTH + sizeof(hw_objectID); + build_msg_header(&msg, length, childIDs[i], GETOBJECT_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { + efree(childIDs); + efree(*weights); + lowerror = LE_MALLOC; + return(-6); + } + + tmp = build_msg_int(msg.buf, childIDs[i]); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + efree(childIDs); + efree(*weights); + return(-7); + } + + efree(msg.buf); + } + efree(childIDs); + + if(NULL == (objptr = (char **) emalloc(*count * sizeof(hw_objrec *)))) { + /* if emalloc fails, get at least all remaining messages from server */ + for(i=0; i<*count; i++) { + retmsg = recv_hg_msg(sockfd); + efree(retmsg->buf); + efree(retmsg); + } + *childrec = NULL; + lowerror = LE_MALLOC; + return(-8); + } else { + *childrec = objptr; + + for(i=0; i<*count; i++) { + retmsg = recv_hg_msg(sockfd); + if ( retmsg != NULL ) { + if(0 == (int) *(retmsg->buf)) { + *objptr = estrdup(retmsg->buf+sizeof(int)); + objptr++; + efree(retmsg->buf); + efree(retmsg); + } else { + *objptr = NULL; + objptr++; + efree(retmsg->buf); + efree(retmsg); + } + } + } + } +#endif + return(0); +} + +int send_getobjbyftquerycoll(int sockfd, hw_objectID collID, char *query, int maxhits, hw_objectID **childIDs, float **weights, int *count) +{ + hg_msg msg, *retmsg; + int length, error; + char *tmp; + int *ptr, i, *ptr1; + float *ptr2; + + length = HEADER_LENGTH + strlen(query) + 1 + sizeof(int) + sizeof(collID); + + build_msg_header(&msg, length, msgid++, GETOBJBYFTQUERYCOLL_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { + lowerror = LE_MALLOC; + return(-1); + } + + tmp = build_msg_int(msg.buf, 1); + tmp = build_msg_int(tmp, collID); + tmp = build_msg_str(tmp, query); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + return(-1); + } + + efree(msg.buf); + retmsg = recv_hg_msg(sockfd); + if ( retmsg == NULL ) + return(-1); + + ptr = (int *) retmsg->buf; + if(ptr == NULL) { + if(retmsg) efree(retmsg); + return -1; + } + if(*ptr++ == 0) { + *count = (*ptr < maxhits) ? *ptr : maxhits; + ptr++; + if(NULL != (*childIDs = emalloc(*count * sizeof(hw_objectID)))) { + ptr1 = *childIDs; + if(NULL != (*weights = emalloc(*count * sizeof(float)))) { + ptr2 = *weights; + for(i=0; i<*count; i++) { + ptr1[i] = *ptr++; + ptr2[i] = (float) *ptr++; + } + } else { + efree(*childIDs); + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + efree(retmsg->buf); + efree(retmsg); + } else { + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + } else { + error = *((int *) retmsg->buf); + efree(retmsg->buf); + efree(retmsg); + return error; + } + return(0); +} + +int send_getobjbyftquerycollobj(int sockfd, hw_objectID collID, char *query, int maxhits, char ***childrec, float **weights, int *count) +{ + hg_msg msg, *retmsg; + int length, i, error; + char *tmp; + hw_objectID *childIDs = NULL; + char **objptr; + int *ptr, *ptr1; + float *ptr2; + + length = HEADER_LENGTH + strlen(query) + 1 + sizeof(int) + sizeof(hw_objectID); + + build_msg_header(&msg, length, msgid++, GETOBJBYFTQUERYCOLL_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { + lowerror = LE_MALLOC; + return(-1); + } + + tmp = build_msg_int(msg.buf, 1); + tmp = build_msg_int(tmp, collID); + tmp = build_msg_str(tmp, query); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + return(-1); + } + + efree(msg.buf); + retmsg = recv_hg_msg(sockfd); + if ( retmsg == NULL ) + return -1; + + ptr = (int *) retmsg->buf; + if(ptr == NULL) { + if(retmsg) efree(retmsg); + return -1; + } + if(*ptr++ == 0) { + *count = (*ptr < maxhits) ? *ptr : maxhits; + ptr++; + if(NULL != (childIDs = emalloc(*count * sizeof(hw_objectID)))) { + ptr1 = childIDs; + if(NULL != (*weights = emalloc(*count * sizeof(float)))) { + ptr2 = *weights; + for(i=0; i<*count; i++) { + ptr1[i] = *ptr++; + ptr2[i] = (float) *ptr++; + } + efree(retmsg->buf); + efree(retmsg); + } else { + efree(childIDs); + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + } else { + efree(retmsg->buf); + efree(retmsg); + lowerror = LE_MALLOC; + return(-1); + } + } else { + error = *((int *) retmsg->buf); + efree(retmsg->buf); + efree(retmsg); + return error; + } + + /* Now get for each child collection the object record */ +#ifdef hw_less_server_stress + if(0 != send_objectbyidquery(sockfd, childIDs, count, NULL, childrec)) { + if(childIDs) efree(childIDs); + if(*weights) efree(weights); + return -2; + } + if(childIDs) efree(childIDs); +#else + for(i=0; i<*count; i++) { + length = HEADER_LENGTH + sizeof(hw_objectID); + build_msg_header(&msg, length, childIDs[i], GETOBJECT_MESSAGE); + + if ( (msg.buf = (char *)emalloc(length-HEADER_LENGTH)) == NULL ) { +/* perror("send_command"); */ + efree(childIDs); + efree(*weights); + lowerror = LE_MALLOC; + return(-1); + } + + tmp = build_msg_int(msg.buf, childIDs[i]); + + if ( send_hg_msg(sockfd, &msg, length) == -1 ) { + efree(msg.buf); + efree(childIDs); + efree(*weights); + return(-1); + } + + efree(msg.buf); + } + efree(childIDs); + + if(NULL == (objptr = (char **) emalloc(*count * sizeof(hw_objrec *)))) { + /* if emalloc fails, get at least all remaining messages from server */ + for(i=0; i<*count; i++) { + retmsg = recv_hg_msg(sockfd); + efree(retmsg->buf); + efree(retmsg); + } + *childrec = NULL; + lowerror = LE_MALLOC; + return(-1); + } else { + *childrec = objptr; + + for(i=0; i<*count; i++) { + retmsg = recv_hg_msg(sockfd); + if ( retmsg != NULL ) { + if(0 == (int) *(retmsg->buf)) { + *objptr = estrdup(retmsg->buf+sizeof(int)); + objptr++; + efree(retmsg->buf); + efree(retmsg); + } else { + *objptr = NULL; + objptr++; + efree(retmsg->buf); + efree(retmsg); + } + } + } + } +#endif + return(0); +} + int send_getparents(int sockfd, hw_objectID objectID, hw_objectID **childIDs, int *count) { hg_msg msg, *retmsg; diff --git a/ext/hyperwave/hg_comm.h b/ext/hyperwave/hg_comm.h index 575e70e645..94826e4dbf 100644 --- a/ext/hyperwave/hg_comm.h +++ b/ext/hyperwave/hg_comm.h @@ -68,6 +68,8 @@ #define UNLOCK_MESSAGE 30 #define INCOLLECTIONS_MESSAGE 31 #define INSERTOBJECT_MESSAGE 32 +#define GETOBJBYFTQUERY_MESSAGE 34 +#define GETOBJBYFTQUERYCOLL_MESSAGE 35 #define PIPEDOCUMENT_MESSAGE 36 #define DELETEOBJECT_MESSAGE 37 #define PUTDOCUMENT_MESSAGE 38 @@ -144,6 +146,7 @@ typedef struct { typedef int hw_objectID; typedef char hw_objrec; +typedef float hw_float; #ifdef newlist void fnDeleteAnchor(void *ptr1); @@ -204,6 +207,10 @@ extern int send_getobjbyquery(int sockfd, char *query, int maxhits, hw_objectID extern int send_getobjbyqueryobj(int sockfd, char *query, int maxhits, char ***childrec, int *count); extern int send_getobjbyquerycoll(int sockfd, hw_objectID collID, char *query, int maxhits, hw_objectID **childIDs, int *count); extern int send_getobjbyquerycollobj(int sockfd, hw_objectID collID, char *query, int maxhits, char ***childrec, int *count); +extern int send_getobjbyftquery(int sockfd, char *query, int maxhits, hw_objectID **childIDs, float **weights, int *count); +extern int send_getobjbyftqueryobj(int sockfd, char *query, int maxhits, char ***childrec, float **weights, int *count); +extern int send_getobjbyftquerycoll(int sockfd, hw_objectID collID, char *query, int maxhits, hw_objectID **childIDs, float **weight, int *count); +extern int send_getobjbyftquerycollobj(int sockfd, hw_objectID collID, char *query, int maxhits, char ***childrec, float **weight, int *count); extern int send_identify(int sockfd, char *name, char *passwd, char **userdata); extern int send_getparents(int sockfd, hw_objectID objectID, hw_objectID **childIDs, int *count); extern int send_children(int sockfd, hw_objectID objectID, hw_objectID **childIDs, int *count); diff --git a/ext/hyperwave/hw.c b/ext/hyperwave/hw.c index d5e802efb1..d383d30629 100644 --- a/ext/hyperwave/hw.c +++ b/ext/hyperwave/hw.c @@ -82,6 +82,10 @@ function_entry hw_functions[] = { PHP_FE(hw_getobjectbyqueryobj, NULL) PHP_FE(hw_getobjectbyquerycoll, NULL) PHP_FE(hw_getobjectbyquerycollobj, NULL) + PHP_FE(hw_getobjectbyftquery, NULL) + PHP_FE(hw_getobjectbyftqueryobj, NULL) + PHP_FE(hw_getobjectbyftquerycoll, NULL) + PHP_FE(hw_getobjectbyftquerycollobj, NULL) PHP_FE(hw_getchilddoccoll, NULL) PHP_FE(hw_getchilddoccollobj, NULL) PHP_FE(hw_getanchors, NULL) @@ -2457,7 +2461,7 @@ PHP_FUNCTION(hw_new_document) { free(doc); RETURN_FALSE; } - memcpy(doc->data, arg2->value.str.val, arg3->value.lval); + memcpy(doc->data, arg2->value.str.val, arg3->value.lval); ptr = doc->data; ptr[arg3->value.lval] = '\0'; doc->attributes = strdup(arg1->value.str.val); @@ -3150,6 +3154,176 @@ PHP_FUNCTION(hw_getobjectbyquerycollobj) { } /* }}} */ +/* {{{ proto array hw_getobjectbyftquery(int link, string query, int maxhits) + Search for query as fulltext and return maxhits objids */ +PHP_FUNCTION(hw_getobjectbyftquery) { + pval **arg1, **arg2, **arg3; + int link, type, maxhits; + char *query; + int count, i; + int *childIDs = NULL; + float *weights; + hw_connection *ptr; + + if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex(arg1); + convert_to_string_ex(arg2); + convert_to_long_ex(arg3); + link=(*arg1)->value.lval; + query=(*arg2)->value.str.val; + maxhits=(*arg3)->value.lval; + if (maxhits < 0) maxhits=0x7FFFFFFF; + ptr = zend_list_find(link,&type); + if(!ptr || (type!=HwSG(le_socketp) && type!=HwSG(le_psocketp))) { + php_error(E_WARNING,"Unable to find file identifier %d",link); + RETURN_FALSE; + } + + set_swap(ptr->swap_on); + if (0 != (ptr->lasterror = send_getobjbyftquery(ptr->socket, query, maxhits, &childIDs, &weights, &count))) { + php_error(E_WARNING, "send_command (getobjectbyftquery) returned %d\n", ptr->lasterror); + RETURN_FALSE; + } + + if (array_init(return_value) == FAILURE) { + efree(childIDs); + RETURN_FALSE; + } + + for(i=0; ivalue.lval; + query=(*arg2)->value.str.val; + maxhits=(*arg3)->value.lval; + if (maxhits < 0) maxhits=0x7FFFFFFF; + ptr = zend_list_find(link,&type); + if(!ptr || (type!=HwSG(le_socketp) && type!=HwSG(le_psocketp))) { + php_error(E_WARNING,"Unable to find file identifier %d",link); + RETURN_FALSE; + } + + set_swap(ptr->swap_on); + if (0 != (ptr->lasterror = send_getobjbyftqueryobj(ptr->socket, query, maxhits, &childObjRecs, &weights, &count))) { + php_error(E_WARNING, "send_command (getobjectbyftqueryobj) returned %d\n", ptr->lasterror); + RETURN_FALSE; + } + + /* create return value and free all memory */ + if( 0 > make_return_objrec(&return_value, childObjRecs, count)) + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto array hw_getobjectbyftquerycoll(int link, int collid, string query, int maxhits) + Search for fulltext query in collection and return maxhits objids */ +PHP_FUNCTION(hw_getobjectbyftquerycoll) { + pval **arg1, **arg2, **arg3, **arg4; + int link, id, type, maxhits; + char *query; + int count, i; + hw_connection *ptr; + int *childIDs = NULL; + float *weights; + + if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex(arg1); + convert_to_long_ex(arg2); + convert_to_string_ex(arg3); + convert_to_long_ex(arg4); + link=(*arg1)->value.lval; + id=(*arg2)->value.lval; + query=(*arg3)->value.str.val; + maxhits=(*arg4)->value.lval; + if (maxhits < 0) maxhits=0x7FFFFFFF; + ptr = zend_list_find(link,&type); + if(!ptr || (type!=HwSG(le_socketp) && type!=HwSG(le_psocketp))) { + php_error(E_WARNING,"Unable to find file identifier %d",id); + RETURN_FALSE; + } + + set_swap(ptr->swap_on); + if (0 != (ptr->lasterror = send_getobjbyftquerycoll(ptr->socket, id, query, maxhits, &childIDs, &weights, &count))) { + php_error(E_WARNING, "send_command (getobjectbyquerycoll) returned %d\n", ptr->lasterror); + RETURN_FALSE; + } + + if (array_init(return_value) == FAILURE) { + efree(childIDs); + RETURN_FALSE; + } + + for(i=0; ivalue.lval; + id=(*arg2)->value.lval; + query=(*arg3)->value.str.val; + maxhits=(*arg4)->value.lval; + if (maxhits < 0) maxhits=0x7FFFFFFF; + ptr = zend_list_find(link,&type); + if(!ptr || (type!=HwSG(le_socketp) && type!=HwSG(le_psocketp))) { + php_error(E_WARNING,"Unable to find file identifier %d",id); + RETURN_FALSE; + } + + set_swap(ptr->swap_on); + if (0 != (ptr->lasterror = send_getobjbyftquerycollobj(ptr->socket, id, query, maxhits, &childObjRecs, &weights, &count))) { + php_error(E_WARNING, "send_command (getobjectbyquerycollobj) returned %d\n", ptr->lasterror); + RETURN_FALSE; + } + + /* create return value and free all memory */ + if( 0 > make_return_objrec(&return_value, childObjRecs, count)) + RETURN_FALSE; +} +/* }}} */ + /* {{{ proto array hw_getchilddoccoll(int link, int objid) Returns all children ids which are documents */ PHP_FUNCTION(hw_getchilddoccoll) { diff --git a/ext/hyperwave/php_hyperwave.h b/ext/hyperwave/php_hyperwave.h index 79218ce2fc..6e1be6fde1 100644 --- a/ext/hyperwave/php_hyperwave.h +++ b/ext/hyperwave/php_hyperwave.h @@ -115,6 +115,10 @@ PHP_FUNCTION(hw_getobjectbyquery); PHP_FUNCTION(hw_getobjectbyqueryobj); PHP_FUNCTION(hw_getobjectbyquerycoll); PHP_FUNCTION(hw_getobjectbyquerycollobj); +PHP_FUNCTION(hw_getobjectbyftquery); +PHP_FUNCTION(hw_getobjectbyftqueryobj); +PHP_FUNCTION(hw_getobjectbyftquerycoll); +PHP_FUNCTION(hw_getobjectbyftquerycollobj); PHP_FUNCTION(hw_getchilddoccoll); PHP_FUNCTION(hw_getchilddoccollobj); PHP_FUNCTION(hw_getanchors);