ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ILIAS\KeyValueStorage\Internal\NamespacedStore Class Reference

A store bound to one namespace of one repository. More...

+ Inheritance diagram for ILIAS\KeyValueStorage\Internal\NamespacedStore:
+ Collaboration diagram for ILIAS\KeyValueStorage\Internal\NamespacedStore:

Public Member Functions

 __construct (private readonly StorageNamespace $namespace, private readonly Repository $repository, private readonly KeyRules $key_rules, private readonly Values $values)
 
 has (string $key)
 Reads the value along, so that a following get() does not read again. More...
 
 get (string $key, Transformation $transformation)
 Reads a value and applies a Refinery transformation, like HTTP request wrappers. More...
 
 getMany (array $transformations)
 Reads several known keys, each with its own Refinery transformation. More...
 
 keys ()
 The keys this namespace currently holds, sorted as strings. More...
 
 set (string $key, mixed $value)
 
 delete (string $key)
 
 clear ()
 Removes every entry of this namespace, and only of this namespace. More...
 
 has (string $key)
 Reads the value along, so that a following get() does not read again. More...
 
 get (string $key, Transformation $transformation)
 Reads a value and applies a Refinery transformation, like HTTP request wrappers. More...
 
 getMany (array $transformations)
 Reads several known keys, each with its own Refinery transformation. More...
 
 keys ()
 The keys this namespace currently holds, sorted as strings. More...
 
 set (string $key, mixed $value)
 
 delete (string $key)
 
 clear ()
 Removes every entry of this namespace, and only of this namespace. More...
 

Private Member Functions

 readDecoded (string $key)
 
 mustLoadNamespaceFor (array $requested)
 
 loadNamespace ()
 

Private Attributes

array $seen = []
 
bool $namespace_loaded = false
 True once every entry of the namespace has been read. More...
 

Detailed Description

A store bound to one namespace of one repository.

Validates the keys, encodes the values and remembers what it has already seen during this request, so that reading the same key twice does not hit the session or the database twice. Reading the keys of the namespace or several keys at once remembers every key of it. This is not a cross-request cache - use ILIAS\Cache for that.

Definition at line 38 of file NamespacedStore.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\KeyValueStorage\Internal\NamespacedStore::__construct ( private readonly StorageNamespace  $namespace,
private readonly Repository  $repository,
private readonly KeyRules  $key_rules,
private readonly Values  $values 
)

Definition at line 53 of file NamespacedStore.php.

58 {
59 }

Member Function Documentation

◆ clear()

ILIAS\KeyValueStorage\Internal\NamespacedStore::clear ( )

Removes every entry of this namespace, and only of this namespace.

Implements ILIAS\KeyValueStorage\Store.

Definition at line 151 of file NamespacedStore.php.

151 : void
152 {
153 $this->repository->removeAll($this->namespace);
154 $this->seen = [];
155 $this->namespace_loaded = false;
156 }

References ILIAS\UI\examples\Deck\repository().

+ Here is the call graph for this function:

◆ delete()

ILIAS\KeyValueStorage\Internal\NamespacedStore::delete ( string  $key)
Exceptions

InvalidArgumentException if the key is invalid

Implements ILIAS\KeyValueStorage\Store.

Definition at line 143 of file NamespacedStore.php.

143 : void
144 {
145 $this->key_rules->check($key);
146
147 $this->repository->remove($this->namespace, $key);
148 $this->seen[$key] = [false, null];
149 }

References ILIAS\UI\examples\Deck\repository().

+ Here is the call graph for this function:

◆ get()

ILIAS\KeyValueStorage\Internal\NamespacedStore::get ( string  $key,
Transformation  $transformation 
)

Reads a value and applies a Refinery transformation, like HTTP request wrappers.

Absent keys are passed to the transformation as null.

Exceptions

InvalidArgumentException if the key is invalid

Exceptions
Exception

InvalidStoredValueException if the stored value cannot be read back

Implements ILIAS\KeyValueStorage\Store.

Definition at line 70 of file NamespacedStore.php.

70 : mixed
71 {
72 $this->key_rules->check($key);
73
74 [$is_present, $value] = $this->readDecoded($key);
75
76 return $transformation->transform($is_present ? $value : null);
77 }
transform($from)
Perform the transformation.

References ILIAS\KeyValueStorage\Internal\NamespacedStore\readDecoded().

+ Here is the call graph for this function:

◆ getMany()

ILIAS\KeyValueStorage\Internal\NamespacedStore::getMany ( array  $transformations)

Reads several known keys, each with its own Refinery transformation.

The result has the same keys as the given map, in the same order. Absent keys are passed to their transformation as null, like {

See also
self::get()}. Keys that the namespace holds besides those named here are left out.
Parameters
array<string,Transformation>$transformations
Returns
array<string, mixed>
Exceptions

InvalidArgumentException if a key is invalid or an entry is not a transformation

Exceptions
Exception

InvalidStoredValueException if a stored value cannot be read back

Implements ILIAS\KeyValueStorage\Store.

Definition at line 79 of file NamespacedStore.php.

79 : array
80 {
81 $requested = [];
82 foreach ($transformations as $key => $transformation) {
83 $key = (string) $key;
84 if (!$transformation instanceof Transformation) {
85 throw new \InvalidArgumentException(
86 'Each getMany() entry must be a ' . Transformation::class . ', got '
87 . \get_debug_type($transformation) . ' for key "' . $key . '".'
88 );
89 }
90 $this->key_rules->check($key);
91 $requested[$key] = $transformation;
92 }
93
94 if ($this->mustLoadNamespaceFor($requested)) {
95 $this->loadNamespace();
96 }
97
98 $entries = [];
99 foreach ($requested as $key => $transformation) {
100 $key = (string) $key;
101 [$is_present, $value] = $this->readDecoded($key);
102 $entries[$key] = $transformation->transform($is_present ? $value : null);
103 }
104
105 return $entries;
106 }
A transformation is a function from one datatype to another.

References ILIAS\KeyValueStorage\Internal\NamespacedStore\loadNamespace(), ILIAS\KeyValueStorage\Internal\NamespacedStore\mustLoadNamespaceFor(), and ILIAS\KeyValueStorage\Internal\NamespacedStore\readDecoded().

+ Here is the call graph for this function:

◆ has()

ILIAS\KeyValueStorage\Internal\NamespacedStore::has ( string  $key)

Reads the value along, so that a following get() does not read again.

Exceptions

InvalidArgumentException if the key is invalid

Exceptions
Exception

InvalidStoredValueException if the stored value cannot be read back

Implements ILIAS\KeyValueStorage\Store.

Definition at line 61 of file NamespacedStore.php.

61 : bool
62 {
63 $this->key_rules->check($key);
64
65 // reads the value along, since consumers like `$storage[$key] ?? null`
66 // ask has() right before get().
67 return $this->readDecoded($key)[0];
68 }

References ILIAS\KeyValueStorage\Internal\NamespacedStore\readDecoded().

+ Here is the call graph for this function:

◆ keys()

ILIAS\KeyValueStorage\Internal\NamespacedStore::keys ( )

The keys this namespace currently holds, sorted as strings.

Returns
list<string>

Implements ILIAS\KeyValueStorage\Store.

Definition at line 108 of file NamespacedStore.php.

108 : array
109 {
110 $this->loadNamespace();
111
112 $keys = [];
113 foreach ($this->seen as $key => [$is_present]) {
114 if ($is_present) {
115 $keys[] = (string) $key;
116 }
117 }
118
119 \sort($keys, \SORT_STRING);
120
121 return $keys;
122 }

References ILIAS\KeyValueStorage\Internal\NamespacedStore\loadNamespace(), and ILIAS\UI\examples\Symbol\Glyph\Sort\sort().

+ Here is the call graph for this function:

◆ loadNamespace()

ILIAS\KeyValueStorage\Internal\NamespacedStore::loadNamespace ( )
private

Definition at line 195 of file NamespacedStore.php.

195 : void
196 {
197 if ($this->namespace_loaded) {
198 return;
199 }
200
201 $decoded = [];
202 foreach ($this->repository->readAll($this->namespace) as $key => $stored) {
203 $key = (string) $key;
204 if (isset($this->seen[$key])) {
205 continue;
206 }
207
208 $decoded[$key] = [true, $this->values->decode($stored)];
209 }
210
211 $this->seen += $decoded;
212 $this->namespace_loaded = true;
213 }

References ILIAS\UI\examples\Deck\repository().

Referenced by ILIAS\KeyValueStorage\Internal\NamespacedStore\getMany(), and ILIAS\KeyValueStorage\Internal\NamespacedStore\keys().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mustLoadNamespaceFor()

ILIAS\KeyValueStorage\Internal\NamespacedStore::mustLoadNamespaceFor ( array  $requested)
private
Parameters
array<string,Transformation>$requested

Definition at line 180 of file NamespacedStore.php.

180 : bool
181 {
182 if ($requested === [] || $this->namespace_loaded) {
183 return false;
184 }
185
186 foreach ($requested as $key => $_) {
187 if (!isset($this->seen[(string) $key])) {
188 return true;
189 }
190 }
191
192 return false;
193 }

Referenced by ILIAS\KeyValueStorage\Internal\NamespacedStore\getMany().

+ Here is the caller graph for this function:

◆ readDecoded()

ILIAS\KeyValueStorage\Internal\NamespacedStore::readDecoded ( string  $key)
private
Returns
array{bool, mixed}

Definition at line 161 of file NamespacedStore.php.

161 : array
162 {
163 if (!isset($this->seen[$key])) {
164 if ($this->namespace_loaded) {
165 $this->seen[$key] = [false, null];
166 } else {
167 $stored = $this->repository->read($this->namespace, $key);
168 $this->seen[$key] = $stored === null
169 ? [false, null]
170 : [true, $this->values->decode($stored)];
171 }
172 }
173
174 return $this->seen[$key];
175 }

References ILIAS\UI\examples\Deck\repository().

Referenced by ILIAS\KeyValueStorage\Internal\NamespacedStore\get(), ILIAS\KeyValueStorage\Internal\NamespacedStore\getMany(), and ILIAS\KeyValueStorage\Internal\NamespacedStore\has().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set()

ILIAS\KeyValueStorage\Internal\NamespacedStore::set ( string  $key,
mixed  $value 
)
Exceptions

InvalidArgumentException if the key is invalid or the value cannot be stored

Implements ILIAS\KeyValueStorage\Store.

Definition at line 124 of file NamespacedStore.php.

124 : void
125 {
126 $this->key_rules->check($key);
127
128 $encoded = $this->values->encode($value);
129 // the decoded form is remembered, so that reading a value back within
130 // this request yields exactly what a later request would read.
131 $decoded = $this->values->decode($encoded);
132
133 // consumers like the UI tables store their state on every rendering, which
134 // must not become a write per page view when nothing changed.
135 if (($this->seen[$key] ?? null) === [true, $decoded]) {
136 return;
137 }
138
139 $this->repository->write($this->namespace, $key, $encoded);
140 $this->seen[$key] = [true, $decoded];
141 }

References ILIAS\UI\examples\Deck\repository().

+ Here is the call graph for this function:

Field Documentation

◆ $namespace_loaded

bool ILIAS\KeyValueStorage\Internal\NamespacedStore::$namespace_loaded = false
private

True once every entry of the namespace has been read.

Later lookups of keys this request has not touched can then be answered without the backend.

Definition at line 51 of file NamespacedStore.php.

◆ $seen

array ILIAS\KeyValueStorage\Internal\NamespacedStore::$seen = []
private

Definition at line 45 of file NamespacedStore.php.


The documentation for this class was generated from the following file: