From 8b2f6c01fb3e15007ec7466f6a8f53e3b13ca41b Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 29 Dec 2009 22:02:46 -0800 Subject: add inbox data class --- classes/Inbox.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 classes/Inbox.php (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php new file mode 100755 index 000000000..35f532c06 --- /dev/null +++ b/classes/Inbox.php @@ -0,0 +1,51 @@ +. + * + * @category Data + * @package StatusNet + * @author Evan Prodromou + * @copyright 2009 StatusNet Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; + +class Inbox extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'inbox'; // table name + public $user_id; // int(4) primary_key not_null + public $notice_ids; // blob + + /* Static get */ + function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Inbox',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + function sequenceKey() + { + return array(false, false, false); + } +} -- cgit v1.2.3-54-g00ecf From 475bf6010f18a17042d9171998c82e787c5abf74 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 29 Dec 2009 22:03:06 -0800 Subject: flip exe bit --- classes/Inbox.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 classes/Inbox.php (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php old mode 100755 new mode 100644 -- cgit v1.2.3-54-g00ecf From f2a403589cc8858ec3d3ce70f09709f36277c348 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 30 Dec 2009 09:06:07 -1000 Subject: Use inbox instead of notice_inbox --- classes/Inbox.php | 53 +++++++++++++++++++++++++++++++++++ classes/Notice.php | 20 ++------------ classes/User.php | 81 ++++++++++++------------------------------------------ 3 files changed, 72 insertions(+), 82 deletions(-) (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php index 35f532c06..de48d7381 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -31,6 +31,8 @@ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; class Inbox extends Memcached_DataObject { + const BOXCAR = 128; + ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ @@ -48,4 +50,55 @@ class Inbox extends Memcached_DataObject { return array(false, false, false); } + + static function insertNotice($user_id, $notice_id) + { + $inbox = new Inbox(); + + $inbox->query(sprintf('UPDATE inbox '. + 'set notice_ids = concat(cast(%08x as binary(4)), '. + 'substr(notice_ids, 1, 4092)) '. + 'WHERE user_id = %d', + $notice_id, $user_id)); + } + + static function bulkInsert($notice_id, $user_ids) + { + $cnt = count($user_ids); + + for ($off = 0; $off < $cnt; $off += self::BOXCAR) { + + $boxcar = array_slice($user_ids, $off, self::BOXCAR); + + if (empty($boxcar)) { // jump in, hobo! + break; + } + + $inbox = new Inbox(); + + $inbox->query(sprintf('UPDATE inbox '. + 'set notice_ids = concat(cast(%08x as binary(4)), '. + 'substr(notice_ids, 1, 4092)) '. + 'WHERE user_id in (%s)', + $notice_id, implode(',', $boxcar))); + + $inbox->free(); + } + } + + function stream($user_id, $offset, $limit, $since_id, $max_id, $since, $own=false) + { + $inbox = Inbox::staticGet('user_id', $user_id); + + if (empty($inbox)) { + return array(); + } + + $ids = unpack('L*', $inbox->notice_ids); + + // XXX: handle since_id + // XXX: handle max_id + + $ids = array_slice($ids, $offset, $limit); + } } diff --git a/classes/Notice.php b/classes/Notice.php index 9bda47827..8783912e8 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -125,8 +125,7 @@ class Notice extends Memcached_DataObject 'Fave', 'Notice_tag', 'Group_inbox', - 'Queue_item', - 'Notice_inbox'); + 'Queue_item'); foreach ($related as $cls) { $inst = new $cls(); @@ -504,17 +503,6 @@ class Notice extends Memcached_DataObject unset($original); } - $ni = new Notice_inbox(); - - $ni->notice_id = $this->id; - - if ($ni->find()) { - while ($ni->fetch()) { - $tmk = common_cache_key('user:repeated_to_me:'.$ni->user_id); - $cache->delete($tmk); - } - } - $ni->free(); unset($ni); } @@ -844,10 +832,6 @@ class Notice extends Memcached_DataObject function addToInboxes() { - // XXX: loads constants - - $inbox = new Notice_inbox(); - $users = $this->getSubscribedUsers(); // FIXME: kind of ignoring 'transitional'... @@ -887,7 +871,7 @@ class Notice extends Memcached_DataObject } } - Notice_inbox::bulkInsert($this->id, $this->created, $ni); + Inbox::bulkInsert($this->id, array_keys($ni)); return; } diff --git a/classes/User.php b/classes/User.php index 34151778c..773723da4 100644 --- a/classes/User.php +++ b/classes/User.php @@ -291,6 +291,19 @@ class User extends Memcached_DataObject return false; } + // Everyone gets an inbox + + $inbox = new Inbox(); + + $inbox->user_id = $user->id; + + $result = $inbox->insert(); + + if (!$result) { + common_log_db_error($inbox, 'INSERT', __FILE__); + return false; + } + // Everyone is subscribed to themself $subscription = new Subscription(); @@ -482,89 +495,30 @@ class User extends Memcached_DataObject function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { - $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false); - + $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false); return Notice::getStreamByIds($ids); } function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { - $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true); - + $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true); return Notice::getStreamByIds($ids); } function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { - $ids = Notice::stream(array($this, '_friendsTimelineDirect'), - array(false), - 'user:friends_timeline:'.$this->id, - $offset, $limit, $since_id, $before_id, $since); + $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false); return Notice::getStreamByIds($ids); } function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { - $ids = Notice::stream(array($this, '_friendsTimelineDirect'), - array(true), - 'user:friends_timeline_own:'.$this->id, - $offset, $limit, $since_id, $before_id, $since); + $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true); return Notice::getStreamByIds($ids); } - function _friendsTimelineDirect($own, $offset, $limit, $since_id, $max_id, $since) - { - $qry = - 'SELECT notice.id AS id ' . - 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' . - 'WHERE notice_inbox.user_id = ' . $this->id . ' ' . - 'AND notice.repeat_of IS NULL '; - - if (!$own) { - // XXX: autoload notice inbox for constant - $inbox = new Notice_inbox(); - - $qry .= 'AND notice_inbox.source != ' . NOTICE_INBOX_SOURCE_GATEWAY . ' '; - } - - if ($since_id != 0) { - $qry .= 'AND notice.id > ' . $since_id . ' '; - } - - if ($max_id != 0) { - $qry .= 'AND notice.id <= ' . $max_id . ' '; - } - - if (!is_null($since)) { - $qry .= 'AND notice.modified > \'' . date('Y-m-d H:i:s', $since) . '\' '; - } - - // NOTE: we sort by fave time, not by notice time! - - $qry .= 'ORDER BY notice_id DESC '; - - if (!is_null($offset)) { - $qry .= "LIMIT $limit OFFSET $offset"; - } - - $ids = array(); - - $notice = new Notice(); - - $notice->query($qry); - - while ($notice->fetch()) { - $ids[] = $notice->id; - } - - $notice->free(); - $notice = NULL; - - return $ids; - } - function blowFavesCache() { $cache = common_memcache(); @@ -777,7 +731,6 @@ class User extends Memcached_DataObject 'Remember_me', 'Foreign_link', 'Invitation', - 'Notice_inbox', ); Event::handle('UserDeleteRelated', array($this, &$related)); -- cgit v1.2.3-54-g00ecf From 1e7ec69190e089f89b57181d561e2df2c8bdd3b5 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Thu, 31 Dec 2009 09:09:07 -1000 Subject: some formatting changes to make inblobs work --- classes/Inbox.php | 8 +++++--- classes/User.php | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php index de48d7381..610b5fceb 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -56,7 +56,7 @@ class Inbox extends Memcached_DataObject $inbox = new Inbox(); $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(%08x as binary(4)), '. + 'set notice_ids = concat(cast(0x%08x as binary(4)), '. 'substr(notice_ids, 1, 4092)) '. 'WHERE user_id = %d', $notice_id, $user_id)); @@ -77,7 +77,7 @@ class Inbox extends Memcached_DataObject $inbox = new Inbox(); $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(%08x as binary(4)), '. + 'set notice_ids = concat(cast(0x%08x as binary(4)), '. 'substr(notice_ids, 1, 4092)) '. 'WHERE user_id in (%s)', $notice_id, implode(',', $boxcar))); @@ -94,11 +94,13 @@ class Inbox extends Memcached_DataObject return array(); } - $ids = unpack('L*', $inbox->notice_ids); + $ids = unpack('N*', $inbox->notice_ids); // XXX: handle since_id // XXX: handle max_id $ids = array_slice($ids, $offset, $limit); + + return $ids; } } diff --git a/classes/User.php b/classes/User.php index 773723da4..bde3f71b9 100644 --- a/classes/User.php +++ b/classes/User.php @@ -296,6 +296,7 @@ class User extends Memcached_DataObject $inbox = new Inbox(); $inbox->user_id = $user->id; + $inbox->notice_ids = ''; $result = $inbox->insert(); -- cgit v1.2.3-54-g00ecf From 920c878221cf87c1cf74baabbf0636337550eeab Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 9 Jan 2010 10:01:07 -0800 Subject: initialize an Inbox from Notice_inbox records --- classes/Inbox.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 7 deletions(-) (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php index 610b5fceb..de52e1e0c 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -51,15 +51,73 @@ class Inbox extends Memcached_DataObject return array(false, false, false); } - static function insertNotice($user_id, $notice_id) + /** + * Create a new inbox from existing Notice_inbox stuff + */ + + static function initialize($user_id) { + $ids = array(); + + $ni = new Notice_inbox(); + + $ni->user_id = $user_id; + $ni->selectAdd(); + $ni->selectAdd('notice_id'); + $ni->orderBy('notice_id DESC'); + $ni->limit(0, 1024); + + if ($ni->find()) { + while($ni->fetch()) { + $ids[] = $ni->notice_id; + } + } + + $ni->free(); + unset($ni); + $inbox = new Inbox(); - $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(0x%08x as binary(4)), '. - 'substr(notice_ids, 1, 4092)) '. - 'WHERE user_id = %d', - $notice_id, $user_id)); + $inbox->user_id = $user_id; + $inbox->notice_ids = pack('N*', $ids); + + $result = $inbox->insert(); + + if (!$result) { + common_log_db_error($inbox, 'INSERT', __FILE__); + return null; + } + + return $inbox; + } + + static function insertNotice($user_id, $notice_id) + { + $inbox = Inbox::staticGet('user_id', $user_id); + + if (empty($inbox)) { + $inbox = Inbox::initialize($user_id); + } + + if (empty($inbox)) { + return false; + } + + $result = $inbox->query(sprintf('UPDATE inbox '. + 'set notice_ids = concat(cast(0x%08x as binary(4)), '. + 'substr(notice_ids, 1, 4092)) '. + 'WHERE user_id = %d', + $notice_id, $user_id)); + + if ($result) { + $c = $this->memcache(); + + if (!empty($c)) { + $c->delete($this->cacheKey($this->tableName(), 'user_id', $user_id)); + } + } + + return $result; } static function bulkInsert($notice_id, $user_ids) @@ -91,7 +149,10 @@ class Inbox extends Memcached_DataObject $inbox = Inbox::staticGet('user_id', $user_id); if (empty($inbox)) { - return array(); + $inbox = Inbox::initialize($user_id); + if (empty($inbox)) { + return array(); + } } $ids = unpack('N*', $inbox->notice_ids); -- cgit v1.2.3-54-g00ecf From 5e81149e4773799b2dcf426987e6263b8841c5cc Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 9 Jan 2010 10:26:50 -0800 Subject: create new Inbox from Notice_inbox if not exists at read/write time --- classes/Inbox.php | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'classes/Inbox.php') diff --git a/classes/Inbox.php b/classes/Inbox.php index de52e1e0c..e14d4f4e7 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -79,7 +79,7 @@ class Inbox extends Memcached_DataObject $inbox = new Inbox(); $inbox->user_id = $user_id; - $inbox->notice_ids = pack('N*', $ids); + $inbox->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids)); $result = $inbox->insert(); @@ -110,10 +110,10 @@ class Inbox extends Memcached_DataObject $notice_id, $user_id)); if ($result) { - $c = $this->memcache(); + $c = self::memcache(); if (!empty($c)) { - $c->delete($this->cacheKey($this->tableName(), 'user_id', $user_id)); + $c->delete(self::cacheKey('inbox', 'user_id', $user_id)); } } @@ -122,25 +122,9 @@ class Inbox extends Memcached_DataObject static function bulkInsert($notice_id, $user_ids) { - $cnt = count($user_ids); - - for ($off = 0; $off < $cnt; $off += self::BOXCAR) { - - $boxcar = array_slice($user_ids, $off, self::BOXCAR); - - if (empty($boxcar)) { // jump in, hobo! - break; - } - - $inbox = new Inbox(); - - $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(0x%08x as binary(4)), '. - 'substr(notice_ids, 1, 4092)) '. - 'WHERE user_id in (%s)', - $notice_id, implode(',', $boxcar))); - - $inbox->free(); + foreach ($user_ids as $user_id) + { + Inbox::insertNotice($user_id, $notice_id); } } -- cgit v1.2.3-54-g00ecf