Skip to content

Commit

Permalink
For clarity, use constants to identify mongo field names
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Mar 5, 2014
1 parent fa012ff commit 7156c45
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions framework/Imap_Client/lib/Horde/Imap/Client/Cache/Backend/Mongo.php
Expand Up @@ -28,6 +28,24 @@ class Horde_Imap_Client_Cache_Backend_Mongo extends Horde_Imap_Client_Cache_Back
const MD = 'horde_imap_client_cache_metadata';
const MSG = 'horde_imap_client_cache_message';

/** Mongo field names: BASE collection. */
const BASE_HOSTSPEC = 'hostspec';
const BASE_MAILBOX = 'mailbox';
const BASE_MODIFIED = 'modified';
const BASE_PORT = 'port';
const BASE_UID = 'data';
const BASE_USERNAME = 'username';

/** Mongo field names: MD collection. */
const MD_DATA = 'data';
const MD_FIELD = 'field';
const MD_UID = 'uid';

/** Mongo field names: MSG collection. */
const MSG_DATA = 'data';
const MSG_MSGUID = 'msguid';
const MSG_UID = 'uid';

/**
* The MongoDB object for the cache data.
*
Expand All @@ -43,16 +61,16 @@ class Horde_Imap_Client_Cache_Backend_Mongo extends Horde_Imap_Client_Cache_Back
protected $_indices = array(
self::BASE => array(
'base_index_1' => array(
'hostspec' => 1,
'mailbox' => 1,
'port' => 1,
'username' => 1,
self::BASE_HOSTSPEC => 1,
self::BASE_MAILBOX => 1,
self::BASE_PORT => 1,
self::BASE_USERNAME => 1,
)
),
self::MSG => array(
'msg_index_1' => array(
'uid' => 1,
'msguid' => 1
self::MSG_MSGUID => 1,
self::MSG_UID => 1
)
)
);
Expand Down Expand Up @@ -94,20 +112,20 @@ public function get($mailbox, $uids, $fields, $uidvalid)

$out = array();
$query = array(
'msguid' => array('$in' => array_map('strval', $uids)),
'uid' => $uid
self::MSG_MSGUID => array('$in' => array_map('strval', $uids)),
self::MSG_UID => $uid
);

try {
$cursor = $this->_db->selectCollection(self::MSG)->find(
$query,
array(
'data' => true,
'msguid' => true
self::MSG_DATA => true,
self::MSG_MSGUID => true
)
);
foreach ($cursor as $val) {
$out[$val['msguid']] = $this->_value($val['data']);
$out[$val[self::MSG_MSGUID]] = $this->_value($val[self::MSG_DATA]);
}
} catch (MongoException $e) {}

Expand All @@ -126,18 +144,18 @@ public function getCachedUids($mailbox, $uidvalid)

$out = array();
$query = array(
'uid' => $uid
self::MSG_UID => $uid
);

try {
$cursor = $this->_db->selectCollection(self::MSG)->find(
$query,
array(
'msguid' => true
self::MSG_MSGUID => true
)
);
foreach ($cursor as $val) {
$out[] = $val['msguid'];
$out[] = $val[self::MSG_MSGUID];
}
} catch (MongoException $e) {}

Expand All @@ -161,18 +179,18 @@ public function set($mailbox, $data, $uidvalid)
try {
if (isset($res[$key])) {
$coll->update(array(
'msguid' => strval($key),
'uid' => $uid
self::MSG_MSGUID => strval($key),
self::MSG_UID => $uid
), array(
'data' => $this->_value(array_merge($res[$key], $val)),
'msguid' => strval($key),
'uid' => $uid
self::MSG_DATA => $this->_value(array_merge($res[$key], $val)),
self::MSG_MSGUID => strval($key),
self::MSG_UID => $uid
));
} else {
$coll->insert(array(
'data' => $this->_value($val),
'msguid' => strval($key),
'uid' => $uid
self::MSG_DATA => $this->_value($val),
self::MSG_MSGUID => strval($key),
self::MSG_UID => $uid
));
}
} catch (MongoException $e) {}
Expand All @@ -181,8 +199,8 @@ public function set($mailbox, $data, $uidvalid)
/* Update modified time. */
try {
$this->_db->selectCollection(self::BASE)->update(
array('uid' => $uid),
array('modified' => time())
array(self::BASE_UID => $uid),
array(self::BASE_MODIFIED => time())
);
} catch (MongoException $e) {}

Expand All @@ -200,12 +218,12 @@ public function getMetaData($mailbox, $uidvalid, $entries)

$out = array();
$query = array(
'uid' => $uid
self::MD_UID => $uid
);

if (!empty($entries)) {
$entries[] = 'uidvalid';
$query['field'] = array(
$query[self::MD_FIELD] = array(
'$in' => array_unique($entries)
);
}
Expand All @@ -214,12 +232,12 @@ public function getMetaData($mailbox, $uidvalid, $entries)
$cursor = $this->_db->selectCollection(self::MD)->find(
$query,
array(
'data' => true,
'field' => true
self::MD_DATA => true,
self::MD_FIELD => true
)
);
foreach ($cursor as $val) {
$out[$val['field']] = $this->_value($val['data']);
$out[$val[self::MD_FIELD]] = $this->_value($val[self::MD_DATA]);
}

if (is_null($uidvalid) ||
Expand Down Expand Up @@ -248,13 +266,13 @@ public function setMetaData($mailbox, $data)
try {
$coll->update(
array(
'field' => $key,
'uid' => $uid
self::MD_FIELD => $key,
self::MD_UID => $uid
),
array(
'data' => $this->_value($val),
'field' => $key,
'uid' => $uid
self::MD_DATA => $this->_value($val),
self::MD_FIELD => $key,
self::MD_UID => $uid
),
array('upsert' => true)
);
Expand All @@ -269,8 +287,8 @@ public function deleteMsgs($mailbox, $uids)
if ($uid = $this->_getUid($mailbox)) {
try {
$this->_db->selectCollection(self::MSG)->remove(array(
'msguid' => array('$in' => array_map('strval', $uids)),
'uid' => $uid
self::MSG_MSGUID => array('$in' => array_map('strval', $uids)),
self::MSG_UID => $uid
));
} catch (MongoException $e) {}
}
Expand Down Expand Up @@ -305,7 +323,7 @@ public function clear($lifetime)
}

$query = array(
'modified' => array('$lt' => (time() - $lifetime))
self::BASE_MODIFIED => array('$lt' => (time() - $lifetime))
);
$uids = array();

Expand Down Expand Up @@ -339,10 +357,10 @@ public function clear($lifetime)
protected function _getUid($mailbox)
{
$query = array(
'hostspec' => $this->_params['hostspec'],
'mailbox' => $mailbox,
'port' => $this->_params['port'],
'username' => $this->_params['username']
self::BASE_HOSTSPEC => $this->_params['hostspec'],
self::BASE_MAILBOX => $mailbox,
self::BASE_PORT => $this->_params['port'],
self::BASE_USERNAME => $this->_params['username']
);

try {
Expand All @@ -364,10 +382,10 @@ protected function _getUid($mailbox)
protected function _createUid($mailbox)
{
$this->_db->selectCollection(self::BASE)->insert(array(
'hostspec' => $this->_params['hostspec'],
'mailbox' => $mailbox,
'port' => $this->_params['port'],
'username' => $this->_params['username']
self::BASE_HOSTSPEC => $this->_params['hostspec'],
self::BASE_MAILBOX => $mailbox,
self::BASE_PORT => $this->_params['port'],
self::BASE_USERNAME => $this->_params['username']
));

return $this->_getUid($mailbox);
Expand Down

0 comments on commit 7156c45

Please sign in to comment.