refactor: optimize timestamp handling in FindMatches function

This commit is contained in:
Chigozirim Igweamaka 2025-03-08 06:01:16 +01:00
parent f1430ffb8b
commit 3f8f1b0514

View file

@ -48,7 +48,7 @@ func FindMatches(audioSample []float64, audioDuration float64, sampleRate int) (
} }
matches := map[uint32][][2]uint32{} // songID -> [(sampleTime, dbTime)] matches := map[uint32][][2]uint32{} // songID -> [(sampleTime, dbTime)]
timestamps := map[uint32][]uint32{} timestamps := map[uint32]uint32{} // songID -> earliest timestamp
targetZones := map[uint32]map[uint32]int{} // songID -> timestamp -> count targetZones := map[uint32]map[uint32]int{} // songID -> timestamp -> count
for address, couples := range m { for address, couples := range m {
@ -57,7 +57,10 @@ func FindMatches(audioSample []float64, audioDuration float64, sampleRate int) (
matches[couple.SongID], matches[couple.SongID],
[2]uint32{sampleFingerprint[address].AnchorTimeMs, couple.AnchorTimeMs}, [2]uint32{sampleFingerprint[address].AnchorTimeMs, couple.AnchorTimeMs},
) )
timestamps[couple.SongID] = append(timestamps[couple.SongID], couple.AnchorTimeMs)
if existingTime, ok := timestamps[couple.SongID]; !ok || couple.AnchorTimeMs < existingTime {
timestamps[couple.SongID] = couple.AnchorTimeMs
}
if _, ok := targetZones[couple.SongID]; !ok { if _, ok := targetZones[couple.SongID]; !ok {
targetZones[couple.SongID] = make(map[uint32]int) targetZones[couple.SongID] = make(map[uint32]int)
@ -83,11 +86,7 @@ func FindMatches(audioSample []float64, audioDuration float64, sampleRate int) (
continue continue
} }
sort.Slice(timestamps[songID], func(i, j int) bool { match := Match{songID, song.Title, song.Artist, song.YouTubeID, timestamps[songID], points}
return timestamps[songID][i] < timestamps[songID][j]
})
match := Match{songID, song.Title, song.Artist, song.YouTubeID, timestamps[songID][0], points}
matchList = append(matchList, match) matchList = append(matchList, match)
} }