PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/nmred/kafka-php/src/Kafka/Protocol/Fetch/Partition.php

https://gitlab.com/link233/bootmw
PHP | 374 lines | 108 code | 51 blank | 215 comment | 5 complexity | 1c9d304e05cfe8a80ee0e1e8b5bdbb28 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3. // +---------------------------------------------------------------------------
  4. // | SWAN [ $_SWANBR_SLOGAN_$ ]
  5. // +---------------------------------------------------------------------------
  6. // | Copyright $_SWANBR_COPYRIGHT_$
  7. // +---------------------------------------------------------------------------
  8. // | Version $_SWANBR_VERSION_$
  9. // +---------------------------------------------------------------------------
  10. // | Licensed ( $_SWANBR_LICENSED_URL_$ )
  11. // +---------------------------------------------------------------------------
  12. // | $_SWANBR_WEB_DOMAIN_$
  13. // +---------------------------------------------------------------------------
  14. namespace Kafka\Protocol\Fetch;
  15. use \Kafka\Protocol\Decoder;
  16. /**
  17. +------------------------------------------------------------------------------
  18. * Kafka protocol since Kafka v0.8
  19. +------------------------------------------------------------------------------
  20. *
  21. * @package
  22. * @version $_SWANBR_VERSION_$
  23. * @copyright Copyleft
  24. * @author $_SWANBR_AUTHOR_$
  25. +------------------------------------------------------------------------------
  26. */
  27. class Partition implements \Iterator, \Countable
  28. {
  29. // {{{ members
  30. /**
  31. * kafka socket object
  32. *
  33. * @var mixed
  34. * @access private
  35. */
  36. private $stream = null;
  37. /**
  38. * validCount
  39. *
  40. * @var float
  41. * @access private
  42. */
  43. private $validCount = 0;
  44. /**
  45. * partitions count
  46. *
  47. * @var float
  48. * @access private
  49. */
  50. private $partitionCount = false;
  51. /**
  52. * current topic
  53. *
  54. * @var mixed
  55. * @access private
  56. */
  57. private $current = null;
  58. /**
  59. * current iterator key
  60. * partition id
  61. *
  62. * @var string
  63. * @access private
  64. */
  65. private $key = null;
  66. /**
  67. * partition errCode
  68. *
  69. * @var float
  70. * @access private
  71. */
  72. private $errCode = 0;
  73. /**
  74. * partition offset
  75. *
  76. * @var float
  77. * @access private
  78. */
  79. private $offset = 0;
  80. /**
  81. * partition current fetch offset
  82. *
  83. * @var float
  84. * @access private
  85. */
  86. private $currentOffset = 0;
  87. /**
  88. * valid
  89. *
  90. * @var mixed
  91. * @access private
  92. */
  93. private $valid = false;
  94. /**
  95. * current topic name
  96. *
  97. * @var string
  98. * @access private
  99. */
  100. private $topicName = '';
  101. /**
  102. * request fetch context
  103. *
  104. * @var array
  105. */
  106. private $context = array();
  107. // }}}
  108. // {{{ functions
  109. // {{{ public function __construct()
  110. /**
  111. * __construct
  112. *
  113. * @param \Kafka\Protocol\Fetch\Topic $topic
  114. * @param array $context
  115. * @access public
  116. */
  117. public function __construct(\Kafka\Protocol\Fetch\Topic $topic, $context = array())
  118. {
  119. $this->stream = $topic->getStream();
  120. $this->topicName = $topic->key();
  121. $this->context = $context;
  122. $this->partitionCount = $this->getPartitionCount();
  123. }
  124. // }}}
  125. // {{{ public function current()
  126. /**
  127. * current
  128. *
  129. * @access public
  130. * @return mixed
  131. */
  132. public function current()
  133. {
  134. return $this->current;
  135. }
  136. // }}}
  137. // {{{ public function key()
  138. /**
  139. * key
  140. *
  141. * @access public
  142. * @return string
  143. */
  144. public function key()
  145. {
  146. return $this->key;
  147. }
  148. // }}}
  149. // {{{ public function rewind()
  150. /**
  151. * implements Iterator function
  152. *
  153. * @access public
  154. * @return integer
  155. */
  156. public function rewind()
  157. {
  158. $this->valid = $this->loadNextPartition();
  159. }
  160. // }}}
  161. // {{{ public function valid()
  162. /**
  163. * implements Iterator function
  164. *
  165. * @access public
  166. * @return integer
  167. */
  168. public function valid()
  169. {
  170. return $this->valid && $this->validCount <= $this->partitionCount;
  171. }
  172. // }}}
  173. // {{{ public function next()
  174. /**
  175. * implements Iterator function
  176. *
  177. * @access public
  178. * @return void
  179. */
  180. public function next()
  181. {
  182. $this->valid = $this->loadNextPartition();
  183. }
  184. // }}}
  185. // {{{ public function count()
  186. /**
  187. * implements Countable function
  188. *
  189. * @access public
  190. * @return integer
  191. */
  192. public function count()
  193. {
  194. return $this->partitionCount;
  195. }
  196. // }}}
  197. // {{{ public function getErrCode()
  198. /**
  199. * get partition errcode
  200. *
  201. * @access public
  202. * @return float
  203. */
  204. public function getErrCode()
  205. {
  206. return $this->errCode;
  207. }
  208. // }}}
  209. // {{{ public function getHighOffset()
  210. /**
  211. * get partition high offset
  212. *
  213. * @access public
  214. * @return float
  215. */
  216. public function getHighOffset()
  217. {
  218. return $this->offset;
  219. }
  220. // }}}
  221. // {{{ public function getTopicName()
  222. /**
  223. * get partition topic name
  224. *
  225. * @access public
  226. * @return string
  227. */
  228. public function getTopicName()
  229. {
  230. return $this->topicName;
  231. }
  232. // }}}
  233. // {{{ public function getStream()
  234. /**
  235. * get current stream
  236. *
  237. * @access public
  238. * @return \Kafka\Socket
  239. */
  240. public function getStream()
  241. {
  242. return $this->stream;
  243. }
  244. // }}}
  245. // {{{ protected function getPartitionCount()
  246. /**
  247. * get message size
  248. * only use to object init
  249. *
  250. * @access protected
  251. * @return integer
  252. */
  253. protected function getPartitionCount()
  254. {
  255. // read topic count
  256. $data = $this->stream->read(4, true);
  257. $data = Decoder::unpack(Decoder::BIT_B32, $data);
  258. $count = array_shift($data);
  259. if ($count <= 0) {
  260. throw new \Kafka\Exception\OutOfRange($count . ' is not a valid partition count');
  261. }
  262. return $count;
  263. }
  264. // }}}
  265. // {{{ public function loadNextPartition()
  266. /**
  267. * load next partition
  268. *
  269. * @access public
  270. * @return bool
  271. */
  272. public function loadNextPartition()
  273. {
  274. if ($this->validCount >= $this->partitionCount) {
  275. return false;
  276. }
  277. try {
  278. $partitionId = $this->stream->read(4, true);
  279. $partitionId = Decoder::unpack(Decoder::BIT_B32, $partitionId);
  280. $partitionId = array_shift($partitionId);
  281. \Kafka\Log::log("kafka client:fetch partition:" . $partitionId, LOG_INFO);
  282. $errCode = $this->stream->read(2, true);
  283. $errCode = Decoder::unpack(Decoder::BIT_B16, $errCode);
  284. $this->errCode = array_shift($errCode);
  285. if ($this->errCode != 0) {
  286. throw new \Kafka\Exception(\Kafka\Protocol\Decoder::getError($this->errCode));
  287. }
  288. $offset = $this->stream->read(8, true);
  289. $this->offset = \Kafka\Protocol\Decoder::unpack(Decoder::BIT_B64, $offset);
  290. $this->key = $partitionId;
  291. $this->current = new MessageSet($this, $this->context);
  292. } catch (\Kafka\Exception $e) {
  293. return false;
  294. }
  295. $this->validCount++;
  296. return true;
  297. }
  298. // }}}
  299. // {{{ public function setMessageOffset()
  300. /**
  301. * set messageSet fetch offset current
  302. *
  303. * @param int $offset
  304. * @return void
  305. */
  306. public function setMessageOffset($offset)
  307. {
  308. $this->currentOffset = $offset;
  309. }
  310. // }}}
  311. // {{{ public function getMessageOffset()
  312. /**
  313. * get messageSet fetch offset current
  314. *
  315. * @return int
  316. */
  317. public function getMessageOffset()
  318. {
  319. return $this->currentOffset;
  320. }
  321. // }}}
  322. // }}}
  323. }