PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Bucket.php

https://bitbucket.org/intel352/riiak
PHP | 375 lines | 148 code | 42 blank | 185 comment | 15 complexity | d3f16e55ee5e9cd01b8f226736d807e9 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. namespace riiak;
  3. use \CComponent,
  4. \CJSON,
  5. \Exception,
  6. \Yii;
  7. /**
  8. * The Bucket object allows you to access and change information
  9. * about a Riak bucket, and provides methods to create or retrieve
  10. * objects within the bucket.
  11. * @package riiak
  12. *
  13. * @property array $properties
  14. * @property bool $allowMultiples
  15. * @property int $dw
  16. * @property int $nVal
  17. * @property int $r
  18. * @property int $w
  19. *
  20. * @property-read array $keys
  21. */
  22. class Bucket extends CComponent {
  23. /**
  24. * Client instance
  25. *
  26. * @var \riiak\Riiak
  27. */
  28. public $client;
  29. /**
  30. * Bucket name
  31. *
  32. * @var string
  33. */
  34. public $name;
  35. /**
  36. * R-Value
  37. *
  38. * @var int
  39. */
  40. protected $_r;
  41. /**
  42. * W-Value
  43. *
  44. * @var int
  45. */
  46. protected $_w;
  47. /**
  48. * DW-Value
  49. *
  50. * @var int
  51. */
  52. protected $_dw;
  53. /**
  54. * @var array
  55. */
  56. protected $_properties;
  57. /**
  58. * @var array
  59. */
  60. protected $_keys;
  61. public function __construct(Riiak $client, $name) {
  62. $this->client = $client;
  63. $this->name = $name;
  64. }
  65. /**
  66. * Get the R-Value for this bucket, if set. Falls back to client value.
  67. *
  68. * @param int $r Optional: The R-Value to be returned if not null
  69. * @return int
  70. */
  71. public function getR($r = null) {
  72. if ($r !== null)
  73. return $r;
  74. if ($this->_r !== null)
  75. return $this->_r;
  76. return $this->client->r;
  77. }
  78. /**
  79. * Set the R-value for this bucket.
  80. * get/getBinary operations may use this value
  81. *
  82. * @param int $r The new R-Value
  83. * @return \riiak\Bucket
  84. */
  85. public function setR($r) {
  86. $this->_r = $r;
  87. return $this;
  88. }
  89. /**
  90. * Get the W-Value for this bucket, if set. Falls back to client value.
  91. *
  92. * @param int $w Optional: The W-Value to be returned if not null
  93. * @return int
  94. */
  95. public function getW($w=null) {
  96. if ($w !== null)
  97. return $w;
  98. if ($this->_w !== null)
  99. return $this->_w;
  100. return $this->client->w;
  101. }
  102. /**
  103. * Set the W-Value for this bucket
  104. * get/getBinary operations may use this value
  105. *
  106. * @param int $w The new W-Value
  107. * @return \riiak\Bucket
  108. */
  109. public function setW($w) {
  110. $this->_w = $w;
  111. return $this;
  112. }
  113. /**
  114. * Get the DW-Value for this bucket, if set. Falls back to client value.
  115. *
  116. * @param int $dw Optional: The DW-Value to be returned if not null
  117. * @return int
  118. */
  119. public function getDW($dw=null) {
  120. if ($dw !== null)
  121. return $dw;
  122. if ($this->_dw !== null)
  123. return $this->_dw;
  124. return $this->client->dw;
  125. }
  126. /**
  127. * Set the DW-Value for this bucket
  128. * get/getBinary operations may use this value
  129. *
  130. * @param int $dw The new DW-Value
  131. * @return \riiak\Bucket
  132. */
  133. public function setDW($dw) {
  134. $this->_dw = $dw;
  135. return $this;
  136. }
  137. /**
  138. * Create a new Riak object that will be stored as JSON
  139. *
  140. * @param string $key optional Key value
  141. * @param mixed $data optional Data to store (Default: null)
  142. * @return \riiak\Object
  143. */
  144. public function newObject($key = null, $data = null) {
  145. return $this->newBinary($key, $data, 'application/json', true);
  146. }
  147. /**
  148. * Create a new Riak object that will be stored as Binary
  149. *
  150. * @param string $key optional Key value
  151. * @param mixed $data optional Data to store
  152. * @param string $contentType optional Content type of the object (Default: application/json)
  153. * @param bool $jsonize optional Whether to treat the object as JSON (Default: false)
  154. * @return \riiak\Object
  155. */
  156. public function newBinary($key = null, $data = null, $contentType = 'application/json', $jsonize = false) {
  157. $obj = new Object($this->client, $this, $key);
  158. $obj->data = $data;
  159. $obj->contentType = $contentType;
  160. $obj->jsonize = $jsonize;
  161. return $obj;
  162. }
  163. /**
  164. * Retrieve a JSON-encoded object from Riak
  165. *
  166. * @param string $key Name of the key
  167. * @param int $r optional R-Value of the request (Default: bucket's R)
  168. * @return \riiak\Object
  169. */
  170. public function get($key, $r = null) {
  171. return $this->getBinary($key, $r, true);
  172. }
  173. /**
  174. * Retrieves binary/string object from Riak
  175. *
  176. * @param string $key Name of the key
  177. * @param int $r R-Value of the request (Default: bucket's R)
  178. * @param bool $jsonize Whether to treat the object as JSON (Default: false)
  179. * @return \riiak\Object
  180. */
  181. public function getBinary($key, $r = null, $jsonize = false) {
  182. $obj = new Object($this->client, $this, $key);
  183. $obj->jsonize = $jsonize;
  184. $r = $this->getR($r);
  185. return $obj->reload($r);
  186. }
  187. public function getMulti(array $keys, $r = null) {
  188. return $this->getMultiBinary($keys, $r, true);
  189. }
  190. public function getMultiBinary(array $keys, $r = null, $jsonize = false) {
  191. $bucket = $this;
  192. $client = $this->client;
  193. $objects = array_map(function($key)use($jsonize, $client, $bucket) {
  194. $obj = new Object($client, $bucket, $key);
  195. $obj->jsonize = $jsonize;
  196. return $obj;
  197. }, $keys);
  198. $r = $this->getR($r);
  199. return Object::reloadMulti($client, $objects, $r);
  200. }
  201. /**
  202. * Set N-value for this bucket. Controls number replicas of each object
  203. * that will be written. Set once before writing data to the bucket.
  204. * Should never change this value from the initially used N-Val, otherwise
  205. * unexpected results may occur. Only use if you know what you're doing
  206. *
  207. * @param int $nval The new N-Val
  208. */
  209. public function setNVal($nval) {
  210. $this->setProperty('n_val', $nval);
  211. }
  212. /**
  213. * Retrieve the N-value for this bucket
  214. *
  215. * @return int
  216. */
  217. public function getNVal() {
  218. return $this->getProperty('n_val');
  219. }
  220. /**
  221. * Whether writes can have conflicting data. Detect by calling hasSiblings()
  222. * and getSiblings(). Only use if you know what you are doing
  223. *
  224. * @param bool $bool True to store & return conflicting writes
  225. */
  226. public function setAllowMultiples($bool) {
  227. $this->setProperty('allow_mult', $bool);
  228. }
  229. /**
  230. * Retrieve the 'allow multiples' setting
  231. *
  232. * @return bool
  233. */
  234. public function getAllowMultiples() {
  235. return 'true' == $this->getProperty('allow_mult');
  236. }
  237. /**
  238. * Set a bucket property. Only use if you know what you're doing
  239. *
  240. * @param string $key
  241. * @param mixed $value
  242. */
  243. public function setProperty($key, $value) {
  244. $this->setProperties(array($key => $value));
  245. }
  246. /**
  247. * Retrieve a bucket property
  248. *
  249. * @param string $key The property to retrieve
  250. * @return mixed|null
  251. */
  252. public function getProperty($key) {
  253. $props = $this->getProperties();
  254. if (array_key_exists($key, $props))
  255. return $props[$key];
  256. else
  257. return null;
  258. }
  259. /**
  260. * Set multiple bucket properties in one call. Only use if you know
  261. * what you're doing
  262. *
  263. * @param array $props An associative array of $key=>$value
  264. * @return Bucket
  265. */
  266. public function setProperties(array $props) {
  267. Yii::trace('Setting Bucket properties for bucket "' . $this->name . '"', 'ext.riiak.Bucket');
  268. $this->client->transport->setBucket($this, array('props'=>$props));
  269. $this->_properties = null;
  270. return $this;
  271. }
  272. /**
  273. * Retrieve an associative array of all bucket properties
  274. *
  275. * @param bool $refresh
  276. * @return array
  277. */
  278. public function getProperties($refresh = false) {
  279. if ($refresh || !is_array($this->_properties)) {
  280. Yii::trace('Fetching properties for bucket "' . $this->name . '"', 'ext.riiak.Bucket');
  281. $props = $this->client->transport->listBucketProps($this);
  282. if(empty($props))
  283. return array();
  284. $this->_properties = $props;
  285. }
  286. return $this->_properties;
  287. }
  288. /**
  289. * Retrieve an array of all keys in this bucket
  290. * Note: this operation is pretty slow
  291. *
  292. * @param bool $refresh
  293. * @return array
  294. */
  295. public function getKeys($refresh = false) {
  296. if ($refresh || !is_array($this->_keys)) {
  297. Yii::log('Bucket key listing is a very intensive operation, and should never occur in production!', \CLogger::LEVEL_WARNING);
  298. Yii::trace('Fetching keys for bucket "' . $this->name . '"', 'ext.riiak.Bucket');
  299. $this->_keys = $this->client->transport->listBucketKeys($this);
  300. }
  301. return $this->_keys;
  302. }
  303. /**
  304. * Search a secondary index
  305. * @author Eric Stevens <estevens@taglabsinc.com>
  306. * @param string $name - The name of the index to search
  307. * @param string $type - The type of index ('int' or 'bin')
  308. * @param string|int $startOrExact
  309. * @param string|int $end optional
  310. * @param bool $dedupe - whether to eliminate duplicate entries if any
  311. * @return array of Links
  312. */
  313. public function indexSearch($name, $type, $startOrExact, $end = NULL, $dedupe = false) {
  314. $url = $this->client->transport->buildBucketIndexPath($this, $name . '_' . $type, $startOrExact, $end);
  315. $response = $this->client->transport->get($url);
  316. $obj = Object::populateResponse(new Object($this->client, $this), $response, 'secondaryIndex');
  317. if (!$obj->exists)
  318. throw new Exception('Error searching index.');
  319. $data = $obj->data;
  320. $keys = array_map('urldecode', $data['keys']);
  321. /**
  322. * Combo of array_keys+array_flip is faster than array_unique
  323. */
  324. if ($dedupe)
  325. $keys = array_keys(array_flip($keys));
  326. array_walk($keys, array($this, 'inflateLinkCallback'));
  327. return $keys;
  328. }
  329. protected function inflateLinkCallback(&$key, $k) {
  330. $key = new Link($this->name, $key);
  331. $key->client = $this->client;
  332. }
  333. }