forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging and profiling at Mongo improved.
- Loading branch information
1 parent
e2f587a
commit 99b6ae2
Showing
4 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,13 @@ | |
|
||
use yii\base\Object; | ||
use Yii; | ||
use yii\helpers\Json; | ||
|
||
/** | ||
* Database represents the Mongo database information. | ||
* | ||
* @property string $name name of this database. This property is read-only. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 2.0 | ||
*/ | ||
|
@@ -31,6 +34,14 @@ class Database extends Object | |
*/ | ||
private $_fileCollections = []; | ||
|
||
/** | ||
* @return string name of this database. | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->mongoDb->__toString(); | ||
} | ||
|
||
/** | ||
* Returns the Mongo collection with the given name. | ||
* @param string $name collection name | ||
|
@@ -93,20 +104,69 @@ protected function selectFileCollection($prefix) | |
* @param string $name name of the collection | ||
* @param array $options collection options in format: "name" => "value" | ||
* @return \MongoCollection new mongo collection instance. | ||
* @throws Exception on failure. | ||
*/ | ||
public function createCollection($name, $options = []) | ||
{ | ||
return $this->mongoDb->createCollection($name, $options); | ||
$token = $this->getName() . '.create(' . $name . ', ' . Json::encode($options) . ')'; | ||
Yii::info($token, __METHOD__); | ||
try { | ||
Yii::beginProfile($token, __METHOD__); | ||
$result = $this->mongoDb->createCollection($name, $options); | ||
Yii::endProfile($token, __METHOD__); | ||
return $result; | ||
} catch (\Exception $e) { | ||
Yii::endProfile($token, __METHOD__); | ||
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* Executes Mongo command. | ||
* @param array $command command specification. | ||
* @param array $options options in format: "name" => "value" | ||
* @return array database response. | ||
* @throws Exception on failure. | ||
*/ | ||
public function execute($command, $options = []) | ||
public function executeCommand($command, $options = []) | ||
{ | ||
return $this->mongoDb->command($command, $options); | ||
$token = $this->getName() . '.$cmd(' . Json::encode($command) . ', ' . Json::encode($options) . ')'; | ||
Yii::info($token, __METHOD__); | ||
try { | ||
Yii::beginProfile($token, __METHOD__); | ||
$result = $this->mongoDb->command($command, $options); | ||
$this->tryResultError($result); | ||
Yii::endProfile($token, __METHOD__); | ||
return $result; | ||
} catch (\Exception $e) { | ||
Yii::endProfile($token, __METHOD__); | ||
throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if command execution result ended with an error. | ||
* @param mixed $result raw command execution result. | ||
* @throws Exception if an error occurred. | ||
*/ | ||
protected function tryResultError($result) | ||
{ | ||
if (is_array($result)) { | ||
if (!empty($result['errmsg'])) { | ||
$errorMessage = $result['errmsg']; | ||
} elseif (!empty($result['err'])) { | ||
$errorMessage = $result['err']; | ||
} | ||
if (isset($errorMessage)) { | ||
if (array_key_exists('ok', $result)) { | ||
$errorCode = (int)$result['ok']; | ||
} else { | ||
$errorCode = 0; | ||
} | ||
throw new Exception($errorMessage, $errorCode); | ||
} | ||
} elseif (!$result) { | ||
throw new Exception('Unknown error, use "w=1" option to enable error tracking'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters