/* {{{ proto bool SolrInputDocument::setBoost(float document_boost) Sets the boost for the document. */ PHP_METHOD(SolrInputDocument, setBoost) { double boost_value = 0.0f; solr_document_t *doc_entry = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &boost_value) == FAILURE) { RETURN_FALSE; } /* If the submitted boost_value is negative. */ if (boost_value < 0.0) { RETURN_FALSE; } /* Retrieve the document entry for this SolrDocument */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { doc_entry->document_boost = boost_value; RETURN_TRUE; } RETURN_FALSE; }
/* {{{ proto float SolrInputDocument::getFieldBoost(string fieldname) Returns the boost value for the specified field. */ PHP_METHOD(SolrInputDocument, getFieldBoost) { solr_char_t *field_name = NULL; COMPAT_ARG_SIZE_T field_name_length = 0; solr_document_t *doc_entry = NULL; zend_string *field_str = NULL; /* Process the parameters passed to the default constructor */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &field_name, &field_name_length) == FAILURE) { RETURN_FALSE; } if (!field_name_length) { RETURN_FALSE; } field_str = zend_string_init(field_name, field_name_length, SOLR_DOCUMENT_FIELD_PERSISTENT); /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t **field_values = NULL; if ((field_values = zend_hash_find_ptr(doc_entry->fields, field_str)) != NULL) { RETURN_DOUBLE((*field_values)->field_boost); } RETURN_FALSE; } RETURN_FALSE; }
/* {{{ proto float SolrInputDocument::getFieldBoost(string fieldname) Returns the boost value for the specified field. */ PHP_METHOD(SolrInputDocument, getFieldBoost) { solr_char_t *field_name = NULL; int field_name_length = 0; solr_document_t *doc_entry = NULL; /* Process the parameters passed to the default constructor */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &field_name, &field_name_length) == FAILURE) { RETURN_FALSE; } if (!field_name_length) { RETURN_FALSE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t **field_values = NULL; if (zend_hash_find(doc_entry->fields, (char *) field_name, field_name_length, (void **) &field_values) == SUCCESS) { RETURN_DOUBLE((*field_values)->field_boost); } RETURN_FALSE; } RETURN_FALSE; }
/* {{{ proto array SolrInputDocument::getFieldNames(void) Returns an array of all the field names in the document. */ PHP_METHOD(SolrInputDocument, getFieldNames) { solr_document_t *doc_entry = NULL; /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { HashTable *fields_ht = doc_entry->fields; register zend_bool duplicate = 0; array_init(return_value); SOLR_HASHTABLE_FOR_LOOP(fields_ht) { char *fieldname = NULL; uint fieldname_length = 0U; ulong num_index = 0L; solr_field_list_t **field = NULL; zend_bool duplicate_field_name = 1; zend_hash_get_current_key_ex(fields_ht, &fieldname, &fieldname_length, &num_index, duplicate, NULL); zend_hash_get_current_data_ex(fields_ht, (void **) &field, NULL); add_next_index_string(return_value, (char *) (*field)->field_name, duplicate_field_name); } /* We are done */ return; }
/* {{{ proto bool SolrInputDocument::addField(string field_name, field_value [, float field_boost]) Adds a field to the document. Can be called multiple times. */ PHP_METHOD(SolrInputDocument, addField) { solr_char_t *field_name = NULL, *field_value = NULL; COMPAT_ARG_SIZE_T field_name_length = 0, field_value_length = 0; solr_document_t *doc_entry = NULL; double field_boost = 0.0; /* Process the parameters passed to the method */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|d", &field_name, &field_name_length, &field_value, &field_value_length, &field_boost) == FAILURE) { RETURN_FALSE; } if (!field_name_length) { RETURN_FALSE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t *field_values = NULL; /* If the field already exists in the SolrDocument instance append the value to the field list queue */ if ((field_values = (solr_field_list_t *)zend_hash_str_find_ptr(doc_entry->fields, field_name, field_name_length)) != NULL) { if (solr_document_insert_field_value(field_values, (solr_char_t *)field_value, field_boost) == FAILURE) { RETURN_FALSE; } } else { /* Otherwise, create a new one and add it to the hash table */ field_values = (solr_field_list_t *) pemalloc(sizeof(solr_field_list_t), SOLR_DOCUMENT_FIELD_PERSISTENT); memset(field_values, 0, sizeof(solr_field_list_t)); field_values->count = 0L; field_values->field_boost = 0.0; field_values->field_name = (solr_char_t *) pestrdup((char *)field_name, SOLR_DOCUMENT_FIELD_PERSISTENT); field_values->head = NULL; field_values->last = NULL; if (solr_document_insert_field_value(field_values, field_value, field_boost) == FAILURE) { solr_destroy_field_list(field_values); RETURN_FALSE; } if (zend_hash_str_add_ptr(doc_entry->fields, field_name, field_name_length,(void *) field_values) == NULL) { solr_destroy_field_list(field_values); RETURN_FALSE; } /* Increment field count only when HEAD is added */ doc_entry->field_count++; } RETURN_TRUE; } RETURN_FALSE; }
/* {{{ static int solr_document_get_field(zval *objptr, zval *return_value, solr_char_t *field_name, int field_name_length TSRMLS_DC) */ static int solr_document_get_field(zval *objptr, zval *return_value, solr_char_t *field_name, int field_name_length TSRMLS_DC) { solr_document_t *doc_entry = NULL; if (!field_name_length) { return FAILURE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(objptr, &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t **field_values = NULL; if (zend_hash_find(doc_entry->fields, (char *)field_name, field_name_length, (void **) &field_values) == SUCCESS) { solr_create_document_field_object(*field_values, &return_value TSRMLS_CC); /* The field was retrieved, so we're done here */ return SUCCESS; } return FAILURE; } return FAILURE; }
/* {{{ proto array SolrInputDocument::toArray(void) Returns an array representation of the object. */ PHP_METHOD(SolrInputDocument, toArray) { solr_document_t *doc_entry = NULL; zval fields_array; /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { HashTable *fields_ht; array_init(return_value); array_init(&fields_array); zend_hash_init(Z_ARRVAL(fields_array), zend_hash_num_elements(doc_entry->fields), NULL, ZVAL_PTR_DTOR, 0); add_assoc_double(return_value, "document_boost", doc_entry->document_boost); add_assoc_long(return_value, "field_count", doc_entry->field_count); add_assoc_zval(return_value, "fields", &fields_array); fields_ht = doc_entry->fields; SOLR_HASHTABLE_FOR_LOOP(fields_ht) { solr_field_list_t *field = NULL; zval current_field; zval *current_field_ptr = ¤t_field; field = zend_hash_get_current_data_ptr(fields_ht); /* create SolrDocumentField */ solr_create_document_field_object(field, ¤t_field_ptr TSRMLS_CC); /* create SolrDocumentField to the fields HT */ add_next_index_zval(&fields_array, current_field_ptr); } /* We are done */ return; }
/* {{{ proto bool SolrInputDocument::updateField(string fieldName, int modifier, string value) */ PHP_METHOD(SolrInputDocument, updateField) { solr_char_t *field_name = NULL, *field_value = NULL; COMPAT_ARG_SIZE_T field_name_length = 0, field_value_len = 0; solr_document_t *doc_entry; solr_field_list_t *field; uint field_exists = 0; long modifier = 0L; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &field_name, &field_name_length, &modifier, &field_value, &field_value_len) == FAILURE) { return; } if (!field_name_length || !field_value_len) { RETURN_FALSE; } if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == FAILURE) { return; } switch (modifier) { case SOLR_FIELD_VALUE_MOD_ADD: case SOLR_FIELD_VALUE_MOD_REMOVE: case SOLR_FIELD_VALUE_MOD_REMOVEREGEX: case SOLR_FIELD_VALUE_MOD_SET: case SOLR_FIELD_VALUE_MOD_INC: break; default: solr_throw_exception_ex(solr_ce_SolrIllegalArgumentException, SOLR_ERROR_4003 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_4003_MSG); return; } if ((field = zend_hash_str_find_ptr(doc_entry->fields, field_name, field_name_length)) == NULL){ field = (solr_field_list_t *)pemalloc(sizeof(solr_field_list_t), SOLR_DOCUMENT_FIELD_PERSISTENT); memset(field, 0, sizeof(solr_field_list_t)); field->field_name = pestrdup(field_name, SOLR_DOCUMENT_FIELD_PERSISTENT); field->count = 1; field->head = NULL; field->last = NULL; if (modifier > 0) { field->modified = 1; } doc_entry->field_count++; if (zend_hash_str_add_ptr(doc_entry->fields, field_name, field_name_length, field) == NULL) { RETURN_FALSE; } } else if (field->modified == 0) { solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_4004 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_4004_MSG); RETURN_FALSE; } solr_document_insert_field_value(field, field_value, 0.0, modifier); }
/* {{{ proto float SolrInputDocument::getBoost(void) Retrieves the boost for the document. */ PHP_METHOD(SolrInputDocument, getBoost) { solr_document_t *doc_entry = NULL; if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { RETURN_DOUBLE(doc_entry->document_boost); } RETURN_FALSE; }
/* {{{ proto int SolrInputDocument::getFieldCount(void) Returns the number of fields in document. Multivalued fields are only counted once. */ PHP_METHOD(SolrInputDocument, getFieldCount) { solr_document_t *doc_entry = NULL; /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { RETURN_LONG(zend_hash_num_elements(doc_entry->fields)); } RETURN_FALSE; }
/* {{{ proto int SolrInputDocument::getVersion( void ) */ PHP_METHOD(SolrInputDocument, getVersion) { solr_document_t *doc_entry = NULL; solr_char_t *field_name = "_version_"; COMPAT_ARG_SIZE_T field_name_length = sizeof("_version_"); solr_field_list_t *field = NULL; if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == FAILURE) { RETURN_NULL(); } if ((field = zend_hash_str_find_ptr(doc_entry->fields, field_name, field_name_length)) != NULL) { RETURN_LONG(atol(field->head->field_value)); } RETURN_NULL(); }
/* {{{ proto void SolrInputDocument::__destruct(void) Destructor for SolrInputDocument */ PHP_METHOD(SolrInputDocument, __destruct) { solr_document_t *doc_entry = NULL; /* Retrieve the document entry for this SolrDocument */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { zend_hash_index_del(SOLR_GLOBAL(documents), doc_entry->document_index); /* Keep track of how many SolrDocument instances we currently have */ SOLR_GLOBAL(document_count)--; return ; } }
/* {{{ proto bool SolrInputDocument::clear(void) Discards all the fields and resets the document boost to zero. */ PHP_METHOD(SolrInputDocument, clear) { solr_document_t *doc_entry = NULL; if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { doc_entry->document_boost = 0.0; doc_entry->field_count = 0L; zend_hash_clean(doc_entry->fields); RETURN_TRUE; } RETURN_FALSE; }
/* {{{ proto void SolrInputDocument::__clone(void) Clones the current object. Not to be called directly. */ PHP_METHOD(SolrInputDocument, __clone) { zval *objptr = getThis(); solr_document_t new_solr_doc; solr_document_t *new_doc_entry = NULL, *old_doc_entry = NULL; ulong document_index = SOLR_UNIQUE_DOCUMENT_INDEX(); new_doc_entry = &new_solr_doc; new_doc_entry = (solr_document_t *) pemalloc(sizeof(solr_document_t), SOLR_DOCUMENT_PERSISTENT); memset(&new_solr_doc, 0, sizeof(solr_document_t)); /* Retrieve the document entry for the original SolrDocument */ if (solr_fetch_document_entry(objptr, &old_doc_entry TSRMLS_CC) == FAILURE) { return ; } /* Duplicate the doc_entry contents */ memcpy(new_doc_entry, old_doc_entry, sizeof(solr_document_t)); /* Override the document index with a new one and create a new HashTable */ new_doc_entry->document_index = document_index; /* Allocate new memory for the fields HashTable, using fast cache for HashTables */ ALLOC_HASHTABLE(new_doc_entry->fields); ALLOC_HASHTABLE(new_doc_entry->children); /* Initializing the hash table used for storing fields in this SolrDocument */ zend_hash_init(new_doc_entry->fields, old_doc_entry->fields->nTableSize, NULL, (dtor_func_t) solr_destroy_field_list_ht_dtor, SOLR_DOCUMENT_FIELD_PERSISTENT); zend_hash_init(new_doc_entry->children, old_doc_entry->children->nTableSize, NULL, ZVAL_PTR_DTOR, SOLR_DOCUMENT_FIELD_PERSISTENT); /* Copy the contents of the old fields HashTable to the new SolrDocument */ zend_hash_copy(new_doc_entry->fields, old_doc_entry->fields, (copy_ctor_func_t) field_copy_constructor); zend_hash_copy(new_doc_entry->children, old_doc_entry->children, (copy_ctor_func_t) zval_add_ref); /* Add the document entry to the directory of documents */ zend_hash_index_update_ptr(SOLR_GLOBAL(documents), document_index, (void *) new_doc_entry); /* Set the value of the internal id property */ zend_update_property_long(solr_ce_SolrInputDocument, objptr, SOLR_INDEX_PROPERTY_NAME, sizeof(SOLR_INDEX_PROPERTY_NAME) - 1, document_index TSRMLS_CC); /* Keep track of how many SolrDocument instances we currently have */ SOLR_GLOBAL(document_count)++; }
/* {{{ proto bool SolrInputDocument::setFieldBoost(string fieldname, float boost_value) Sets the boost for the specified field. */ PHP_METHOD(SolrInputDocument, setFieldBoost) { solr_char_t *field_name = NULL; int field_name_length = 0; double field_boost = 0.0; solr_document_t *doc_entry = NULL; /* Process the parameters passed to the default constructor */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd", &field_name, &field_name_length, &field_boost) == FAILURE) { RETURN_FALSE; } if (!field_name_length) { RETURN_FALSE; } if (field_boost < 0.0) { RETURN_FALSE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t **field_values = NULL; /* If the field already exists in the SolrDocument instance append the value to the field list queue */ if (zend_hash_find(doc_entry->fields, (char *) field_name, field_name_length, (void **) &field_values) == SUCCESS) { (*field_values)->field_boost = field_boost; RETURN_TRUE; } RETURN_FALSE; } RETURN_FALSE; }
/** * {{{ proto bool SolrInputDocument::setVersion(int version) * Enable optimistic concurrency using assertions */ PHP_METHOD(SolrInputDocument, setVersion) { long version = 0; solr_document_t *doc_entry = NULL; solr_field_list_t *field = NULL; solr_char_t *field_name = "_version_"; COMPAT_ARG_SIZE_T field_name_length = sizeof("_version_"); char version_str[80]; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, solr_ce_SolrIllegalArgumentException, &error_handling TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &version) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } zend_restore_error_handling(&error_handling TSRMLS_CC); if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == FAILURE) { return; } field = pemalloc(sizeof(solr_field_list_t), SOLR_DOCUMENT_FIELD_PERSISTENT); field->count = 0L; field->field_boost = 0.0f; field->field_name = pestrdup(field_name, SOLR_DOCUMENT_FIELD_PERSISTENT); field->head = field->last = NULL; snprintf(version_str, 80, "%ld", version); solr_document_insert_field_value(field, version_str, 0.0); if ((zend_hash_str_update_ptr(doc_entry->fields, field_name, field_name_length, field) == NULL )) { solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1008 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1008_MSG); solr_destroy_field_list(&field); return; } RETURN_TRUE; }
/* {{{ proto SolrDocumentField SolrInputDocument::getField(string fieldname) Returns the requested field. */ PHP_METHOD(SolrInputDocument, getField) { solr_char_t *field_name = NULL; COMPAT_ARG_SIZE_T field_name_length = 0; solr_document_t *doc_entry = NULL; zend_string *field_str = NULL; /* Process the parameters passed to the default constructor */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &field_name, &field_name_length) == FAILURE) { RETURN_FALSE; } if (!field_name_length) { RETURN_FALSE; } field_str = zend_string_init(field_name, field_name_length, SOLR_DOCUMENT_FIELD_PERSISTENT); /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(getThis(), &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t *field_values = NULL; if ((field_values = zend_hash_find_ptr(doc_entry->fields, field_str)) != NULL) { solr_create_document_field_object(field_values, &return_value TSRMLS_CC); /* The field was retrieved, so we're done here */ zend_string_release(field_str); return ; } goto return_false; } return_false: zend_string_release(field_str); RETURN_FALSE; }
/* {{{ static int solr_document_field_exists(zval *objptr, solr_char_t *field_name, int field_name_length TSRMLS_DC) */ static int solr_document_field_exists(zval *objptr, solr_char_t *field_name, int field_name_length TSRMLS_DC) { solr_document_t *doc_entry = NULL; if (!field_name_length) { return FAILURE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(objptr, &doc_entry TSRMLS_CC) == SUCCESS) { if (zend_hash_exists(doc_entry->fields, field_name, field_name_length)) { return SUCCESS; } else { return FAILURE; } } return FAILURE; }
/* {{{ static int solr_document_set_field(zval *objptr, solr_char_t *field_name, int field_name_length, solr_char_t *field_value, int field_value_length TSRMLS_DC) */ static int solr_document_set_field(zval *objptr, solr_char_t *field_name, int field_name_length, solr_char_t *field_value, int field_value_length TSRMLS_DC) { double field_boost = 0.0f; solr_document_t *doc_entry = NULL; if (!field_name_length) { return FAILURE; } if (!field_value_length) { return FAILURE; } /* Retrieve the document entry for the SolrDocument instance */ if (solr_fetch_document_entry(objptr, &doc_entry TSRMLS_CC) == SUCCESS) { solr_field_list_t **field_values_ptr = NULL; solr_field_list_t *field_values = NULL; /* If the field already exists in the SolrDocument instance append the value to the field list queue */ if (zend_hash_find(doc_entry->fields, field_name, field_name_length, (void **) &field_values_ptr) == SUCCESS) { if (solr_document_insert_field_value(*field_values_ptr, field_value, field_boost) == FAILURE) { return FAILURE; } } else { /* Otherwise, create a new one and add it to the hash table */ field_values = (solr_field_list_t *) pemalloc(sizeof(solr_field_list_t), SOLR_DOCUMENT_FIELD_PERSISTENT); memset(field_values, 0, sizeof(solr_field_list_t)); field_values_ptr = &field_values; field_values->count = 0L; field_values->field_boost = 0.0; field_values->field_name = (solr_char_t *) pestrdup(field_name,SOLR_DOCUMENT_FIELD_PERSISTENT); field_values->head = NULL; field_values->last = NULL; if (solr_document_insert_field_value(field_values, field_value, field_boost) == FAILURE) { solr_destroy_field_list(&field_values); return FAILURE; } if (zend_hash_add(doc_entry->fields, field_name, field_name_length, (void *) field_values_ptr, sizeof(solr_field_list_t *), (void **) NULL) == FAILURE) { solr_destroy_field_list(&field_values); return FAILURE; } /* Increment field count only when HEAD is added */ doc_entry->field_count++; } return SUCCESS; } return FAILURE; }