Lines 97.72% 43 / 44
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 add 100.00% 11 / 11 100.00% 1 / 1 2
 remove 100.00% 12 / 12 100.00% 1 / 1 4
 findMatching 92.30% 12 / 13 0.00% 0 / 1 6.02
 getRoutingStats 100.00% 7 / 7 100.00% 1 / 1 1
 buildCacheKey 100.00% 1 / 1 100.00% 1 / 1 1
13final class SelectorIndex
14{
15    private const int CACHE_LIMIT = 10_000;
16
17    /** @var array<string, IndexedSelector> */
18    private array $entries = [];
19
20    /** @var array<string, IndexedSelector> */
21    private array $patterns = [];
22
23    /** @var array<string, array<string, SubscriptionId>> */
24    private array $exact = [];
25
26    /** @var array<string, SubscriptionIdCollection> */
27    private array $cache = [];
28
29    private int $cacheHits = 0;
30
31    private int $cacheMisses = 0;
32
33    public function add(SubscriptionId $subscriptionId, SubscriptionKind $kind, Selector $selector): void
34    {
35        $this->remove($subscriptionId);
36
37        $entry = new IndexedSelector($subscriptionId, $kind, $selector);
38        $this->entries[$subscriptionId->value] = $entry;
39        $exactKey = $selector->findExactPattern();
40
41        if ($exactKey === null) {
42            $this->patterns[$subscriptionId->value] = $entry;
43            $this->cache = [];
44
45            return;
46        }
47
48        $cacheKey = $this->buildCacheKey($kind, $exactKey);
49        $this->exact[$cacheKey][$subscriptionId->value] = $subscriptionId;
50        unset($this->cache[$cacheKey]);
51    }
52
53    public function remove(SubscriptionId $subscriptionId): void
54    {
55        $entry = $this->entries[$subscriptionId->value] ?? null;
56
57        if ($entry === null) {
58            return;
59        }
60
61        unset($this->entries[$subscriptionId->value], $this->patterns[$subscriptionId->value]);
62
63        $exactKey = $entry->selector->findExactPattern();
64
65        if ($exactKey === null) {
66            $this->cache = [];
67
68            return;
69        }
70
71        $cacheKey = $this->buildCacheKey($entry->kind, $exactKey);
72        unset($this->exact[$cacheKey][$subscriptionId->value], $this->cache[$cacheKey]);
73
74        if (($this->exact[$cacheKey] ?? null) === []) {
75            unset($this->exact[$cacheKey]);
76        }
77    }
78
79    public function findMatching(SubscriptionKind $kind, string $key): SubscriptionIdCollection
80    {
81        $cacheKey = $this->buildCacheKey($kind, $key);
82        $cached = $this->cache[$cacheKey] ?? null;
83
84        if ($cached !== null) {
85            ++$this->cacheHits;
86
87            return $cached;
88        }
89
90        ++$this->cacheMisses;
91
92        $matched = array_values($this->exact[$cacheKey] ?? []);
93
94        foreach ($this->patterns as $entry) {
95            if ($entry->kind === $kind && $entry->selector->matches($key)) {
96                $matched[] = $entry->subscriptionId;
97            }
98        }
99
100        if (\count($this->cache) >= self::CACHE_LIMIT) {
101            $this->cache = [];
102        }
103
104        return $this->cache[$cacheKey] = SubscriptionIdCollection::fromList($matched);
105    }
106
107    public function getRoutingStats(): RoutingStats
108    {
109        return new RoutingStats(
110            subscriptions: \count($this->entries),
111            patterns: \count($this->patterns),
112            cachedKeys: \count($this->cache),
113            cacheHits: $this->cacheHits,
114            cacheMisses: $this->cacheMisses,
115        );
116    }
117
118    private function buildCacheKey(SubscriptionKind $kind, string $key): string
119    {
120        return $kind->value . "\0" . $key;
121    }
122}