From 8fb1317c59e982cdcd19ae0a3c3235447b470c8c Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 3 Jan 2014 11:24:11 -0800 Subject: [PATCH] Use file handle caching in newSequentialPerNodeIDs() * This is now similar to the other methods in that regard Change-Id: I36163fa7f1bc13d493df9d77b139a99881d0de45 --- includes/utils/UIDGenerator.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/includes/utils/UIDGenerator.php b/includes/utils/UIDGenerator.php index 47cef8b6f4..e60293b803 100644 --- a/includes/utils/UIDGenerator.php +++ b/includes/utils/UIDGenerator.php @@ -243,6 +243,21 @@ class UIDGenerator { * @since 1.23 */ public static function newSequentialPerNodeIDs( $bucket, $bits, $count, $flags = 0 ) { + $gen = self::singleton(); + return $gen->getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ); + } + + /** + * Return IDs that are sequential *only* for this node and bucket + * + * @see UIDGenerator::newSequentialPerNodeID() + * @param string $bucket Arbitrary bucket name (should be ASCII) + * @param integer $bits Bit size (16 to 48) of resulting numbers before wrap-around + * @param integer $count Number of IDs to return (1 to 10000) + * @param integer $flags (supports UIDGenerator::QUICK_VOLATILE) + * @return array Ordered list of float integer values + */ + protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) { if ( $count <= 0 ) { return array(); // nothing to do } elseif ( $count > 10000 ) { @@ -274,7 +289,13 @@ class UIDGenerator { // Note: use of fmod() avoids "division by zero" on 32 bit machines if ( $counter === null ) { $path = wfTempDir() . '/mw-' . __CLASS__ . '-' . rawurlencode( $bucket ) . '-48'; - $handle = fopen( $path, 'cb+' ); + // Get the UID lock file handle + if ( isset( $this->fileHandles[$path] ) ) { + $handle = $this->fileHandles[$path]; + } else { + $handle = fopen( $path, 'cb+' ); + $this->fileHandles[$path] = $handle ?: null; // cache + } // Acquire the UID lock file if ( $handle === false ) { throw new MWException( "Could not open '{$path}'." ); @@ -292,8 +313,8 @@ class UIDGenerator { fflush( $handle ); // Release the UID lock file flock( $handle, LOCK_UN ); - fclose( $handle ); } + $ids = array(); $divisor = pow( 2, $bits ); $currentId = floor( $counter - $count ); // pre-increment counter value -- 2.20.1