Skip to content

Commit a1f4b59

Browse files
Merge pull request #54003 from nextcloud/enh/noid/appconfig-get-fast-keys
2 parents dc48b6b + 20b908c commit a1f4b59

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

lib/private/AppConfig.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ public function getApps(): array {
9595
* @inheritDoc
9696
*
9797
* @param string $app id of the app
98-
*
9998
* @return list<string> list of stored config keys
99+
* @see searchKeys to not load lazy config keys
100+
*
100101
* @since 29.0.0
101102
*/
102103
public function getKeys(string $app): array {
@@ -108,6 +109,32 @@ public function getKeys(string $app): array {
108109
return array_values(array_unique($keys));
109110
}
110111

112+
/**
113+
* @inheritDoc
114+
*
115+
* @param string $app id of the app
116+
* @param string $prefix returns only keys starting with this value
117+
* @param bool $lazy TRUE to search in lazy config keys
118+
* @return list<string> list of stored config keys
119+
* @since 32.0.0
120+
*/
121+
public function searchKeys(string $app, string $prefix = '', bool $lazy = false): array {
122+
$this->assertParams($app);
123+
$this->loadConfig($app, $lazy);
124+
if ($lazy) {
125+
$keys = array_keys($this->lazyCache[$app] ?? []);
126+
} else {
127+
$keys = array_keys($this->fastCache[$app] ?? []);
128+
}
129+
130+
if ($prefix !== '') {
131+
$keys = array_filter($keys, static fn (string $key): bool => str_starts_with($key, $prefix));
132+
}
133+
134+
sort($keys);
135+
return array_values(array_unique($keys));
136+
}
137+
111138
/**
112139
* @inheritDoc
113140
*

lib/public/IAppConfig.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,26 @@ public function getApps(): array;
6565
* **WARNING:** ignore lazy filtering, all config values are loaded from database
6666
*
6767
* @param string $app id of the app
68-
*
6968
* @return list<string> list of stored config keys
69+
* @see searchKeys to avoid loading lazy config keys
70+
*
7071
* @since 29.0.0
7172
*/
7273
public function getKeys(string $app): array;
7374

75+
/**
76+
* Returns list of keys stored in database, related to an app.
77+
* Please note that the values are not returned.
78+
*
79+
* @param string $app id of the app
80+
* @param string $prefix returns only keys starting with this value
81+
* @param bool $lazy TRUE to search in lazy config keys
82+
*
83+
* @return list<string> list of stored config keys
84+
* @since 32.0.0
85+
*/
86+
public function searchKeys(string $app, string $prefix = '', bool $lazy = false): array;
87+
7488
/**
7589
* Check if a key exists in the list of stored config values.
7690
*

tests/lib/AppConfigTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class AppConfigTest extends TestCase {
4747
'deletethis' => ['deletethis', 'deletethis'],
4848
'key' => ['key', 'value']
4949
],
50+
'searchtest' => [
51+
'search_key1' => ['search_key1', 'key1', IAppConfig::VALUE_STRING],
52+
'search_key2' => ['search_key2', 'key2', IAppConfig::VALUE_STRING],
53+
'search_key3' => ['search_key3', 'key3', IAppConfig::VALUE_STRING],
54+
'searchnot_key4' => ['searchnot_key4', 'key4', IAppConfig::VALUE_STRING],
55+
'search_key5_lazy' => ['search_key5_lazy', 'key5', IAppConfig::VALUE_STRING, true],
56+
],
5057
'someapp' => [
5158
'key' => ['key', 'value'],
5259
'otherkey' => ['otherkey', 'othervalue']
@@ -1454,6 +1461,23 @@ public function testUpdateNonSensitiveValueToSensitiveWithUpdateSensitive(): voi
14541461
$this->assertConfigValueNotEquals('testapp', $key, $secret);
14551462
}
14561463

1464+
public function testSearchKeyNoLazyLoading(): void {
1465+
$appConfig = $this->generateAppConfig();
1466+
$appConfig->searchKeys('searchtest', 'search_');
1467+
$status = $appConfig->statusCache();
1468+
$this->assertFalse($status['lazyLoaded'], 'searchKeys() loaded lazy config');
1469+
}
1470+
1471+
public function testSearchKeyFast(): void {
1472+
$appConfig = $this->generateAppConfig();
1473+
$this->assertEquals(['search_key1', 'search_key2', 'search_key3'], $appConfig->searchKeys('searchtest', 'search_'));
1474+
}
1475+
1476+
public function testSearchKeyLazy(): void {
1477+
$appConfig = $this->generateAppConfig();
1478+
$this->assertEquals(['search_key5_lazy'], $appConfig->searchKeys('searchtest', 'search_', true));
1479+
}
1480+
14571481
protected function loadConfigValueFromDatabase(string $app, string $key): string|false {
14581482
$sql = $this->connection->getQueryBuilder();
14591483
$sql->select('configvalue')

0 commit comments

Comments
 (0)