Commit a94886fa by Carsten Brandt

elasticsearch AR WIP copied parts from redis implementation

parent 91c1805e
...@@ -15,6 +15,7 @@ services: ...@@ -15,6 +15,7 @@ services:
before_script: before_script:
- composer self-update && composer --version - composer self-update && composer --version
- composer require satooshi/php-coveralls 0.6.* - composer require satooshi/php-coveralls 0.6.*
- composer require guzzle/http v3.7.3
- mysql -e 'CREATE DATABASE yiitest;'; - mysql -e 'CREATE DATABASE yiitest;';
- psql -U postgres -c 'CREATE DATABASE yiitest;'; - psql -U postgres -c 'CREATE DATABASE yiitest;';
- tests/unit/data/travis/apc-setup.sh - tests/unit/data/travis/apc-setup.sh
......
...@@ -74,6 +74,7 @@ ...@@ -74,6 +74,7 @@
"psr-0": { "yii\\": "/" } "psr-0": { "yii\\": "/" }
}, },
"suggest": { "suggest": {
"guzzle/http": "Required by elasticsearch.",
"michelf/php-markdown": "Required by Markdown.", "michelf/php-markdown": "Required by Markdown.",
"twig/twig": "Required by TwigViewRenderer.", "twig/twig": "Required by TwigViewRenderer.",
"smarty/smarty": "Required by SmartyViewRenderer." "smarty/smarty": "Required by SmartyViewRenderer."
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\elasticsearch;
/**
* ActiveQuery represents a DB query associated with an Active Record class.
*
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ActiveQuery extends \yii\base\Component
{
/**
* @var string the name of the ActiveRecord class.
*/
public $modelClass;
/**
* @var array list of relations that this query should be performed with
*/
public $with;
/**
* @var string the name of the column by which query results should be indexed by.
* This is only used when the query result is returned as an array when calling [[all()]].
*/
public $indexBy;
/**
* @var boolean whether to return each record as an array. If false (default), an object
* of [[modelClass]] will be created to represent each record.
*/
public $asArray;
/**
* @var integer maximum number of records to be returned. If not set or less than 0, it means no limit.
*/
public $limit;
/**
* @var integer zero-based offset from where the records are to be returned.
* If not set, it means starting from the beginning.
* If less than zero it means starting n elements from the end.
*/
public $offset;
/**
* @var array array of primary keys of the records to find.
*/
public $primaryKeys;
/**
* List of multiple pks must be zero based
*
* @param $primaryKeys
* @return ActiveQuery
*/
public function primaryKeys($primaryKeys) {
if (is_array($primaryKeys) && isset($primaryKeys[0])) {
$this->primaryKeys = $primaryKeys;
} else {
$this->primaryKeys = array($primaryKeys);
}
return $this;
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all()
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
if (($primaryKeys = $this->primaryKeys) === null) {
$start = $this->offset === null ? 0 : $this->offset;
$end = $this->limit === null ? -1 : $start + $this->limit;
$primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $end));
}
$rows = array();
foreach($primaryKeys as $pk) {
$key = $modelClass::tableName() . ':a:' . $modelClass::hashPk($pk);
// get attributes
$data = $db->executeCommand('HGETALL', array($key));
$row = array();
for($i=0;$i<count($data);) {
$row[$data[$i++]] = $data[$i++];
}
$rows[] = $row;
}
if ($rows !== array()) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
$this->populateRelations($models, $this->with);
}
return $models;
} else {
return array();
}
}
/**
* Executes query and returns a single row of result.
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
* the query result may be either an array or an ActiveRecord object. Null will be returned
* if the query results in nothing.
*/
public function one()
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
if (($primaryKeys = $this->primaryKeys) === null) {
$start = $this->offset === null ? 0 : $this->offset;
$primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $start + 1));
}
$pk = reset($primaryKeys);
$key = $modelClass::tableName() . ':a:' . $modelClass::hashPk($pk);
// get attributes
$data = $db->executeCommand('HGETALL', array($key));
if ($data === array()) {
return null;
}
$row = array();
for($i=0;$i<count($data);) {
$row[$data[$i++]] = $data[$i++];
}
if (!$this->asArray) {
/** @var $class ActiveRecord */
$class = $this->modelClass;
$model = $class::create($row);
if (!empty($this->with)) {
$models = array($model);
$this->populateRelations($models, $this->with);
$model = $models[0];
}
return $model;
} else {
return $row;
}
}
/**
* Returns the number of records.
* @param string $q the COUNT expression. Defaults to '*'.
* Make sure you properly quote column names.
* @return integer number of records
*/
public function count()
{
$modelClass = $this->modelClass;
/** @var Connection $db */
$db = $modelClass::getDb();
return $db->executeCommand('LLEN', array($modelClass::tableName()));
}
/**
* Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results.
* @return string|boolean the value of the first column in the first row of the query result.
* False is returned if the query result is empty.
*/
public function scalar($column)
{
$record = $this->one();
return $record->$column;
}
/**
* Returns a value indicating whether the query result contains any row of data.
* @return boolean whether the query result contains any row of data.
*/
public function exists()
{
return $this->one() !== null;
}
/**
* Sets the [[asArray]] property.
* TODO: refactor, it is duplicated from yii/db/ActiveQuery
* @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
* @return ActiveQuery the query object itself
*/
public function asArray($value = true)
{
$this->asArray = $value;
return $this;
}
/**
* Sets the LIMIT part of the query.
* TODO: refactor, it is duplicated from yii/db/Query
* @param integer $limit the limit
* @return Query the query object itself
*/
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* Sets the OFFSET part of the query.
* TODO: refactor, it is duplicated from yii/db/Query
* @param integer $offset the offset
* @return Query the query object itself
*/
public function offset($offset)
{
$this->offset = $offset;
return $this;
}
/**
* Specifies the relations with which this query should be performed.
*
* The parameters to this method can be either one or multiple strings, or a single array
* of relation names and the optional callbacks to customize the relations.
*
* The followings are some usage examples:
*
* ~~~
* // find customers together with their orders and country
* Customer::find()->with('orders', 'country')->all();
* // find customers together with their country and orders of status 1
* Customer::find()->with(array(
* 'orders' => function($query) {
* $query->andWhere('status = 1');
* },
* 'country',
* ))->all();
* ~~~
*
* TODO: refactor, it is duplicated from yii/db/ActiveQuery
* @return ActiveQuery the query object itself
*/
public function with()
{
$this->with = func_get_args();
if (isset($this->with[0]) && is_array($this->with[0])) {
// the parameter is given as an array
$this->with = $this->with[0];
}
return $this;
}
/**
* Sets the [[indexBy]] property.
* TODO: refactor, it is duplicated from yii/db/ActiveQuery
* @param string $column the name of the column by which the query results should be indexed by.
* @return ActiveQuery the query object itself
*/
public function indexBy($column)
{
$this->indexBy = $column;
return $this;
}
// TODO: refactor, it is duplicated from yii/db/ActiveQuery
private function createModels($rows)
{
$models = array();
if ($this->asArray) {
if ($this->indexBy === null) {
return $rows;
}
foreach ($rows as $row) {
$models[$row[$this->indexBy]] = $row;
}
} else {
/** @var $class ActiveRecord */
$class = $this->modelClass;
if ($this->indexBy === null) {
foreach ($rows as $row) {
$models[] = $class::create($row);
}
} else {
foreach ($rows as $row) {
$model = $class::create($row);
$models[$model->{$this->indexBy}] = $model;
}
}
}
return $models;
}
// TODO: refactor, it is duplicated from yii/db/ActiveQuery
private function populateRelations(&$models, $with)
{
$primaryModel = new $this->modelClass;
$relations = $this->normalizeRelations($primaryModel, $with);
foreach ($relations as $name => $relation) {
if ($relation->asArray === null) {
// inherit asArray from primary query
$relation->asArray = $this->asArray;
}
$relation->findWith($name, $models);
}
}
/**
* TODO: refactor, it is duplicated from yii/db/ActiveQuery
* @param ActiveRecord $model
* @param array $with
* @return ActiveRelation[]
*/
private function normalizeRelations($model, $with)
{
$relations = array();
foreach ($with as $name => $callback) {
if (is_integer($name)) {
$name = $callback;
$callback = null;
}
if (($pos = strpos($name, '.')) !== false) {
// with sub-relations
$childName = substr($name, $pos + 1);
$name = substr($name, 0, $pos);
} else {
$childName = null;
}
$t = strtolower($name);
if (!isset($relations[$t])) {
$relation = $model->getRelation($name);
$relation->primaryModel = null;
$relations[$t] = $relation;
} else {
$relation = $relations[$t];
}
if (isset($childName)) {
$relation->with[$childName] = $callback;
} elseif ($callback !== null) {
call_user_func($callback, $relation);
}
}
return $relations;
}
}
...@@ -5,13 +5,15 @@ ...@@ -5,13 +5,15 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\elasticsearch; namespace yii\elasticsearch;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
/** /**
* elasticsearch Connection is used to connect to an elasticsearch cluster version 0.20 or higher
*
* *
* @author Carsten Brandt <mail@cebe.cc> * @author Carsten Brandt <mail@cebe.cc>
* @since 2.0 * @since 2.0
...@@ -24,7 +26,13 @@ class Connection extends Component ...@@ -24,7 +26,13 @@ class Connection extends Component
const EVENT_AFTER_OPEN = 'afterOpen'; const EVENT_AFTER_OPEN = 'afterOpen';
// TODO add autodetection of cluster nodes // TODO add autodetection of cluster nodes
public $nodes = array(); // http://localhost:9200/_cluster/nodes
public $nodes = array(
array(
'host' => 'localhost',
'port' => 9200,
)
);
// TODO use timeouts // TODO use timeouts
/** /**
...@@ -71,6 +79,11 @@ class Connection extends Component ...@@ -71,6 +79,11 @@ class Connection extends Component
*/ */
public function open() public function open()
{ {
foreach($this->nodes as $key => $node) {
if (is_array($node)) {
$this->nodes[$key] = new Node($node);
}
}
/* if ($this->_socket === null) { /* if ($this->_socket === null) {
if (empty($this->dsn)) { if (empty($this->dsn)) {
throw new InvalidConfigException('Connection.dsn cannot be empty.'); throw new InvalidConfigException('Connection.dsn cannot be empty.');
...@@ -141,4 +154,33 @@ class Connection extends Component ...@@ -141,4 +154,33 @@ class Connection extends Component
{ {
return 'elasticsearch'; return 'elasticsearch';
} }
public function getNodeInfo()
{
// TODO HTTP request to localhost:9200/
}
public function http()
{
return new \Guzzle\Http\Client('http://localhost:9200/');
}
public function get($url)
{
$c = $this->initCurl($url);
$result = curl_exec($c);
curl_close($c);
}
private function initCurl($url)
{
$c = curl_init('http://localhost:9200/' . $url);
$fp = fopen("example_homepage.txt", "w");
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($c, CURLOPT_FILE, $fp);
curl_setopt($c, CURLOPT_HEADER, 0);
}
} }
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\elasticsearch; namespace yii\elasticsearch;
use yii\base\Object; use yii\base\Object;
......
...@@ -5,6 +5,11 @@ define('YII_DEBUG', true); ...@@ -5,6 +5,11 @@ define('YII_DEBUG', true);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__; $_SERVER['SCRIPT_FILENAME'] = __FILE__;
// require composer autoloader if available
$composerAutoload = __DIR__ . '/../../vendor/autoload.php';
if (is_file($composerAutoload)) {
require_once($composerAutoload);
}
require_once(__DIR__ . '/../../framework/yii/Yii.php'); require_once(__DIR__ . '/../../framework/yii/Yii.php');
Yii::setAlias('@yiiunit', __DIR__); Yii::setAlias('@yiiunit', __DIR__);
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\ar\elasticsearch;
/**
* ActiveRecord is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveRecord extends \yii\elasticsearch\ActiveRecord
{
public static $db;
public static function getDb()
{
return self::$db;
}
}
<?php
namespace yiiunit\data\ar\elasticsearch;
/**
* Class Customer
*
* @property integer $id
* @property string $name
* @property string $email
* @property string $address
* @property integer $status
*/
class Customer extends ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 2;
public $status2;
public static function columns()
{
return array(
'id' => 'integer',
'name' => 'string',
'email' => 'string',
'address' => 'string',
'status' => 'integer',
);
}
public function getOrders()
{
return $this->hasMany('Order', array('customer_id' => 'id'))->orderBy('id');
}
public static function active($query)
{
$query->andWhere('status=1');
}
}
<?php
namespace yiiunit\data\ar\elasticsearch;
/**
* Class Item
*
* @property integer $id
* @property string $name
* @property integer $category_id
*/
class Item extends ActiveRecord
{
public static function columns()
{
return array(
'id' => 'integer',
'name' => 'string',
'category_id' => 'integer',
);
}
}
<?php
namespace yiiunit\data\ar\elasticsearch;
/**
* Class Order
*
* @property integer $id
* @property integer $customer_id
* @property integer $create_time
* @property string $total
*/
class Order extends ActiveRecord
{
public static function columns()
{
return array(
'id' => 'integer',
'customer_id' => 'integer',
'create_time' => 'integer',
'total' => 'integer',
);
}
public function getCustomer()
{
return $this->hasOne('Customer', array('id' => 'customer_id'));
}
public function getOrderItems()
{
return $this->hasMany('OrderItem', array('order_id' => 'id'));
}
public function getItems()
{
return $this->hasMany('Item', array('id' => 'item_id'))
->via('orderItems', function ($q) {
// additional query configuration
})->orderBy('id');
}
public function getBooks()
{
return $this->hasMany('Item', array('id' => 'item_id'))
->viaTable('tbl_order_item', array('order_id' => 'id'))
->where(array('category_id' => 1));
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->create_time = time();
return true;
} else {
return false;
}
}
}
<?php
namespace yiiunit\data\ar\elasticsearch;
/**
* Class OrderItem
*
* @property integer $order_id
* @property integer $item_id
* @property integer $quantity
* @property string $subtotal
*/
class OrderItem extends ActiveRecord
{
public static function columns()
{
return array(
'order_id' => 'integer',
'item_id' => 'integer',
'quantity' => 'integer',
'subtotal' => 'integer',
);
}
public function getOrder()
{
return $this->hasOne('Order', array('id' => 'order_id'));
}
public function getItem()
{
return $this->hasOne('Item', array('id' => 'item_id'));
}
}
...@@ -29,5 +29,8 @@ return array( ...@@ -29,5 +29,8 @@ return array(
'password' => 'postgres', 'password' => 'postgres',
'fixture' => __DIR__ . '/postgres.sql', 'fixture' => __DIR__ . '/postgres.sql',
), ),
'elasticsearch' => array(
'dsn' => 'elasticsearch://localhost:9200'
),
), ),
); );
<?php
namespace yiiunit\framework\elasticsearch;
use yii\redis\Connection;
class ElasticSearchConnectionTest extends ElasticSearchTestCase
{
/**
* Empty DSN should throw exception
* @expectedException \yii\base\InvalidConfigException
*/
public function testEmptyDSN()
{
$db = new Connection();
$db->open();
}
/**
* test connection to redis and selection of db
*/
public function testConnect()
{
$db = new Connection();
$db->dsn = 'redis://localhost:6379';
$db->open();
$this->assertTrue($db->ping());
$db->set('YIITESTKEY', 'YIITESTVALUE');
$db->close();
$db = new Connection();
$db->dsn = 'redis://localhost:6379/0';
$db->open();
$this->assertEquals('YIITESTVALUE', $db->get('YIITESTKEY'));
$db->close();
$db = new Connection();
$db->dsn = 'redis://localhost:6379/1';
$db->open();
$this->assertNull($db->get('YIITESTKEY'));
$db->close();
}
public function keyValueData()
{
return array(
array(123),
array(-123),
array(0),
array('test'),
array("test\r\ntest"),
array(''),
);
}
/**
* @dataProvider keyValueData
*/
public function testStoreGet($data)
{
$db = $this->getConnection(true);
$db->set('hi', $data);
$this->assertEquals($data, $db->get('hi'));
}
}
\ No newline at end of file
<?php
namespace yiiunit\framework\elasticsearch;
use yii\elasticsearch\Connection;
use yiiunit\TestCase;
/**
* RedisTestCase is the base class for all redis related test cases
*/
class ElasticSearchTestCase extends TestCase
{
protected function setUp()
{
$this->mockApplication();
$databases = $this->getParam('databases');
$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : null;
if ($params === null || !isset($params['dsn'])) {
$this->markTestSkipped('No elasticsearch server connection configured.');
}
$dsn = explode('/', $params['dsn']);
$host = $dsn[2];
if (strpos($host, ':')===false) {
$host .= ':9200';
}
if(!@stream_socket_client($host, $errorNumber, $errorDescription, 0.5)) {
$this->markTestSkipped('No elasticsearch server running at ' . $params['dsn'] . ' : ' . $errorNumber . ' - ' . $errorDescription);
}
parent::setUp();
}
/**
* @param bool $reset whether to clean up the test database
* @return Connection
*/
public function getConnection($reset = true)
{
$databases = $this->getParam('databases');
$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : array();
$db = new Connection;
if ($reset) {
$db->open();
}
return $db;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment