Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
102f3868
Commit
102f3868
authored
Nov 18, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Index schema type support added to Sphinx
parent
bbb5e1db
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
9 deletions
+76
-9
IndexSchema.php
extensions/sphinx/IndexSchema.php
+8
-0
Schema.php
extensions/sphinx/Schema.php
+53
-8
SchemaTest.php
tests/unit/extensions/sphinx/SchemaTest.php
+15
-1
No files found.
extensions/sphinx/IndexSchema.php
View file @
102f3868
...
...
@@ -25,6 +25,14 @@ class IndexSchema extends Object
*/
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.
*/
public
$primaryKey
;
...
...
extensions/sphinx/Schema.php
View file @
102f3868
...
...
@@ -40,6 +40,10 @@ class Schema extends Object
*/
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)
*/
private
$_indexes
=
[];
...
...
@@ -74,6 +78,7 @@ class Schema extends Object
{
$index
=
new
IndexSchema
;
$this
->
resolveIndexNames
(
$index
,
$name
);
$this
->
resolveIndexType
(
$index
);
if
(
$this
->
findColumns
(
$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.
* @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.
...
...
@@ -162,7 +178,7 @@ class Schema extends Object
* @return IndexSchema[] the metadata for all indexes in the Sphinx.
* Each array element is an instance of [[IndexSchema]] or its child class.
*/
public
function
get
Table
Schemas
(
$refresh
=
false
)
public
function
get
Index
Schemas
(
$refresh
=
false
)
{
$indexes
=
[];
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,
* 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
)
{
if
(
!
isset
(
$this
->
_indexNames
)
||
$refresh
)
{
$this
->
_indexNames
=
$this
->
findIndexNames
();
$this
->
initIndexesInfo
();
}
return
$this
->
_indexNames
;
}
/**
* Returns all index names in the database.
* @return array all index names in the database. The names have NO schema name prefix.
* Returns all index types in the Sphinx.
* @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
findIndex
Nam
es
()
protected
function
findIndexes
()
{
$sql
=
'SHOW TABLES'
;
return
$this
->
db
->
createCommand
(
$sql
)
->
query
Column
();
return
$this
->
db
->
createCommand
(
$sql
)
->
query
All
();
}
/**
...
...
tests/unit/extensions/sphinx/SchemaTest.php
View file @
102f3868
...
...
@@ -24,7 +24,7 @@ class SchemaTest extends SphinxTestCase
{
$schema
=
$this
->
getConnection
()
->
schema
;
$indexes
=
$schema
->
get
Table
Schemas
();
$indexes
=
$schema
->
get
Index
Schemas
();
$this
->
assertEquals
(
count
(
$schema
->
getIndexNames
()),
count
(
$indexes
));
foreach
(
$indexes
as
$index
)
{
$this
->
assertInstanceOf
(
'yii\sphinx\IndexSchema'
,
$index
);
...
...
@@ -68,4 +68,17 @@ class SchemaTest extends SphinxTestCase
}
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
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment