-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_update.go
246 lines (207 loc) · 6.09 KB
/
database_update.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright 2012, mokasin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"database/sql"
"github.com/mokasin/musicrawler/lib/database"
"github.com/mokasin/musicrawler/lib/database/mod"
"github.com/mokasin/musicrawler/lib/database/query"
"github.com/mokasin/musicrawler/lib/source"
"github.com/mokasin/musicrawler/model/album"
"github.com/mokasin/musicrawler/model/artist"
"github.com/mokasin/musicrawler/model/track"
)
// define databse actions
const (
TRACK_NOUPDATE = iota
TRACK_UPDATE
TRACK_ADD
)
type trackMtime struct {
ID int `column:"ID" set:"0"`
Mtime int64 `column:"filemtime"`
}
type trackDBMtime struct {
DBMtime int64 `column:"dbmtime"`
}
// Holds information of how the track at path was handeled. If the transaction
// was successfully err is nil.
type UpdateStatus struct {
Path string
Action uint8
Err error
}
// Holds information if the operation was successful.
type UpdateResult struct {
Deleted int64
Err error
}
// Update is a wrapper for update method, that should be called when using in a
// goroutine.
//
// It makes sure everything is cleaned up nicely before the signal gets emmitted
// to prevent racing conditions when closing the database connection.
func UpdateDatabase(db *database.Database, tracks <-chan source.TrackInfo,
status chan<- *UpdateStatus, result chan<- *UpdateResult) {
// signal is emitted, not untils index.Update() has cleaned up everything
result <- updateDatabase(db, tracks, status)
}
// Updates or adds tracks that are received at the tracks channel.
//
// For every track a status update UpdateStatus is emitted to the status
// channel. If the method finishes, the overall result is emitted on the result
// channel.
func updateDatabase(db *database.Database, tracks <-chan source.TrackInfo,
status chan<- *UpdateStatus) *UpdateResult {
err := db.BeginTransaction()
if err != nil {
close(status)
return &UpdateResult{Err: err}
}
defer db.EndTransaction()
var trackAction uint8
tm := &trackMtime{}
martists := mod.New(db, "artist")
malbums := mod.New(db, "album")
mtracks := mod.New(db, "track")
// traverse all catched pathes and update or add database entries
for ti := range tracks {
var statusErr error
trackAction = TRACK_NOUPDATE
// check if mtime has changed and decide what to do
err := query.New(db, "track").Where("path =", ti.Path()).Exec(tm)
switch {
case err == nil: // track is in database
// check if track has changed since the last time
if ti.Mtime() != tm.Mtime {
trackAction = TRACK_UPDATE
} else {
tdbm := &trackDBMtime{db.Mtime()}
statusErr = mtracks.Update(tm.ID, tdbm)
}
case err == sql.ErrNoRows: // track is not in database
trackAction = TRACK_ADD
tag, err := ti.Tags()
if err != nil {
statusErr = err
break
}
artist := &artist.Artist{
Name: tag.Artist,
}
res, err := martists.InsertIgnore(artist)
if err != nil {
statusErr = err
break
}
aff, _ := res.RowsAffected()
var artist_id int64
// if entry exists
if aff == 0 {
err = query.New(db, "artist").
Where("name =", tag.Artist).Limit(1).Exec(artist)
if err != nil {
statusErr = err
break
}
artist_id = artist.Id
} else {
artist_id, _ = res.LastInsertId()
}
album := &album.Album{
Name: tag.Album,
ArtistID: artist_id,
}
res, err = malbums.InsertIgnore(album)
if err != nil {
statusErr = err
break
}
aff, _ = res.RowsAffected()
var album_id int64
// if entry exists
if aff == 0 {
err = query.New(db, "album").
Where("name =", tag.Album).
Where("artist_id =", artist_id).
Limit(1).Exec(album)
if err != nil {
statusErr = err
break
}
album_id = album.Id
} else {
album_id, _ = res.LastInsertId()
}
track := &track.RawTrack{
Path: ti.Path(),
Title: tag.Title,
Tracknumber: tag.Track,
Year: tag.Year,
Length: tag.Length,
Genre: tag.Genre,
AlbumID: album_id,
Filemtime: ti.Mtime(),
DBMtime: db.Mtime(),
}
_, err = mtracks.Insert(track)
if err != nil {
statusErr = err
break
}
statusErr = nil
default:
// if something is wrong update timestamp, so track is not
// deleted the next time
tdbm := &trackDBMtime{db.Mtime()}
mtracks.Update(tm.ID, tdbm)
statusErr = err
}
status <- &UpdateStatus{
Path: ti.Path(),
Action: trackAction,
Err: statusErr}
}
// clean up
del, err := deleteDanglingEntries(db)
close(status)
return &UpdateResult{Err: err, Deleted: del}
}
// Deletes all entries that have an outdated timestamp dbmtime. Also cleans up
// entries in Artist and Album table that are not referenced anymore in the
// Track-table.
//
// Returns the number of deleted rows and an error.
func deleteDanglingEntries(db *database.Database) (int64, error) {
r, err := db.Execute("DELETE FROM Track WHERE dbmtime <> ?", db.Mtime())
deletedTracks, _ := r.RowsAffected()
if err != nil {
return deletedTracks, err
}
if _, err := db.Execute("DELETE FROM Album WHERE ID IN " +
"(SELECT Album.ID FROM Album LEFT JOIN Track ON " +
"Album.ID = Track.album_id WHERE Track.album_id " +
"IS NULL);"); err != nil {
return deletedTracks, err
}
if _, err := db.Execute("DELETE FROM Artist WHERE ID IN " +
"(SELECT Artist.ID FROM Artist LEFT JOIN Album ON " +
"Artist.ID = Album.artist_id WHERE Album.artist_id " +
"IS NULL);"); err != nil {
return deletedTracks, err
}
return deletedTracks, nil
}