break up big inbox queries into lots of small ones
This commit is contained in:
parent
b53b6b8769
commit
994768b821
@ -14,8 +14,14 @@ class Group_inbox extends Memcached_DataObject
|
|||||||
public $created; // datetime() not_null
|
public $created; // datetime() not_null
|
||||||
|
|
||||||
/* Static get */
|
/* Static get */
|
||||||
|
|
||||||
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_inbox',$k,$v); }
|
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_inbox',$k,$v); }
|
||||||
|
|
||||||
/* the code above is auto generated do not remove the tag below */
|
/* the code above is auto generated do not remove the tag below */
|
||||||
###END_AUTOCODE
|
###END_AUTOCODE
|
||||||
|
|
||||||
|
function &pkeyGet($kv)
|
||||||
|
{
|
||||||
|
return Memcached_DataObject::pkeyGet('Group_inbox', $kv);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -832,24 +832,60 @@ class Notice extends Memcached_DataObject
|
|||||||
$enabled = common_config('inboxes', 'enabled');
|
$enabled = common_config('inboxes', 'enabled');
|
||||||
|
|
||||||
if ($enabled === true || $enabled === 'transitional') {
|
if ($enabled === true || $enabled === 'transitional') {
|
||||||
$inbox = new Notice_inbox();
|
|
||||||
$UT = common_config('db','type')=='pgsql'?'"user"':'user';
|
$users = $this->getSubscribedUsers();
|
||||||
$qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
|
|
||||||
"SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
|
// FIXME: kind of ignoring 'transitional'...
|
||||||
"FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
|
// we'll probably stop supporting inboxless mode
|
||||||
'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
|
// in 0.9.x
|
||||||
'AND NOT EXISTS (SELECT user_id, notice_id ' .
|
|
||||||
'FROM notice_inbox ' .
|
foreach ($users as $id) {
|
||||||
"WHERE user_id = $UT.id " .
|
$this->addToUserInbox($id, NOTICE_INBOX_SOURCE_SUB);
|
||||||
'AND notice_id = ' . $this->id . ' )';
|
|
||||||
if ($enabled === 'transitional') {
|
|
||||||
$qry .= " AND $UT.inboxed = 1";
|
|
||||||
}
|
}
|
||||||
$inbox->query($qry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSubscribedUsers()
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
|
||||||
|
$qry =
|
||||||
|
'SELECT id ' .
|
||||||
|
'FROM user JOIN subscription '.
|
||||||
|
'ON user.id = subscription.subscriber ' .
|
||||||
|
'WHERE subscription.subscribed = %d ';
|
||||||
|
|
||||||
|
$user->query(sprintf($qry, $this->profile_id));
|
||||||
|
|
||||||
|
$ids = array();
|
||||||
|
|
||||||
|
while ($user->fetch()) {
|
||||||
|
$ids[] = $user->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->free();
|
||||||
|
|
||||||
|
return $ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addToUserInbox($user_id, $source)
|
||||||
|
{
|
||||||
|
$inbox = Notice_inbox::pkeyGet(array('user_id' => $user_id,
|
||||||
|
'notice_id' => $this->id));
|
||||||
|
if (empty($inbox)) {
|
||||||
|
$inbox = new Notice_inbox();
|
||||||
|
$inbox->user_id = $user_id;
|
||||||
|
$inbox->notice_id = $this->id;
|
||||||
|
$inbox->source = $source;
|
||||||
|
$inbox->created = $this->created;
|
||||||
|
return $inbox->insert();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function saveGroups()
|
function saveGroups()
|
||||||
{
|
{
|
||||||
$enabled = common_config('inboxes', 'enabled');
|
$enabled = common_config('inboxes', 'enabled');
|
||||||
@ -888,13 +924,7 @@ class Notice extends Memcached_DataObject
|
|||||||
|
|
||||||
if ($profile->isMember($group)) {
|
if ($profile->isMember($group)) {
|
||||||
|
|
||||||
$gi = new Group_inbox();
|
$result = $this->addToGroupInbox($group);
|
||||||
|
|
||||||
$gi->group_id = $group->id;
|
|
||||||
$gi->notice_id = $this->id;
|
|
||||||
$gi->created = common_sql_now();
|
|
||||||
|
|
||||||
$result = $gi->insert();
|
|
||||||
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
common_log_db_error($gi, 'INSERT', __FILE__);
|
common_log_db_error($gi, 'INSERT', __FILE__);
|
||||||
@ -902,27 +932,37 @@ class Notice extends Memcached_DataObject
|
|||||||
|
|
||||||
// FIXME: do this in an offline daemon
|
// FIXME: do this in an offline daemon
|
||||||
|
|
||||||
$this->addToGroupInboxes($group);
|
$this->addToGroupMemberInboxes($group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToGroupInboxes($group)
|
function addToGroupInbox($group)
|
||||||
{
|
{
|
||||||
$inbox = new Notice_inbox();
|
$gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
|
||||||
$UT = common_config('db','type')=='pgsql'?'"user"':'user';
|
'notice_id' => $this->id));
|
||||||
$qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
|
|
||||||
"SELECT $UT.id, " . $this->id . ", '" . $this->created . "', " . NOTICE_INBOX_SOURCE_GROUP . " " .
|
if (empty($gi)) {
|
||||||
"FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
|
|
||||||
'WHERE group_member.group_id = ' . $group->id . ' ' .
|
$gi = new Group_inbox();
|
||||||
'AND NOT EXISTS (SELECT user_id, notice_id ' .
|
|
||||||
'FROM notice_inbox ' .
|
$gi->group_id = $group->id;
|
||||||
"WHERE user_id = $UT.id " .
|
$gi->notice_id = $this->id;
|
||||||
'AND notice_id = ' . $this->id . ' )';
|
$gi->created = $this->created;
|
||||||
if ($enabled === 'transitional') {
|
|
||||||
$qry .= " AND $UT.inboxed = 1";
|
return $gi->insert();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addToGroupMemberInboxes($group)
|
||||||
|
{
|
||||||
|
$users = $group->getUserMembers();
|
||||||
|
|
||||||
|
foreach ($users as $id) {
|
||||||
|
$this->addToUserInbox($id, NOTICE_INBOX_SOURCE_GROUP);
|
||||||
}
|
}
|
||||||
$result = $inbox->query($qry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveReplies()
|
function saveReplies()
|
||||||
|
@ -27,6 +27,7 @@ define('INBOX_CACHE_WINDOW', 101);
|
|||||||
|
|
||||||
define('NOTICE_INBOX_SOURCE_SUB', 1);
|
define('NOTICE_INBOX_SOURCE_SUB', 1);
|
||||||
define('NOTICE_INBOX_SOURCE_GROUP', 2);
|
define('NOTICE_INBOX_SOURCE_GROUP', 2);
|
||||||
|
define('NOTICE_INBOX_SOURCE_REPLY', 3);
|
||||||
define('NOTICE_INBOX_SOURCE_GATEWAY', -1);
|
define('NOTICE_INBOX_SOURCE_GATEWAY', -1);
|
||||||
|
|
||||||
class Notice_inbox extends Memcached_DataObject
|
class Notice_inbox extends Memcached_DataObject
|
||||||
|
@ -289,4 +289,52 @@ class Profile extends Memcached_DataObject
|
|||||||
return Avatar::defaultImage($size);
|
return Avatar::defaultImage($size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSubscriptions($offset=0, $limit=null)
|
||||||
|
{
|
||||||
|
$qry =
|
||||||
|
'SELECT profile.* ' .
|
||||||
|
'FROM profile JOIN subscription ' .
|
||||||
|
'ON profile.id = subscription.subscribed ' .
|
||||||
|
'WHERE subscription.subscriber = %d ' .
|
||||||
|
'AND subscription.subscribed != subscription.subscriber ' .
|
||||||
|
'ORDER BY subscription.created DESC ';
|
||||||
|
|
||||||
|
if (common_config('db','type') == 'pgsql') {
|
||||||
|
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||||
|
} else {
|
||||||
|
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$profile = new Profile();
|
||||||
|
|
||||||
|
$profile->query(sprintf($qry, $this->id));
|
||||||
|
|
||||||
|
return $profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSubscribers($offset=0, $limit=null)
|
||||||
|
{
|
||||||
|
$qry =
|
||||||
|
'SELECT profile.* ' .
|
||||||
|
'FROM profile JOIN subscription ' .
|
||||||
|
'ON profile.id = subscription.subscriber ' .
|
||||||
|
'WHERE subscription.subscribed = %d ' .
|
||||||
|
'AND subscription.subscribed != subscription.subscriber ' .
|
||||||
|
'ORDER BY subscription.created DESC ';
|
||||||
|
|
||||||
|
if ($offset) {
|
||||||
|
if (common_config('db','type') == 'pgsql') {
|
||||||
|
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||||
|
} else {
|
||||||
|
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$profile = new Profile();
|
||||||
|
|
||||||
|
$cnt = $profile->query(sprintf($qry, $this->id));
|
||||||
|
|
||||||
|
return $profile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -600,50 +600,16 @@ class User extends Memcached_DataObject
|
|||||||
|
|
||||||
function getSubscriptions($offset=0, $limit=null)
|
function getSubscriptions($offset=0, $limit=null)
|
||||||
{
|
{
|
||||||
$qry =
|
$profile = $this->getProfile();
|
||||||
'SELECT profile.* ' .
|
assert(!empty($profile));
|
||||||
'FROM profile JOIN subscription ' .
|
return $profile->getSubscriptions($offset, $limit);
|
||||||
'ON profile.id = subscription.subscribed ' .
|
|
||||||
'WHERE subscription.subscriber = %d ' .
|
|
||||||
'AND subscription.subscribed != subscription.subscriber ' .
|
|
||||||
'ORDER BY subscription.created DESC ';
|
|
||||||
|
|
||||||
if (common_config('db','type') == 'pgsql') {
|
|
||||||
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
|
|
||||||
} else {
|
|
||||||
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$profile = new Profile();
|
|
||||||
|
|
||||||
$profile->query(sprintf($qry, $this->id));
|
|
||||||
|
|
||||||
return $profile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSubscribers($offset=0, $limit=null)
|
function getSubscribers($offset=0, $limit=null)
|
||||||
{
|
{
|
||||||
$qry =
|
$profile = $this->getProfile();
|
||||||
'SELECT profile.* ' .
|
assert(!empty($profile));
|
||||||
'FROM profile JOIN subscription ' .
|
return $profile->getSubscribers($offset, $limit);
|
||||||
'ON profile.id = subscription.subscriber ' .
|
|
||||||
'WHERE subscription.subscribed = %d ' .
|
|
||||||
'AND subscription.subscribed != subscription.subscriber ' .
|
|
||||||
'ORDER BY subscription.created DESC ';
|
|
||||||
|
|
||||||
if ($offset) {
|
|
||||||
if (common_config('db','type') == 'pgsql') {
|
|
||||||
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
|
|
||||||
} else {
|
|
||||||
$qry .= ' LIMIT ' . $offset . ', ' . $limit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$profile = new Profile();
|
|
||||||
|
|
||||||
$cnt = $profile->query(sprintf($qry, $this->id));
|
|
||||||
|
|
||||||
return $profile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTaggedSubscribers($tag, $offset=0, $limit=null)
|
function getTaggedSubscribers($tag, $offset=0, $limit=null)
|
||||||
|
@ -246,4 +246,28 @@ class User_group extends Memcached_DataObject
|
|||||||
return Design::staticGet('id', $this->design_id);
|
return Design::staticGet('id', $this->design_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getUserMembers()
|
||||||
|
{
|
||||||
|
// XXX: cache this
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
|
||||||
|
$qry =
|
||||||
|
'SELECT id ' .
|
||||||
|
'FROM user JOIN group_member '.
|
||||||
|
'ON user.id = group_member.profile_id ' .
|
||||||
|
'WHERE group_member.group_id = %d ';
|
||||||
|
|
||||||
|
$user->query(sprintf($qry, $this->id));
|
||||||
|
|
||||||
|
$ids = array();
|
||||||
|
|
||||||
|
while ($user->fetch()) {
|
||||||
|
$ids[] = $user->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->free();
|
||||||
|
|
||||||
|
return $ids;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user