-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
256 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
func init() { | ||
goose.AddMigrationContext(upAddFolderTable, downAddFolderTable) | ||
} | ||
|
||
func upAddFolderTable(ctx context.Context, tx *sql.Tx) error { | ||
_, err := tx.ExecContext(ctx, ` | ||
create table if not exists folder( | ||
id varchar not null | ||
primary key, | ||
library_id integer not null | ||
references library (id) | ||
on delete cascade, | ||
path varchar default '' not null, | ||
name varchar default '' not null, | ||
updated_at timestamp default current_timestamp not null, | ||
created_at timestamp default current_timestamp not null, | ||
parent_id varchar default null | ||
references folder (id) | ||
on delete cascade | ||
); | ||
`) | ||
|
||
return err | ||
} | ||
|
||
func downAddFolderTable(ctx context.Context, tx *sql.Tx) error { | ||
// This code is executed when the migration is rolled back. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package model | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Folder struct { | ||
ID string | ||
LibraryID int | ||
Path string | ||
Name string | ||
ParentID string | ||
UpdateAt time.Time | ||
CreatedAt time.Time | ||
} | ||
|
||
func FolderID(lib Library, path string) string { | ||
path = strings.TrimPrefix(path, lib.Path) | ||
key := fmt.Sprintf("%d:%s", lib.ID, path) | ||
return fmt.Sprintf("%x", md5.Sum([]byte(key))) | ||
} | ||
func NewFolder(lib Library, path string) *Folder { | ||
id := FolderID(lib, path) | ||
dir, name := filepath.Split(path) | ||
return &Folder{ | ||
ID: id, | ||
Path: dir, | ||
Name: name, | ||
} | ||
} | ||
|
||
type FolderRepository interface { | ||
Get(lib Library, path string) (*Folder, error) | ||
GetAll(lib Library) ([]Folder, error) | ||
GetLastUpdates(lib Library) (map[string]time.Time, error) | ||
Put(lib Library, path string) error | ||
Touch(lib Library, path string, t time.Time) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package persistence | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
. "github.com/Masterminds/squirrel" | ||
"github.com/navidrome/navidrome/model" | ||
"github.com/pocketbase/dbx" | ||
) | ||
|
||
type folderRepository struct { | ||
sqlRepository | ||
} | ||
|
||
func newFolderRepository(ctx context.Context, db dbx.Builder) model.FolderRepository { | ||
r := &folderRepository{} | ||
r.ctx = ctx | ||
r.db = db | ||
r.tableName = "folder" | ||
return r | ||
} | ||
|
||
func (r folderRepository) Get(lib model.Library, path string) (*model.Folder, error) { | ||
id := model.NewFolder(lib, path).ID | ||
sq := r.newSelect().Where(Eq{"id": id}) | ||
var res model.Folder | ||
err := r.queryOne(sq, res) | ||
return &res, err | ||
} | ||
|
||
func (r folderRepository) GetAll(lib model.Library) ([]model.Folder, error) { | ||
sq := r.newSelect().Columns("*").Where(Eq{"library_id": lib.ID}) | ||
var res []model.Folder | ||
err := r.queryAll(sq, &res) | ||
return res, err | ||
} | ||
|
||
func (r folderRepository) GetLastUpdates(lib model.Library) (map[string]time.Time, error) { | ||
sq := r.newSelect().Columns("id", "updated_at").Where(Eq{"library_id": lib.ID}) | ||
var res []struct { | ||
ID string | ||
UpdatedAt time.Time | ||
} | ||
err := r.queryAll(sq, &res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m := make(map[string]time.Time, len(res)) | ||
for _, f := range res { | ||
m[f.ID] = f.UpdatedAt | ||
} | ||
return m, nil | ||
} | ||
|
||
func (r folderRepository) Put(lib model.Library, path string) error { | ||
folder := model.NewFolder(lib, path) | ||
_, err := r.put(folder.ID, folder) | ||
return err | ||
} | ||
|
||
func (r folderRepository) Touch(lib model.Library, path string, t time.Time) error { | ||
id := model.FolderID(lib, path) | ||
sq := Update(r.tableName).Set("updated_at", t).Where(Eq{"id": id}) | ||
_, err := r.executeSQL(sq) | ||
return err | ||
} | ||
|
||
var _ model.FolderRepository = (*folderRepository)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
package scanner2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/navidrome/navidrome/model" | ||
) | ||
|
||
type scanContext struct { | ||
lib model.Library | ||
startTime time.Time | ||
lib model.Library | ||
ds model.DataStore | ||
startTime time.Time | ||
lastUpdates map[string]time.Time | ||
lock sync.RWMutex | ||
} | ||
|
||
func newScannerContext(lib model.Library) *scanContext { | ||
func newScannerContext(ctx context.Context, ds model.DataStore, lib model.Library) (*scanContext, error) { | ||
lastUpdates, err := ds.Folder(ctx).GetLastUpdates(lib) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting last updates: %w", err) | ||
} | ||
return &scanContext{ | ||
lib: lib, | ||
startTime: time.Now(), | ||
lib: lib, | ||
ds: ds, | ||
startTime: time.Now(), | ||
lastUpdates: lastUpdates, | ||
}, nil | ||
} | ||
|
||
func (s *scanContext) getLastUpdatedInDB(id string) time.Time { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
t, ok := s.lastUpdates[id] | ||
if !ok { | ||
return time.Time{} | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters