Переглянути джерело

Merge branch 'slave-okay-helper'

* slave-okay-helper:
  Use slaveOkay helper in classes
  Add new slaveOkay helper
Andreas Braun 10 роки тому
батько
коміт
1b1945590a

+ 25 - 0
lib/Alcaeus/MongoDbAdapter/Helper/ReadPreference.php

@@ -69,6 +69,14 @@ trait ReadPreference
     }
 
     /**
+     * @return bool
+     */
+    protected function getSlaveOkayFromReadPreference()
+    {
+        return $this->readPreference->getMode() != \MongoDB\Driver\ReadPreference::RP_PRIMARY;
+    }
+
+    /**
      * @param string $readPreference
      * @param array $tags
      * @return bool
@@ -123,4 +131,21 @@ trait ReadPreference
 
         return $this->setReadPreferenceFromParameters($readPreference, $tags);
     }
+
+    /**
+     * @param bool $ok
+     * @return bool
+     */
+    protected function setReadPreferenceFromSlaveOkay($ok = true)
+    {
+        $result = $this->getSlaveOkayFromReadPreference();
+        $readPreference = new \MongoDB\Driver\ReadPreference(
+            $ok ? \MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED : \MongoDB\Driver\ReadPreference::RP_PRIMARY,
+            $ok ? $this->readPreference->getTagSets() : []
+        );
+
+        $this->readPreference = $readPreference;
+
+        return $result;
+    }
 }

+ 52 - 0
lib/Alcaeus/MongoDbAdapter/Helper/SlaveOkay.php

@@ -0,0 +1,52 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace Alcaeus\MongoDbAdapter\Helper;
+
+/**
+ * @internal
+ */
+trait SlaveOkay
+{
+    /**
+     * @return bool
+     */
+    abstract protected function getSlaveOkayFromReadPreference();
+
+    /**
+     * @param bool $ok
+     * @return bool
+     */
+    abstract protected function setReadPreferenceFromSlaveOkay($ok = true);
+
+    /**
+     * @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
+     * @return bool
+     */
+    public function getSlaveOkay()
+    {
+        return $this->getSlaveOkayFromReadPreference();
+    }
+
+    /**
+     * @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
+     * @param bool $ok
+     * @return bool
+     */
+    public function setSlaveOkay($ok = true)
+    {
+        return $this->setReadPreferenceFromSlaveOkay($ok);
+    }
+}

+ 1 - 19
lib/Mongo/MongoCollection.php

@@ -23,6 +23,7 @@ use Alcaeus\MongoDbAdapter\TypeConverter;
 class MongoCollection
 {
     use Helper\ReadPreference;
+    use Helper\SlaveOkay;
     use Helper\WriteConcern;
 
     const ASCENDING = 1;
@@ -186,25 +187,6 @@ class MongoCollection
     }
 
     /**
-     * @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
-     * @return bool
-     */
-    public function getSlaveOkay()
-    {
-        $this->notImplemented();
-    }
-
-    /**
-     * @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
-     * @param bool $ok
-     * @return bool
-     */
-    public function setSlaveOkay($ok = true)
-    {
-        $this->notImplemented();
-    }
-
-    /**
      * {@inheritdoc}
      */
     public function setReadPreference($readPreference, $tags = null)

+ 2 - 4
lib/Mongo/MongoCursor.php

@@ -326,12 +326,10 @@ class MongoCursor extends AbstractCursor implements Iterator
     public function slaveOkay($okay = true)
     {
         $this->errorIfOpened();
-        static::$slaveOkay = $okay;
 
-        $readPreferenceArray = $this->getReadPreference();
-        $readPreferenceArray['type'] = $okay ? \MongoClient::RP_SECONDARY_PREFERRED : \MongoClient::RP_PRIMARY;
+        $this->setReadPreferenceFromSlaveOkay($okay);
 
-        $this->setReadPreferenceFromArray($readPreferenceArray);
+        return $this;
     }
 
     /**

+ 1 - 26
lib/Mongo/MongoDB.php

@@ -23,6 +23,7 @@ use MongoDB\Model\CollectionInfo;
 class MongoDB
 {
     use Helper\ReadPreference;
+    use Helper\SlaveOkay;
     use Helper\WriteConcern;
 
     const PROFILING_OFF = 0;
@@ -168,17 +169,6 @@ class MongoDB
     }
 
     /**
-     * (PECL mongo &gt;= 1.1.0)<br/>
-     * Get slaveOkay setting for this database
-     * @link http://www.php.net/manual/en/mongodb.getslaveokay.php
-     * @return bool Returns the value of slaveOkay for this instance.
-     */
-    public function getSlaveOkay()
-    {
-        return false;
-    }
-
-    /**
      * (PECL mongo &gt;= 0.9.0)<br/>
      * Sets this database's profiling level
      * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php
@@ -229,21 +219,6 @@ class MongoDB
     }
 
     /**
-     * (PECL mongo &gt;= 1.1.0)<br/>
-     * Change slaveOkay setting for this database
-     * @link http://php.net/manual/en/mongodb.setslaveokay.php
-     * @param bool $ok [optional] <p>
-     * If reads should be sent to secondary members of a replica set for all
-     * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance.
-     * </p>
-     * @return bool Returns the former value of slaveOkay for this instance.
-     */
-    public function setSlaveOkay ($ok = true)
-    {
-        return false;
-    }
-
-    /**
      * Creates a collection
      * @link http://www.php.net/manual/en/mongodb.createcollection.php
      * @param string $name The name of the collection.

+ 8 - 0
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -141,15 +141,23 @@ class MongoCollectionTest extends TestCase
     {
         $collection = $this->getCollection();
         $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
+        $this->assertFalse($collection->getSlaveOkay());
 
         $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
         $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
+        $this->assertTrue($collection->getSlaveOkay());
 
         // Only way to check whether options are passed down is through debugInfo
         $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
 
         $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
         $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
+
+        $this->assertTrue($collection->setSlaveOkay(true));
+        $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
+
+        $this->assertTrue($collection->setSlaveOkay(false));
+        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
     }
 
     public function testReadPreferenceIsInherited()

+ 8 - 0
tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

@@ -46,15 +46,23 @@ class MongoDBTest extends TestCase
     {
         $database = $this->getDatabase();
         $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
+        $this->assertFalse($database->getSlaveOkay());
 
         $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
         $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
+        $this->assertTrue($database->getSlaveOkay());
 
         // Only way to check whether options are passed down is through debugInfo
         $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
 
         $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
         $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
+
+        $this->assertTrue($database->setSlaveOkay(true));
+        $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
+
+        $this->assertTrue($database->setSlaveOkay(false));
+        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
     }
 
     public function testReadPreferenceIsInherited()