public function testGetIndexes() { $table = new MingoTable('tablename'); $table->setIndex('index1', array('one', 'two')); $index_list = $table->getIndexes(); $this->assertEquals(1, count($index_list)); $table->setIndex('index2', array('two', 'three')); $index_list = $table->getIndexes(); $this->assertEquals(2, count($index_list)); }
/** * converts the map into a document so Lucene can save it * * @param MingoTable $table * @param array $map the raw map passed to {@link insert()} or {@link update()} * @return Zend_Search_Lucene_Document */ protected function normalizeMap(MingoTable $table, array $map) { $document = new Zend_Search_Lucene_Document(); // add some fields that will be present in all documents... $document->addField(Zend_Search_Lucene_Field::unStored('_id', $map['_id'])); $document->addField(Zend_Search_Lucene_Field::binary('body', $this->getBody($map))); // add all the indexes into the document... foreach ($table->getIndexes() as $index) { foreach ($index->getFields() as $name => $options) { // use array_key... to account for null values... if (array_key_exists($name, $map)) { $val = null; if (is_array($map[$name])) { $val = join(' ', $map[$name]); } else { $val = $map[$name]; } //if/else $document->addField(Zend_Search_Lucene_Field::UnStored($name, $val)); // let's not try and add it twice... unset($map[$name]); } //if } //foreach } //foreach return $document; }
/** * find the index table name from the table and the list of fields the index comprises * * @param MingoTable $table the main table's name * @param MingoCriteria $where_criteria * @return string the index table name */ protected function findIndexTableName(MingoTable $table, MingoCriteria $where_criteria) { if (!$where_criteria->hasWhere() && !$where_criteria->hasSort()) { return ''; } //if $ret_str = ''; $is_match = false; $where_map = $where_criteria->getWhere(); $sort_map = $where_criteria->getSort(); // php >= 5.3, use when Mingo is ported to namespaces... ///$field_list = array_keys(array_replace($where_map,$sort_map)); // we need to get a list of all the fields used in the order they will be used $field_list = array_keys($where_map); if (!empty($sort_map)) { $field_list = array_unique(array_merge($field_list, array_keys($sort_map))); } //if // now go through the index and see if it matches all the fields... foreach ($table->getIndexes() as $index) { $field_i = 0; foreach ($index->getFieldNames() as $field) { if (isset($field_list[$field_i])) { if ($field === $field_list[$field_i]) { $is_match = true; $field_i++; } else { $is_match = false; break; } //if/else } else { break; } //if/else } //foreach if ($is_match) { // we're done, we found a match... $ret_str = $this->getIndexTableName($table, $index); break; } //if } //foreach if (!$is_match) { // since we couldn't find an index table, make sure the query can be valid // on the main table // we are selecting on the main table (no index is being used) so we can only // select or sort on 4 fields (by default): _id, _created, and _updated foreach ($field_list as $field) { // if a field in the where map is not in the main table we've got trouble // since an index table couldn't be found if (!in_array($field, $this->non_body_fields)) { $e_msg = sprintf('Could not match fields: [%s] sorted by fields: [%s] with an index table.', join(',', array_keys($where_map)), join(',', array_keys($sort_map))); // list the available index tables if we are in debug mode... if ($this->hasDebug()) { $e_msg .= ' Indexes available: '; $index_list = $this->getIndexes($table); $e_index_list = array(); foreach ($index_list as $index) { $e_index_list[] = sprintf('%s(%s)', $index->getName(), join(',', $index->getFieldNames())); } //foreach $e_msg .= join(', ', $e_index_list); } //if throw new RuntimeException($e_msg); } //if } //foreach } //if/else return $ret_str; }