PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/SongDatabase/SongIndex.vb

#
Visual Basic | 136 lines | 69 code | 29 blank | 38 comment | 3 complexity | f1c36317a883aa38391fe6a771b04dd6 MD5 | raw file
  1Imports System.IO
  2Imports PowerSong.FastSearching
  3Imports PowerSong.SongDatabase.Items
  4
  5''' <summary>
  6''' Represents the song index, which is used to speed up the searching for songs.
  7''' </summary>
  8Public Class SongIndex
  9
 10    Private FIndex As New Indexer
 11    Private FSearcher As New Searcher(FIndex)
 12    Private FFileName As String = ""
 13
 14    Private Sub New(ByVal directoryName As String)
 15        FFileName = directoryName + "\Songs.Index"
 16    End Sub
 17
 18    ''' <summary>
 19    ''' Creates a new song index at the specified directory name.
 20    ''' </summary>
 21    ''' <param name="directoryName">Name of the directory in which to create the song index (Songs.Index).</param>
 22    ''' <returns>The song index that is created.</returns>
 23    Friend Shared Function Create(ByVal directoryName As String) As SongIndex
 24
 25        Dim Result As New SongIndex(directoryName)
 26        Result.Save()
 27        Return Result
 28
 29    End Function
 30
 31    ''' <summary>
 32    ''' Loads the song index located at the specified directory name.
 33    ''' </summary>
 34    ''' <param name="directoryName">Name of the directory containing Songs.Index.</param>
 35    ''' <returns></returns>
 36    Public Shared Function Load(ByVal directoryName As String) As SongIndex
 37
 38        Dim Result As New SongIndex(directoryName)
 39        Result.FIndex = Indexer.LoadIndex(directoryName + "\Songs.Index")
 40        Result.FSearcher = New Searcher(Result.FIndex)
 41        Return Result
 42
 43    End Function
 44
 45    ''' <summary>
 46    ''' Adds the given song to the index.
 47    ''' </summary>
 48    ''' <param name="song">The song to add to the song index.</param>
 49    Public Sub AddToIndex(ByVal song As SongItem)
 50
 51        Try
 52
 53            ' Add the title to the song contents, enhancing search scores for title searches
 54            Dim SongContents As String = song.Title + Environment.NewLine + song.CombineIntoString
 55
 56            ' Index the song
 57            FIndex.AddFileContentsToIndex(song.SongID.ToString + ".song", SongContents)
 58
 59        Catch ex As Exception
 60            MsgBox("The song could not be added to the index:" + Environment.NewLine + _
 61                   ex.Message + Environment.NewLine + Environment.NewLine + _
 62                   "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
 63                   MsgBoxStyle.Exclamation)
 64        End Try
 65
 66    End Sub
 67
 68    ''' <summary>
 69    ''' Removes the given song from the song index.
 70    ''' </summary>
 71    ''' <param name="songID">The song ID of the song to remove from the index.</param>
 72    Public Sub RemoveFromIndex(ByVal songID As Guid)
 73
 74        Try
 75
 76            FIndex.RemoveDocumentFromIndex(songID.ToString + ".song")
 77
 78        Catch ex As Exception
 79            MsgBox("The song could not be removed from the index:" + Environment.NewLine + _
 80                   ex.Message + Environment.NewLine + Environment.NewLine + _
 81                   "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
 82                   MsgBoxStyle.Exclamation)
 83        End Try
 84
 85    End Sub
 86
 87    ''' <summary>
 88    ''' Updates the index with changes made to the given song.
 89    ''' </summary>
 90    ''' <param name="song">The song, which will be removed and then added again to the song index.</param>
 91    Public Sub UpdateIndex(ByVal song As SongItem)
 92
 93        Try
 94            RemoveFromIndex(song.SongID)
 95            AddToIndex(song)
 96        Catch ex As Exception
 97            MsgBox("The song index could not be updated:" + Environment.NewLine + _
 98                   ex.Message + Environment.NewLine + Environment.NewLine + _
 99                   "You will be unable to search for songs unless you recreate the index (from the options dialog).", _
100                   MsgBoxStyle.Exclamation)
101        End Try
102
103    End Sub
104
105    ''' <summary>
106    ''' Finds and returns songs in the song index, based on the search string.
107    ''' </summary>
108    ''' <param name="searchString">The search string.</param>
109    ''' <returns>All of the song IDs containing one or more words from the search string.</returns>
110    Public Function FindSongs(ByVal searchString As String) As List(Of Guid)
111
112        Dim SearchResults As List(Of String) = FSearcher.Search(searchString)
113        Dim Result As New List(Of Guid)
114        For Each FileName As String In SearchResults
115            Result.Add(New Guid(Path.GetFileNameWithoutExtension(FileName)))
116        Next
117        Return Result
118
119    End Function
120
121    ''' <summary>
122    ''' Clears the song index.
123    ''' </summary>
124    Public Sub Clear()
125        FIndex.Clear()
126        Save()
127    End Sub
128
129    ''' <summary>
130    ''' Saves the song index.
131    ''' </summary>
132    Public Sub Save()
133        FIndex.Save(FFileName)
134    End Sub
135
136End Class