Skip to content

Commit 2b45c0a

Browse files
committed
PHPLIB-110: Extract Find and FindOne operation classes
1 parent 60931d8 commit 2b45c0a

File tree

4 files changed

+330
-191
lines changed

4 files changed

+330
-191
lines changed

src/Collection.php

Lines changed: 15 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MongoDB\Driver\Command;
77
use MongoDB\Driver\Cursor;
88
use MongoDB\Driver\Manager;
9-
use MongoDB\Driver\Query;
109
use MongoDB\Driver\ReadPreference;
1110
use MongoDB\Driver\Server;
1211
use MongoDB\Driver\WriteConcern;
@@ -20,6 +19,8 @@
2019
use MongoDB\Operation\Distinct;
2120
use MongoDB\Operation\DropCollection;
2221
use MongoDB\Operation\DropIndexes;
22+
use MongoDB\Operation\Find;
23+
use MongoDB\Operation\FindOne;
2324
use MongoDB\Operation\FindOneAndDelete;
2425
use MongoDB\Operation\FindOneAndReplace;
2526
use MongoDB\Operation\FindOneAndUpdate;
@@ -29,20 +30,6 @@
2930
class Collection
3031
{
3132
/* {{{ consts & vars */
32-
const QUERY_FLAG_TAILABLE_CURSOR = 0x02;
33-
const QUERY_FLAG_SLAVE_OKAY = 0x04;
34-
const QUERY_FLAG_OPLOG_REPLY = 0x08;
35-
const QUERY_FLAG_NO_CURSOR_TIMEOUT = 0x10;
36-
const QUERY_FLAG_AWAIT_DATA = 0x20;
37-
const QUERY_FLAG_EXHAUST = 0x40;
38-
const QUERY_FLAG_PARTIAL = 0x80;
39-
40-
41-
const CURSOR_TYPE_NON_TAILABLE = 0x00;
42-
const CURSOR_TYPE_TAILABLE = self::QUERY_FLAG_TAILABLE_CURSOR;
43-
//self::QUERY_FLAG_TAILABLE_CURSOR | self::QUERY_FLAG_AWAIT_DATA;
44-
const CURSOR_TYPE_TAILABLE_AWAIT = 0x22;
45-
4633
protected $manager;
4734
protected $ns;
4835
protected $wc;
@@ -396,50 +383,37 @@ public function dropIndexes()
396383
}
397384

398385
/**
399-
* Performs a find (query) on the collection
386+
* Finds documents matching the query.
400387
*
388+
* @see Find::__construct() for supported options
401389
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
402-
* @see Collection::getFindOptions() for supported $options
403-
*
404-
* @param array $filter The find query to execute
405-
* @param array $options Additional options
390+
* @param array $filter Query by which to filter documents
391+
* @param array $options Additional options
406392
* @return Cursor
407393
*/
408394
public function find(array $filter = array(), array $options = array())
409395
{
410-
$options = array_merge($this->getFindOptions(), $options);
411-
412-
$query = $this->_buildQuery($filter, $options);
413-
414-
$cursor = $this->manager->executeQuery($this->ns, $query, $this->rp);
396+
$operation = new Find($this->dbname, $this->collname, $filter, $options);
397+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
415398

416-
return $cursor;
399+
return $operation->execute($server);
417400
}
418401

419402
/**
420-
* Performs a find (query) on the collection, returning at most one result
403+
* Finds a single document matching the query.
421404
*
405+
* @see FindOne::__construct() for supported options
422406
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
423-
* @see Collection::getFindOptions() for supported $options
424-
*
425407
* @param array $filter The find query to execute
426408
* @param array $options Additional options
427-
* @return array|false The matched document, or false on failure
409+
* @return object|null
428410
*/
429411
public function findOne(array $filter = array(), array $options = array())
430412
{
431-
$options = array_merge($this->getFindOptions(), array("limit" => 1), $options);
432-
433-
$query = $this->_buildQuery($filter, $options);
434-
435-
$cursor = $this->manager->executeQuery($this->ns, $query, $this->rp);
436-
437-
$array = iterator_to_array($cursor);
438-
if ($array) {
439-
return $array[0];
440-
}
413+
$operation = new FindOne($this->dbname, $this->collname, $filter, $options);
414+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
441415

442-
return false;
416+
return $operation->execute($server);
443417
}
444418

445419
/**
@@ -536,107 +510,6 @@ public function getDatabaseName()
536510
return $this->dbname;
537511
}
538512

539-
/**
540-
* Retrieves all find options with their default values.
541-
*
542-
* @return array of Collection::find() options
543-
*/
544-
public function getFindOptions()
545-
{
546-
return array(
547-
/**
548-
* Get partial results from a mongos if some shards are down (instead of throwing an error).
549-
*
550-
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
551-
*/
552-
"allowPartialResults" => false,
553-
554-
/**
555-
* The number of documents to return per batch.
556-
*
557-
* @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
558-
*/
559-
"batchSize" => 101,
560-
561-
/**
562-
* Attaches a comment to the query. If $comment also exists
563-
* in the modifiers document, the comment field overwrites $comment.
564-
*
565-
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
566-
*/
567-
"comment" => "",
568-
569-
/**
570-
* Indicates the type of cursor to use. This value includes both
571-
* the tailable and awaitData options.
572-
* The default is Collection::CURSOR_TYPE_NON_TAILABLE.
573-
*
574-
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
575-
*/
576-
"cursorType" => self::CURSOR_TYPE_NON_TAILABLE,
577-
578-
/**
579-
* The maximum number of documents to return.
580-
*
581-
* @see http://docs.mongodb.org/manual/reference/method/cursor.limit/
582-
*/
583-
"limit" => 0,
584-
585-
/**
586-
* The maximum amount of time to allow the query to run. If $maxTimeMS also exists
587-
* in the modifiers document, the maxTimeMS field overwrites $maxTimeMS.
588-
*
589-
* @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/
590-
*/
591-
"maxTimeMS" => 0,
592-
593-
/**
594-
* Meta-operators modifying the output or behavior of a query.
595-
*
596-
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
597-
*/
598-
"modifiers" => array(),
599-
600-
/**
601-
* The server normally times out idle cursors after an inactivity period (10 minutes)
602-
* to prevent excess memory use. Set this option to prevent that.
603-
*
604-
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
605-
*/
606-
"noCursorTimeout" => false,
607-
608-
/**
609-
* Internal replication use only - driver should not set
610-
*
611-
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
612-
* @internal
613-
*/
614-
"oplogReplay" => false,
615-
616-
/**
617-
* Limits the fields to return for all matching documents.
618-
*
619-
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
620-
*/
621-
"projection" => array(),
622-
623-
/**
624-
* The number of documents to skip before returning.
625-
*
626-
* @see http://docs.mongodb.org/manual/reference/method/cursor.skip/
627-
*/
628-
"skip" => 0,
629-
630-
/**
631-
* The order in which to return matching documents. If $orderby also exists
632-
* in the modifiers document, the sort field overwrites $orderby.
633-
*
634-
* @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
635-
*/
636-
"sort" => array(),
637-
);
638-
}
639-
640513
/**
641514
* Return the collection namespace.
642515
*
@@ -793,35 +666,6 @@ public function updateOne(array $filter, array $update, array $options = array()
793666
return new UpdateResult($wr);
794667
}
795668

796-
/**
797-
* Helper to build a Query object
798-
*
799-
* @param array $filter the query document
800-
* @param array $options query/protocol options
801-
* @return Query
802-
* @internal
803-
*/
804-
final protected function _buildQuery($filter, $options)
805-
{
806-
if ($options["comment"]) {
807-
$options["modifiers"]['$comment'] = $options["comment"];
808-
}
809-
if ($options["maxTimeMS"]) {
810-
$options["modifiers"]['$maxTimeMS'] = $options["maxTimeMS"];
811-
}
812-
if ($options["sort"]) {
813-
$options['$orderby'] = $options["sort"];
814-
}
815-
816-
$flags = $this->_opQueryFlags($options);
817-
$options["cursorFlags"] = $flags;
818-
819-
820-
$query = new Query($filter, $options);
821-
822-
return $query;
823-
}
824-
825669
/**
826670
* Internal helper for delete one/many documents
827671
* @internal
@@ -835,26 +679,6 @@ final protected function _delete($filter, $limit = 1)
835679
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
836680
}
837681

838-
/**
839-
* Constructs the Query Wire Protocol field 'flags' based on $options
840-
* provided to other helpers
841-
*
842-
* @param array $options
843-
* @return integer OP_QUERY Wire Protocol flags
844-
* @internal
845-
*/
846-
final protected function _opQueryFlags($options)
847-
{
848-
$flags = 0;
849-
850-
$flags |= $options["allowPartialResults"] ? self::QUERY_FLAG_PARTIAL : 0;
851-
$flags |= $options["cursorType"] ? $options["cursorType"] : 0;
852-
$flags |= $options["oplogReplay"] ? self::QUERY_FLAG_OPLOG_REPLY: 0;
853-
$flags |= $options["noCursorTimeout"] ? self::QUERY_FLAG_NO_CURSOR_TIMEOUT : 0;
854-
855-
return $flags;
856-
}
857-
858682
/**
859683
* Internal helper for replacing/updating one/many documents
860684
* @internal

0 commit comments

Comments
 (0)