PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Command/CompareSets.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 126 lines | 87 code | 16 blank | 23 comment | 17 complexity | 89b40193aff3ef24b2f4e31c746edb69 MD5 | raw file
  1. <?php
  2. /**
  3. * Abstract class for union, intersection and diff of sets
  4. *
  5. * @author Ivan Shumkov
  6. * @package Rediska
  7. * @subpackage Commands
  8. * @version 0.5.7
  9. * @link http://rediska.geometria-lab.net
  10. * @license http://www.opensource.org/licenses/bsd-license.php
  11. */
  12. abstract class Rediska_Command_CompareSets extends Rediska_Command_Abstract
  13. {
  14. protected $_storeConnection;
  15. protected $_command;
  16. protected $_storeCommand;
  17. /**
  18. * Create command
  19. *
  20. * @param array $keys Array of key names
  21. * @param string[optional] $storeKey Store to set with key name
  22. * @return Rediska_Connection_Exec
  23. */
  24. public function create(array $keys, $storeKey = null)
  25. {
  26. if (empty($keys)) {
  27. throw new Rediska_Command_Exception('You must specify sets');
  28. }
  29. $connections = array();
  30. $keysByConnections = array();
  31. foreach ($keys as $key) {
  32. $connection = $this->_rediska->getConnectionByKeyName($key);
  33. $connectionAlias = $connection->getAlias();
  34. if (!array_key_exists($connectionAlias, $connections)) {
  35. $connections[$connectionAlias] = $connection;
  36. $keysByConnections[$connectionAlias] = array();
  37. }
  38. $keysByConnections[$connectionAlias][] = $key;
  39. }
  40. if (count($connections) == 1) {
  41. $connectionValues = array_values($connections);
  42. $connection = $connectionValues[0];
  43. if (!is_null($storeKey)) {
  44. $storeConnection = $this->_rediska->getConnectionByKeyName($storeKey);
  45. if ($storeConnection === $connection) {
  46. $command = array($this->_storeCommand,
  47. $this->_rediska->getOption('namespace') . $storeKey);
  48. } else {
  49. $this->setAtomic(false);
  50. $this->_storeConnection = $storeConnection;
  51. $command = array($this->_command);
  52. }
  53. } else {
  54. $command = array($this->_command);
  55. }
  56. $connectionKeys = array_keys($connections);
  57. $connectionAlias = $connectionKeys[0];
  58. foreach($keysByConnections[$connectionAlias] as $key) {
  59. $command[] = $this->_rediska->getOption('namespace') . $key;
  60. }
  61. return new Rediska_Connection_Exec($connection, $command);
  62. } else {
  63. $this->setAtomic(false);
  64. $commands = array();
  65. foreach($keysByConnections as $connectionAlias => $keys) {
  66. foreach ($keys as $key) {
  67. $command = array('SMEMBERS',
  68. $this->_rediska->getOption('namespace') . $key);
  69. $commands[] = new Rediska_Connection_Exec($connections[$connectionAlias], $command);
  70. }
  71. }
  72. return $commands;
  73. }
  74. }
  75. /**
  76. * Parse responses
  77. *
  78. * @param array $responses
  79. * @return boolean|array
  80. */
  81. public function parseResponses($responses)
  82. {
  83. if (!$this->isAtomic()) {
  84. if ($this->_storeConnection) {
  85. $values = $responses[0];
  86. } else {
  87. $values = array_values($this->_compareSets($responses));
  88. }
  89. $unserializedValues = array_map(array($this->_rediska->getSerializer(), 'unserialize'), $values);
  90. if (is_null($this->storeKey)) {
  91. return $unserializedValues;
  92. } else {
  93. $this->_rediska->delete($this->storeKey);
  94. foreach($unserializedValues as $value) {
  95. $this->_rediska->addToSet($this->storeKey, $value);
  96. }
  97. return true;
  98. }
  99. } else {
  100. $reply = $responses[0];
  101. if (is_null($this->storeKey)) {
  102. $reply = array_map(array($this->_rediska->getSerializer(), 'unserialize'), $reply);
  103. } else {
  104. $reply = (boolean)$reply;
  105. }
  106. return $reply;
  107. }
  108. }
  109. abstract protected function _compareSets($sets);
  110. }