/Source Code/SongDatabase/SongIndex.vb
Visual Basic | 136 lines | 69 code | 29 blank | 38 comment | 3 complexity | f1c36317a883aa38391fe6a771b04dd6 MD5 | raw file
- Imports System.IO
- Imports PowerSong.FastSearching
- Imports PowerSong.SongDatabase.Items
-
- ''' <summary>
- ''' Represents the song index, which is used to speed up the searching for songs.
- ''' </summary>
- Public Class SongIndex
-
- Private FIndex As New Indexer
- Private FSearcher As New Searcher(FIndex)
- Private FFileName As String = ""
-
- Private Sub New(ByVal directoryName As String)
- FFileName = directoryName + "\Songs.Index"
- End Sub
-
- ''' <summary>
- ''' Creates a new song index at the specified directory name.
- ''' </summary>
- ''' <param name="directoryName">Name of the directory in which to create the song index (Songs.Index).</param>
- ''' <returns>The song index that is created.</returns>
- Friend Shared Function Create(ByVal directoryName As String) As SongIndex
-
- Dim Result As New SongIndex(directoryName)
- Result.Save()
- Return Result
-
- End Function
-
- ''' <summary>
- ''' Loads the song index located at the specified directory name.
- ''' </summary>
- ''' <param name="directoryName">Name of the directory containing Songs.Index.</param>
- ''' <returns></returns>
- Public Shared Function Load(ByVal directoryName As String) As SongIndex
-
- Dim Result As New SongIndex(directoryName)
- Result.FIndex = Indexer.LoadIndex(directoryName + "\Songs.Index")
- Result.FSearcher = New Searcher(Result.FIndex)
- Return Result
-
- End Function
-
- ''' <summary>
- ''' Adds the given song to the index.
- ''' </summary>
- ''' <param name="song">The song to add to the song index.</param>
- Public Sub AddToIndex(ByVal song As SongItem)
-
- Try
-
- ' Add the title to the song contents, enhancing search scores for title searches
- Dim SongContents As String = song.Title + Environment.NewLine + song.CombineIntoString
-
- ' Index the song
- FIndex.AddFileContentsToIndex(song.SongID.ToString + ".song", SongContents)
-
- Catch ex As Exception
- MsgBox("The song could not be added to the index:" + Environment.NewLine + _
- ex.Message + Environment.NewLine + Environment.NewLine + _
- "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
- MsgBoxStyle.Exclamation)
- End Try
-
- End Sub
-
- ''' <summary>
- ''' Removes the given song from the song index.
- ''' </summary>
- ''' <param name="songID">The song ID of the song to remove from the index.</param>
- Public Sub RemoveFromIndex(ByVal songID As Guid)
-
- Try
-
- FIndex.RemoveDocumentFromIndex(songID.ToString + ".song")
-
- Catch ex As Exception
- MsgBox("The song could not be removed from the index:" + Environment.NewLine + _
- ex.Message + Environment.NewLine + Environment.NewLine + _
- "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
- MsgBoxStyle.Exclamation)
- End Try
-
- End Sub
-
- ''' <summary>
- ''' Updates the index with changes made to the given song.
- ''' </summary>
- ''' <param name="song">The song, which will be removed and then added again to the song index.</param>
- Public Sub UpdateIndex(ByVal song As SongItem)
-
- Try
- RemoveFromIndex(song.SongID)
- AddToIndex(song)
- Catch ex As Exception
- MsgBox("The song index could not be updated:" + Environment.NewLine + _
- ex.Message + Environment.NewLine + Environment.NewLine + _
- "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
- MsgBoxStyle.Exclamation)
- End Try
-
- End Sub
-
- ''' <summary>
- ''' Finds and returns songs in the song index, based on the search string.
- ''' </summary>
- ''' <param name="searchString">The search string.</param>
- ''' <returns>All of the song IDs containing one or more words from the search string.</returns>
- Public Function FindSongs(ByVal searchString As String) As List(Of Guid)
-
- Dim SearchResults As List(Of String) = FSearcher.Search(searchString)
- Dim Result As New List(Of Guid)
- For Each FileName As String In SearchResults
- Result.Add(New Guid(Path.GetFileNameWithoutExtension(FileName)))
- Next
- Return Result
-
- End Function
-
- ''' <summary>
- ''' Clears the song index.
- ''' </summary>
- Public Sub Clear()
- FIndex.Clear()
- Save()
- End Sub
-
- ''' <summary>
- ''' Saves the song index.
- ''' </summary>
- Public Sub Save()
- FIndex.Save(FFileName)
- End Sub
-
- End Class