Skip to content

Commit 6ed66f3

Browse files
authored
Ignore missing scenes when submitting fingerprints (#5799)
1 parent 2eb7bde commit 6ed66f3

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

internal/api/resolver_mutation_stash_box.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (r *mutationResolver) SubmitStashBoxFingerprints(ctx context.Context, input
2929
var scenes []*models.Scene
3030

3131
if err := r.withReadTxn(ctx, func(ctx context.Context) error {
32-
scenes, err = r.sceneService.FindMany(ctx, ids, scene.LoadStashIDs, scene.LoadFiles)
32+
scenes, err = r.sceneService.FindByIDs(ctx, ids, scene.LoadStashIDs, scene.LoadFiles)
3333
return err
3434
}); err != nil {
3535
return false, err

internal/manager/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SceneService interface {
1515
Merge(ctx context.Context, sourceIDs []int, destinationID int, fileDeleter *scene.FileDeleter, options scene.MergeOptions) error
1616
Destroy(ctx context.Context, scene *models.Scene, fileDeleter *scene.FileDeleter, deleteGenerated, deleteFile bool) error
1717

18-
FindMany(ctx context.Context, ids []int, load ...scene.LoadRelationshipOption) ([]*models.Scene, error)
18+
FindByIDs(ctx context.Context, ids []int, load ...scene.LoadRelationshipOption) ([]*models.Scene, error)
1919
sceneFingerprintGetter
2020
}
2121

pkg/models/mocks/SceneReaderWriter.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/models/mocks/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func (s *sceneResolver) FindMany(ctx context.Context, ids []int) ([]*models.Scen
1818
return s.scenes, nil
1919
}
2020

21+
func (s *sceneResolver) FindByIDs(ctx context.Context, ids []int) ([]*models.Scene, error) {
22+
return s.scenes, nil
23+
}
24+
2125
func SceneQueryResult(scenes []*models.Scene, count int) *models.SceneQueryResult {
2226
ret := models.NewSceneQueryResult(&sceneResolver{
2327
scenes: scenes,

pkg/models/repository_scene.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type SceneGetter interface {
1010
// TODO - rename this to Find and remove existing method
1111
FindMany(ctx context.Context, ids []int) ([]*Scene, error)
1212
Find(ctx context.Context, id int) (*Scene, error)
13+
// FindByIDs works the same way as FindMany, but it ignores any scenes not found
14+
// Scenes are not guaranteed to be in the same order as the input
15+
FindByIDs(ctx context.Context, ids []int) ([]*Scene, error)
1316
}
1417

1518
// SceneFinder provides methods to find scenes.

pkg/scene/find.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,31 @@ func LoadFiles(ctx context.Context, scene *models.Scene, r models.SceneReader) e
3333
return nil
3434
}
3535

36-
// FindMany retrieves multiple scenes by their IDs.
36+
// FindByIDs retrieves multiple scenes by their IDs.
37+
// Missing scenes will be ignored, and the returned scenes are unsorted.
38+
// This method will load the specified relationships for each scene.
39+
func (s *Service) FindByIDs(ctx context.Context, ids []int, load ...LoadRelationshipOption) ([]*models.Scene, error) {
40+
var scenes []*models.Scene
41+
qb := s.Repository
42+
43+
var err error
44+
scenes, err = qb.FindByIDs(ctx, ids)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
// TODO - we should bulk load these relationships
50+
for _, scene := range scenes {
51+
if err := s.LoadRelationships(ctx, scene, load...); err != nil {
52+
return nil, err
53+
}
54+
}
55+
56+
return scenes, nil
57+
}
58+
59+
// FindMany retrieves multiple scenes by their IDs. Return value is guaranteed to be in the same order as the input.
60+
// Missing scenes will return an error.
3761
// This method will load the specified relationships for each scene.
3862
func (s *Service) FindMany(ctx context.Context, ids []int, load ...LoadRelationshipOption) ([]*models.Scene, error) {
3963
var scenes []*models.Scene

pkg/sqlite/scene.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,11 @@ func (qb *SceneStore) Find(ctx context.Context, id int) (*models.Scene, error) {
493493
return ret, err
494494
}
495495

496-
func (qb *SceneStore) FindMany(ctx context.Context, ids []int) ([]*models.Scene, error) {
497-
scenes := make([]*models.Scene, len(ids))
496+
// FindByIDs finds multiple scenes by their IDs.
497+
// No check is made to see if the scenes exist, and the order of the returned scenes
498+
// is not guaranteed to be the same as the order of the input IDs.
499+
func (qb *SceneStore) FindByIDs(ctx context.Context, ids []int) ([]*models.Scene, error) {
500+
scenes := make([]*models.Scene, 0, len(ids))
498501

499502
table := qb.table()
500503
if err := batchExec(ids, defaultBatchSize, func(batch []int) error {
@@ -504,16 +507,29 @@ func (qb *SceneStore) FindMany(ctx context.Context, ids []int) ([]*models.Scene,
504507
return err
505508
}
506509

507-
for _, s := range unsorted {
508-
i := slices.Index(ids, s.ID)
509-
scenes[i] = s
510-
}
510+
scenes = append(scenes, unsorted...)
511511

512512
return nil
513513
}); err != nil {
514514
return nil, err
515515
}
516516

517+
return scenes, nil
518+
}
519+
520+
func (qb *SceneStore) FindMany(ctx context.Context, ids []int) ([]*models.Scene, error) {
521+
scenes := make([]*models.Scene, len(ids))
522+
523+
unsorted, err := qb.FindByIDs(ctx, ids)
524+
if err != nil {
525+
return nil, err
526+
}
527+
528+
for _, s := range unsorted {
529+
i := slices.Index(ids, s.ID)
530+
scenes[i] = s
531+
}
532+
517533
for i := range scenes {
518534
if scenes[i] == nil {
519535
return nil, fmt.Errorf("scene with id %d not found", ids[i])

0 commit comments

Comments
 (0)