Changeset 1545

Show
Ignore:
Timestamp:
05/11/08 22:58:17 (8 months ago)
Author:
vollmerk
Message:

added in some caching and add the database upgrade that will make the taging mostly work

Location:
trunk
Files:
16 modified

Legend:

Unmodified
Added
Removed
  • trunk/browse.php

    r1543 r1545  
    5757                Browse::set_sort('name','ASC'); 
    5858                $album_ids = Browse::get_objects();  
     59                Album::build_cache($album_ids);  
    5960                Browse::show_objects($album_ids);  
    6061        break; 
     
    6263                Browse::set_sort('name','ASC'); 
    6364                $artist_ids = Browse::get_objects();  
     65                Artist::build_cache($artist_ids);  
    6466                Browse::show_objects($artist_ids);  
    6567        break; 
     
    7274                Browse::set_sort('title','ASC'); 
    7375                $song_ids = Browse::get_objects();  
     76                Song::build_cache($song_ids);  
    7477                Browse::show_objects($song_ids);  
    7578        break; 
  • trunk/docs/CHANGELOG

    r1538 r1545  
    22---------                Ampache -- CHANGELOG                    ---------    
    33-------------------------------------------------------------------------- 
     4 
     5-------------------------------------------------------------------------- 
     6  v.3.5-Alpha1 
     7        - Upped Minimum requirements to Mysql 5.x 
     8        - Add codeunde1load's Web 2.0 style tag patch 
     9        - Fixed typo in e-mail From: name (Thx Xgizzmo) 
     10        - Fixed typo in browse auto_init() which could cause ampache to not 
     11                remember your start point in some situations. (Thx Xgizzmo) 
    412 
    513-------------------------------------------------------------------------- 
  • trunk/docs/README

    r1518 r1545  
    121121        PHP5 ICONV  
    122122        PHP5 ZLIB support (recommended) 
    123   MySQL >= 4.1+ http://www.mysql.com 
     123  MySQL >= 5.x+ http://www.mysql.com 
    124124 
    1251253. Setting Up 
  • trunk/lib/class/album.class.php

    r1543 r1545  
    2525 * it is related to the album table in the database. 
    2626 */ 
    27 class Album { 
     27class Album extends database_object { 
    2828 
    2929        /* Variables from DB */ 
     
    5151         * get any of the counts. 
    5252         */ 
    53         public function __construct($album_id='') { 
    54  
    55                 if (!$album_id) { return false; }  
     53        public function __construct($id='') { 
     54 
     55                if (!$id) { return false; }  
     56 
    5657 
    5758                /* Assign id for use in get_info() */ 
    58                 $this->id = intval($album_id); 
     59                $this->id = intval($id); 
    5960 
    6061                /* Get the information from the db */ 
     
    7172                return true;  
    7273 
    73         } //constructor 
     74        } // constructor 
    7475 
    7576        /** 
     
    9192 
    9293        } // construct_from_array 
    93         public static function build_cache($ids, $fields='*') { 
    94           $idlist = '(' . implode(',', $ids) . ')'; 
    95           $sql = "SELECT $fields FROM album WHERE id in $idlist"; 
    96           $db_results = Dba::query($sql); 
    97           global $album_cache; 
    98           $album_cache = array(); 
    99           while ($results = Dba::fetch_assoc($db_results)) { 
    100             $album_cache[intval($results['id'])] = $results; 
    101           } 
    102         } 
     94 
     95        /** 
     96         * build_cache 
     97         * This takes an array of object ids and caches all of their information 
     98         * with a single query 
     99         */ 
     100        public static function build_cache($ids) { 
     101                $idlist = '(' . implode(',', $ids) . ')'; 
     102 
     103                $sql = "SELECT * FROM `album` WHERE `id` IN $idlist"; 
     104                $db_results = Dba::query($sql); 
     105           
     106                while ($row = Dba::fetch_assoc($db_results)) { 
     107                        parent::add_to_cache('album',$row['id'],$row);  
     108                } 
     109 
     110        } // build_cache 
     111 
    103112        /** 
    104113         * _get_info 
     
    107116         */ 
    108117        private function _get_info() { 
    109           global $album_cache; 
    110                 if (isset($album_cache[intval($this->id)])) 
    111                   return $album_cache[intval($this->id)]; 
     118 
     119                $id = intval($this->id);  
     120 
     121                if (parent::is_cached('album',$id)) {  
     122                        return parent::get_from_cache('album',$id);  
     123                }  
     124 
    112125                // Just get the album information 
    113                 $sql = "SELECT * FROM `album` WHERE `id`='" . $this->id . "'";  
     126                $sql = "SELECT * FROM `album` WHERE `id`='$id'";  
    114127                $db_results = Dba::query($sql); 
    115128 
    116129                $results = Dba::fetch_assoc($db_results); 
     130 
     131                // Cache the object 
     132                parent::add_to_cache('album',$id,$results);  
    117133 
    118134                return $results; 
  • trunk/lib/class/artist.class.php

    r1543 r1545  
    22/* 
    33 
    4  Copyright (c) 2001 - 2008 Ampache.org 
     4 Copyright (c) Ampache.org 
    55 All rights reserved. 
    66 
     
    2323 * Artist Class 
    2424 */ 
    25 class Artist { 
     25class Artist extends database_object { 
    2626 
    2727        /* Variables from DB */ 
     
    7777 
    7878        } // construct_from_array 
    79         public static function build_cache($ids, $fields='*') { 
    80           $idlist = '(' . implode(',', $ids) . ')'; 
    81           $sql = "SELECT $fields FROM artist WHERE id in $idlist"; 
    82           $db_results = Dba::query($sql); 
    83           global $artist_cache; 
    84           $artist_cache = array(); 
    85           while ($results = Dba::fetch_assoc($db_results)) { 
    86             $artist_cache[intval($results['id'])] = $results; 
    87           } 
    88         } 
     79 
     80        /** 
     81         * this attempts to build a cache of the data from the passed albums all in one query 
     82         */ 
     83        public static function build_cache($ids) { 
     84                $idlist = '(' . implode(',', $ids) . ')'; 
     85 
     86                $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; 
     87                $db_results = Dba::query($sql); 
     88 
     89                while ($row = Dba::fetch_assoc($db_results)) { 
     90                        parent::add_to_cache('artist',$row['id'],$row);  
     91                } 
     92 
     93        } // build_cache 
     94 
    8995        /** 
    9096         * _get_info 
     
    9298        */ 
    9399        private function _get_info() { 
    94                 global $artist_cache; 
    95                 if (isset($artist_cache[intval($this->id)])) 
    96                   return $artist_cache[intval($this->id)]; 
     100 
     101                $id = intval($this->id);  
     102 
     103                if (parent::is_cached('artist',$id)) {  
     104                        return parent::get_from_cache('artist',$id);  
     105                }  
     106         
    97107                /* Grab the basic information from the catalog and return it */ 
    98                 $sql = "SELECT * FROM artist WHERE id='" . Dba::escape($this->id) . "'"; 
     108                $sql = "SELECT * FROM artist WHERE id='$id'"; 
    99109                $db_results = Dba::query($sql); 
    100110 
    101111                $results = Dba::fetch_assoc($db_results); 
     112 
     113                parent::add_to_cache('artist',$id,$results); 
    102114 
    103115                return $results; 
  • trunk/lib/class/browse.class.php

    r1543 r1545  
    7373                        break; 
    7474                       case 'tag': 
    75                          //var_dump($value); 
    76                            if (is_array($value)) 
    77                              $_SESSION['browse']['filter'][$key] = $value; 
    78                            else if (is_numeric($value)) 
    79                              $_SESSION['browse']['filter'][$key] = 
    80                                array($value); 
    81                            else 
    82                              $_SESSION['browse']['filter'][$key] = array(); 
    83                          break; 
    84       case 'artist': 
    85       case 'album': 
    86         $_SESSION['browse']['filter'][$key] = $value; 
    87         break; 
     75                                if (is_array($value)) {  
     76                                        $_SESSION['browse']['filter'][$key] = $value; 
     77                                }  
     78                                elseif (is_numeric($value)) {  
     79                                        $_SESSION['browse']['filter'][$key] = array($value); 
     80                                }  
     81                                else {  
     82                                        $_SESSION['browse']['filter'][$key] = array(); 
     83                                }  
     84                        break; 
     85                        case 'artist': 
     86                        case 'album': 
     87                                $_SESSION['browse']['filter'][$key] = $value; 
     88                        break; 
    8889                        case 'min_count': 
    8990         
     
    9495                        case 'alpha_match': 
    9596                                if (self::$static_content) { return false; } 
    96                                 //if ($value == _('All')) { $value = ''; }  
    9797                                $_SESSION['browse']['filter'][$key] = $value;  
    9898                        break; 
     
    365365 
    366366                $results = array();  
    367                 while ($data = Dba::fetch_assoc($db_results)) 
    368                   $results[] = $data; 
    369                 var_dump($results); 
     367                while ($data = Dba::fetch_assoc($db_results)) {  
     368                        $results[] = $data; 
     369                } 
     370 
    370371                $results = self::post_process($results); 
    371372                $filtered = array(); 
     
    507508 
    508509                $sql = $sql . $order_sql;  
    509                 var_dump($sql); 
    510510                return $sql; 
    511511 
    512512        } // get_sql  
    513         private static function post_process($results) 
    514         { 
    515           $tags = $_SESSION['browse']['filter']['tag']; 
    516           if (!is_array($tags) || sizeof($tags) < 2) 
    517             return $results; 
    518           $cnt = sizeof($tags); 
    519           $ar = array(); 
    520           foreach($results as $row) 
    521             $ar[$row['id']]++; 
    522           $res = array(); 
    523           foreach($ar as $k=>$v) 
    524             if ($v >= $cnt) 
    525               $res[] = array('id' => $k); 
    526           return $res; 
    527         } 
     513 
     514        /** 
     515         * post_process 
     516         * This does some additional work on the results that we've received before returning them 
     517         */ 
     518        private static function post_process($results) { 
     519 
     520                $tags = $_SESSION['browse']['filter']['tag']; 
     521 
     522                if (!is_array($tags) || sizeof($tags) < 2) {  
     523                        return $results; 
     524                }  
     525                $cnt = sizeof($tags); 
     526                $ar = array(); 
     527 
     528                foreach($results as $row) {  
     529                        $ar[$row['id']]++; 
     530                } 
     531 
     532                $res = array(); 
     533 
     534                foreach($ar as $k=>$v) {  
     535                        if ($v >= $cnt) {  
     536                                $res[] = array('id' => $k); 
     537                        } 
     538                } // end foreach  
     539 
     540                return $res; 
     541 
     542        } // post_process 
     543 
    528544        /** 
    529545         * sql_filter 
     
    541557                  || $_SESSION['browse']['type'] == 'album' 
    542558                  )) { 
    543                 //var_dump($value); 
    544559                   if (is_array($value) && sizeof($value)) 
    545560                     $vals = '(' . implode(',',$value) . ')'; 
     
    797812                // Load any additional object we need for this 
    798813                $extra_objects = self::get_supplemental_objects();  
    799                 var_dump($object_ids); 
    800814                foreach ($extra_objects as $class_name => $id) {  
    801815                        ${$class_name} = new $class_name($id);  
     
    806820                  array('artist','album','song'))) { 
    807821                  $tagcloudHead = "Matching tags"; 
    808                   $tagcloudList =    
    809                     TagCloud::get_tags($_SESSION['browse']['type'],  $all_ids); 
     822                  $tagcloudList = TagCloud::get_tags($_SESSION['browse']['type'],  $all_ids); 
    810823                    require_once Config::get('prefix') . '/templates/show_tagcloud.inc.php';  
    811824                } 
    812                 Dba::show_profile(); 
     825                 
    813826                Ajax::start_container('browse_content'); 
    814827                // Switch on the type of browsing we're doing 
     
    966979        public static function set_filter_from_request($r) 
    967980        { 
    968           //var_dump($r); 
    969981          foreach ($r as $k=>$v) { 
    970982            //reinterpret v as a list of int 
    971983            $vl = explode(',', $v); 
    972             //var_dump($vl); 
    973984            $ok = 1; 
    974985            foreach($vl as $i) { 
  • trunk/lib/class/dba.class.php

    r1543 r1545  
    3333class Dba {  
    3434 
     35        public static $stats = array('query'=>0);  
     36 
    3537        private static $_default_db; 
    36  
    3738        private static $_sql;  
    3839        private static $config;  
     
    5455         */ 
    5556        public static function query($sql) {  
    56                 /*if ($_REQUEST['profiling']) { 
    57                   $sql = rtrim($sql, '; '); 
    58                   $sql .= ' SQL_NO_CACHE'; 
    59                 }*/ 
     57                 
    6058                // Run the query 
    6159                $resource = mysql_query($sql,self::dbh());  
     
    6462                // Save the query, to make debug easier 
    6563                self::$_sql = $sql;  
     64                self::$stats['query']++;  
    6665 
    6766                return $resource;  
     
    201200        } // _connect 
    202201 
     202        /** 
     203         * show_profile 
     204         * This function is used for debug, helps with profiling 
     205         */ 
    203206        public static function show_profile() { 
    204           if ($_REQUEST['profiling']) { 
    205             print '<br/>Profiling data: <br/>'; 
    206             $res = Dba::query('show profiles'); 
    207             print '<table>'; 
    208             while ($r = Dba::fetch_row($res)) { 
    209               print '<tr><td>' . implode('</td><td>', $r) . '</td></tr>'; 
    210             } 
    211             print '</table>'; 
    212           } 
    213         } 
     207 
     208                if (Config::get('sql_profiling')) { 
     209                    print '<br/>Profiling data: <br/>'; 
     210                    $res = Dba::query('show profiles'); 
     211                    print '<table>'; 
     212                    while ($r = Dba::fetch_row($res)) { 
     213                      print '<tr><td>' . implode('</td><td>', $r) . '</td></tr>'; 
     214                    } 
     215                    print '</table>'; 
     216                } 
     217        } // show_profile 
     218 
    214219        /** 
    215220         * dbh 
  • trunk/lib/class/localplay.abstract.php

    r1405 r1545  
    22/* 
    33 
    4  Copyright (c) 2001 - 2007 Ampache.org 
     4 Copyright (c) Ampache.org 
    55 All Rights Reserved 
    66 
  • trunk/lib/class/rating.class.php

    r1543 r1545  
    2525 * to track ratings for songs, albums and artists.  
    2626*/ 
    27 class Rating { 
     27class Rating extends database_object { 
    2828 
    2929        /* Provided vars */ 
     
    5757 
    5858        } // Constructor 
     59 
     60        /** 
     61         * build_cache 
     62         * This attempts to get everything we'll need for this page load in a single query, saving 
     63         * the connection overhead 
     64         * //FIXME: Improve logic so that misses get cached as average 
     65         */ 
    5966        public static function build_cache($type, $ids) { 
    60           $idlist = '(' . implode(',', $ids) . ')'; 
    61           $sql = "SELECT `rating`, object_id FROM `rating` WHERE `user`='$user_id' AND `object_id` in $idlist AND `object_type`='$type'"; 
    62           global $rating_cache; 
    63           $rating_cache = array(); 
    64           $db_results = Dba::query($sql); 
    65           while ($results = Dba::fetch_assoc($db_results)) { 
    66             $rating_cache[intval($results['object_id'])] = $results; 
    67           } 
    68         } 
     67         
     68                $user_id = Dba::escape($GLOBALS['user']->id);  
     69 
     70                $idlist = '(' . implode(',', $ids) . ')'; 
     71                $sql = "SELECT `rating`, `object_id` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " .  
     72                        "AND `object_type`='$type'"; 
     73                $db_results = Dba::query($sql); 
     74 
     75                while ($row = Dba::fetch_assoc($db_results)) { 
     76                        $rating[$row['id']] = $row['rating']; 
     77                } 
     78                 
     79                $user_cache_name = 'rating_' . $type . '_user';  
     80 
     81                foreach ($ids as $id) {  
     82                        parent::add_to_cache($user_cache_name,$id,intval($rating[$id]));  
     83                } // end foreach  
     84 
     85 
     86        } // build_cache 
     87 
    6988        /** 
    7089         * get_user 
     
    7392         */ 
    7493         public function get_user($user_id) { 
    75            global $rating_cache; 
    76            if (isset($rating_cache[intval($this->id)])); 
    77              return $rating_cache[intval($this->id)]['rating']; 
    78                 $user_id        = Dba::escape($user_id);  
    79  
    80                 $sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'"; 
     94                 
     95                $id = intval($this->id);  
     96 
     97                if (parent::is_cached('rating_' . $this->type . '_user',$id)) {  
     98                        return parent::get_from_cache('rating_' . $this->type . '_user',$id);  
     99                }  
     100 
     101                $user_id = Dba::escape($user_id);  
     102 
     103                $sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$id' AND `object_type`='$this->type'"; 
    81104                $db_results = Dba::query($sql); 
    82105                 
    83106                $results = Dba::fetch_assoc($db_results); 
     107 
     108                parent::add_to_cache('rating_' . $this->type . '_user',$id,$results['rating']);  
    84109                 
    85110                return $results['rating']; 
  • trunk/lib/class/song.class.php

    r1543 r1545  
    2020*/ 
    2121 
    22 class Song { 
     22class Song extends database_object { 
    2323 
    2424        /* Variables from DB */ 
     
    5353        public function __construct($id='') { 
    5454 
     55                if (!$id) { return false; }  
     56 
    5557                /* Assign id for use in get_info() */ 
    5658                $this->id = intval($id); 
    57  
    58                 if (!$this->id) { return false; }  
    5959 
    6060                /* Get the information from the db */ 
     
    7171 
    7272        } // constructor 
    73         public static function build_cache($ids) 
    74         { 
    75           $idlist = '(' . implode(',', $ids) . ')'; 
     73 
     74        /** 
     75         * build_cache 
     76         * This attempts to reduce # of queries by asking for everything in the browse 
     77         * all at once and storing it in the cache, this can help if the db connection 
     78         * is the slow point 
     79         */ 
     80        public static function build_cache($song_ids) { 
     81 
     82                $idlist = '(' . implode(',', $song_ids) . ')'; 
    7683           
    77           // Song data cache 
    78           $sql = "SELECT song.id,file,catalog,album,year,artist,". 
    79                         "title,bitrate,rate,mode,size,time,track,genre,played,song.enabled,update_time,". 
    80                         "addition_time FROM `song` WHERE `song`.`id` in 
    81                         $idlist"; 
    82           $db_results = Dba::query($sql); 
    83           global $song_cache; 
    84           $song_cache = array(); 
    85           while ($results = Dba::fetch_assoc($db_results)) 
    86           { 
    87             $song_cache[intval($results['id'])] = $results; 
    88           } 
     84                // Song data cache 
     85                $sql = "SELECT song.id,file,catalog,album,year,artist,". 
     86                                "title,bitrate,rate,mode,size,time,track,genre,played,song.enabled,update_time,". 
     87                                "addition_time FROM `song` WHERE `song`.`id` IN 
     88                                $idlist"; 
     89                $db_results = Dba::query($sql); 
    8990           
    90           // Extra sound data cache 
    91           global $song_data_cache; 
    92           $song_data_cache = array(); 
    93           $sql = "SELECT * FROM song_data WHERE song_id in $idlist"; 
    94           $db_results = Dba::query($sql); 
    95           while ($results = Dba::fetch_assoc($db_results)) 
    96           { 
    97             $song_data_cache[intval($results['song_id'])] = $results; 
    98           } 
    99            
    100           // Get all artist, album, genre ids. 
    101           $artists = array(); 
    102           $albums = array(); 
    103           $genre = array(); 
    104           foreach ($song_cache as $i) 
    105           { 
    106             $artists[$i['artist']] = 1; 
    107             $albums[$i['album']] = 1; 
    108             $genre[$i['genre']] = 1; 
    109           } 
    110