|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright MacFJA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 14 | + * Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace MacFJA\RediSearch\Redis\Command\CreateCommand; |
| 23 | + |
| 24 | +use function in_array; |
| 25 | +use InvalidArgumentException; |
| 26 | +use function is_int; |
| 27 | +use MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption; |
| 28 | +use MacFJA\RediSearch\Redis\Command\Option\GroupedOption; |
| 29 | +use MacFJA\RediSearch\Redis\Command\Option\NamedOption; |
| 30 | +use MacFJA\RediSearch\Redis\Command\Option\NamelessOption; |
| 31 | +use MacFJA\RediSearch\Redis\Command\Option\OptionListOption; |
| 32 | +use MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait; |
| 33 | +use OutOfRangeException; |
| 34 | +use RuntimeException; |
| 35 | + |
| 36 | +/** |
| 37 | + * @method VectorFieldOption setField(string $name) |
| 38 | + * @method VectorFieldOption setNoIndex(bool $active) |
| 39 | + * @method VectorFieldOption setAlgorithm(string $algorithm) |
| 40 | + */ |
| 41 | +class VectorFieldOption extends GroupedOption implements CreateCommandFieldOption |
| 42 | +{ |
| 43 | + use BaseCreateFieldOptionTrait; |
| 44 | + use WithPublicGroupedSetterTrait; |
| 45 | + |
| 46 | + public const ALGORITHM_FLAT = 'FLAT'; |
| 47 | + public const ALGORITHM_HNSW = 'HNSW'; |
| 48 | + public const TYPE_FLOAT32 = 'FLOAT32'; |
| 49 | + public const DISTANCE_METRIC_L2 = 'L2'; |
| 50 | + public const DISTANCE_METRIC_IP = 'IP'; |
| 51 | + public const DISTANCE_METRIC_COSINE = 'COSINE'; |
| 52 | + private const TYPES = [self::TYPE_FLOAT32]; |
| 53 | + private const DISTANCE_METRICS = [self::DISTANCE_METRIC_IP, self::DISTANCE_METRIC_COSINE, self::DISTANCE_METRIC_L2]; |
| 54 | + |
| 55 | + public function __construct() |
| 56 | + { |
| 57 | + parent::__construct($this->getConstructorOptions('VECTOR', [ |
| 58 | + 'algorithm' => CustomValidatorOption::allowedValues(new NamelessOption(null, '>=2.4.0'), [self::ALGORITHM_FLAT, self::ALGORITHM_HNSW]), |
| 59 | + 'count' => CustomValidatorOption::isNumeric(new NamelessOption(null, '>=2.4.0')), |
| 60 | + 'attributes' => new OptionListOption('>=2.4.0'), |
| 61 | + ]), ['field', 'algorithm', 'count', 'type'], ['type'], '>=2.4.0'); |
| 62 | + } |
| 63 | + |
| 64 | + public function getFieldName(): string |
| 65 | + { |
| 66 | + return $this->getDataOfOption('field'); |
| 67 | + } |
| 68 | + |
| 69 | + public function addFlatAttribute(string $type, int $dimension, string $distanceMetric, ?int $initialCapacity = null, ?int $blockSize = null): self |
| 70 | + { |
| 71 | + if (!(self::ALGORITHM_FLAT === $this->getDataOfOption('algorithm'))) { |
| 72 | + throw new RuntimeException(); |
| 73 | + } |
| 74 | + |
| 75 | + return $this->addAttribute($type, $dimension, $distanceMetric, $initialCapacity, $blockSize); |
| 76 | + } |
| 77 | + |
| 78 | + public function addHnswAttribute(string $type, int $dimension, string $distanceMetric, ?int $initialCapacity = null, ?int $maxEdge = null, ?int $efConstruction = null, ?int $efRuntime = null): self |
| 79 | + { |
| 80 | + if (!(self::ALGORITHM_HNSW === $this->getDataOfOption('algorithm'))) { |
| 81 | + throw new RuntimeException(); |
| 82 | + } |
| 83 | + |
| 84 | + return $this->addAttribute($type, $dimension, $distanceMetric, $initialCapacity, null, $maxEdge, $efConstruction, $efRuntime); |
| 85 | + } |
| 86 | + |
| 87 | + public function addAttribute(string $type, int $dimension, string $distanceMetric, ?int $initialCapacity = null, ?int $blockSize = null, ?int $maxEdge = null, ?int $efConstruction = null, ?int $efRuntime = null): self |
| 88 | + { |
| 89 | + $algorithm = $this->getDataOfOption('algorithm'); |
| 90 | + |
| 91 | + if (self::ALGORITHM_FLAT === $algorithm) { |
| 92 | + $efRuntime = null; |
| 93 | + $efConstruction = null; |
| 94 | + $maxEdge = null; |
| 95 | + } |
| 96 | + if (self::ALGORITHM_HNSW === $algorithm) { |
| 97 | + $blockSize = null; |
| 98 | + } |
| 99 | + |
| 100 | + $this->validateArguments($type, $dimension, $distanceMetric, $initialCapacity, $blockSize, $maxEdge, $efConstruction, $efRuntime); |
| 101 | + |
| 102 | + $attribute = new GroupedOption([ |
| 103 | + 'type' => new NamedOption('TYPE', $type, '>=2.4.0'), |
| 104 | + 'dim' => new NamedOption('DIM', $dimension, '>=2.4.0'), |
| 105 | + 'distance_metric' => new NamedOption('DISTANCE_METRIC', $distanceMetric, '>=2.4.0'), |
| 106 | + 'initial_cap' => new NamedOption('INITIAL_CAP', $initialCapacity, '>=2.4.0'), |
| 107 | + 'block_size' => new NamedOption('BLOCK_SIZE', $blockSize, '>=2.4.0'), |
| 108 | + 'm' => new NamedOption('M', $maxEdge, '>=2.4.0'), |
| 109 | + 'ef_construction' => new NamedOption('EF_CONSTRUCTION', $efConstruction, '>=2.4.0'), |
| 110 | + 'ef_runtime' => new NamedOption('EF_RUNTIME', $efRuntime, '>=2.4.0'), |
| 111 | + ], ['type', 'dim', 'distance_metric'], ['type', 'dim', 'distance_metric', 'initial_cap', 'block_size', 'm', 'ef_construction', 'ef_runtime']); |
| 112 | + |
| 113 | + /** @var array<GroupedOption> $options */ |
| 114 | + $options = $this->getDataOfOption('attributes'); |
| 115 | + $options[] = $attribute; |
| 116 | + $this->setDataOfOption('attributes', ...$options); |
| 117 | + |
| 118 | + $countAddition = 6 |
| 119 | + + (is_int($initialCapacity) ? 2 : 0) |
| 120 | + + (is_int($blockSize) ? 2 : 0) |
| 121 | + + (is_int($maxEdge) ? 2 : 0) |
| 122 | + + (is_int($efConstruction) ? 2 : 0) |
| 123 | + + (is_int($efRuntime) ? 2 : 0) |
| 124 | + ; |
| 125 | + |
| 126 | + /** @var int $count */ |
| 127 | + $count = $this->getDataOfOption('count') ?? 0; |
| 128 | + $count += $countAddition; |
| 129 | + $this->setDataOfOption('count', $count); |
| 130 | + |
| 131 | + return $this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return string[] |
| 136 | + */ |
| 137 | + protected function publicSetter(): array |
| 138 | + { |
| 139 | + return ['field', 'algorithm', 'no_index']; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) -- Dynamic variable name |
| 144 | + */ |
| 145 | + private function validateArguments(string $type, int $dimension, string $distanceMetric, ?int $initialCapacity = null, ?int $blockSize = null, ?int $maxEdge = null, ?int $efConstruction = null, ?int $efRuntime = null): void |
| 146 | + { |
| 147 | + $atLeastOne = ['dimension', 'initialCapacity', 'blockSize', 'efConstruction', 'efRuntime']; |
| 148 | + |
| 149 | + if (!in_array($type, self::TYPES, true)) { |
| 150 | + throw new InvalidArgumentException(sprintf( |
| 151 | + '"%s" is not a known type (expect one of %s)', |
| 152 | + $type, |
| 153 | + implode(', ', self::TYPES) |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + foreach ($atLeastOne as $varName) { |
| 158 | + if (is_int(${$varName}) && ${$varName} < 1) { |
| 159 | + throw new OutOfRangeException(sprintf('$%s must be greater than 0', $varName)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if (!in_array($distanceMetric, self::DISTANCE_METRICS, true)) { |
| 164 | + throw new InvalidArgumentException(sprintf( |
| 165 | + '"%s" is not a known distance metric (expect one of %s)', |
| 166 | + $distanceMetric, |
| 167 | + implode(', ', self::DISTANCE_METRICS) |
| 168 | + )); |
| 169 | + } |
| 170 | + |
| 171 | + if (is_int($maxEdge) && $maxEdge < 0) { |
| 172 | + throw new OutOfRangeException('$maxEdge must be a positive number'); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments