Skip to content

Commit

Permalink
feat(user-prefs): iterator instead of array on search
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Oct 21, 2024
1 parent 35b558f commit 5c300fc
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 130 deletions.
14 changes: 6 additions & 8 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
'OCP\\Common\\Exception\\NotFoundException' => $baseDir . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php',
'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php',
'OCP\\Config\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/Config/Exceptions/IncorrectTypeException.php',
'OCP\\Config\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/Config/Exceptions/TypeConflictException.php',
'OCP\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/Config/Exceptions/UnknownKeyException.php',
'OCP\\Config\\IUserPreferences' => $baseDir . '/lib/public/Config/IUserPreferences.php',
'OCP\\Config\\ValueType' => $baseDir . '/lib/public/Config/ValueType.php',
'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php',
'OCP\\Console\\ReservedOptions' => $baseDir . '/lib/public/Console/ReservedOptions.php',
'OCP\\Constants' => $baseDir . '/lib/public/Constants.php',
Expand Down Expand Up @@ -836,13 +841,6 @@
'OCP\\UserMigration\\ISizeEstimationMigrator' => $baseDir . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
'OCP\\UserMigration\\UserMigrationException' => $baseDir . '/lib/public/UserMigration/UserMigrationException.php',
'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php',
'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php',
'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php',
'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php',
'OCP\\UserPreferences\\IUserPreferences' => $baseDir . '/lib/public/UserPreferences/IUserPreferences.php',
'OCP\\UserPreferences\\ValueType' => $baseDir . '/lib/public/UserPreferences/ValueType.php',
'OCP\\UserPreferences\\ValueTypeDefinition' => $baseDir . '/lib/public/UserPreferences/ValueTypeDefinition.php',
'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',
'OCP\\UserStatus\\IProvider' => $baseDir . '/lib/public/UserStatus/IProvider.php',
'OCP\\UserStatus\\IUserStatus' => $baseDir . '/lib/public/UserStatus/IUserStatus.php',
Expand Down Expand Up @@ -1122,6 +1120,7 @@
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => $baseDir . '/lib/private/Config.php',
'OC\\Config\\UserPreferences' => $baseDir . '/lib/private/Config/UserPreferences.php',
'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php',
'OC\\Console\\TimestampFormatter' => $baseDir . '/lib/private/Console/TimestampFormatter.php',
'OC\\ContactsManager' => $baseDir . '/lib/private/ContactsManager.php',
Expand Down Expand Up @@ -2002,7 +2001,6 @@
'OC\\Updater\\Exceptions\\ReleaseMetadataException' => $baseDir . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php',
'OC\\Updater\\ReleaseMetadata' => $baseDir . '/lib/private/Updater/ReleaseMetadata.php',
'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php',
'OC\\UserPreferences' => $baseDir . '/lib/private/UserPreferences.php',
'OC\\UserStatus\\ISettableProvider' => $baseDir . '/lib/private/UserStatus/ISettableProvider.php',
'OC\\UserStatus\\Manager' => $baseDir . '/lib/private/UserStatus/Manager.php',
'OC\\User\\AvailabilityCoordinator' => $baseDir . '/lib/private/User/AvailabilityCoordinator.php',
Expand Down
14 changes: 6 additions & 8 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Common\\Exception\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php',
'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php',
'OCP\\Config\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/IncorrectTypeException.php',
'OCP\\Config\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/TypeConflictException.php',
'OCP\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/UnknownKeyException.php',
'OCP\\Config\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/Config/IUserPreferences.php',
'OCP\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/public/Config/ValueType.php',
'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php',
'OCP\\Console\\ReservedOptions' => __DIR__ . '/../../..' . '/lib/public/Console/ReservedOptions.php',
'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php',
Expand Down Expand Up @@ -869,13 +874,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\UserMigration\\ISizeEstimationMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
'OCP\\UserMigration\\UserMigrationException' => __DIR__ . '/../../..' . '/lib/public/UserMigration/UserMigrationException.php',
'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php',
'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php',
'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php',
'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php',
'OCP\\UserPreferences\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/IUserPreferences.php',
'OCP\\UserPreferences\\ValueType' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueType.php',
'OCP\\UserPreferences\\ValueTypeDefinition' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueTypeDefinition.php',
'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
'OCP\\UserStatus\\IProvider' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IProvider.php',
'OCP\\UserStatus\\IUserStatus' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IUserStatus.php',
Expand Down Expand Up @@ -1155,6 +1153,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
'OC\\Config\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/Config/UserPreferences.php',
'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php',
'OC\\Console\\TimestampFormatter' => __DIR__ . '/../../..' . '/lib/private/Console/TimestampFormatter.php',
'OC\\ContactsManager' => __DIR__ . '/../../..' . '/lib/private/ContactsManager.php',
Expand Down Expand Up @@ -2035,7 +2034,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Updater\\Exceptions\\ReleaseMetadataException' => __DIR__ . '/../../..' . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php',
'OC\\Updater\\ReleaseMetadata' => __DIR__ . '/../../..' . '/lib/private/Updater/ReleaseMetadata.php',
'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php',
'OC\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/UserPreferences.php',
'OC\\UserStatus\\ISettableProvider' => __DIR__ . '/../../..' . '/lib/private/UserStatus/ISettableProvider.php',
'OC\\UserStatus\\Manager' => __DIR__ . '/../../..' . '/lib/private/UserStatus/Manager.php',
'OC\\User\\AvailabilityCoordinator' => __DIR__ . '/../../..' . '/lib/private/User/AvailabilityCoordinator.php',
Expand Down
11 changes: 6 additions & 5 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/
namespace OC;

use OC\Config\UserPreferences;
use OCP\Cache\CappedMemoryCache;
use OCP\Config\Exceptions\TypeConflictException;
use OCP\Config\IUserPreferences;
use OCP\Config\ValueType;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\PreConditionNotMetException;
use OCP\UserPreferences\Exceptions\TypeConflictException;
use OCP\UserPreferences\IUserPreferences;
use OCP\UserPreferences\ValueType;

/**
* Class to combine all the configuration options ownCloud offers
Expand Down Expand Up @@ -380,7 +381,7 @@ public function getUserValueForUsers($appName, $key, $userIds) {
* @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly
*/
public function getUsersForUserValue($appName, $key, $value) {
return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value);
return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value));
}

/**
Expand All @@ -397,7 +398,7 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
return $this->getUsersForUserValue($appName, $key, strtolower($value));
}

return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value, true);
return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value, true));
}

public function getSystemConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC;
namespace OC\Config;

use Generator;
use InvalidArgumentException;
use JsonException;
use OCP\Config\Exceptions\IncorrectTypeException;
use OCP\Config\Exceptions\TypeConflictException;
use OCP\Config\Exceptions\UnknownKeyException;
use OCP\Config\IUserPreferences;
use OCP\Config\ValueType;
use OCP\DB\Exception as DBException;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use OCP\UserPreferences\Exceptions\IncorrectTypeException;
use OCP\UserPreferences\Exceptions\TypeConflictException;
use OCP\UserPreferences\Exceptions\UnknownKeyException;
use OCP\UserPreferences\IUserPreferences;
use OCP\UserPreferences\ValueType;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -358,7 +359,7 @@ public function getValuesByUsers(
while ($row = $result->fetch()) {
$value = $row['configvalue'];
try {
$value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int) $row['type']));
$value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type']));
} catch (IncorrectTypeException) {
}
$values[$row['userid']] = $value;
Expand Down Expand Up @@ -393,40 +394,24 @@ public function getValuesByUsers(
* @param string $value preference value
* @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string
*
* @return list<string>
* @return Generator<string>
* @since 31.0.0
*/
public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array {
public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator {
return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive);
}

/**
* @inheritDoc
*
* @param string $app id of the app
* @param string $key preference key
* @param string $value preference value
* @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string
* @internal
* @deprecated since 31.0.0 - {@see }
* @return list<string>
* @since 31.0.0
*/
public function searchUsersByValueDeprecated(string $app, string $key, string $value, bool $caseInsensitive = false): array {
return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive, true);
}

/**
* @inheritDoc
*
* @param string $app id of the app
* @param string $key preference key
* @param int $value preference value
*
* @return list<string>
* @return Generator<string>
* @since 31.0.0
*/
public function searchUsersByValueInt(string $app, string $key, int $value): array {
public function searchUsersByValueInt(string $app, string $key, int $value): Generator {
return $this->searchUsersByValueString($app, $key, (string)$value);
}

Expand All @@ -437,10 +422,10 @@ public function searchUsersByValueInt(string $app, string $key, int $value): arr
* @param string $key preference key
* @param array $values list of preference values
*
* @return list<string>
* @return Generator<string>
* @since 31.0.0
*/
public function searchUsersByValues(string $app, string $key, array $values): array {
public function searchUsersByValues(string $app, string $key, array $values): Generator {
return $this->searchUsersByTypedValue($app, $key, $values);
}

Expand All @@ -451,10 +436,10 @@ public function searchUsersByValues(string $app, string $key, array $values): ar
* @param string $key preference key
* @param bool $value preference value
*
* @return list<string>
* @return Generator<string>
* @since 31.0.0
*/
public function searchUsersByValueBool(string $app, string $key, bool $value): array {
public function searchUsersByValueBool(string $app, string $key, bool $value): Generator {
$values = ['0', 'off', 'false', 'no'];
if ($value) {
$values = ['1', 'on', 'true', 'yes'];
Expand All @@ -470,11 +455,10 @@ public function searchUsersByValueBool(string $app, string $key, bool $value): a
* @param string $key
* @param string|array $value
* @param bool $caseInsensitive
* @param bool $withinNotIndexedField DEPRECATED: should only be used to stay compatible with not-indexed/pre-31 preferences value
*
* @return list<string>
* @return Generator<string>
*/
private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): array {
private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator {
$this->assertParams('', $app, $key, allowEmptyUser: true);

$qb = $this->connection->getQueryBuilder();
Expand Down Expand Up @@ -515,15 +499,10 @@ private function searchUsersByTypedValue(string $app, string $key, string|array
}

$qb->andWhere($where);

$userIds = [];
$result = $qb->executeQuery();
$rows = $result->fetchAll();
foreach ($rows as $row) {
$userIds[] = $row['userid'];
while ($row = $result->fetch()) {
yield $row['userid'];
}

return $userIds;
}

/**
Expand Down Expand Up @@ -726,7 +705,7 @@ private function getTypedValue(
bool $lazy,
ValueType $type,
): string {
$this->assertParams($userId, $app, $key, valueType: $type);
$this->assertParams($userId, $app, $key);
$this->loadPreferences($userId, $lazy);

/**
Expand Down Expand Up @@ -978,7 +957,7 @@ public function setValueBool(
string $key,
bool $value,
bool $lazy = false,
int $flags = 0
int $flags = 0,
): bool {
return $this->setTypedValue(
$userId,
Expand Down Expand Up @@ -1058,7 +1037,7 @@ private function setTypedValue(
int $flags,
ValueType $type,
): bool {
$this->assertParams($userId, $app, $key, valueType: $type);
$this->assertParams($userId, $app, $key);
$this->loadPreferences($userId, $lazy);

$inserted = $refreshCache = false;
Expand All @@ -1074,7 +1053,7 @@ private function setTypedValue(
if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) {
if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) {
$this->logger->warning('sensitive value are not to be indexed');
} else if (strlen($value) > self::USER_MAX_LENGTH) {
} elseif (strlen($value) > self::USER_MAX_LENGTH) {
$this->logger->warning('value is too lengthy to be indexed');
} else {
$indexed = $value;
Expand Down Expand Up @@ -1203,7 +1182,7 @@ private function setTypedValue(
* @since 31.0.0
*/
public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool {
$this->assertParams($userId, $app, $key, valueType: $type);
$this->assertParams($userId, $app, $key);
$this->loadPreferencesAll($userId);
$this->isLazy($userId, $app, $key); // confirm key exists

Expand Down Expand Up @@ -1348,11 +1327,11 @@ public function updateIndexed(string $userId, string $app, string $key, bool $in

$update = $this->connection->getQueryBuilder();
$update->update('preferences')
->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
->set('indexed', $update->createNamedParameter($indexed))
->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
->set('indexed', $update->createNamedParameter($indexed))
->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
$update->executeStatement();

$this->valueDetails[$userId][$app][$key]['flags'] = $flags;
Expand Down Expand Up @@ -1633,8 +1612,7 @@ private function assertParams(
string $app = '',
string $prefKey = '',
bool $allowEmptyUser = false,
bool $allowEmptyApp = false,
?ValueType $valueType = null,
bool $allowEmptyApp = false
): void {
if (!$allowEmptyUser && $userId === '') {
throw new InvalidArgumentException('userId cannot be an empty string');
Expand All @@ -1651,12 +1629,6 @@ private function assertParams(
if (strlen($prefKey) > self::KEY_MAX_LENGTH) {
throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')');
}
if ($valueType !== null) {
// $valueFlag = $valueType->value;
// if (ValueType::tryFrom($valueFlag) === null) {
// throw new InvalidArgumentException('Unknown value type');
// }
}
}

private function loadPreferencesAll(string $userId): void {
Expand Down Expand Up @@ -1697,7 +1669,7 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void {
} else {
$this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? '';
}
$this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int) $row['flags']];
$this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']];
}
$result->closeCursor();
$this->setAsLoaded($userId, $lazy);
Expand Down
Loading

0 comments on commit 5c300fc

Please sign in to comment.