[prev in list] [next in list] [prev in thread] [next in thread]
List: pecl-cvs
Subject: [PECL-CVS] com =?UTF-8?Q?pecl/search=5Fengine/solr=3A=20Request=20=23=37=30=37=33=39?= =?UTF-8?Q?=3A
From: Omar Shaban <omars () php ! net>
Date: 2015-11-30 12:57:33
Message-ID: php-mail-275339dc430bbbb2051047dfcc3b5855392196752 () git ! php ! net
[Download RAW message or body]
Commit: b36b4c8a9615808297337c33cd8bca935d695f91
Author: Omar Shaban <omars@php.net> Mon, 30 Nov 2015 14:36:36 +0200
Parents: a33af3ae1cfefe67e037b3981765f4678a823eb8
Branches: master
Link: http://git.php.net/?p=pecl/search_engine/solr.git;a=commitdiff;h=b36b4c8a9615808297337c33cd8bca935d695f91
Log:
Request #70739: Added SolrInputDocument new methods getChildDocumentsCount, \
addChildDocuments
Added new methods:
int SolrInputDocument::getChildDocumentsCount( void )
void SolrInputDocument::addChildDocuments( array )
Changed signature:
void SolrInputDocument::addChildDocument( SolrInputDocument )
Added field validation on SolrInputDocument::addChildDocument
Bugs:
https://bugs.php.net/70739
Changed paths:
M php_solr.c
M php_solr.h
M php_solr_client.c
M php_solr_input_document.c
M solr_macros.h
A tests/053.solrinputdocument_getchilddocumentscount.phpt
["diff_b36b4c8a9615808297337c33cd8bca935d695f91.txt" (text/plain)]
diff --git a/php_solr.c b/php_solr.c
index ad63250..efeed6d 100644
--- a/php_solr.c
+++ b/php_solr.c
@@ -209,6 +209,10 @@ ZEND_BEGIN_ARG_INFO_EX(SolrInputDocument_addChildDocument_args, \
SOLR_ARG_PASS_RE ZEND_ARG_OBJ_INFO(SOLR_ARG_PASS_BY_REF_TRUE, child, \
SolrInputDocument, SOLR_ARG_ALLOW_NULL_FALSE) ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(SolrInputDocument_addChildDocuments_args, \
SOLR_ARG_PASS_REMAINING_BY_REF_FALSE, SOLR_METHOD_RETURN_REFERENCE_TRUE, 1) \
+ZEND_ARG_ARRAY_INFO(SOLR_ARG_PASS_BY_REF_TRUE, docs, SOLR_ARG_ALLOW_NULL_FALSE) \
+ZEND_END_ARG_INFO() +
/* }}} */
/* {{{ SolrClient arguments */
@@ -634,7 +638,8 @@ static zend_function_entry solr_input_document_methods[] = {
PHP_ME(SolrInputDocument, addChildDocument, \
SolrInputDocument_addChildDocument_args, ZEND_ACC_PUBLIC) PHP_ME(SolrInputDocument, \
getChildDocuments, Solr_no_args, ZEND_ACC_PUBLIC) PHP_ME(SolrInputDocument, \
hasChildDocuments, Solr_no_args, ZEND_ACC_PUBLIC)
-
+ PHP_ME(SolrInputDocument, getChildDocumentsCount, Solr_no_args, ZEND_ACC_PUBLIC)
+ PHP_ME(SolrInputDocument, addChildDocuments, \
SolrInputDocument_addChildDocuments_args, ZEND_ACC_PUBLIC) { NULL, NULL, NULL }
};
/* }}} */
diff --git a/php_solr.h b/php_solr.h
index 2d3ad37..7341c13 100644
--- a/php_solr.h
+++ b/php_solr.h
@@ -244,6 +244,8 @@ PHP_METHOD(SolrInputDocument, merge);
PHP_METHOD(SolrInputDocument, addChildDocument);
PHP_METHOD(SolrInputDocument, getChildDocuments);
PHP_METHOD(SolrInputDocument, hasChildDocuments);
+PHP_METHOD(SolrInputDocument, getChildDocumentsCount);
+PHP_METHOD(SolrInputDocument, addChildDocuments);
/* }}} */
/* {{{ SolrClient methods declarations */
diff --git a/php_solr_client.c b/php_solr_client.c
index 30195e1..18b8ecf 100644
--- a/php_solr_client.c
+++ b/php_solr_client.c
@@ -22,15 +22,6 @@
#include "php_solr.h"
/* {{{ Macros */
-/* Used to release the (solr_document_t **) pointers */
-#define SOLR_FREE_DOC_ENTRIES(ptr) { \
- if ((ptr) != NULL)\
- { \
- pefree(ptr, SOLR_DOCUMENT_PERSISTENT); \
- ptr = NULL; \
- } \
-}
-/* }}} */
/* {{{ static void solr_client_init_urls(solr_client_t *solr_client) */
static void solr_client_init_urls(solr_client_t *solr_client)
diff --git a/php_solr_input_document.c b/php_solr_input_document.c
index 0b0952f..ce76003 100644
--- a/php_solr_input_document.c
+++ b/php_solr_input_document.c
@@ -741,29 +741,137 @@ PHP_METHOD(SolrInputDocument, merge)
}
/* }}} */
-/* {{{ proto bool SolrInputDocument::addChildDocument(SolrInputDocument child)
+/* {{{ proto void SolrInputDocument::addChildDocument(SolrInputDocument child)
Adds a child document */
PHP_METHOD(SolrInputDocument, addChildDocument)
{
zval *child_obj = NULL;
solr_document_t *solr_doc = NULL;
+ HashTable * document_fields = NULL;
+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &child_obj, \
solr_ce_SolrInputDocument) == FAILURE) {
RETURN_FALSE;
}
- if (solr_fetch_document_entry(getThis(), &solr_doc TSRMLS_CC) == FAILURE)
+ if (solr_fetch_document_entry(child_obj, &solr_doc TSRMLS_CC) == FAILURE)
{
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to fetch document entry \
for current object"); + solr_throw_exception_ex(solr_ce_SolrException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "Internal Error: Unable to fetch \
document_entry."); + return;
+ }
+
+ document_fields = solr_doc->fields;
+
+ /* SolrInputDocument must contain at least one field */
+ if (0 == zend_hash_num_elements(document_fields)) {
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "Child document has no fields"); + \
return; }
if (zend_hash_next_index_insert(solr_doc->children, &child_obj, sizeof(zval *), \
NULL) == FAILURE) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to insert child \
document"); + solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_4000 \
TSRMLS_CC, SOLR_FILE_LINE_FUNC, "Internal Error: Unable to add child to the \
hashtable."); } else {
Z_ADDREF_P(child_obj);
}
+}
+/* }}} */
+
+/* {{{ proto void SolrInputDocument::addChildDocuments(array)
+ Adds a child documents */
+PHP_METHOD(SolrInputDocument, addChildDocuments)
+{
+ HashTable *solr_input_docs;
+ solr_document_t *solr_doc = NULL;
+ zval *docs_array = NULL;
+ int num_input_docs = 0, curr_pos = 0;
+ size_t pos = 0U;
+ zval ***input_docs = NULL, **current_input_doc = NULL;
+
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &docs_array) == \
FAILURE) { + return;
+ }
+
+ if (solr_fetch_document_entry(getThis(), &solr_doc TSRMLS_CC) == FAILURE)
+ {
+ solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_4000 TSRMLS_CC, \
SOLR_FILE_LINE_FUNC, "Internal Error: Unable to fetch document_entry."); + }
+
+ solr_input_docs = Z_ARRVAL_P(docs_array);
+ num_input_docs = zend_hash_num_elements(solr_input_docs);
+
+ if(!num_input_docs)
+ {
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "The array parameter passed is \
empty"); + return;
+ }
+
+ /* This should be released if there is an error */
+ input_docs = (zval ***) pemalloc((sizeof(zval **) * (num_input_docs + 1)), \
SOLR_DOCUMENT_PERSISTENT); +
+ memset(input_docs, 0, sizeof(zval **) * (num_input_docs + 1));
+
+ /* Please check all the SolrInputDocument instances passed via the array */
+ SOLR_HASHTABLE_FOR_LOOP(solr_input_docs)
+ {
+ zval **solr_input_doc = NULL;
+ solr_document_t *doc_entry = NULL;
+ HashTable *document_fields;
+
+ zend_hash_get_current_data_ex(solr_input_docs, (void **) &solr_input_doc, \
((HashPosition *)0)); +
+ if (Z_TYPE_PP(solr_input_doc) != IS_OBJECT || \
!instanceof_function(Z_OBJCE_PP(solr_input_doc), solr_ce_SolrInputDocument \
TSRMLS_CC)) + {
+ SOLR_FREE_DOC_ENTRIES(input_docs);
+
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "SolrInputDocument number %u is not a \
valid SolrInputDocument instance", (curr_pos + 1U)); +
+ return;
+ }
+
+ if (solr_fetch_document_entry((*solr_input_doc), &doc_entry TSRMLS_CC) == \
FAILURE) { +
+ SOLR_FREE_DOC_ENTRIES(input_docs);
+
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "SolrInputDocument number %u is not \
valid. Object not present in HashTable", (curr_pos + 1U)); +
+ return;
+ }
+
+ document_fields = doc_entry->fields;
+
+ /* SolrInputDocument must contain at least one field */
+ if (0 == zend_hash_num_elements(document_fields)) {
+
+ SOLR_FREE_DOC_ENTRIES(input_docs);
+
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "SolrInputDocument number %u has no \
fields", (curr_pos + 1U)); +
+ return;
+ }
+
+ input_docs[curr_pos] = solr_input_doc;
+
+ curr_pos++;
+ }
+
+ /* Grab the first (solr_document_t *) pointer */
+ current_input_doc = input_docs[pos];
- RETURN_TRUE;
+ while(current_input_doc != NULL)
+ {
+ if (zend_hash_next_index_insert(solr_doc->children, current_input_doc, \
sizeof(zval *), NULL) == FAILURE) + {
+ solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, \
SOLR_ERROR_4000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, "SolrInputDocument number %u has no \
fields", (pos + 1U)); + SOLR_FREE_input_docs(input_docs);
+ return;
+ }
+ pos++;
+
+ current_input_doc = input_docs[pos];
+ }
+
+ SOLR_FREE_DOC_ENTRIES(input_docs);
}
/* }}} */
@@ -786,6 +894,7 @@ PHP_METHOD(SolrInputDocument, getChildDocuments)
}
}
/* }}} */
+
/* {{{ proto bool SolrInputDocument::hasChildDocuments (void)
Checks whether this document has got child documents */
PHP_METHOD(SolrInputDocument, hasChildDocuments)
@@ -806,6 +915,22 @@ PHP_METHOD(SolrInputDocument, hasChildDocuments)
}
/* }}} */
+/* {{{ proto int SolrInputDocument::getChildDocumentsCount (void)
+ Returns the number of child documents */
+PHP_METHOD(SolrInputDocument, getChildDocumentsCount)
+{
+ solr_document_t *solr_doc = NULL;
+
+ if (solr_fetch_document_entry(getThis(), &solr_doc TSRMLS_CC))
+ {
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to fetch document entry \
for current object"); + }
+
+ Z_LVAL_P(return_value) = zend_hash_num_elements(solr_doc->children);
+ Z_TYPE_P(return_value) = IS_LONG;
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
diff --git a/solr_macros.h b/solr_macros.h
index 7a81bc9..54c4e91 100644
--- a/solr_macros.h
+++ b/solr_macros.h
@@ -135,6 +135,16 @@
} \
}
+/* Used to release the (solr_document_t **) pointers */
+#define SOLR_FREE_DOC_ENTRIES(ptr) { \
+ if ((ptr) != NULL)\
+ { \
+ pefree(ptr, SOLR_DOCUMENT_PERSISTENT); \
+ ptr = NULL; \
+ } \
+}
+/* }}} */
+
#endif /* SOLR_MACROS_H */
/*
diff --git a/tests/053.solrinputdocument_getchilddocumentscount.phpt \
b/tests/053.solrinputdocument_getchilddocumentscount.phpt new file mode 100644
index 0000000..6c341e7
--- /dev/null
+++ b/tests/053.solrinputdocument_getchilddocumentscount.phpt
@@ -0,0 +1,27 @@
+--TEST--
+SolrInputDocument::getChildDocumentsCount() - test
+--FILE--
+<?php
+
+require_once "bootstrap.inc";
+
+$doc = new SolrInputDocument();
+
+$secondDoc = new SolrInputDocument();
+
+$doc->addField('id', 1123);
+$doc->addField('features', "PHP Client Side");
+$doc->addField('features', "Fast development cycles");
+$doc->cat = 'Information Technology';
+
+$secondDoc->addField('cat', 'Lucene Search');
+$secondDoc->cat = 'Custom Search';
+
+$doc->addChildDocument($secondDoc);
+
+var_dump($doc->getChildDocumentsCount());
+var_dump($secondDoc->getChildDocumentsCount());
+?>
+--EXPECT--
+int(1)
+int(0)
\ No newline at end of file
--
PECL CVS Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic