Skip to content

Commit

Permalink
MAGE-838 Add max replicas per index method
Browse files Browse the repository at this point in the history
  • Loading branch information
cammonro committed Jun 24, 2024
1 parent 91b3f88 commit 41bcdec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 8 additions & 1 deletion Api/Product/ReplicaManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface ReplicaManagerInterface

public const SORT_KEY_ATTRIBUTE_NAME = 'attribute';
public const SORT_KEY_VIRTUAL_REPLICA = 'virtualReplica';
// https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#what-are-virtual-replicas
public const MAX_VIRTUAL_REPLICA_LIMIT = 20;


Expand All @@ -40,4 +39,12 @@ public function handleReplicas(string $primaryIndexName, int $storeId, array $pr
* @return bool
*/
public function isReplicaSyncEnabled(int $storeId): bool;

/**
* Return the number of virtual replicas permitted per index
* @link https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#differences
*
* @return int
*/
public function getMaxVirtualReplicasPerIndex() : int;
}
8 changes: 8 additions & 0 deletions Model/Product/ReplicaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,12 @@ public function isReplicaSyncEnabled(int $storeId): bool
{
return $this->configHelper->isInstantEnabled($storeId);
}

/**
* @inheritDoc
*/
public function getMaxVirtualReplicasPerIndex() : int
{
return self::MAX_VIRTUAL_REPLICA_LIMIT;
}
}
11 changes: 8 additions & 3 deletions Validator/VirtualReplicaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ class VirtualReplicaValidator
protected bool $replicaLimitExceeded = false;
protected bool $tooManyCustomerGroups = false;

public function __construct(
protected ReplicaManagerInterface $replicaManager
)
{}

public function isReplicaConfigurationValid(array $replicas): bool
{
foreach ($replicas as $replica) {
// TODO: Implement replica limit override
$maxReplicas = $this->replicaManager->getMaxVirtualReplicasPerIndex();
if (!empty($replica[ReplicaManagerInterface::SORT_KEY_VIRTUAL_REPLICA])) {
$this->replicaCount++;
if ($replica[ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME] == ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE) {
$this->priceSortReplicaCount++;
}
}

if ($this->replicaCount > ReplicaManagerInterface::MAX_VIRTUAL_REPLICA_LIMIT) {
if ($this->replicaCount > $maxReplicas) {
$this->replicaLimitExceeded = true;
$this->tooManyCustomerGroups = $this->priceSortReplicaCount > ReplicaManagerInterface::MAX_VIRTUAL_REPLICA_LIMIT;
$this->tooManyCustomerGroups = $this->priceSortReplicaCount > $maxReplicas;
}
}
return !$this->replicaLimitExceeded;
Expand Down

0 comments on commit 41bcdec

Please sign in to comment.