PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/SongDatabase/SongIndex.vb

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