Skip to content

Commit

Permalink
Fix a bug where multiple scene files caused duplicate ids.
Browse files Browse the repository at this point in the history
Fix a bug where phash distance didnt work.
Fix a bug where i wrongly wrote getDBLike in a string in captionCriterionHandler.
  • Loading branch information
NodudeWasTaken committed Nov 11, 2024
1 parent 23ba774 commit cd0f2c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
18 changes: 9 additions & 9 deletions pkg/sqlite/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,11 +922,11 @@ func (qb *SceneStore) makeQuery(ctx context.Context, sceneFilter *models.SceneFi
query.addJoins(
join{
table: scenesFilesTable,
onClause: "scenes_files.scene_id = scenes.id",
onClause: "scenes_files.scene_id = scenes.id AND scenes_files.primary = " + getDBBoolean(true),
},
join{
table: fileTable,
onClause: "scenes_files.file_id = files.id",
onClause: "scenes_files.file_id = files.id AND scenes_files.primary = " + getDBBoolean(true),
},
join{
table: folderTable,
Expand Down Expand Up @@ -999,11 +999,11 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce
query.addJoins(
join{
table: scenesFilesTable,
onClause: "scenes_files.scene_id = scenes.id",
onClause: "scenes_files.scene_id = scenes.id AND scenes_files.primary = " + getDBBoolean(true),
},
join{
table: videoFileTable,
onClause: "scenes_files.file_id = video_files.file_id",
onClause: "scenes_files.file_id = video_files.file_id AND scenes_files.primary = " + getDBBoolean(true),
},
)
query.addColumn("COALESCE(video_files.duration, 0) as duration")
Expand All @@ -1015,11 +1015,11 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce
query.addJoins(
join{
table: scenesFilesTable,
onClause: "scenes_files.scene_id = scenes.id",
onClause: "scenes_files.scene_id = scenes.id AND scenes_files.primary = " + getDBBoolean(true),
},
join{
table: fileTable,
onClause: "scenes_files.file_id = files.id",
onClause: "scenes_files.file_id = files.id AND scenes_files.primary = " + getDBBoolean(true),
},
)
query.addColumn("COALESCE(files.size, 0) as size")
Expand Down Expand Up @@ -1099,11 +1099,11 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
query.addJoins(
join{
table: scenesFilesTable,
onClause: "scenes_files.scene_id = scenes.id",
onClause: "scenes_files.scene_id = scenes.id AND scenes_files.primary = " + getDBBoolean(true),
},
join{
table: fileTable,
onClause: "scenes_files.file_id = files.id",
onClause: "scenes_files.file_id = files.id AND scenes_files.primary = " + getDBBoolean(true),
},
)
}
Expand Down Expand Up @@ -1158,7 +1158,7 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
join{
table: fingerprintTable,
as: "fingerprints_phash",
onClause: "scenes_files.file_id = fingerprints_phash.file_id AND fingerprints_phash.type = 'phash'",
onClause: "scenes_files.file_id = fingerprints_phash.file_id AND scenes_files.primary = " + getDBBoolean(true) + " AND fingerprints_phash.type = 'phash'",
},
)

Expand Down
30 changes: 13 additions & 17 deletions pkg/sqlite/scene_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (qb *sceneFilterHandler) criterionHandler() criterionHandler {
criterionHandlerFunc(func(ctx context.Context, f *filterBuilder) {
if sceneFilter.Oshash != nil {
qb.addSceneFilesTable(f)
f.addLeftJoin(fingerprintTable, "fingerprints_oshash", "scenes_files.file_id = fingerprints_oshash.file_id AND fingerprints_oshash.type = 'oshash'")
f.addLeftJoin(fingerprintTable, "fingerprints_oshash", "scenes_files.file_id = fingerprints_oshash.file_id AND scenes_files.primary = "+getDBBoolean(true)+" AND fingerprints_oshash.type = 'oshash'")
}

stringCriterionHandler(sceneFilter.Oshash, "fingerprints_oshash.fingerprint")(ctx, f)
Expand All @@ -74,7 +74,7 @@ func (qb *sceneFilterHandler) criterionHandler() criterionHandler {
criterionHandlerFunc(func(ctx context.Context, f *filterBuilder) {
if sceneFilter.Checksum != nil {
qb.addSceneFilesTable(f)
f.addLeftJoin(fingerprintTable, "fingerprints_md5", "scenes_files.file_id = fingerprints_md5.file_id AND fingerprints_md5.type = 'md5'")
f.addLeftJoin(fingerprintTable, "fingerprints_md5", "scenes_files.file_id = fingerprints_md5.file_id AND scenes_files.primary = "+getDBBoolean(true)+" AND fingerprints_md5.type = 'md5'")
}

stringCriterionHandler(sceneFilter.Checksum, "fingerprints_md5.fingerprint")(ctx, f)
Expand Down Expand Up @@ -214,12 +214,12 @@ func (qb *sceneFilterHandler) criterionHandler() criterionHandler {
}

func (qb *sceneFilterHandler) addSceneFilesTable(f *filterBuilder) {
f.addLeftJoin(scenesFilesTable, "", "scenes_files.scene_id = scenes.id")
f.addLeftJoin(scenesFilesTable, "", "scenes_files.scene_id = scenes.id AND scenes_files.primary = "+getDBBoolean(true))
}

func (qb *sceneFilterHandler) addFilesTable(f *filterBuilder) {
qb.addSceneFilesTable(f)
f.addLeftJoin(fileTable, "", "scenes_files.file_id = files.id")
f.addLeftJoin(fileTable, "", "scenes_files.file_id = files.id AND scenes_files.primary = "+getDBBoolean(true))
}

func (qb *sceneFilterHandler) addFoldersTable(f *filterBuilder) {
Expand All @@ -229,7 +229,7 @@ func (qb *sceneFilterHandler) addFoldersTable(f *filterBuilder) {

func (qb *sceneFilterHandler) addVideoFilesTable(f *filterBuilder) {
qb.addSceneFilesTable(f)
f.addLeftJoin(videoFileTable, "", "video_files.file_id = scenes_files.file_id")
f.addLeftJoin(videoFileTable, "", "video_files.file_id = scenes_files.file_id AND scenes_files.primary = "+getDBBoolean(true))
}

func (qb *sceneFilterHandler) playCountCriterionHandler(count *models.IntCriterionInput) criterionHandlerFunc {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (qb *sceneFilterHandler) phashDuplicatedCriterionHandler(duplicatedFilter *
v = "="
}

f.addInnerJoin("(SELECT file_id FROM files_fingerprints INNER JOIN (SELECT fingerprint FROM files_fingerprints WHERE type = 'phash' GROUP BY fingerprint HAVING COUNT (fingerprint) "+v+" 1) dupes on files_fingerprints.fingerprint = dupes.fingerprint)", "scph", "scenes_files.file_id = scph.file_id")
f.addInnerJoin("(SELECT file_id FROM files_fingerprints INNER JOIN (SELECT fingerprint FROM files_fingerprints WHERE type = 'phash' GROUP BY fingerprint HAVING COUNT (fingerprint) "+v+" 1) dupes on files_fingerprints.fingerprint = dupes.fingerprint)", "scph", "scenes_files.file_id = scph.file_id AND scenes_files.primary = "+getDBBoolean(true))
}
}
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (qb *sceneFilterHandler) isMissingCriterionHandler(isMissing *string) crite
f.addWhere("scene_stash_ids.scene_id IS NULL")
case "phash":
qb.addSceneFilesTable(f)
f.addLeftJoin(fingerprintTable, "fingerprints_phash", "scenes_files.file_id = fingerprints_phash.file_id AND fingerprints_phash.type = 'phash'")
f.addLeftJoin(fingerprintTable, "fingerprints_phash", "scenes_files.file_id = fingerprints_phash.file_id AND scenes_files.primary = "+getDBBoolean(true)+" AND fingerprints_phash.type = 'phash'")
f.addWhere("fingerprints_phash.fingerprint IS NULL")
case "cover":
f.addWhere("scenes.cover_blob IS NULL")
Expand Down Expand Up @@ -389,7 +389,7 @@ func (qb *sceneFilterHandler) captionCriterionHandler(captions *models.StringCri
excludeClause := `scenes.id NOT IN (
SELECT scenes_files.scene_id from scenes_files
INNER JOIN video_captions on video_captions.file_id = scenes_files.file_id
WHERE video_captions.language_code " + getDBLike() + " ?
WHERE LOWER(video_captions.language_code) LIKE LOWER(?)
)`
f.addWhere(excludeClause, criterion.Value)

Expand Down Expand Up @@ -544,7 +544,7 @@ func (qb *sceneFilterHandler) phashDistanceCriterionHandler(phashDistance *model
return func(ctx context.Context, f *filterBuilder) {
if phashDistance != nil {
qb.addSceneFilesTable(f)
f.addLeftJoin(fingerprintTable, "fingerprints_phash", "scenes_files.file_id = fingerprints_phash.file_id AND fingerprints_phash.type = 'phash'")
f.addLeftJoin(fingerprintTable, "fingerprints_phash", "scenes_files.file_id = fingerprints_phash.file_id AND scenes_files.primary = "+getDBBoolean(true)+" AND fingerprints_phash.type = 'phash'")

value, _ := utils.StringToPhash(phashDistance.Value)
distance := 0
Expand All @@ -557,23 +557,19 @@ func (qb *sceneFilterHandler) phashDistanceCriterionHandler(phashDistance *model
intCriterionHandler(&models.IntCriterionInput{
Value: int(value),
Modifier: phashDistance.Modifier,
}, "fingerprints_phash.fingerprint", nil)(ctx, f)
}, "CAST(fingerprints_phash.fingerprint AS bigint)", nil)(ctx, f)
}

switch {
case phashDistance.Modifier == models.CriterionModifierEquals && distance > 0:
// needed to avoid a type mismatch
f.addWhere("typeof(fingerprints_phash.fingerprint) = 'integer'")
f.addWhere("phash_distance(fingerprints_phash.fingerprint, ?) < ?", value, distance)
f.addWhere("phash_distance(CAST(fingerprints_phash.fingerprint AS bigint), ?) < ?", value, distance)
case phashDistance.Modifier == models.CriterionModifierNotEquals && distance > 0:
// needed to avoid a type mismatch
f.addWhere("typeof(fingerprints_phash.fingerprint) = 'integer'")
f.addWhere("phash_distance(fingerprints_phash.fingerprint, ?) > ?", value, distance)
f.addWhere("phash_distance(CAST(fingerprints_phash.fingerprint AS bigint), ?) > ?", value, distance)
default:
intCriterionHandler(&models.IntCriterionInput{
Value: int(value),
Modifier: phashDistance.Modifier,
}, "fingerprints_phash.fingerprint", nil)(ctx, f)
}, "CAST(fingerprints_phash.fingerprint AS bigint)", nil)(ctx, f)
}
}
}
Expand Down

0 comments on commit cd0f2c5

Please sign in to comment.