Skip to content

Commit

Permalink
Cleanup info of scanned albums on Full Scan
Browse files Browse the repository at this point in the history
  • Loading branch information
rigon committed Feb 19, 2025
1 parent c14cdb4 commit fd0d259
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
43 changes: 31 additions & 12 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Cache struct {
wgFlush sync.WaitGroup
}

// Flag album as loaded
// Flag album as fully scanned
type AlbumSaved struct{}

// Flag album with photos missing thumbnails
Expand Down Expand Up @@ -111,6 +111,8 @@ func (c *Cache) End() error {
return c.store.Close()
}

// Cache List Albums

func (c *Cache) SetListAlbums(albums ...*Album) {
// Empty map
c.albums.Range(func(key, value any) bool {
Expand All @@ -120,19 +122,16 @@ func (c *Cache) SetListAlbums(albums ...*Album) {

c.AddToListAlbums(albums...)
}

func (c *Cache) AddToListAlbums(albums ...*Album) {
for _, album := range albums {
c.albums.Store(album.Name, true)
}
}

func (c *Cache) RemoveFromListAlbums(albums ...string) {
for _, album := range albums {
c.albums.Delete(album)
}
}

func (c *Cache) IsListAlbumsLoaded() bool {
var ret = false
c.albums.Range(func(key, value any) bool {
Expand All @@ -141,13 +140,14 @@ func (c *Cache) IsListAlbumsLoaded() bool {
})
return ret
}

func (c *Cache) IsAlbum(albumName string) bool {
// Check if value is present
_, present := c.albums.Load(albumName)
return present
}

// Cache Albums

func (c *Cache) GetAlbum(albumName string) (*Album, error) {
// Check if value is present
album, err := c.mem.Get(albumName)
Expand All @@ -156,24 +156,43 @@ func (c *Cache) GetAlbum(albumName string) (*Album, error) {
}
return album.(*Album), nil
}

func (c *Cache) SaveAlbum(album *Album) error {
// Cache album in memory
return c.mem.Set(album.Name, album)
}

// Album Fully Scanned

func (c *Cache) SetAlbumFullyScanned(album *Album) error {
return c.store.Upsert(album.Name, AlbumSaved{})
}

func (c *Cache) IsAlbumFullyScanned(album *Album) bool {
var a AlbumSaved
return c.store.Get(album.Name, &a) == nil
}
func (c *Cache) RemoveAlbumSaved(albumName string) bool {
var a AlbumSaved
return c.store.Delete(albumName, &a) == nil
func (c *Cache) UnsetAlbumFullyScanned(albumName string) error {
return c.store.Delete(albumName, AlbumSaved{})
}
func (c *Cache) ResetAlbumsFullyScanned() error {
return c.store.DeleteMatching(AlbumSaved{}, nil) // Delete all
}

// Album Thumbnail queue

func (c *Cache) SetAlbumToThumbQueue(album string) error {
return c.store.Upsert(album, AlbumThumbs{album})
}
func (c *Cache) TxSetAlbumToThumbQueue(tx *bolt.Tx, album string) error {
return c.store.TxUpsert(tx, album, AlbumThumbs{album})
}
func (c *Cache) UnsetAlbumFromThumbQueue(albumName string) bool {
return c.store.Delete(albumName, AlbumThumbs{}) == nil
}
func (c *Cache) ResetAlbumsInThumbQueue() error {
return c.store.DeleteMatching(AlbumThumbs{}, nil) // Delete all
}

// Cache data

func (photo *Photo) Key() string {
return PhotoKey(photo.Album, photo.Id)
Expand Down Expand Up @@ -205,9 +224,9 @@ func (c *Cache) addInfoBatcher() {
log.Println(err)
}

// Add album from the thumbnail queue
// Add album to the thumbnail queue
if !photo.HasThumb {
err = c.store.TxUpsert(tx, photo.Album, AlbumThumbs{photo.Album})
err = c.TxSetAlbumToThumbQueue(tx, photo.Album)
if err != nil {
log.Println(err)
}
Expand Down
15 changes: 9 additions & 6 deletions server/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"math"
"os"
Expand Down Expand Up @@ -83,15 +82,18 @@ func (c *Collection) Info() CollectionInfo {
// For that use Album.GetPhotos()
func (c *Collection) GetAlbums() ([]*Album, error) {
albums := make([]*Album, 0)
files, err := ioutil.ReadDir(c.PhotosPath)
files, err := os.ReadDir(c.PhotosPath)
if err != nil {
return nil, err
}

for _, file := range files {
album, err := readAlbum(file)
fileInfo, err := file.Info()
if err == nil {
albums = append(albums, album)
album, err := readAlbum(fileInfo)
if err == nil {
albums = append(albums, album)
}
}
}
// Save to cache in background
Expand Down Expand Up @@ -187,7 +189,7 @@ func (c *Collection) GetAlbumWithPhotos(albumName string, forceUpdate bool, runn
// ...and save to cache
c.cache.SaveAlbum(album)
// Set album as fully scanned
if len(photosToLoad) < 1 { // only when partial load is not performed
if len(photosToLoad) < 1 { // skip on partial scans!
c.cache.SetAlbumFullyScanned(album)
}

Expand Down Expand Up @@ -250,7 +252,8 @@ func (c *Collection) DeleteAlbum(album *Album) error {

// Remove from cache
c.cache.RemoveFromListAlbums(album.Name)
c.cache.RemoveAlbumSaved(album.Name)
c.cache.UnsetAlbumFullyScanned(album.Name)
c.cache.UnsetAlbumFromThumbQueue(album.Name)
return nil
}

Expand Down
8 changes: 6 additions & 2 deletions server/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (collection *Collection) Scan(fullScan bool) {
}

// Full scan

collection.cache.ResetAlbumsFullyScanned()
collection.cache.ResetAlbumsInThumbQueue()

for _, album := range albums {
// Load album
album, err = collection.GetAlbumWithPhotos(album.Name, true, true)
Expand Down Expand Up @@ -64,7 +68,7 @@ func (collection *Collection) Scan(fullScan bool) {
}

// Clean entries in the cacheDB of deleted albums
log.Printf("Clean entries of deleted albums %s...\n", collection.Name)
log.Printf("Cleaning entries of deleted albums in %s...\n", collection.Name)
var photos []*Photo
err = collection.cache.store.Find(&photos, bolthold.Where("Album").MatchFunc(
func(album string) (bool, error) {
Expand Down Expand Up @@ -116,7 +120,7 @@ func (collection *Collection) CreateThumbnails() {
// Update flag to indicate that the thumbnail was generated
collection.cache.FlushInfo()
// Thumbnails created, remove album from the queue
err = collection.cache.store.Delete(albumThumb.Name, albumThumb)
err = collection.cache.UnsetAlbumFullyScanned(albumThumb.Name)
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit fd0d259

Please sign in to comment.