PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/components/simplesearch/model/simplesearch/driver/libs/Elastica/Transport/Thrift.php

https://gitlab.com/haque.mdmanzurul/nga-loyaltymatters
PHP | 167 lines | 101 code | 24 blank | 42 comment | 13 complexity | 0d5b076b8bcd813140750272088c8851 MD5 | raw file
  1. <?php
  2. namespace Elastica\Transport;
  3. use Elastica\Exception\Connection\ThriftException;
  4. use Elastica\Exception\ResponseException;
  5. use Elastica\Exception\RuntimeException;
  6. use Elastica\Request;
  7. use Elastica\Response;
  8. use Elastica\Connection;
  9. use Elasticsearch\Method;
  10. use Elasticsearch\RestResponse;
  11. use Elasticsearch\RestClient;
  12. use Elasticsearch\RestRequest;
  13. use Thrift\Transport\TSocket;
  14. use Thrift\Transport\TFramedTransport;
  15. use Thrift\Transport\TBufferedTransport;
  16. use Thrift\Protocol\TBinaryProtocolAccelerated;
  17. use Thrift\Exception\TException;
  18. /**
  19. * Elastica Thrift Transport object
  20. *
  21. * @category Xodoa
  22. * @package Elastica
  23. * @author Mikhail Shamin <munk13@gmail.com>
  24. */
  25. class Thrift extends AbstractTransport
  26. {
  27. /**
  28. * @var RestClient[]
  29. */
  30. protected $_clients = array();
  31. /**
  32. * Construct transport
  33. *
  34. * @param \Elastica\Connection $connection Connection object
  35. * @throws \Elastica\Exception\RuntimeException
  36. */
  37. public function __construct(Connection $connection = null)
  38. {
  39. parent::__construct($connection);
  40. if (!class_exists('Elasticsearch\\RestClient')) {
  41. throw new RuntimeException('Elasticsearch\\RestClient class not found. Check that suggested package munkie/elasticsearch-thrift-php is required in composer.json');
  42. }
  43. }
  44. /**
  45. * @param string $host
  46. * @param int $port
  47. * @param int $sendTimeout msec
  48. * @param int $recvTimeout msec
  49. * @param bool $framedTransport
  50. * @return \Elasticsearch\RestClient
  51. */
  52. protected function _createClient($host, $port, $sendTimeout = null, $recvTimeout = null, $framedTransport = false)
  53. {
  54. $socket = new TSocket($host, $port, true);
  55. if (null !== $sendTimeout) {
  56. $socket->setSendTimeout($sendTimeout);
  57. }
  58. if (null !== $recvTimeout) {
  59. $socket->setRecvTimeout($recvTimeout);
  60. }
  61. if ($framedTransport) {
  62. $transport = new TFramedTransport($socket);
  63. } else {
  64. $transport = new TBufferedTransport($socket);
  65. }
  66. $protocol = new TBinaryProtocolAccelerated($transport);
  67. $client = new RestClient($protocol);
  68. $transport->open();
  69. return $client;
  70. }
  71. /**
  72. * @param string $host
  73. * @param int $port
  74. * @param int $sendTimeout
  75. * @param int $recvTimeout
  76. * @param bool $framedTransport
  77. * @return \Elasticsearch\RestClient
  78. */
  79. protected function _getClient($host, $port, $sendTimeout = null, $recvTimeout = null, $framedTransport = false)
  80. {
  81. $key = $host . ':' . $port;
  82. if (!isset($this->_clients[$key])) {
  83. $this->_clients[$key] = $this->_createClient($host, $port, $sendTimeout, $recvTimeout, $framedTransport);
  84. }
  85. return $this->_clients[$key];
  86. }
  87. /**
  88. * Makes calls to the elasticsearch server
  89. *
  90. * @param \Elastica\Request $request
  91. * @param array $params Host, Port, ...
  92. * @throws \Elastica\Exception\Connection\ThriftException
  93. * @throws \Elastica\Exception\ResponseException
  94. * @return \Elastica\Response Response object
  95. */
  96. public function exec(Request $request, array $params)
  97. {
  98. $connection = $this->getConnection();
  99. $sendTimeout = $connection->hasConfig('sendTimeout') ? $connection->getConfig('sendTimeout') : null;
  100. $recvTimeout = $connection->hasConfig('recvTimeout') ? $connection->getConfig('recvTimeout') : null;
  101. $framedTransport = $connection->hasConfig('framedTransport') ? (bool) $connection->getConfig('framedTransport') : false;
  102. try {
  103. $client = $this->_getClient(
  104. $connection->getHost(),
  105. $connection->getPort(),
  106. $sendTimeout,
  107. $recvTimeout,
  108. $framedTransport
  109. );
  110. $restRequest = new RestRequest();
  111. $restRequest->method = array_search($request->getMethod(), Method::$__names);
  112. $restRequest->uri = $request->getPath();
  113. $query = $request->getQuery();
  114. if (!empty($query)) {
  115. $restRequest->parameters = $query;
  116. }
  117. $data = $request->getData();
  118. if (!empty($data)) {
  119. if (is_array($data)) {
  120. $content = json_encode($data);
  121. } else {
  122. $content = $data;
  123. }
  124. $restRequest->body = $content;
  125. }
  126. /* @var $result RestResponse */
  127. $start = microtime(true);
  128. $result = $client->execute($restRequest);
  129. $response = new Response($result->body);
  130. $end = microtime(true);
  131. } catch (TException $e) {
  132. $response = new Response('');
  133. throw new ThriftException($e, $request, $response);
  134. }
  135. if (defined('DEBUG') && DEBUG) {
  136. $response->setQueryTime($end - $start);
  137. }
  138. if ($response->hasError()) {
  139. throw new ResponseException($request, $response);
  140. }
  141. return $response;
  142. }
  143. }