public function __construct( BagOStuff $store, array $client, $posIndex = null ) {
$this->store = $store;
$this->clientId = md5( $client['ip'] . "\n" . $client['agent'] );
- $this->key = $store->makeGlobalKey( __CLASS__, $this->clientId, 'v1' );
+ $this->key = $store->makeGlobalKey( __CLASS__, $this->clientId, 'v2' );
$this->waitForPosIndex = $posIndex;
$this->logger = new NullLogger();
}
namespace Wikimedia\Rdbms;
use InvalidArgumentException;
+use UnexpectedValueException;
/**
* DBMasterPos class for MySQL/MariaDB
* @param float $asOfTime UNIX timestamp
*/
public function __construct( $position, $asOfTime ) {
+ $this->init( $position, $asOfTime );
+ }
+
+ /**
+ * @param string $position
+ * @param float $asOfTime
+ */
+ protected function init( $position, $asOfTime ) {
$m = [];
if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', $position, $m ) ) {
$this->binlog = $m[1]; // ideally something like host name
} else {
$gtids = array_filter( array_map( 'trim', explode( ',', $position ) ) );
foreach ( $gtids as $gtid ) {
- if ( !$this->parseGTID( $gtid ) ) {
+ if ( !self::parseGTID( $gtid ) ) {
throw new InvalidArgumentException( "Invalid GTID '$gtid'." );
}
$this->gtids[] = $gtid;
? [ 'binlog' => $this->binlog, 'pos' => $this->pos ]
: false;
}
+
+ public function serialize() {
+ return serialize( [ 'position' => $this->__toString(), 'asOfTime' => $this->asOfTime ] );
+ }
+
+ public function unserialize( $serialized ) {
+ $data = unserialize( $serialized );
+ if ( !is_array( $data ) ) {
+ throw new UnexpectedValueException( __METHOD__ . ": cannot unserialize position" );
+ }
+
+ $this->init( $data['position'], $data['asOfTime'] );
+ }
}
$db->clearFlag( Database::DBO_IGNORE );
}
+
+ /**
+ * @covers Wikimedia\Rdbms\MySQLMasterPos
+ */
+ public function testSerialize() {
+ $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
+ $roundtripPos = unserialize( serialize( $pos ) );
+
+ $this->assertEquals( $pos, $roundtripPos );
+
+ $pos = new MySQLMasterPos( '255-11-23', 53636363 );
+ $roundtripPos = unserialize( serialize( $pos ) );
+
+ $this->assertEquals( $pos, $roundtripPos );
+ }
}