Skip to content

Commit 754705d

Browse files
committed
Conpherence - get back-end prepped for loading less transactions all the time
Summary: this just does the back-end migration. I realized that we don't need to keep track of cacheTitle and cachePhoto since those are based off recent participation handles and dynamic relative to who is viewing it. Also kept the "last seen phid" as I think that will be useful to have auto-scroll to where you last read. Ref T2867. Test Plan: did the migration. observed sensical values in the database. created a new conpherence - again sensical values. updated a conpherence - more sensical values. Reviewers: epriestley, chad Reviewed By: epriestley CC: aran, Korvin, AnhNhan Maniphest Tasks: T2867 Differential Revision: https://secure.phabricator.com/D5567
1 parent a56846e commit 754705d

File tree

8 files changed

+132
-6
lines changed

8 files changed

+132
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER TABLE {$NAMESPACE}_conpherence.conpherence_thread
2+
ADD recentParticipantPHIDs LONGTEXT NOT NULL COLLATE utf8_bin AFTER title,
3+
ADD messageCount BIGINT UNSIGNED NOT NULL AFTER title;
4+
5+
ALTER TABLE {$NAMESPACE}_conpherence.conpherence_participant
6+
ADD seenMessageCount BIGINT UNSIGNED NOT NULL AFTER behindTransactionPHID;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
echo "Migrating data from conpherence transactions to conpherence 'cache'...\n";
4+
5+
$table = new ConpherenceThread();
6+
$table->openTransaction();
7+
$conn_w = $table->establishConnection('w');
8+
9+
$participant_table = new ConpherenceParticipant();
10+
11+
$conpherences = new LiskMigrationIterator($table);
12+
foreach ($conpherences as $conpherence) {
13+
echo 'Migrating conpherence #'.$conpherence->getID()."\n";
14+
15+
$participants = id(new ConpherenceParticipant())
16+
->loadAllWhere('conpherencePHID = %s', $conpherence->getPHID());
17+
18+
$transactions = id(new ConpherenceTransaction())
19+
->loadAllWhere('objectPHID = %s', $conpherence->getPHID());
20+
21+
$participation_hash = mgroup($participants, 'getBehindTransactionPHID');
22+
23+
$message_count = 0;
24+
$participants_to_cache = array();
25+
foreach ($transactions as $transaction) {
26+
$participants_to_cache[] = $transaction->getAuthorPHID();
27+
if ($transaction->getTransactionType() ==
28+
PhabricatorTransactions::TYPE_COMMENT) {
29+
$message_count++;
30+
}
31+
$participants_to_update = idx(
32+
$participation_hash,
33+
$transaction->getPHID(),
34+
array());
35+
if ($participants_to_update) {
36+
queryfx(
37+
$conn_w,
38+
'UPDATE %T SET seenMessageCount = %d '.
39+
'WHERE conpherencePHID = %s AND participantPHID IN (%Ls)',
40+
$participant_table->getTableName(),
41+
$message_count,
42+
$conpherence->getPHID(),
43+
mpull($participants_to_update, 'getParticipantPHID'));
44+
}
45+
}
46+
47+
$participants_to_cache = array_slice(
48+
array_unique(array_reverse($participants_to_cache)),
49+
0,
50+
10);
51+
queryfx(
52+
$conn_w,
53+
'UPDATE %T '.
54+
'SET recentParticipantPHIDs = %s, '.
55+
'messageCount = %d '.
56+
'WHERE phid = %s',
57+
$table->getTableName(),
58+
json_encode($participants_to_cache),
59+
$message_count,
60+
$conpherence->getPHID());
61+
}
62+
63+
$table->saveTransaction();
64+
echo "\nDone.\n";

src/applications/conpherence/controller/ConpherenceNewController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function processRequest() {
1111

1212
$conpherence = id(new ConpherenceThread())
1313
->attachParticipants(array())
14-
->attachFilePHIDs(array());
14+
->attachFilePHIDs(array())
15+
->setMessageCount(0);
1516
$title = pht('New Conversation');
1617
$participants = array();
1718
$message = '';
@@ -34,6 +35,8 @@ public function processRequest() {
3435
} else {
3536
$participants[] = $user->getPHID();
3637
$participants = array_unique($participants);
38+
$conpherence->setRecentParticipantPHIDs(
39+
array_slice($participants, 0, 10));
3740
}
3841

3942
$message = $request->getStr('message');

src/applications/conpherence/controller/ConpherenceViewController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function processRequest() {
4949
$transactions = $conpherence->getTransactions();
5050
$latest_transaction = end($transactions);
5151
$write_guard = AphrontWriteGuard::beginScopedUnguardedWrites();
52-
$participant->markUpToDate($latest_transaction);
52+
$participant->markUpToDate($conpherence, $latest_transaction);
5353
unset($write_guard);
5454

5555
$header = $this->renderHeaderPaneContent();

src/applications/conpherence/editor/ConpherenceEditor.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,32 @@ protected function getCustomTransactionNewValue(
8686
}
8787
}
8888

89-
protected function applyCustomInternalTransaction(
89+
/**
90+
* We really only need a read lock if we have a comment. In that case, we
91+
* must update the messagesCount field on the conpherence and
92+
* seenMessagesCount(s) for the participant(s).
93+
*/
94+
protected function shouldReadLock(
9095
PhabricatorLiskDAO $object,
9196
PhabricatorApplicationTransaction $xaction) {
9297

98+
$lock = false;
9399
switch ($xaction->getTransactionType()) {
100+
case PhabricatorTransactions::TYPE_COMMENT:
101+
$lock = true;
102+
break;
103+
}
104+
105+
return $lock;
106+
}
107+
108+
protected function applyCustomInternalTransaction(
109+
PhabricatorLiskDAO $object,
110+
PhabricatorApplicationTransaction $xaction) {
111+
switch ($xaction->getTransactionType()) {
112+
case PhabricatorTransactions::TYPE_COMMENT:
113+
$object->setMessageCount((int)$object->getMessageCount() + 1);
114+
break;
94115
case ConpherenceTransactionType::TYPE_TITLE:
95116
$object->setTitle($xaction->getNewValue());
96117
break;
@@ -105,6 +126,18 @@ protected function applyCustomInternalTransaction(
105126
ConpherenceImageData::SIZE_HEAD);
106127
break;
107128
}
129+
$this->updateRecentParticipantPHIDs($object, $xaction);
130+
}
131+
132+
private function updateRecentParticipantPHIDs(
133+
PhabricatorLiskDAO $object,
134+
PhabricatorApplicationTransaction $xaction) {
135+
136+
$participants = $object->getRecentParticipantPHIDs();
137+
array_unshift($participants, $xaction->getAuthorPHID());
138+
$participants = array_slice(array_unique($participants), 0, 10);
139+
140+
$object->setRecentParticipantPHIDs($participants);
108141
}
109142

110143
/**
@@ -148,10 +181,13 @@ protected function applyCustomExternalTransaction(
148181
if ($phid != $user->getPHID()) {
149182
if ($participant->getParticipationStatus() != $behind) {
150183
$participant->setBehindTransactionPHID($xaction_phid);
184+
// decrement one as this is the message putting them behind!
185+
$participant->setSeenMessageCount($object->getMessageCount() - 1);
151186
}
152187
$participant->setParticipationStatus($behind);
153188
$participant->setDateTouched($time);
154189
} else {
190+
$participant->setSeenMessageCount($object->getMessageCount());
155191
$participant->setParticipationStatus($up_to_date);
156192
$participant->setDateTouched($time);
157193
}
@@ -176,8 +212,10 @@ protected function applyCustomExternalTransaction(
176212
foreach ($add as $phid) {
177213
if ($phid == $this->getActor()->getPHID()) {
178214
$status = ConpherenceParticipationStatus::UP_TO_DATE;
215+
$message_count = $object->getMessageCount();
179216
} else {
180217
$status = ConpherenceParticipationStatus::BEHIND;
218+
$message_count = 0;
181219
}
182220
$participants[$phid] =
183221
id(new ConpherenceParticipant())
@@ -186,6 +224,7 @@ protected function applyCustomExternalTransaction(
186224
->setParticipationStatus($status)
187225
->setDateTouched(time())
188226
->setBehindTransactionPHID($xaction->getPHID())
227+
->setSeenMessageCount($message_count)
189228
->save();
190229
}
191230
$object->attachParticipants($participants);

src/applications/conpherence/storage/ConpherenceParticipant.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ final class ConpherenceParticipant extends ConpherenceDAO {
99
protected $conpherencePHID;
1010
protected $participationStatus;
1111
protected $behindTransactionPHID;
12+
protected $seenMessageCount;
1213
protected $dateTouched;
1314
protected $settings = array();
1415

@@ -24,10 +25,13 @@ public function getSettings() {
2425
return nonempty($this->settings, array());
2526
}
2627

27-
public function markUpToDate(ConpherenceTransaction $xaction) {
28+
public function markUpToDate(
29+
ConpherenceThread $conpherence,
30+
ConpherenceTransaction $xaction) {
2831
if (!$this->isUpToDate()) {
2932
$this->setParticipationStatus(ConpherenceParticipationStatus::UP_TO_DATE);
3033
$this->setBehindTransactionPHID($xaction->getPHID());
34+
$this->setSeenMessageCount($conpherence->getMessageCount());
3135
$this->save();
3236
}
3337
return $this;

src/applications/conpherence/storage/ConpherenceThread.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ final class ConpherenceThread extends ConpherenceDAO
99
protected $id;
1010
protected $phid;
1111
protected $title;
12+
protected $messageCount;
13+
protected $recentParticipantPHIDs = array();
1214
protected $imagePHIDs = array();
1315
protected $mailKey;
1416

@@ -23,7 +25,8 @@ public function getConfiguration() {
2325
return array(
2426
self::CONFIG_AUX_PHID => true,
2527
self::CONFIG_SERIALIZATION => array(
26-
'imagePHIDs' => self::SERIALIZATION_JSON,
28+
'recentParticipantPHIDs' => self::SERIALIZATION_JSON,
29+
'imagePHIDs' => self::SERIALIZATION_JSON,
2730
),
2831
) + parent::getConfiguration();
2932
}

src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,14 @@ public function getPatches() {
12261226
'type' => 'sql',
12271227
'name' => $this->getPatchPath('20130330.phrequent.sql'),
12281228
),
1229+
'20130403.conpherencecache.sql' => array(
1230+
'type' => 'sql',
1231+
'name' => $this->getPatchPath('20130403.conpherencecache.sql'),
1232+
),
1233+
'20130403.conpherencecachemig.php' => array(
1234+
'type' => 'php',
1235+
'name' => $this->getPatchPath('20130403.conpherencecachemig.php'),
1236+
)
12291237
);
12301238
}
1231-
12321239
}

0 commit comments

Comments
 (0)