Browse Source

Index schema type support added to Sphinx

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
102f386873
  1. 8
      extensions/sphinx/IndexSchema.php
  2. 61
      extensions/sphinx/Schema.php
  3. 15
      tests/unit/extensions/sphinx/SchemaTest.php

8
extensions/sphinx/IndexSchema.php

@ -25,6 +25,14 @@ class IndexSchema extends Object
*/ */
public $name; public $name;
/** /**
* @var string type of the index.
*/
public $type;
/**
* @var boolean whether this index is a runtime index.
*/
public $isRuntime;
/**
* @var string primary key of this index. * @var string primary key of this index.
*/ */
public $primaryKey; public $primaryKey;

61
extensions/sphinx/Schema.php

@ -40,6 +40,10 @@ class Schema extends Object
*/ */
private $_indexNames; private $_indexNames;
/** /**
* @var array list of ALL index types in the Sphinx (index name => index type)
*/
private $_indexTypes;
/**
* @var array list of loaded index metadata (index name => IndexSchema) * @var array list of loaded index metadata (index name => IndexSchema)
*/ */
private $_indexes = []; private $_indexes = [];
@ -74,6 +78,7 @@ class Schema extends Object
{ {
$index = new IndexSchema; $index = new IndexSchema;
$this->resolveIndexNames($index, $name); $this->resolveIndexNames($index, $name);
$this->resolveIndexType($index);
if ($this->findColumns($index)) { if ($this->findColumns($index)) {
return $index; return $index;
@ -93,6 +98,17 @@ class Schema extends Object
} }
/** /**
* Resolves the index name.
* @param IndexSchema $index the index metadata object
*/
protected function resolveIndexType($index)
{
$indexTypes = $this->getIndexTypes();
$index->type = array_key_exists($index->name, $indexTypes) ? $indexTypes[$index->name] : 'unknown';
$index->isRuntime = ($index->type == 'rt');
}
/**
* Obtains the metadata for the named index. * Obtains the metadata for the named index.
* @param string $name index name. The index name may contain schema name if any. Do not quote the index name. * @param string $name index name. The index name may contain schema name if any. Do not quote the index name.
* @param boolean $refresh whether to reload the index schema even if it is found in the cache. * @param boolean $refresh whether to reload the index schema even if it is found in the cache.
@ -162,7 +178,7 @@ class Schema extends Object
* @return IndexSchema[] the metadata for all indexes in the Sphinx. * @return IndexSchema[] the metadata for all indexes in the Sphinx.
* Each array element is an instance of [[IndexSchema]] or its child class. * Each array element is an instance of [[IndexSchema]] or its child class.
*/ */
public function getTableSchemas($refresh = false) public function getIndexSchemas($refresh = false)
{ {
$indexes = []; $indexes = [];
foreach ($this->getIndexNames($refresh) as $name) { foreach ($this->getIndexNames($refresh) as $name) {
@ -174,27 +190,56 @@ class Schema extends Object
} }
/** /**
* Returns all index names in the database. * Returns all index names in the Sphinx.
* @param boolean $refresh whether to fetch the latest available index names. If this is false, * @param boolean $refresh whether to fetch the latest available index names. If this is false,
* index names fetched previously (if available) will be returned. * index names fetched previously (if available) will be returned.
* @return string[] all index names in the database. * @return string[] all index names in the Sphinx.
*/ */
public function getIndexNames($refresh = false) public function getIndexNames($refresh = false)
{ {
if (!isset($this->_indexNames) || $refresh) { if (!isset($this->_indexNames) || $refresh) {
$this->_indexNames = $this->findIndexNames(); $this->initIndexesInfo();
} }
return $this->_indexNames; return $this->_indexNames;
} }
/** /**
* Returns all index names in the database. * Returns all index types in the Sphinx.
* @return array all index names in the database. The names have NO schema name prefix. * @param boolean $refresh whether to fetch the latest available index types. If this is false,
* index types fetched previously (if available) will be returned.
* @return array all index types in the Sphinx in format: index name => index type.
*/
public function getIndexTypes($refresh = false)
{
if (!isset($this->_indexTypes) || $refresh) {
$this->initIndexesInfo();
}
return $this->_indexTypes;
}
/**
* Initializes information about name and type of all index in the Sphinx.
*/
protected function initIndexesInfo()
{
$this->_indexNames = [];
$this->_indexTypes = [];
$indexes = $this->findIndexes();
foreach ($indexes as $index) {
$indexName = $index['Index'];
$this->_indexNames[] = $indexName;
$this->_indexTypes[$indexName] = $index['Type'];
}
}
/**
* Returns all index names in the Sphinx.
* @return array all index names in the Sphinx.
*/ */
protected function findIndexNames() protected function findIndexes()
{ {
$sql = 'SHOW TABLES'; $sql = 'SHOW TABLES';
return $this->db->createCommand($sql)->queryColumn(); return $this->db->createCommand($sql)->queryAll();
} }
/** /**

15
tests/unit/extensions/sphinx/SchemaTest.php

@ -24,7 +24,7 @@ class SchemaTest extends SphinxTestCase
{ {
$schema = $this->getConnection()->schema; $schema = $this->getConnection()->schema;
$indexes = $schema->getTableSchemas(); $indexes = $schema->getIndexSchemas();
$this->assertEquals(count($schema->getIndexNames()), count($indexes)); $this->assertEquals(count($schema->getIndexNames()), count($indexes));
foreach($indexes as $index) { foreach($indexes as $index) {
$this->assertInstanceOf('yii\sphinx\IndexSchema', $index); $this->assertInstanceOf('yii\sphinx\IndexSchema', $index);
@ -68,4 +68,17 @@ class SchemaTest extends SphinxTestCase
} }
fclose($fp); fclose($fp);
} }
public function testIndexType()
{
$schema = $this->getConnection()->schema;
$index = $schema->getIndexSchema('yii2_test_article_index');
$this->assertEquals('local', $index->type);
$this->assertFalse($index->isRuntime);
$index = $schema->getIndexSchema('yii2_test_rt_index');
$this->assertEquals('rt', $index->type);
$this->assertTrue($index->isRuntime);
}
} }
Loading…
Cancel
Save