Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimenting with result formatters and MapReduce
- Loading branch information
Showing
5 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\ORM; | ||
|
||
use \IteratorAggregate; | ||
use \ArrayIterator; | ||
|
||
class MapReduce implements IteratorAggregate { | ||
|
||
protected $_intermediate = []; | ||
|
||
protected $_result = []; | ||
|
||
protected $_executed = false; | ||
|
||
protected $_data; | ||
|
||
protected $_mapper; | ||
|
||
protected $_reducer; | ||
|
||
protected $_counter = 0; | ||
|
||
public function __construct($data, callable $mapper, callable $reducer) { | ||
$this->_data = $data; | ||
$this->_mapper = $mapper; | ||
$this->_reducer = $reducer; | ||
} | ||
|
||
public function getIterator() { | ||
if (!$this->_executed) { | ||
$this->_execute(); | ||
} | ||
return new ArrayIterator($this->_result); | ||
} | ||
|
||
public function emitIntermediate($key, $value) { | ||
$this->_intermediate[$key][] = $value; | ||
} | ||
|
||
public function emit($value, $slot = null) { | ||
$this->_result[$slot === null ? $this->_counter : $slot] = $value; | ||
} | ||
|
||
protected function _execute() { | ||
foreach ($this->_data as $key => $value) { | ||
$this->_mapper->__invoke($key, $value, $this); | ||
} | ||
|
||
foreach ($this->_intermediate as $key => $list) { | ||
$this->_reducer->__invoke($key, $list, $this); | ||
$this->_counter++; | ||
} | ||
$this->_execute = true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\ORM; | ||
|
||
use \IteratorIterator; | ||
use \Iterator; | ||
|
||
class ResultSetDecorator implements Iterator { | ||
|
||
protected $_keyCallback; | ||
|
||
protected $_currentCallback; | ||
|
||
protected $_results; | ||
|
||
protected $_current; | ||
|
||
public function __construct(\Traversable $results, $callbacks = []) { | ||
if (!empty($callbacks['key'])) { | ||
$this->_keyCallback = $callbacks['key']; | ||
} | ||
if (!empty($callbacks['current'])) { | ||
$this->_currentCallback = $callbacks['current']; | ||
} | ||
$this->_results = $results; | ||
|
||
if ($results instanceOf \IteratorAggregate) { | ||
$this->_results = $results->getIterator(); | ||
} | ||
} | ||
|
||
public function key() { | ||
$key = $this->_results->key(); | ||
if ($this->_keyCallback) { | ||
$current = $this->_results->current(); | ||
$key = $this->_keyCallback->__invoke($current, $key); | ||
} | ||
return $key; | ||
} | ||
|
||
public function current() { | ||
if ($this->_current) { | ||
return $this->_current; | ||
} | ||
$current = $this->_results->current(); | ||
if ($current && $this->_currentCallback) { | ||
$current = $this->_currentCallback->__invoke($current); | ||
} | ||
return $current; | ||
} | ||
|
||
public function next() { | ||
$this->_current = null; | ||
$this->_results->next(); | ||
} | ||
|
||
public function rewind() { | ||
$this->_current = null; | ||
$this->_results->rewind(); | ||
} | ||
|
||
public function valid() { | ||
return $this->_results->valid(); | ||
} | ||
|
||
public function toArray() { | ||
return iterator_to_array($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters