Commit e2f587a3 by Paul Klimov

Mongo log tokens improved.

parent 0fea8a50
...@@ -10,6 +10,7 @@ namespace yii\mongo; ...@@ -10,6 +10,7 @@ namespace yii\mongo;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\base\Object; use yii\base\Object;
use Yii; use Yii;
use yii\helpers\Json;
/** /**
* Collection represents the Mongo collection information. * Collection represents the Mongo collection information.
...@@ -96,13 +97,28 @@ class Collection extends Object ...@@ -96,13 +97,28 @@ class Collection extends Object
} }
/** /**
* Composes log/profile token.
* @param string $command command name
* @param array $arguments command arguments.
* @return string token.
*/
protected function composeLogToken($command, $arguments = [])
{
$parts = [];
foreach ($arguments as $argument) {
$parts[] = is_scalar($argument) ? $argument : Json::encode($argument);
}
return $this->getFullName() . '.' . $command . '(' . implode(', ', $parts) . ')';
}
/**
* Drops this collection. * Drops this collection.
* @throws Exception on failure. * @throws Exception on failure.
* @return boolean whether the operation successful. * @return boolean whether the operation successful.
*/ */
public function drop() public function drop()
{ {
$token = 'Drop collection ' . $this->getFullName(); $token = $this->composeLogToken('drop');
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
...@@ -139,12 +155,12 @@ class Collection extends Object ...@@ -139,12 +155,12 @@ class Collection extends Object
if (!is_array($columns)) { if (!is_array($columns)) {
$columns = [$columns]; $columns = [$columns];
} }
$token = 'Creating index at ' . $this->getFullName() . ' on ' . implode(',', $columns); $keys = $this->normalizeIndexKeys($columns);
$token = $this->composeLogToken('createIndex', [$keys, $options]);
$options = array_merge(['w' => 1], $options);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$keys = $this->normalizeIndexKeys($columns);
$options = array_merge(['w' => 1], $options);
$result = $this->mongoCollection->ensureIndex($keys, $options); $result = $this->mongoCollection->ensureIndex($keys, $options);
$this->tryResultError($result); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
...@@ -177,10 +193,10 @@ class Collection extends Object ...@@ -177,10 +193,10 @@ class Collection extends Object
if (!is_array($columns)) { if (!is_array($columns)) {
$columns = [$columns]; $columns = [$columns];
} }
$token = 'Drop index at ' . $this->getFullName() . ' on ' . implode(',', $columns); $keys = $this->normalizeIndexKeys($columns);
$token = $this->composeLogToken('dropIndex', [$keys]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
$keys = $this->normalizeIndexKeys($columns);
$result = $this->mongoCollection->deleteIndex($keys); $result = $this->mongoCollection->deleteIndex($keys);
$this->tryResultError($result); $this->tryResultError($result);
return true; return true;
...@@ -215,7 +231,7 @@ class Collection extends Object ...@@ -215,7 +231,7 @@ class Collection extends Object
*/ */
public function dropAllIndexes() public function dropAllIndexes()
{ {
$token = 'Drop ALL indexes at ' . $this->getFullName(); $token = $this->composeLogToken('dropIndexes');
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
$result = $this->mongoCollection->deleteIndexes(); $result = $this->mongoCollection->deleteIndexes();
...@@ -249,7 +265,7 @@ class Collection extends Object ...@@ -249,7 +265,7 @@ class Collection extends Object
*/ */
public function insert($data, $options = []) public function insert($data, $options = [])
{ {
$token = 'Inserting data into ' . $this->getFullName(); $token = $this->composeLogToken('insert', [$data]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
...@@ -272,7 +288,7 @@ class Collection extends Object ...@@ -272,7 +288,7 @@ class Collection extends Object
*/ */
public function batchInsert($rows, $options = []) public function batchInsert($rows, $options = [])
{ {
$token = 'Inserting batch data into ' . $this->getFullName(); $token = $this->composeLogToken('batchInsert', [$rows]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
...@@ -298,10 +314,7 @@ class Collection extends Object ...@@ -298,10 +314,7 @@ class Collection extends Object
*/ */
public function update($condition, $newData, $options = []) public function update($condition, $newData, $options = [])
{ {
$token = 'Updating data in ' . $this->getFullName(); $condition = $this->buildCondition($condition);
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1, 'multiple' => true], $options); $options = array_merge(['w' => 1, 'multiple' => true], $options);
if ($options['multiple']) { if ($options['multiple']) {
$keys = array_keys($newData); $keys = array_keys($newData);
...@@ -309,7 +322,10 @@ class Collection extends Object ...@@ -309,7 +322,10 @@ class Collection extends Object
$newData = ['$set' => $newData]; $newData = ['$set' => $newData];
} }
} }
$condition = $this->buildCondition($condition); $token = $this->composeLogToken('update', [$condition, $newData, $options]);
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$result = $this->mongoCollection->update($condition, $newData, $options); $result = $this->mongoCollection->update($condition, $newData, $options);
$this->tryResultError($result); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
...@@ -333,7 +349,7 @@ class Collection extends Object ...@@ -333,7 +349,7 @@ class Collection extends Object
*/ */
public function save($data, $options = []) public function save($data, $options = [])
{ {
$token = 'Saving data into ' . $this->getFullName(); $token = $this->composeLogToken('save', [$data]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
...@@ -356,12 +372,13 @@ class Collection extends Object ...@@ -356,12 +372,13 @@ class Collection extends Object
*/ */
public function remove($condition = [], $options = []) public function remove($condition = [], $options = [])
{ {
$token = 'Removing data from ' . $this->getFullName(); $condition = $this->buildCondition($condition);
$options = array_merge(['w' => 1, 'multiple' => true], $options);
$token = $this->composeLogToken('remove', [$condition, $options]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$options = array_merge(['w' => 1, 'multiple' => true], $options); $result = $this->mongoCollection->remove($condition, $options);
$result = $this->mongoCollection->remove($this->buildCondition($condition), $options);
$this->tryResultError($result); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
if (is_array($result) && array_key_exists('n', $result)) { if (is_array($result) && array_key_exists('n', $result)) {
...@@ -380,15 +397,22 @@ class Collection extends Object ...@@ -380,15 +397,22 @@ class Collection extends Object
* @param string $column column to use. * @param string $column column to use.
* @param array $condition query parameters. * @param array $condition query parameters.
* @return array|boolean array of distinct values, or "false" on failure. * @return array|boolean array of distinct values, or "false" on failure.
* @throws Exception on failure.
*/ */
public function distinct($column, $condition = []) public function distinct($column, $condition = [])
{ {
$token = 'Get distinct ' . $column . ' from ' . $this->getFullName(); $condition = $this->buildCondition($condition);
$token = $this->composeLogToken('distinct', [$column, $condition]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$result = $this->mongoCollection->distinct($column, $this->buildCondition($condition)); $result = $this->mongoCollection->distinct($column, $condition);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
return $result; return $result;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
} }
/** /**
...@@ -435,11 +459,6 @@ class Collection extends Object ...@@ -435,11 +459,6 @@ class Collection extends Object
*/ */
public function group($keys, $initial, $reduce, $options = []) public function group($keys, $initial, $reduce, $options = [])
{ {
$token = 'Grouping from ' . $this->getFullName();
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
if (!($reduce instanceof \MongoCode)) { if (!($reduce instanceof \MongoCode)) {
$reduce = new \MongoCode((string)$reduce); $reduce = new \MongoCode((string)$reduce);
} }
...@@ -451,6 +470,10 @@ class Collection extends Object ...@@ -451,6 +470,10 @@ class Collection extends Object
$options['finalize'] = new \MongoCode((string)$options['finalize']); $options['finalize'] = new \MongoCode((string)$options['finalize']);
} }
} }
$token = $this->composeLogToken('group', [$keys, $initial, $reduce, $options]);
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
// Avoid possible E_DEPRECATED for $options: // Avoid possible E_DEPRECATED for $options:
if (empty($options)) { if (empty($options)) {
$result = $this->mongoCollection->group($keys, $initial, $reduce); $result = $this->mongoCollection->group($keys, $initial, $reduce);
...@@ -502,11 +525,6 @@ class Collection extends Object ...@@ -502,11 +525,6 @@ class Collection extends Object
*/ */
public function mapReduce($map, $reduce, $out, $condition = []) public function mapReduce($map, $reduce, $out, $condition = [])
{ {
$token = 'Map reduce from ' . $this->getFullName();
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
if (!($map instanceof \MongoCode)) { if (!($map instanceof \MongoCode)) {
$map = new \MongoCode((string)$map); $map = new \MongoCode((string)$map);
} }
...@@ -523,6 +541,11 @@ class Collection extends Object ...@@ -523,6 +541,11 @@ class Collection extends Object
$command['query'] = $this->buildCondition($condition); $command['query'] = $this->buildCondition($condition);
} }
$token = $this->composeLogToken('mapReduce', [$map, $reduce, $out]);
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$command = array_merge(['mapReduce' => $this->getName()], $command);
$result = $this->mongoCollection->db->command($command); $result = $this->mongoCollection->db->command($command);
$this->tryResultError($result); $this->tryResultError($result);
Yii::endProfile($token, __METHOD__); Yii::endProfile($token, __METHOD__);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment