-
Notifications
You must be signed in to change notification settings - Fork 23
/
library.go
201 lines (161 loc) · 4.58 KB
/
library.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package songlist
import (
"fmt"
"strings"
"time"
"github.com/blevesearch/bleve/v2"
"github.com/ambientsound/pms/console"
"github.com/ambientsound/pms/index"
)
// Library is a Songlist which represents the MPD song library.
type Library struct {
BaseSonglist
index *index.Index
version int
shutdownReIndex chan int
}
func NewLibrary() (s *Library) {
s = &Library{
shutdownReIndex: make(chan int, 1),
version: -1,
}
s.clear()
return
}
func (s *Library) Name() string {
return "Library"
}
func (s *Library) SetName(name string) error {
return fmt.Errorf("The song library name cannot be changed.")
}
func (s *Library) Clear() error {
return fmt.Errorf("The song library cannot be cleared because it is read-only.")
}
func (s *Library) Delete() error {
return fmt.Errorf("The song library cannot be deleted using PMS. Try 'rm -rf' in your favorite shell.")
}
func (s *Library) Sort(fields []string) error {
return fmt.Errorf("The song library is read-only. Please make a copy if you want to sort.")
}
func (s *Library) Remove(index int) error {
return fmt.Errorf("The song library is read-only.")
}
func (s *Library) RemoveIndices(indices []int) error {
return fmt.Errorf("The song library is read-only.")
}
// OpenIndex configures the library to use the Bleve search index at the specified path.
func (s *Library) OpenIndex(path string) error {
var err error
if s.HasIndex() {
if err = s.index.Close(); err != nil {
return err
}
s.index = nil
}
s.index, err = index.New(path)
return err
}
// HasIndex returns true if the library has a search index.
func (s *Library) HasIndex() bool {
return s.index != nil
}
// IndexSynced returns true if the search index is up to date with the MPD version.
func (s *Library) IndexSynced() bool {
return s.HasIndex() && s.index.Version() == s.version
}
// CloseIndex closes the Bleve search index.
func (s *Library) CloseIndex() error {
if s.HasIndex() {
return s.index.Close()
}
return nil
}
// SetVersion sets the library version. This is expected to be a UNIX timestamp.
func (s *Library) SetVersion(version int) {
s.version = version
}
// Version returns the library version.
func (s *Library) Version() int {
return s.version
}
// ReIndex starts an asynchronous reindexing job. In case this function is
// called again before reindexing is done, ReIndex will abort the old
// reindexing job.
func (s *Library) ReIndex() {
s.shutdownReIndex <- 0
s.shutdownReIndex = make(chan int, 1)
go func() {
timer := time.Now()
err := s.index.IndexFull(s.Songs(), s.shutdownReIndex)
console.Log("Song library index complete, took %s", time.Since(timer).String())
if err != nil {
console.Log("Error occurred during library reindex: %s", err)
return
}
s.index.SetVersion(s.Version())
}()
}
// Search does a search in the Bleve index for a specific natural language
// query string, and returns a new Songlist with the search results.
func (s *Library) Search(q string) (Songlist, error) {
if !s.HasIndex() {
return nil, fmt.Errorf("Search index is not open.")
}
query := bleve.NewQueryStringQuery(q)
request := bleve.NewSearchRequest(query)
request.Size = s.Len()
ids, _, err := s.index.Query(request)
if err != nil {
return nil, err
}
list := New()
list.SetName(q)
for _, id := range ids {
song := s.Song(id)
if song == nil {
return nil, fmt.Errorf("Search index is corrupt.")
}
list.Add(song)
}
return list, nil
}
// Isolate takes a songlist and a set of tag keys, and matches the tag values
// of the songlist against the search index.
func (s *Library) Isolate(songs Songlist, tags []string) (Songlist, error) {
if !s.HasIndex() {
return nil, fmt.Errorf("Search index is not open.")
}
terms := make(map[string]struct{})
query := bleve.NewBooleanQuery()
// Create a cartesian join for song values and tag list.
for _, song := range songs.Songs() {
subQuery := bleve.NewConjunctionQuery()
for _, tag := range tags {
// Ignore empty values
tagValue := song.StringTags[tag]
if len(tagValue) == 0 {
continue
}
// Name generation
terms[tagValue] = struct{}{}
field := strings.Title(tag)
query := bleve.NewMatchPhraseQuery(tagValue)
query.SetField(field)
subQuery.AddQuery(query)
}
query.AddShould(subQuery)
}
// Construct a fitting name for this track list
names := make([]string, 0)
for k := range terms {
names = append(names, k)
}
name := strings.Join(names, ", ")
// Make the search
request := bleve.NewSearchRequest(query)
request.Size = s.Len()
r, _, err := s.index.Query(request)
list := s.Indices(r)
list.SetName(name)
return list, err
}