Skip to content

Commit

Permalink
Add indexes for improved search performance and use lower uid in query
Browse files Browse the repository at this point in the history
  • Loading branch information
tomneedham committed May 11, 2017
1 parent 3422d3d commit 8e00ed3
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 15 deletions.
17 changes: 15 additions & 2 deletions core/Migrations/Version20170510143952.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@ public function changeSchema(Schema $schema, array $options) {
$prefix = $options['tablePrefix'];
$table = $schema->getTable("{$prefix}accounts");

// Add additional_search_string column
$table->addColumn('searchString', 'string', [
// Add column to hold additional search attributes
$table->addColumn('search_attributes', 'string', [
'notnull' => false,
'length' => 64,
]);

// Add index for search attributes
$table->addIndex(['search_attributes'], 'search_attributes_index');

// Index to improve search performance of display_name column
$table->addIndex(['display_name'], 'display_name_index');

// Index to improve search performance of email column
$table->addIndex(['email'], 'email_index');

// Index to improve search performance of lower_user_id column
$table->addUniqueIndex(['lower_user_id'], 'lower_user_id_index');

}
}
3 changes: 2 additions & 1 deletion lib/private/User/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @method void setQuota(string $quota)
* @method string getHome()
* @method void setHome(string $home)
* @method void setSearchAttributes(string $searchAttributes)
*
* @package OC\User
*/
Expand All @@ -64,7 +65,7 @@ class Account extends Entity {
protected $backend;
protected $state;
protected $home;
protected $searchString;
protected $searchAttributes;

public function __construct() {
$this->addType('state', 'integer');
Expand Down
5 changes: 2 additions & 3 deletions lib/private/User/AccountMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ public function find($pattern, $limit, $offset) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
// TODO we can add a config option to allow search by these fields
->where($qb->expr()->iLike('user_id', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->where($qb->expr()->Like('user_id', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter(strtolower($pattern)) . '%')))
->orWhere($qb->expr()->iLike('display_name', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->orWhere($qb->expr()->iLike('email', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->orWhere($qb->expr()->iLike('additional_search_attributes', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->orWhere($qb->expr()->iLike('search_attributes', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')))
->orderBy('display_name');

return $this->findEntities($qb->getSQL(), $qb->getParameters(), $limit, $offset);
Expand Down
2 changes: 0 additions & 2 deletions lib/private/User/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ abstract class Backend implements UserInterface {
const SET_DISPLAYNAME = 1048576; // 1 << 20
const PROVIDE_AVATAR = 16777216; // 1 << 24
const COUNT_USERS = 268435456; // 1 << 28
const SEARCH_STRING = 4294967296; // 1 << 32

protected $possibleActions = [
self::CREATE_USER => 'createUser',
Expand All @@ -57,7 +56,6 @@ abstract class Backend implements UserInterface {
self::SET_DISPLAYNAME => 'setDisplayName',
self::PROVIDE_AVATAR => 'canChangeAvatar',
self::COUNT_USERS => 'countUsers',
self::SEARCH_STRING => 'searchString',
];

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\User\IProvidesAdditionalSearchAttributesBackend;
use OCP\User\IProvidesEMailBackend;
use OCP\User\IProvidesQuotaBackend;
use OCP\UserInterface;

/**
Expand Down Expand Up @@ -424,6 +426,12 @@ private function newAccount($uid, $backend) {
$account->setQuota($quota);
}
}
if ($backend instanceof IProvidesAdditionalSearchAttributesBackend) {
$searchString = $backend->getSearchAttributes($uid);
if ($searchString !== null) {
$account->setSearchAttributes($searchString);
}
}
$home = false;
if ($backend->implementsActions(Backend::GET_HOME)) {
$home = $backend->getHome($uid);
Expand Down
10 changes: 4 additions & 6 deletions lib/private/User/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/


namespace OC\User;


use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\User\IProvidesAdditionalSearchAttributesBackend;
use OCP\UserInterface;

/**
Expand Down Expand Up @@ -161,9 +159,9 @@ public function setupAccount(Account $a, $uid) {
if ($this->backend->implementsActions(\OC_User_Backend::GET_DISPLAYNAME)) {
$a->setDisplayName($this->backend->getDisplayName($uid));
}
// Check if backend supplys additional search strings
if ($this->backend->implementsActions(\OC_User_Backend::SEARCH_STRING)) {
$a->setSearchString($this->backend->getSearchString($uid));
// Check if backend supplies an additional search string
if ($this->backend instanceof IProvidesAdditionalSearchAttributesBackend) {
$a->setSearchAttributes($this->backend->getSearchAttributes($uid));
}
return $a;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class User implements IUser {
/** @var IURLGenerator */
private $urlGenerator;

/** @var EventDispatcher */
/** @var EventDispatcher */
private $eventDispatcher;

/** @var AccountMapper */
Expand Down
1 change: 1 addition & 0 deletions lib/public/IUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,5 @@ public function getQuota();
* @since 9.0.0
*/
public function setQuota($quota);

}
44 changes: 44 additions & 0 deletions lib/public/User/IProvidesAdditionalSearchAttributesBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author Tom Needham <tom@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/


// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP\User;

/**
* Interface IProvidesAdditionalSearchAttributesBackend
*
* @package OCP\User
* @since 10.0.1
*/
interface IProvidesAdditionalSearchAttributesBackend {

/**
* Get a users search string for core powered user search
*
* @param string $uid The username
* @return string
* @since 10.0.1
*/
public function getSearchAttributes($uid);
}

0 comments on commit 8e00ed3

Please sign in to comment.