diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Bulk')
8 files changed, 735 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action.php new file mode 100644 index 00000000..7922ec13 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action.php @@ -0,0 +1,219 @@ +<?php + +namespace Elastica\Bulk; + +use Elastica\Bulk; +use Elastica\JSON; +use Elastica\Index; +use Elastica\Type; + +class Action +{ + const OP_TYPE_CREATE = 'create'; + const OP_TYPE_INDEX = 'index'; + const OP_TYPE_DELETE = 'delete'; + const OP_TYPE_UPDATE = 'update'; + + /** + * @var array + */ + public static $opTypes = array( + self::OP_TYPE_CREATE, + self::OP_TYPE_INDEX, + self::OP_TYPE_DELETE, + self::OP_TYPE_UPDATE + ); + + /** + * @var string + */ + protected $_opType; + + /** + * @var array + */ + protected $_metadata = array(); + + /** + * @var array + */ + protected $_source = array(); + + /** + * @param string $opType + * @param array $metadata + * @param array $source + */ + public function __construct($opType = self::OP_TYPE_INDEX, array $metadata = array(), array $source = array()) + { + $this->setOpType($opType); + $this->setMetadata($metadata); + $this->setSource($source); + } + + /** + * @param string $type + * @return \Elastica\Bulk\Action + */ + public function setOpType($type) + { + $this->_opType = $type; + + return $this; + } + + /** + * @return string + */ + public function getOpType() + { + return $this->_opType; + } + + /** + * @param array $metadata + * @return \Elastica\Bulk\Action + */ + public function setMetadata(array $metadata) + { + $this->_metadata = $metadata; + + return $this; + } + + /** + * @return array + */ + public function getMetadata() + { + return $this->_metadata; + } + + /** + * @return array + */ + public function getActionMetadata() + { + return array($this->_opType => $this->getMetadata()); + } + + /** + * @param array $source + * @return \Elastica\Bulk\Action + */ + public function setSource($source) + { + $this->_source = $source; + + return $this; + } + + /** + * @return array + */ + public function getSource() + { + return $this->_source; + } + + /** + * @return bool + */ + public function hasSource() + { + return !empty($this->_source); + } + + /** + * @param string|\Elastica\Index $index + * @return \Elastica\Bulk\Action + */ + public function setIndex($index) + { + if ($index instanceof Index) { + $index = $index->getName(); + } + $this->_metadata['_index'] = $index; + + return $this; + } + + /** + * @param string|\Elastica\Type $type + * @return \Elastica\Bulk\Action + */ + public function setType($type) + { + if ($type instanceof Type) { + $this->setIndex($type->getIndex()->getName()); + $type = $type->getName(); + } + $this->_metadata['_type'] = $type; + + return $this; + } + + /** + * @param string $id + * @return \Elastica\Bulk\Action + */ + public function setId($id) + { + $this->_metadata['_id'] = $id; + + return $this; + } + + /** + * @param string $routing + * @return \Elastica\Bulk\Action + */ + public function setRouting($routing) + { + $this->_metadata['_routing'] = $routing; + + return $this; + } + + /** + * @return array + */ + public function toArray() + { + $data[] = $this->getActionMetadata(); + if ($this->hasSource()) { + $data[] = $this->getSource(); + } + return $data; + } + + /** + * @return string + */ + public function toString() + { + $string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT) . Bulk::DELIMITER; + if ($this->hasSource()) { + $source = $this->getSource(); + if (is_string($source)) { + $string.= $source; + } elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) { + $docAsUpsert = (isset($source['doc_as_upsert'])) ? ', "doc_as_upsert": '.$source['doc_as_upsert'] : ''; + $string.= '{"doc": '.$source['doc'].$docAsUpsert.'}'; + } else { + $string.= JSON::stringify($source, 'JSON_ELASTICSEARCH'); + } + $string.= Bulk::DELIMITER; + } + return $string; + } + + /** + * @param string $opType + * @return bool + */ + public static function isValidOpType($opType) + { + return in_array($opType, self::$opTypes); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/AbstractDocument.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/AbstractDocument.php new file mode 100644 index 00000000..545e695c --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/AbstractDocument.php @@ -0,0 +1,162 @@ +<?php + +namespace Elastica\Bulk\Action; + +use Elastica\AbstractUpdateAction; +use Elastica\Bulk\Action; +use Elastica\Document; +use Elastica\Script; + +abstract class AbstractDocument extends Action +{ + /** + * @var \Elastica\Document|\Elastica\Script + */ + protected $_data; + + /** + * @param \Elastica\Document|\Elastica\Script $document + */ + public function __construct($document) + { + $this->setData($document); + } + + /** + * @param \Elastica\Document $document + * @return \Elastica\Bulk\Action\AbstractDocument + */ + public function setDocument(Document $document) + { + $this->_data = $document; + + $metadata = $this->_getMetadata($document); + + $this->setMetadata($metadata); + + return $this; + } + + /** + * @param \Elastica\Script $script + * @return \Elastica\Bulk\Action\AbstractDocument + */ + public function setScript(Script $script) + { + if (!($this instanceof UpdateDocument)) { + throw new \BadMethodCallException("setScript() can only be used for UpdateDocument"); + } + + $this->_data = $script; + + $metadata = $this->_getMetadata($script); + $this->setMetadata($metadata); + + return $this; + } + + /** + * @param \Elastica\Script|\Elastica\Document $data + * @throws \InvalidArgumentException + * @return \Elastica\Bulk\Action\AbstractDocument + */ + public function setData($data) + { + if ($data instanceof Script) { + + $this->setScript($data); + + }else if ($data instanceof Document) { + + $this->setDocument($data); + + }else{ + throw new \InvalidArgumentException("Data should be a Document or a Script."); + } + + return $this; + } + + /** + * Note: This is for backwards compatibility. + * @return \Elastica\Document + */ + public function getDocument() + { + if ($this->_data instanceof Document) { + return $this->_data; + } + + return null; + } + + /** + * Note: This is for backwards compatibility. + * @return \Elastica\Script + */ + public function getScript() + { + if ($this->_data instanceof Script) { + return $this->_data; + } + + return null; + } + + /** + * @return \Elastica\Document|\Elastica\Script + */ + public function getData() + { + return $this->_data; + } + + /** + * @param \Elastica\AbstractUpdateAction $source + * @return array + */ + abstract protected function _getMetadata(AbstractUpdateAction $source); + + /** + * @param \Elastica\Document|\Elastica\Script $data + * @param string $opType + * @return \Elastica\Bulk\Action\AbstractDocument + */ + public static function create($data, $opType = null) + { + //Check type + if (!($data instanceof Document) && !($data instanceof Script)) { + throw new \InvalidArgumentException("The data needs to be a Document or a Script."); + } + + if (null === $opType && $data->hasOpType()) { + $opType = $data->getOpType(); + } + + //Check that scripts can only be used for updates + if ($data instanceof Script) { + if ($opType === null) { + $opType = self::OP_TYPE_UPDATE; + } else if ($opType != self::OP_TYPE_UPDATE) { + throw new \InvalidArgumentException("Scripts can only be used with the update operation type."); + } + } + + switch ($opType) { + case self::OP_TYPE_DELETE: + $action = new DeleteDocument($data); + break; + case self::OP_TYPE_CREATE: + $action = new CreateDocument($data); + break; + case self::OP_TYPE_UPDATE: + $action = new UpdateDocument($data); + break; + case self::OP_TYPE_INDEX: + default: + $action = new IndexDocument($data); + break; + } + return $action; + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/CreateDocument.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/CreateDocument.php new file mode 100644 index 00000000..ae868b49 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/CreateDocument.php @@ -0,0 +1,11 @@ +<?php + +namespace Elastica\Bulk\Action; + +class CreateDocument extends IndexDocument +{ + /** + * @var string + */ + protected $_opType = self::OP_TYPE_CREATE; +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/DeleteDocument.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/DeleteDocument.php new file mode 100644 index 00000000..572f80b6 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/DeleteDocument.php @@ -0,0 +1,33 @@ +<?php + +namespace Elastica\Bulk\Action; + +use Elastica\AbstractUpdateAction; + +class DeleteDocument extends AbstractDocument +{ + /** + * @var string + */ + protected $_opType = self::OP_TYPE_DELETE; + + /** + * @param \Elastica\AbstractUpdateAction $action + * @return array + */ + protected function _getMetadata(AbstractUpdateAction $action) + { + $params = array( + 'index', + 'type', + 'id', + 'version', + 'version_type', + 'routing', + 'parent' + ); + $metadata = $action->getOptions($params, true); + + return $metadata; + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/IndexDocument.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/IndexDocument.php new file mode 100644 index 00000000..d405563e --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/IndexDocument.php @@ -0,0 +1,52 @@ +<?php + +namespace Elastica\Bulk\Action; + +use Elastica\AbstractUpdateAction; +use Elastica\Bulk\Action; +use Elastica\Document; + +class IndexDocument extends AbstractDocument +{ + /** + * @var string + */ + protected $_opType = self::OP_TYPE_INDEX; + + /** + * @param \Elastica\Document $document + * @return \Elastica\Bulk\Action\IndexDocument + */ + public function setDocument(Document $document) + { + parent::setDocument($document); + + $this->setSource($document->getData()); + + return $this; + } + + /** + * @param \Elastica\AbstractUpdateAction $source + * @return array + */ + protected function _getMetadata(AbstractUpdateAction $action) + { + $params = array( + 'index', + 'type', + 'id', + 'version', + 'version_type', + 'routing', + 'percolate', + 'parent', + 'ttl', + 'timestamp', + 'retry_on_conflict', + ); + $metadata = $action->getOptions($params, true); + + return $metadata; + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/UpdateDocument.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/UpdateDocument.php new file mode 100644 index 00000000..4c1dbaa4 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/UpdateDocument.php @@ -0,0 +1,69 @@ +<?php + +namespace Elastica\Bulk\Action; + +use Elastica\Document; +use Elastica\Script; + +/** + * @package Elastica\Bulk\Action + * @link http://www.elasticsearch.org/guide/reference/api/bulk/ + */ +class UpdateDocument extends IndexDocument +{ + /** + * @var string + */ + protected $_opType = self::OP_TYPE_UPDATE; + + /** + * Set the document for this bulk update action. + * @param \Elastica\Document $document + * @return \Elastica\Bulk\Action\UpdateDocument + */ + public function setDocument(Document $document) + { + parent::setDocument($document); + + $source = array('doc' => $document->getData()); + + if ($document->getDocAsUpsert()) { + $source['doc_as_upsert'] = true; + + }else if ($document->hasUpsert()) { + + $upsert = $document->getUpsert()->getData(); + + if (!empty($upsert)) { + $source['upsert'] = $upsert; + } + } + + $this->setSource($source); + + return $this; + } + + /** + * @param \Elastica\Script $script + * @return \Elastica\Bulk\Action\AbstractDocument + */ + public function setScript(Script $script) + { + parent::setScript($script); + + $source = $script->toArray(); + + if ($script->hasUpsert()) { + $upsert = $script->getUpsert()->getData(); + + if (!empty($upsert)) { + $source['upsert'] = $upsert; + } + } + + $this->setSource($source); + + return $this; + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/Response.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/Response.php new file mode 100644 index 00000000..5abd1850 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/Response.php @@ -0,0 +1,47 @@ +<?php + +namespace Elastica\Bulk; + +use Elastica\Response as BaseResponse; + +class Response extends BaseResponse +{ + /** + * @var \Elastica\Bulk\Action + */ + protected $_action; + + /** + * @var string + */ + protected $_opType; + + /** + * @param array|string $responseData + * @param \Elastica\Bulk\Action $action + * @param string $opType + */ + public function __construct($responseData, Action $action, $opType) + { + parent::__construct($responseData); + + $this->_action = $action; + $this->_opType = $opType; + } + + /** + * @return \Elastica\Bulk\Action + */ + public function getAction() + { + return $this->_action; + } + + /** + * @return string + */ + public function getOpType() + { + return $this->_opType; + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Bulk/ResponseSet.php b/vendor/ruflin/elastica/lib/Elastica/Bulk/ResponseSet.php new file mode 100644 index 00000000..9fd835e4 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Bulk/ResponseSet.php @@ -0,0 +1,142 @@ +<?php + +namespace Elastica\Bulk; + +use Elastica\Response as BaseResponse; + +class ResponseSet extends BaseResponse implements \Iterator, \Countable +{ + /** + * @var \Elastica\Bulk\Response[] + */ + protected $_bulkResponses = array(); + + /** + * @var int + */ + protected $_position = 0; + + /** + * @param \Elastica\Response $response + * @param \Elastica\Bulk\Response[] $bulkResponses + */ + public function __construct(BaseResponse $response, array $bulkResponses) + { + parent::__construct($response->getData()); + + $this->_bulkResponses = $bulkResponses; + } + + /** + * @return \Elastica\Bulk\Response[] + */ + public function getBulkResponses() + { + return $this->_bulkResponses; + } + + /** + * Returns first found error + * + * @return string + */ + public function getError() + { + $error = ''; + + foreach ($this->getBulkResponses() as $bulkResponse) { + if ($bulkResponse->hasError()) { + $error = $bulkResponse->getError(); + break; + } + } + + return $error; + } + + /** + * @return bool + */ + public function isOk() + { + $return = true; + + foreach ($this->getBulkResponses() as $bulkResponse) { + if (!$bulkResponse->isOk()) { + $return = false; + break; + } + } + + return $return; + } + + /** + * @return bool + */ + public function hasError() + { + $return = false; + + foreach ($this->getBulkResponses() as $bulkResponse) { + if ($bulkResponse->hasError()) { + $return = true; + break; + } + } + + return $return; + } + + /** + * @return bool|\Elastica\Bulk\Response + */ + public function current() + { + if ($this->valid()) { + return $this->_bulkResponses[$this->key()]; + } else { + return false; + } + } + + /** + * + */ + public function next() + { + $this->_position++; + } + + /** + * @return int + */ + public function key() + { + return $this->_position; + } + + /** + * @return bool + */ + public function valid() + { + return isset($this->_bulkResponses[$this->key()]); + } + + /** + * + */ + public function rewind() + { + $this->_position = 0; + } + + /** + * @return int + */ + public function count() + { + return count($this->_bulkResponses); + } +} |