From 9215d330adda5abcede1c450e8bcf700c42ac39a Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 8 Sep 2011 14:28:17 -0700 Subject: [PATCH] Fix generateChronologicalKey() for 32-bit machines Summary: This method relies on 64-bit math being available, which isn't a safe assumption. Use the builtin bc functions instead for arbitrarily large integers. Test Plan: @skrul, can you apply this locally and let me know if it works? Reviewers: skrul, hunterbridges, jungejason, nh, tuomaspelkonen, aran Reviewed By: skrul CC: aran, skrul, epriestley Differential Revision: 912 --- .../PhabricatorFeedStoryPublisher.php | 22 ++++++++++++++++++- .../data/PhabricatorObjectHandleData.php | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/applications/feed/publisher/PhabricatorFeedStoryPublisher.php b/src/applications/feed/publisher/PhabricatorFeedStoryPublisher.php index 6f5d541ed6..ae64f0f20f 100644 --- a/src/applications/feed/publisher/PhabricatorFeedStoryPublisher.php +++ b/src/applications/feed/publisher/PhabricatorFeedStoryPublisher.php @@ -108,6 +108,26 @@ final class PhabricatorFeedStoryPublisher { // Generate a random number for the lower 32 bits of the key. $rand = head(unpack('L', Filesystem::readRandomBytes(4))); - return ($time << 32) + ($rand); + // On 32-bit machines, we have to get creative. + if (PHP_INT_SIZE < 8) { + // We're on a 32-bit machine. + if (function_exists('bcadd')) { + // Try to use the 'bc' extension. + return bcadd(bcmul($time, bcpow(2, 32)), $rand); + } else { + // Do the math in MySQL. TODO: If we formalize a bc dependency, get + // rid of this. + $conn_r = id(new PhabricatorFeedStoryData())->establishConnection('r'); + $result = queryfx_one( + $conn_r, + 'SELECT (%d << 32) + %d as N', + $time, + $rand); + return $result['N']; + } + } else { + // This is a 64 bit machine, so we can just do the math. + return ($time << 32) + $rand; + } } } diff --git a/src/applications/phid/handle/data/PhabricatorObjectHandleData.php b/src/applications/phid/handle/data/PhabricatorObjectHandleData.php index 4c526cba7a..3322af127b 100644 --- a/src/applications/phid/handle/data/PhabricatorObjectHandleData.php +++ b/src/applications/phid/handle/data/PhabricatorObjectHandleData.php @@ -21,7 +21,7 @@ class PhabricatorObjectHandleData { private $phids; public function __construct(array $phids) { - $this->phids = $phids; + $this->phids = array_unique($phids); } public function loadObjects() {