]> git.mxchange.org Git - friendica.git/commitdiff
More transactions, more queries on uncommitted data
authorMichael Vogel <ike@pirati.ca>
Mon, 24 Oct 2016 08:10:27 +0000 (08:10 +0000)
committerMichael Vogel <ike@pirati.ca>
Mon, 24 Oct 2016 08:10:27 +0000 (08:10 +0000)
boot.php
include/Photo.php
include/cron.php
include/items.php
mod/item.php
mod/photos.php

index 85270e5305af968a8375289547f3a561be8d32fe..1de2cb123e8792224b719815464511df861f34b4 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -1135,23 +1135,33 @@ class App {
 
                $this->remove_inactive_processes();
 
+               q("START TRANSACTION");
+
                $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')",
                                intval(getmypid()),
                                dbesc($command),
                                dbesc(datetime_convert()));
+               }
+               q("COMMIT");
        }
 
        /**
         * @brief Remove inactive processes
         */
        function remove_inactive_processes() {
+               q("START TRANSACTION");
+
                $r = q("SELECT `pid` FROM `process`");
-               if(dbm::is_result($r))
-                       foreach ($r AS $process)
-                               if (!posix_kill($process["pid"], 0))
+               if(dbm::is_result($r)) {
+                       foreach ($r AS $process) {
+                               if (!posix_kill($process["pid"], 0)) {
                                        q("DELETE FROM `process` WHERE `pid` = %d", intval($process["pid"]));
+                               }
+                       }
+               }
+               q("COMMIT");
        }
 
        /**
index d87bce478704fccb6489097269e2e83163a4fd5f..3ab374bd0b3f0e4655a23ad52fbfe1e07def1f86 100644 (file)
@@ -670,6 +670,12 @@ class Photo {
                dbesc($deny_gid)
            );
        }
+
+       // Update the cached values
+       if ($album != 'Contact Photos') {
+               photo_albums($uid, true);
+       }
+
        return $r;
     }
 }}
@@ -1059,3 +1065,23 @@ function store_photo($a, $uid, $imagedata = "", $url = "") {
        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;
+}
index 6dc34224f7e6ce21a4d681af567c4ae82dd5e18a..c03745a4429e0d38bb1502e433ede5f0b3a516d0 100644 (file)
@@ -134,6 +134,8 @@ function cron_run(&$argv, &$argc){
                } else {
                        proc_run(PRIORITY_LOW, 'include/dbclean.php');
                }
+
+               cron_update_photo_albums();
        }
 
        // Clear cache entries
@@ -155,6 +157,19 @@ function cron_run(&$argv, &$argc){
        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
  */
index 98927e658529c72ecf4add909f84be088d0cf7df..1ee048e48c2c0437a27415f7511f32406b60842a 100644 (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);
 
+       q("COMMIT");
        q("START TRANSACTION;");
 
        $r = dbq("INSERT INTO `item` (`"
index a56dde3c19f3ce057b128024fae94857fcfff763..4b23b219b79be431ecc749618925c2168d534925 100644 (file)
@@ -788,6 +788,7 @@ function item_post(&$a) {
        } else
                $post_id = 0;
 
+       q("COMMIT");
        q("START TRANSACTION;");
 
        $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,
index 34e5eb4ba34d7e5fa96de96a188701fedb63ca4b..edaba5a9027c7427e8c62f80be4db92a7846264a 100644 (file)
@@ -25,7 +25,7 @@ function photos_init(&$a) {
 
        if ($a->argc > 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)
                );
 
@@ -50,21 +50,7 @@ function photos_init(&$a) {
                        '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
                ));
 
-
-               $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 = photo_albums($a->data['user']['uid']);
 
                $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);
 
@@ -159,7 +145,7 @@ function photos_post(&$a) {
                        }
                        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($page_owner_uid)
                                );
@@ -176,7 +162,7 @@ function photos_post(&$a) {
                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",
                intval($page_owner_uid)
        );
@@ -198,7 +184,7 @@ function photos_post(&$a) {
                        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),
                        intval($page_owner_uid)
                );
@@ -1343,7 +1329,7 @@ function photos_content(&$a) {
                        $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 ",
                        dbesc($ph[0]['album']),
                        intval($owner_uid)
@@ -1439,7 +1425,7 @@ function photos_content(&$a) {
 
                if (count($linked_items)) {
                        $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`
                                WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
                                AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
@@ -1455,7 +1441,7 @@ function photos_content(&$a) {
                                $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`.`rel`, `contact`.`thumb`, `contact`.`self`,
                                `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)
        //$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`",
                intval($a->data['user']['uid']),
                dbesc('Contact Photos'),
@@ -1815,7 +1801,7 @@ function photos_content(&$a) {
                $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'
                $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                intval($a->data['user']['uid']),
@@ -1878,4 +1864,3 @@ function photos_content(&$a) {
 
        return $o;
 }
-