Node.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Node\Info;
  4. use Elastica\Node\Stats;
  5. /**
  6. * Elastica cluster node object.
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. */
  10. class Node
  11. {
  12. /**
  13. * Client.
  14. *
  15. * @var \Elastica\Client
  16. */
  17. protected $_client;
  18. /**
  19. * @var string Unique node id
  20. */
  21. protected $_id;
  22. /**
  23. * Node name.
  24. *
  25. * @var string Node name
  26. */
  27. protected $_name;
  28. /**
  29. * Node stats.
  30. *
  31. * @var \Elastica\Node\Stats|null Node Stats
  32. */
  33. protected $_stats;
  34. /**
  35. * Node info.
  36. *
  37. * @var \Elastica\Node\Info|null Node info
  38. */
  39. protected $_info;
  40. /**
  41. * Create a new node object.
  42. *
  43. * @param string $id Node id or name
  44. * @param \Elastica\Client $client Node object
  45. */
  46. public function __construct($id, Client $client)
  47. {
  48. $this->_client = $client;
  49. $this->setId($id);
  50. }
  51. /**
  52. * @return string Unique node id. Can also be name if id not exists.
  53. */
  54. public function getId()
  55. {
  56. return $this->_id;
  57. }
  58. /**
  59. * @param string $id Node id
  60. *
  61. * @return $this Refreshed object
  62. */
  63. public function setId($id)
  64. {
  65. $this->_id = $id;
  66. return $this->refresh();
  67. }
  68. /**
  69. * Get the name of the node.
  70. *
  71. * @return string Node name
  72. */
  73. public function getName()
  74. {
  75. if (empty($this->_name)) {
  76. $this->_name = $this->getInfo()->getName();
  77. }
  78. return $this->_name;
  79. }
  80. /**
  81. * Returns the current client object.
  82. *
  83. * @return \Elastica\Client Client
  84. */
  85. public function getClient()
  86. {
  87. return $this->_client;
  88. }
  89. /**
  90. * Return stats object of the current node.
  91. *
  92. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html
  93. *
  94. * @return \Elastica\Node\Stats Node stats
  95. */
  96. public function getStats()
  97. {
  98. if (!$this->_stats) {
  99. $this->_stats = new Stats($this);
  100. }
  101. return $this->_stats;
  102. }
  103. /**
  104. * Return info object of the current node.
  105. *
  106. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
  107. *
  108. * @return \Elastica\Node\Info Node info object
  109. */
  110. public function getInfo()
  111. {
  112. if (!$this->_info) {
  113. $this->_info = new Info($this);
  114. }
  115. return $this->_info;
  116. }
  117. /**
  118. * Refreshes all node information.
  119. *
  120. * This should be called after updating a node to refresh all information
  121. */
  122. public function refresh()
  123. {
  124. $this->_stats = null;
  125. $this->_info = null;
  126. }
  127. }