Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize checking if track already exists. #9

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions src/stores/entities/create-entities-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Playlist,
MusicItemType,
UnknownTrack,
FileWrapper,
} from '../../types/types'
import { UNKNOWN_ITEM_ID } from '../../types/constants'
import { useToast } from '../../components/toasts/toasts'
Expand Down Expand Up @@ -78,45 +79,55 @@ export const createEntitiesStore = () => {
})
}

// Returns undefined if track already exists or the supplied track.
const checkIfTrackIsNew = async (newTrack: UnknownTrack) => {
const existingTracks = Object.values(state.tracks)
const fileEquals = async (
fileWrapperA: FileWrapper,
fileWrapperB: FileWrapper,
) => {
if (fileWrapperA.type === 'fileRef' && fileWrapperB.type === 'fileRef') {
const fileA = fileWrapperA.file
const fileB = fileWrapperB.file

const newTrackFile = newTrack.fileWrapper
// We want search to run sequently here.
for await (const { fileWrapper: existingTrackFile } of existingTracks) {
if (
existingTrackFile.type === 'fileRef' &&
newTrackFile.type === 'fileRef'
) {
if (await existingTrackFile.file.isSameEntry(newTrackFile.file)) {
return undefined
}
}
return fileA.name === fileB.name && fileA.isSameEntry(fileB)
}

if (existingTrackFile.type === 'file' && newTrackFile.type === 'file') {
const existingFile = existingTrackFile.file
const newFile = newTrackFile.file
if (fileWrapperA.type === 'file' && fileWrapperB.type === 'file') {
const fileA = fileWrapperA.file
const fileB = fileWrapperB.file

// There is no good way to compare two files.
const trackAlreadyExists =
existingFile.name === newFile.name &&
existingFile.size === newFile.size
// There is no good way to compare two files.
return fileA.name === fileB.name && fileA.size === fileB.size
}

if (trackAlreadyExists) {
return undefined
return false
}

const filterExistingTracks = async (newTracks: readonly UnknownTrack[]) => {
const existingTracks = Object.values(state.tracks)

const uniqueTracks: UnknownTrack[] = []
for await (const newTrack of newTracks) {
let foundTrackIndex = -1
let i = 0
for await (const existingTrack of existingTracks) {
if (await fileEquals(newTrack.fileWrapper, existingTrack.fileWrapper)) {
foundTrackIndex = i
break
}
i += 1
}

if (foundTrackIndex === -1) {
uniqueTracks.push(newTrack)
} else {
existingTracks.splice(foundTrackIndex, 1)
}
}

return newTrack
return uniqueTracks
}

const addNewTracks = async (tracks: readonly UnknownTrack[]) => {
// We want independent track checking to occur in parallel.
const newTracksWithHoles = await Promise.all(tracks.map(checkIfTrackIsNew))

const newTracks = newTracksWithHoles.filter(Boolean)
const newTracks = await filterExistingTracks(tracks)

setState(
produce((s: State) => {
Expand Down