Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jun 30, 2016
1 parent cbebe40 commit 4025ff2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 81 deletions.
34 changes: 14 additions & 20 deletions framework/Imap_Client/lib/Horde/Imap/Client/Cache/Backend/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* A MongoDB database implementation for caching IMAP/POP data.
*
* Requires the Horde_Mongo class.
*
* @author Michael Slusarz <slusarz@horde.org>
Expand Down Expand Up @@ -121,10 +122,7 @@ public function get($mailbox, $uids, $fields, $uidvalid)
try {
$cursor = $this->_db->selectCollection(self::MSG)->find(
$query,
array(
self::MSG_DATA => true,
self::MSG_MSGUID => true
)
array(self::MSG_DATA => true, self::MSG_MSGUID => true)
);
foreach ($cursor as $val) {
try {
Expand Down Expand Up @@ -153,10 +151,7 @@ public function getCachedUids($mailbox, $uidvalid)

try {
$cursor = $this->_db->selectCollection(self::MSG)->find(
$query,
array(
self::MSG_MSGUID => true
)
$query, array(self::MSG_MSGUID => true)
);
foreach ($cursor as $val) {
$out[] = $val[self::MSG_MSGUID];
Expand Down Expand Up @@ -236,10 +231,7 @@ public function getMetaData($mailbox, $uidvalid, $entries)
try {
$cursor = $this->_db->selectCollection(self::MD)->find(
$query,
array(
self::MD_DATA => true,
self::MD_FIELD => true
)
array(self::MD_DATA => true, self::MD_FIELD => true)
);
foreach ($cursor as $val) {
try {
Expand Down Expand Up @@ -294,7 +286,9 @@ public function deleteMsgs($mailbox, $uids)
if (!empty($uids) && ($uid = $this->_getUid($mailbox))) {
try {
$this->_db->selectCollection(self::MSG)->remove(array(
self::MSG_MSGUID => array('$in' => array_map('strval', $uids)),
self::MSG_MSGUID => array(
'$in' => array_map('strval', $uids)
),
self::MSG_UID => $uid
));
} catch (MongoException $e) {}
Expand All @@ -311,9 +305,8 @@ public function deleteMailbox($mailbox)

foreach (array(self::BASE, self::MD, self::MSG) as $val) {
try {
$this->_db->selectCollection($val)->remove(array(
'uid' => $uid
));
$this->_db->selectCollection($val)
->remove(array('uid' => $uid));
} catch (MongoException $e) {}
}
}
Expand Down Expand Up @@ -347,9 +340,8 @@ public function clear($lifetime)

foreach (array(self::BASE, self::MD, self::MSG) as $val) {
try {
$this->_db->selectCollection($val)->remove(array(
'uid' => array('$in' => $uids)
));
$this->_db->selectCollection($val)
->remove(array('uid' => array('$in' => $uids)));
} catch (MongoException $e) {}
}
}
Expand Down Expand Up @@ -416,7 +408,9 @@ protected function _value($data)

return ($data instanceof MongoBinData)
? @unserialize($compress->decompress($data->bin))
: new MongoBinData($compress->compress(serialize($data)), MongoBinData::BYTE_ARRAY);
: new MongoBinData(
$compress->compress(serialize($data)), MongoBinData::BYTE_ARRAY
);
}

/* Horde_Mongo_Collection_Index methods. */
Expand Down
78 changes: 43 additions & 35 deletions framework/Lock/lib/Horde/Lock/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function __construct(array $params = array())
'collection' => 'horde_locks'
), $params));

$this->_db = $this->_params['mongo_db']->selectCollection(null, $this->_params['collection']);
$this->_db = $this->_params['mongo_db']
->selectCollection(null, $this->_params['collection']);
}

/**
Expand All @@ -95,14 +96,8 @@ public function getLockInfo($lockid)
$query = array(
self::LID => $lockid,
'$or' => array(
array(
self::EXPIRY_TS => array(
'$gte' => time()
),
),
array(
self::EXPIRY_TS => Horde_Lock::PERMANENT
)
array(self::EXPIRY_TS => array('$gte' => time()),),
array(self::EXPIRY_TS => Horde_Lock::PERMANENT)
)
);

Expand All @@ -119,14 +114,8 @@ public function getLocks($scope = null, $principal = null, $type = null)
{
$query = array(
'$or' => array(
array(
self::EXPIRY_TS => array(
'$gte' => time()
),
),
array(
self::EXPIRY_TS => Horde_Lock::PERMANENT
)
array(self::EXPIRY_TS => array('$gte' => time()),),
array(self::EXPIRY_TS => Horde_Lock::PERMANENT)
)
);

Expand Down Expand Up @@ -169,17 +158,18 @@ public function resetLock($lockid, $lifetime)
: ($now + $lifetime);

try {
$this->_db->update(array(
self::EXPIRY_TS => array(
'$ne' => Horde_Lock::PERMANENT
$this->_db->update(
array(
self::EXPIRY_TS => array('$ne' => Horde_Lock::PERMANENT),
self::LID => $lockid
),
self::LID => $lockid
), array(
'$set' => array(
self::EXPIRY_TS => $expiration,
self::UPDATE_TS => $now
array(
'$set' => array(
self::EXPIRY_TS => $expiration,
self::UPDATE_TS => $now
)
)
));
);
} catch (MongoException $e) {
throw new Horde_Lock_Exception($e);
}
Expand All @@ -201,7 +191,13 @@ public function setLock($requestor, $scope, $principal,
if (count($oldlocks) != 0) {
// A lock exists. Deny the new request.
if ($this->_logger) {
$this->_logger->log(sprintf('Lock requested for %s denied due to existing lock.', $principal), 'NOTICE');
$this->_logger->log(
sprintf(
'Lock requested for %s denied due to existing lock.',
$principal
),
'NOTICE'
);
}
return false;
}
Expand Down Expand Up @@ -230,7 +226,13 @@ public function setLock($requestor, $scope, $principal,
}

if ($this->_logger) {
$this->_logger->log(sprintf('Lock %s set successfully by %s in scope %s on "%s"', $lockid, $requestor, $scope, $principal), 'DEBUG');
$this->_logger->log(
sprintf(
'Lock %s set successfully by %s in scope %s on "%s"',
$lockid, $requestor, $scope, $principal
),
'DEBUG'
);
}

return $lockid;
Expand All @@ -245,17 +247,17 @@ public function clearLock($lockid)
}

try {
/* Since we're trying to clear the lock we don't care whether it
is still valid or not. Unconditionally remove it. */
$this->_db->remove(array(
self::LID => $lockid
));
/* Since we're trying to clear the lock we don't care whether it is
* still valid or not. Unconditionally remove it. */
$this->_db->remove(array(self::LID => $lockid));
} catch (MongoException $e) {
throw new Horde_Lock_Exception($e);
}

if ($this->_logger) {
$this->_logger->log(sprintf('Lock %s cleared successfully.', $lockid), 'DEBUG');
$this->_logger->log(
sprintf('Lock %s cleared successfully.', $lockid), 'DEBUG'
);
}

return true;
Expand All @@ -275,7 +277,13 @@ public function gc()
));

if ($this->_logger) {
$this->_logger->log(sprintf('Lock garbage collection cleared %d locks.', $result['n']), 'DEBUG');
$this->_logger->log(
sprintf(
'Lock garbage collection cleared %d locks.',
$result['n']
),
'DEBUG'
);
}
} catch (MongoException $e) {}
}
Expand Down
46 changes: 20 additions & 26 deletions framework/SessionHandler/lib/Horde/SessionHandler/Storage/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ public function close()
{
if ($this->_locked) {
try {
$this->_db->update(array(
self::SID => $this->_locked
), array(
'$unset' => array(self::LOCK => '')
));
$this->_db->update(
array(self::SID => $this->_locked),
array('$unset' => array(self::LOCK => ''))
);
} catch (MongoException $e) {}
$this->_locked = false;
}
Expand All @@ -108,9 +107,7 @@ public function read($id)
* we need findAndModify() for its atomicity for locking, but this
* atomicity means we can't tell the difference between a
* non-existent session and a locked session. */
$exists = $this->_db->count(array(
self::SID => $id
));
$exists = $this->_db->count(array(self::SID => $id));

$exist_check = false;
$i = 0;
Expand All @@ -129,19 +126,18 @@ public function read($id)
* locked. If a session exists, and is locked, $res will contain
* an empty set and we need to sleep and wait for lock to be
* removed. */
$res = $this->_db->findAndModify(array(
self::SID => $id,
self::LOCK => array(
'$exists' => $exist_check
)
), array($data), array(
self::DATA => true
), array(
'update' => array(
'$set' => $data
$res = $this->_db->findAndModify(
array(
self::SID => $id,
self::LOCK => array('$exists' => $exist_check)
),
'upsert' => !$exists
));
array($data),
array(self::DATA => true),
array(
'update' => array('$set' => $data),
'upsert' => !$exists
)
);

if (!$exists || isset($res[self::DATA])) {
break;
Expand All @@ -151,11 +147,10 @@ public function read($id)
* a stale session. This can prevent long waits on a busted PHP
* process. */
if ($i == 10) {
$res = $this->_db->findOne(array(
self::SID => $id
), array(
self::LOCK => true
));
$res = $this->_db->findOne(
array(self::SID => $id),
array(self::LOCK => true)
);

$max = isset($res[self::LOCK])
? ((time() - $res[self::LOCK]) * 10)
Expand Down Expand Up @@ -267,5 +262,4 @@ public function createMongoIndices()
{
$this->_params['mongo_db']->createIndices($this->_db, $this->_indices);
}

}

0 comments on commit 4025ff2

Please sign in to comment.