[CORE] Avoid materialisation in the TaggedProfileNoticeStream query

This is analogous to c862589dcf
This commit is contained in:
Alexei Sorokin 2020-07-07 22:41:03 +03:00 committed by Diogo Peralta Cordeiro
parent e3f1667b47
commit 9ba9006014
2 changed files with 50 additions and 58 deletions

View File

@ -1,53 +1,52 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social 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
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* Stream of notices by a profile with a given tag * Stream of notices by a profile with a given tag
* *
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Stream * @category Stream
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Stream of notices with a given profile and tag * Stream of notices with a given profile and tag
* *
* @category Stream * @category Stream
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class TaggedProfileNoticeStream extends ScopingNoticeStream class TaggedProfileNoticeStream extends ScopingNoticeStream
{ {
function __construct($profile, $tag, Profile $scoped=null) public function __construct($profile, $tag, Profile $scoped = null)
{ {
parent::__construct(new CachingNoticeStream(new RawTaggedProfileNoticeStream($profile, $tag), parent::__construct(
'profile:notice_ids_tagged:'.$profile->id.':'.Cache::keyize($tag)), new CachingNoticeStream(
$scoped); new RawTaggedProfileNoticeStream($profile, $tag),
'profile:notice_ids_tagged:' . $profile->id . ':' . Cache::keyize($tag)
),
$scoped
);
} }
} }
@ -55,11 +54,10 @@ class TaggedProfileNoticeStream extends ScopingNoticeStream
* Raw stream of notices with a given profile and tag * Raw stream of notices with a given profile and tag
* *
* @category Stream * @category Stream
* @package StatusNet * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class RawTaggedProfileNoticeStream extends NoticeStream class RawTaggedProfileNoticeStream extends NoticeStream
@ -67,46 +65,40 @@ class RawTaggedProfileNoticeStream extends NoticeStream
protected $profile; protected $profile;
protected $tag; protected $tag;
function __construct($profile, $tag) public function __construct($profile, $tag)
{ {
$this->profile = $profile; $this->profile = $profile;
$this->tag = $tag; $this->tag = $tag;
} }
function getNoticeIds($offset, $limit, $since_id, $max_id) public function getNoticeIds($offset, $limit, $since_id, $max_id)
{ {
// XXX It would be nice to do this without a join $nt = new Notice_tag();
// (necessary to do it efficiently on accounts with long history)
$notice = new Notice(); $nt->tag = $this->tag;
$query = $nt->selectAdd();
"select id from notice join notice_tag on id=notice_id where tag='". $nt->selectAdd('notice_id');
$notice->escape($this->tag) .
"' and profile_id=" . intval($this->profile->id);
$since = Notice::whereSinceId($since_id, 'id', 'notice.created'); $nt->joinAdd(['notice_id', 'notice:id']);
if ($since) {
$query .= " and ($since)";
}
$max = Notice::whereMaxId($max_id, 'id', 'notice.created'); $nt->whereAdd(sprintf('notice.profile_id = %d', $this->profile->id));
if ($max) {
$query .= " and ($max)";
}
$query .= ' order by notice.created DESC, id DESC'; Notice::addWhereSinceId($nt, $since_id, 'notice_id', 'notice_tag.created');
Notice::addWhereMaxId($nt, $max_id, 'notice_id', 'notice_tag.created');
$nt->orderBy('notice_tag.created DESC, notice_id DESC');
if (!is_null($offset)) { if (!is_null($offset)) {
$query .= " LIMIT " . intval($limit) . " OFFSET " . intval($offset); $nt->limit($offset, $limit);
} }
$notice->query($query); $ids = [];
$ids = array(); if ($nt->find()) {
while ($nt->fetch()) {
while ($notice->fetch()) { $ids[] = $nt->notice_id;
$ids[] = $notice->id; }
} }
return $ids; return $ids;

View File

@ -78,8 +78,8 @@ class RawTagNoticeStream extends NoticeStream
$nt->selectAdd(); $nt->selectAdd();
$nt->selectAdd('notice_id'); $nt->selectAdd('notice_id');
Notice::addWhereSinceId($nt, $since_id, 'notice_id'); Notice::addWhereSinceId($nt, $since_id, 'notice_id', 'notice_tag.created');
Notice::addWhereMaxId($nt, $max_id, 'notice_id'); Notice::addWhereMaxId($nt, $max_id, 'notice_id', 'notice_tag.created');
if (!empty($this->selectVerbs)) { if (!empty($this->selectVerbs)) {
$nt->joinAdd(array('notice_id', 'notice:id')); $nt->joinAdd(array('notice_id', 'notice:id'));