Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ShallowCompactor | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| compactRow | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| compactRows | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getShallow | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResultDuplicator | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| expandCacheResult | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Data\Compactor; |
| 4 | |
| 5 | use Flow\Data\Compactor; |
| 6 | use Flow\Data\Index\UniqueFeatureIndex; |
| 7 | use Flow\Data\Utils\ResultDuplicator; |
| 8 | |
| 9 | /** |
| 10 | * Backs an index with a UniqueFeatureIndex. This index will store only the primary key |
| 11 | * values from the unique index, and on retrieval from cache will materialize the primary key |
| 12 | * values into full rows from the unique index. |
| 13 | */ |
| 14 | class ShallowCompactor implements Compactor { |
| 15 | /** |
| 16 | * @var Compactor |
| 17 | */ |
| 18 | protected $inner; |
| 19 | |
| 20 | /** |
| 21 | * @var UniqueFeatureIndex |
| 22 | */ |
| 23 | protected $shallow; |
| 24 | |
| 25 | /** |
| 26 | * @var string[] |
| 27 | */ |
| 28 | protected $sort; |
| 29 | |
| 30 | /** |
| 31 | * @param Compactor $inner |
| 32 | * @param UniqueFeatureIndex $shallow |
| 33 | * @param string[] $sortedColumns |
| 34 | */ |
| 35 | public function __construct( Compactor $inner, UniqueFeatureIndex $shallow, array $sortedColumns ) { |
| 36 | $this->inner = $inner; |
| 37 | $this->shallow = $shallow; |
| 38 | $this->sort = $sortedColumns; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param array $row |
| 43 | * @return array |
| 44 | */ |
| 45 | public function compactRow( array $row ) { |
| 46 | $keys = array_merge( $this->shallow->getPrimaryKeyColumns(), $this->sort ); |
| 47 | $extra = array_diff( array_keys( $row ), $keys ); |
| 48 | foreach ( $extra as $key ) { |
| 49 | unset( $row[$key] ); |
| 50 | } |
| 51 | return $this->inner->compactRow( $row ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param array $rows |
| 56 | * @return array |
| 57 | */ |
| 58 | public function compactRows( array $rows ) { |
| 59 | return array_map( [ $this, 'compactRow' ], $rows ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return UniqueFeatureIndex |
| 64 | */ |
| 65 | public function getShallow() { |
| 66 | return $this->shallow; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param array $cached |
| 71 | * @param array $keyToQuery |
| 72 | * @return ResultDuplicator |
| 73 | */ |
| 74 | public function getResultDuplicator( array $cached, array $keyToQuery ) { |
| 75 | $results = $this->inner->expandCacheResult( $cached, $keyToQuery ); |
| 76 | // Allows us to flatten $results into a single $query array, then |
| 77 | // rebuild final return value in same structure and order as $results. |
| 78 | $duplicator = new ResultDuplicator( $this->shallow->getPrimaryKeyColumns(), 2 ); |
| 79 | foreach ( $results as $i => $rows ) { |
| 80 | foreach ( $rows as $j => $row ) { |
| 81 | $duplicator->add( $row, [ $i, $j ] ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return $duplicator; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param array $cached |
| 90 | * @param array $keyToQuery |
| 91 | * @return array |
| 92 | */ |
| 93 | public function expandCacheResult( array $cached, array $keyToQuery ) { |
| 94 | $duplicator = $this->getResultDuplicator( $cached, $keyToQuery ); |
| 95 | $queries = $duplicator->getUniqueQueries(); |
| 96 | $innerResult = $this->shallow->findMulti( $queries ); |
| 97 | |
| 98 | foreach ( $innerResult as $rows ) { |
| 99 | // __construct guaranteed the shallow backing index is a unique, so $first is only result |
| 100 | $first = reset( $rows ); |
| 101 | $duplicator->merge( $first, $first ); |
| 102 | } |
| 103 | |
| 104 | return $duplicator->getResult(); |
| 105 | } |
| 106 | } |