From: Bharat M. <bh...@us...> - 2002-10-23 05:30:47
|
Update of /cvsroot/gallery/gallery/classes In directory usw-pr-cvs1:/tmp/cvs-serv8693/classes Modified Files: Tag: albumdb-refactor-branch Album.php AlbumDB.php Log Message: 2002-10-22 Bharat Mediratta <bh...@me...> 1.3.2-cvs-albumdb-refactor-branch-b1 * Integrated Jesse Mullan's albumdb refactoring code in a separate branch. This code separates out the information about each photo from the album metadata which allows us to load much less data into memory when we load an album. This will speed up cross-album operations and use less memory (yay Jesse!) Index: Album.php =================================================================== RCS file: /cvsroot/gallery/gallery/classes/Album.php,v retrieving revision 1.76 retrieving revision 1.76.2.1 diff -u -r1.76 -r1.76.2.1 --- Album.php 24 Sep 2002 04:45:22 -0000 1.76 +++ Album.php 23 Oct 2002 05:30:44 -0000 1.76.2.1 @@ -69,6 +69,9 @@ $this->fields["public_comments"] = $gallery->app->default["public_comments"]; $this->fields["serial_number"] = 0; + $this->fields["cached_photo_count"] = 0; + $this->fields["photos_separate"] = FALSE; + $this->transient->photosloaded = TRUE; // Seed new albums with the appropriate version. $this->version = $gallery->album_version; } @@ -348,9 +351,10 @@ } } - function load($name) { + function load($name,$loadphotos=TRUE) { global $gallery; + $this->transient->photosloaded = FALSE; $dir = $gallery->app->albumDir . "/$name"; if (!$this->loadFromFile("$dir/album.dat")) { @@ -370,15 +374,33 @@ } } + if ($this->fields["photos_separate"] && ($this->fields["cached_photo_count"] > 0)) { + if ($loadphotos) { + $this->loadPhotos($dir); + } + } else { + $this->transient->photosloaded = TRUE; + } $this->fields["name"] = $name; $this->updateSerial = 0; return 1; } + + function loadPhotos($dir){ + if (!$this->loadPhotosFromFile("$dir/photos.dat") && + !$this->loadPhotosFromFile("$dir/photos.dat.bak") && + !$this->loadPhotosFromFile("$dir/photos.bak")) { + /* Uh oh */ + return 0; + } + $this->transient->photosloaded = TRUE; + return 1; + } + function loadFromFile($filename) { - $data = getFile($filename); - $tmp = unserialize($data); + $tmp = unserialize(getFile($filename)); if (strcasecmp(get_class($tmp), "album")) { /* Dunno what we unserialized .. but it wasn't an album! */ return 0; @@ -388,6 +410,23 @@ return 1; } + function loadPhotosFromFile($filename) { + $tmp = unserialize(getFile($filename)); + if (!is_Array($tmp)){ + return 0; + } + if (count($tmp) > 0) { + if (strcasecmp(get_class($tmp[0]), "albumitem")) { + /* Dunno what we unserialized .. but it wasn't an album! */ + return 0; + } + } + + $this->photos = $tmp; + + return 1; + } + function isLoaded() { if ($this->fields["name"]) { return 1; @@ -404,6 +443,7 @@ function save($resetModDate=1) { global $gallery; $dir = $this->getAlbumDir(); + $savephotosuccess = FALSE; if ($resetModDate) { $this->fields["last_mod_time"] = time(); @@ -422,14 +462,32 @@ $this->fields["serial_number"]++; } + if ($this->transient->photosloaded) { + $this->fields["cached_photo_count"] = $this->numPhotos(); + } + + $transient_photos = $this->photos; + + /* Save photo data separately */ + if ($this->transient->photosloaded) { + $savephotosuccess = (safe_serialize($this->photos, "$dir/photos.dat")); + if ($savephotosuccess) { + $this->fields["photos_separate"] = TRUE; + unset ($this->photos); + } + } else { + $savephotosuccess = TRUE; + } + /* Don't save transient data */ $transient_save = $this->transient; unset($this->transient); - $success = safe_serialize($this, "$dir/album.dat"); + $success = (safe_serialize($this, "$dir/album.dat") && $savephotosuccess); /* Restore transient data after saving */ $this->transient = $transient_save; + $this->photos = $transient_photos; /* Create the new album serial file */ if ($this->updateSerial) { Index: AlbumDB.php =================================================================== RCS file: /cvsroot/gallery/gallery/classes/AlbumDB.php,v retrieving revision 1.13 retrieving revision 1.13.2.1 diff -u -r1.13 -r1.13.2.1 --- AlbumDB.php 11 Sep 2002 04:29:32 -0000 1.13 +++ AlbumDB.php 23 Oct 2002 05:30:44 -0000 1.13.2.1 @@ -23,7 +23,7 @@ var $albumList; var $albumOrder; - function AlbumDB() { + function AlbumDB($loadphotos=TRUE) { global $gallery; $dir = $gallery->app->albumDir; @@ -43,7 +43,7 @@ $name = $this->albumOrder[$i]; if (fs_is_dir("$dir/$name")) { $album = new Album; - if ($album->load($name)) { + if ($album->load($name,$loadphotos)) { array_push($this->albumList, $album); } else { array_push($this->brokenAlbums, $name); @@ -63,7 +63,7 @@ strcmp($file, "_vti_cnf") && !in_array($file, $this->albumOrder)) { $album = new Album; - $album->load($file); + $album->load($file,$loadphotos); array_push($this->albumList, $album); array_push($this->albumOrder, $file); $changed = 1; @@ -130,17 +130,33 @@ return $numPhotos; } + function getCachedNumPhotos($user) { + $numPhotos = 0; + foreach ($this->albumList as $album) { + if ($user->canReadAlbum($album)) { + $numPhotos += $album->fields["cached_photo_count"]; + } + } + return $numPhotos; + } + function getAlbum($user, $index) { + global $gallery; $list = $this->getVisibleAlbums($user); + if (!$list[$index-1]->transient->photosloaded) { + $list[$index-1]->loadPhotos($gallery->app->albumDir . "/" . $list[$index-1]->fields["name"]); + } return $list[$index-1]; } function getAlbumbyName($name) { - $list = $this->albumList; - $indexLimit = count($list); - for ($i=0; $i<$indexLimit; $i++) { - if ($list[$i]->fields["name"] == $name) { - return $list[$i]; + global $gallery; + foreach ($this->albumList as $album) { + if ($album->fields["name"] == $name) { + if (!$album->transient->photosloaded) { + $album->loadPhotos($gallery->app->albumDir . "/$name"); + } + return $album; } } return 0; @@ -166,9 +182,9 @@ // Locate absolute indices of the target and destination for ($i = 0; $i < sizeof($this->albumList); $i++) { - if ($this->albumList[$i] == $album1) { + if ($this->albumList[$i]->fields[name] == $album1->fields[name]) { $absIndex = $i; - } else if ($this->albumList[$i] == $album2) { + } else if ($this->albumList[$i]->fields[name] == $album2->fields[name]) { $absNewIndex = $i; } } @@ -196,6 +212,7 @@ } function getVisibleAlbums($user) { + global $gallery; $list = array(); foreach ($this->albumList as $album) { if ($user->canReadAlbum($album) && $album->isRoot()) { |