More transactions, more queries on uncommitted data

This commit is contained in:
Michael Vogel 2016-10-24 08:10:27 +00:00
parent 47b8975bb6
commit 8cab3b5592
6 changed files with 67 additions and 29 deletions

View File

@ -1135,23 +1135,33 @@ class App {
$this->remove_inactive_processes(); $this->remove_inactive_processes();
q("START TRANSACTION");
$r = q("SELECT `pid` FROM `process` WHERE `pid` = %d", intval(getmypid())); $r = q("SELECT `pid` FROM `process` WHERE `pid` = %d", intval(getmypid()));
if(!dbm::is_result($r)) if(!dbm::is_result($r)) {
q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')",
intval(getmypid()), intval(getmypid()),
dbesc($command), dbesc($command),
dbesc(datetime_convert())); dbesc(datetime_convert()));
}
q("COMMIT");
} }
/** /**
* @brief Remove inactive processes * @brief Remove inactive processes
*/ */
function remove_inactive_processes() { function remove_inactive_processes() {
q("START TRANSACTION");
$r = q("SELECT `pid` FROM `process`"); $r = q("SELECT `pid` FROM `process`");
if(dbm::is_result($r)) if(dbm::is_result($r)) {
foreach ($r AS $process) foreach ($r AS $process) {
if (!posix_kill($process["pid"], 0)) if (!posix_kill($process["pid"], 0)) {
q("DELETE FROM `process` WHERE `pid` = %d", intval($process["pid"])); q("DELETE FROM `process` WHERE `pid` = %d", intval($process["pid"]));
}
}
}
q("COMMIT");
} }
/** /**

View File

@ -670,6 +670,12 @@ class Photo {
dbesc($deny_gid) dbesc($deny_gid)
); );
} }
// Update the cached values
if ($album != 'Contact Photos') {
photo_albums($uid, true);
}
return $r; return $r;
} }
}} }}
@ -1059,3 +1065,23 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
return($image); return($image);
} }
function photo_albums($uid, $update = false) {
$sql_extra = permissions_sql($uid);
$key = "photo_albums:".$uid.":".local_user().":".remote_user();
$albums = Cache::get($key);
if (is_null($albums) OR $update) {
/// @todo This query needs to be renewed. It is really slow
// At this time we just store the data in the cache
$albums = qu("SELECT count(distinct `resource-id`) AS `total`, `album`
FROM `photo` USE INDEX (`uid_album_created`)
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
GROUP BY `album` ORDER BY `created` DESC",
intval($uid),
dbesc('Contact Photos'),
dbesc(t('Contact Photos'))
);
Cache::set($key, $albums, CACHE_DAY);
}
return $albums;
}

View File

@ -134,6 +134,8 @@ function cron_run(&$argv, &$argc){
} else { } else {
proc_run(PRIORITY_LOW, 'include/dbclean.php'); proc_run(PRIORITY_LOW, 'include/dbclean.php');
} }
cron_update_photo_albums();
} }
// Clear cache entries // Clear cache entries
@ -155,6 +157,19 @@ function cron_run(&$argv, &$argc){
return; return;
} }
/**
* @brief Update the cached values for the number of photo albums per user
*/
function cron_update_photo_albums() {
$r = q("SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`");
if (!dbm::is_result($r))
return;
foreach ($r AS $user) {
photo_albums($user['uid'], true);
}
}
/** /**
* @brief Expire and remove user entries * @brief Expire and remove user entries
*/ */

View File

@ -770,6 +770,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
logger('item_store: ' . print_r($arr,true), LOGGER_DATA); logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
q("COMMIT");
q("START TRANSACTION;"); q("START TRANSACTION;");
$r = dbq("INSERT INTO `item` (`" $r = dbq("INSERT INTO `item` (`"

View File

@ -788,6 +788,7 @@ function item_post(&$a) {
} else } else
$post_id = 0; $post_id = 0;
q("COMMIT");
q("START TRANSACTION;"); q("START TRANSACTION;");
$r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`, $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,

View File

@ -25,7 +25,7 @@ function photos_init(&$a) {
if ($a->argc > 1) { if ($a->argc > 1) {
$nick = $a->argv[1]; $nick = $a->argv[1];
$user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1", $user = qu("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
dbesc($nick) dbesc($nick)
); );
@ -50,21 +50,7 @@ function photos_init(&$a) {
'$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""), '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
)); ));
$albums = photo_albums($a->data['user']['uid']);
$sql_extra = permissions_sql($a->data['user']['uid']);
$albums = Cache::get("photos-albums:".$a->data['user']['uid']);
if (is_null($albums)) {
/// @todo This query needs to be renewed. It is really slow
// At this time we just store the data in the cache
$albums = qu("SELECT count(distinct `resource-id`) AS `total`, `album` FROM `photo` USE INDEX (`uid_album_created`) WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
$sql_extra GROUP BY `album` ORDER BY `created` DESC",
intval($a->data['user']['uid']),
dbesc('Contact Photos'),
dbesc( t('Contact Photos'))
);
Cache::set("photos-albums:".$a->data['user']['uid'], $albums, CACHE_FIVE_MINUTES);
}
$albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true); $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);
@ -159,7 +145,7 @@ function photos_post(&$a) {
} }
if ($cid) { if ($cid) {
$r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1", $r = qu("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
intval($cid), intval($cid),
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -176,7 +162,7 @@ function photos_post(&$a) {
killme(); killme();
} }
$r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` $r = qu("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1", WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -198,7 +184,7 @@ function photos_post(&$a) {
return; // NOTREACHED return; // NOTREACHED
} }
$r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d", $r = qu("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
dbesc($album), dbesc($album),
intval($page_owner_uid) intval($page_owner_uid)
); );
@ -1343,7 +1329,7 @@ function photos_content(&$a) {
$order = 'DESC'; $order = 'DESC';
$prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 $prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
$sql_extra ORDER BY `created` $order ", $sql_extra ORDER BY `created` $order ",
dbesc($ph[0]['album']), dbesc($ph[0]['album']),
intval($owner_uid) intval($owner_uid)
@ -1439,7 +1425,7 @@ function photos_content(&$a) {
if (count($linked_items)) { if (count($linked_items)) {
$link_item = $linked_items[0]; $link_item = $linked_items[0];
$r = q("SELECT COUNT(*) AS `total` $r = qu("SELECT COUNT(*) AS `total`
FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
@ -1455,7 +1441,7 @@ function photos_content(&$a) {
$a->set_pager_total($r[0]['total']); $a->set_pager_total($r[0]['total']);
$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, $r = qu("SELECT `item`.*, `item`.`id` AS `item_id`,
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`,
`contact`.`rel`, `contact`.`thumb`, `contact`.`self`, `contact`.`rel`, `contact`.`thumb`, `contact`.`self`,
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid` `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
@ -1804,7 +1790,7 @@ function photos_content(&$a) {
// Default - show recent photos with upload link (if applicable) // Default - show recent photos with upload link (if applicable)
//$o = ''; //$o = '';
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $r = qu("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
$sql_extra GROUP BY `resource-id`", $sql_extra GROUP BY `resource-id`",
intval($a->data['user']['uid']), intval($a->data['user']['uid']),
dbesc('Contact Photos'), dbesc('Contact Photos'),
@ -1815,7 +1801,7 @@ function photos_content(&$a) {
$a->set_pager_itemspage(20); $a->set_pager_itemspage(20);
} }
$r = q("SELECT `resource-id`, `id`, `filename`, type, `album`, max(`scale`) AS `scale` FROM `photo` $r = qu("SELECT `resource-id`, `id`, `filename`, type, `album`, max(`scale`) AS `scale` FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
$sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d", $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
intval($a->data['user']['uid']), intval($a->data['user']['uid']),
@ -1878,4 +1864,3 @@ function photos_content(&$a) {
return $o; return $o;
} }