Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match with enable logging and profiling #314

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ public function init()
$this->trigger(self::EVENT_INIT);
}

/**
* Returns the connection used by this ActiveQuery.
* @param Connection $db Mongo connection.
* @return Connection connection instance.
*/
public function getDb($db = null){
if($db !== null){
return $db;
}
$modelClass = $this->modelClass;
return $modelClass::getDb();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -183,15 +196,13 @@ public function modify($update, $options = [], $db = null)
public function getCollection($db = null)
{
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDb();
}

if ($this->from === null) {
$modelClass = $this->modelClass;
$this->from = $modelClass::collectionName();
}

return $db->getCollection($this->from);
return $this->getDb()->getCollection($this->from);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/BatchQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ protected function fetchData()
// @see https://jira.mongodb.org/browse/PHP-457
$this->query->addOptions(['batchSize' => $this->batchSize]);
}
$cursor = $this->query->buildCursor($this->db);
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
$db = $this->db === null ? yii::$app->mongodb : $this->db;
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$cursor = $this->query->buildCursor($db);
if($db->enableLogging){
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
}

if ($cursor instanceof \Iterator) {
$this->_iterator = $cursor;
Expand Down
16 changes: 12 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,25 @@ public function open()
}
$token = 'Opening MongoDB connection: ' . $this->dsn;
try {
Yii::trace($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
if ($this->enableLogging) {
Yii::trace($token, __METHOD__);
}
if ($this->enableProfiling) {
Yii::beginProfile($token, __METHOD__);
}
$options = $this->options;

$this->manager = new Manager($this->dsn, $options, $this->driverOptions);
$this->manager->selectServer($this->manager->getReadPreference());

$this->initConnection();
Yii::endProfile($token, __METHOD__);
if ($this->enableProfiling) {
Yii::endProfile($token, __METHOD__);
}
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
if ($this->enableProfiling) {
Yii::endProfile($token, __METHOD__);
}
throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
}

Expand Down
54 changes: 31 additions & 23 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class Query extends Component implements QueryInterface
*/
public $options = [];

/**
* Returns the connection used by this Query.
* @param Connection $db Mongo connection.
* @return Connection connection instance.
*/
public function getDb($db = null){
return $db === null ? Yii::$app->get('mongodb') : $db;
}

/**
* Returns the Mongo collection for this query.
Expand All @@ -68,11 +76,7 @@ class Query extends Component implements QueryInterface
*/
public function getCollection($db = null)
{
if ($db === null) {
$db = Yii::$app->get('mongodb');
}

return $db->getCollection($this->from);
return $this->getDb($db)->getCollection($this->from);
}

/**
Expand Down Expand Up @@ -203,25 +207,33 @@ public function buildCursor($db = null)

/**
* Fetches rows from the given Mongo cursor.
* @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from.
* @param bool $all whether to fetch all rows or only first one.
* @param string|callable $indexBy the column name or PHP callback,
* by which the query results should be indexed by.
* @param yii\mongodb\Connection $db the MongoDB connection used to fetch rows.
* @throws Exception on failure.
* @return array|bool result.
*/
protected function fetchRows($cursor, $all = true, $indexBy = null)
protected function fetchRows($all = true, $db = null)
{
$db = $this->getDb($db);
$cursor = $this->buildCursor($db);
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
if ($db->enableLogging) {
Yii::info($token, __METHOD__);
}
try {
Yii::beginProfile($token, __METHOD__);
if ($db->enableProfiling) {
Yii::beginProfile($token, __METHOD__);
}
$result = $this->fetchRowsInternal($cursor, $all);
Yii::endProfile($token, __METHOD__);
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
}

return $result;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
if ($db->enableProfiling) {
Yii::endProfile($token, __METHOD__);
}
throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
}
}
Expand Down Expand Up @@ -278,7 +290,7 @@ public function batch($batchSize = 100, $db = null)
'class' => BatchQueryResult::className(),
'query' => $this,
'batchSize' => $batchSize,
'db' => $db,
'db' => $this->getDb($db),
'each' => false,
]);
}
Expand Down Expand Up @@ -306,7 +318,7 @@ public function each($batchSize = 100, $db = null)
'class' => BatchQueryResult::className(),
'query' => $this,
'batchSize' => $batchSize,
'db' => $db,
'db' => $this->getDb($db),
'each' => true,
]);
}
Expand All @@ -322,8 +334,7 @@ public function all($db = null)
if (!empty($this->emulateExecution)) {
return [];
}
$cursor = $this->buildCursor($db);
$rows = $this->fetchRows($cursor, true, $this->indexBy);
$rows = $this->fetchRows(true, $db);
return $this->populate($rows);
}

Expand Down Expand Up @@ -358,8 +369,7 @@ public function one($db = null)
if (!empty($this->emulateExecution)) {
return false;
}
$cursor = $this->buildCursor($db);
return $this->fetchRows($cursor, false);
return $this->fetchRows(false, $db);
}

/**
Expand All @@ -384,8 +394,7 @@ public function scalar($db = null)
$this->select['_id'] = false;
}

$cursor = $this->buildCursor($db);
$row = $this->fetchRows($cursor, false);
$row = $this->fetchRows(false, $db);

if (empty($row)) {
return false;
Expand Down Expand Up @@ -417,8 +426,7 @@ public function column($db = null)
$this->select[] = $this->indexBy;
}

$cursor = $this->buildCursor($db);
$rows = $this->fetchRows($cursor, true);
$rows = $this->fetchRows(true, $db);

if (empty($rows)) {
return [];
Expand Down