-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.cpp
371 lines (342 loc) · 10.2 KB
/
database.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/****
BpmDj v4.2-pl2: Free Dj Tools
Copyright (C) 2001-2011 Werner Van Belle
http://bpmdj.yellowcouch.org/
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 2 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.
See the authors.txt for a full list of people involved.
****/
#ifndef __loaded__database_cpp__
#define __loaded__database_cpp__
using namespace std;
#include <qtreewidget.h>
#include <qlabel.h>
#include <cstdio>
#include "history.h"
#include "database.h"
#include "song-metric.h"
#include "qsong.h"
#include "selector.h"
#include "players-manager.h"
#include "vector-iterator.h"
#include "tags.h"
#include "heap.h"
#include "statistics.h"
#include "bpmdj.h"
#include "existence-scanner.h"
#include "history.h"
#include "database.h"
#include "song-metric.h"
#include "qsong.h"
#include "scripts.h"
#include "info.h"
#include "qt-helpers.h"
void DataBase::reset()
{
clear();
init();
}
DataBase::DataBase() : all()
{
init();
}
Song * DataBase::find(QString song_filename)
{
map<QString,Song*>::iterator ssbf;
ssbf = file2song.find(song_filename);
if (ssbf==file2song.end())
return NULL;
return ssbf->second;
}
DataBase::~DataBase()
{
clear();
}
void DataBase::init()
{
all.clear();
file2song = map<QString,Song*>();
cache.clear();
and_include = NULL;
or_include = NULL;
exclude = NULL;
tag = NULL;
tag_size = 0;
rebuild_cache = false;
}
void DataBase::clear()
{
vectorIterator<Song*> i(all);
ITERATE_OVER(i)
delete i.val(); }
all.clear();
file2song = map<QString,Song*>();
cache.clear();
bpmdj_deallocate(and_include);
bpmdj_deallocate(or_include);
bpmdj_deallocate(exclude);
and_include=NULL;
or_include=NULL;
exclude=NULL;
delete[] tag;
tag=NULL;
}
bool DataBase::cacheValid(SongSelectorLogic * selector)
{
if (rebuild_cache || selector->tagList->topLevelItemCount() != tag_size)
return false;
int i = 0;
stdTreeWidgetIterator t(selector->tagList);
ITERATE_OVER(t)
tag_type ta = Tags::find_tag(t.val()->text(TAGS_TEXT));
if ( (ta != tag[i]) ||
(t.val()->text(TAGS_AND) == TAG_TRUE) != and_include[i] ||
(t.val()->text(TAGS_OR) == TAG_TRUE) != or_include[i] ||
(t.val()->text(TAGS_NOT) == TAG_TRUE) != exclude[i])
return false;
i++;
}
return true;
}
void DataBase::copyTags(SongSelectorLogic * selector)
{
if (and_include) bpmdj_deallocate(and_include);
if (or_include) bpmdj_deallocate(or_include);
if (exclude) bpmdj_deallocate(exclude);
if (tag) delete[](tag);
tag_size = selector->tagList->topLevelItemCount();
and_include = bpmdj_allocate(tag_size,bool);
or_include = bpmdj_allocate(tag_size,bool);
exclude = bpmdj_allocate(tag_size,bool);
tag = new tag_type[tag_size];
and_includes_checked = false;
excludes_checked = false;
int i = 0;
stdTreeWidgetIterator t(selector->tagList);
ITERATE_OVER(t)
tag[i] = Tags::find_tag(t.val()->text(TAGS_TEXT));
// printf("%s = %d\n",(const char*) t->text(TAGS_TEXT),tag[i]);
or_include[i] = t.val()->text(TAGS_OR) == TAG_TRUE;
and_includes_checked |= and_include[i] = t.val()->text(TAGS_AND) == TAG_TRUE;
excludes_checked |= exclude[i] = t.val()->text(TAGS_NOT) == TAG_TRUE;
i++;
}
}
bool DataBase::tagFilter(Song* item)
{
bool matched = false;
// first we check the or_include tags
for (int i = 0; !matched && i < tag_size; i++)
if (or_include[i])
if (item->contains_tag(tag[i]))
matched=true;
if (!matched) return false;
// now we check the and_include tags
if (and_includes_checked)
for(int i = 0; i < tag_size ; i ++)
if(and_include[i])
if (!item->contains_tag(tag[i]))
return false;
// now we check the exclusion tags
if (excludes_checked)
for (int i=0; i < tag_size; i++)
if (exclude[i])
if (item->contains_tag(tag[i]))
return false;
return matched;
}
void DataBase::updateCache(SongSelectorLogic* selector)
{
if (cacheValid(selector)) return;
rebuild_cache = false;
copyTags(selector);
cache.clear();
vectorIterator<Song*> song(all);
ITERATE_OVER(song)
bool vis = tagFilter(song.val());
if (vis) cache.push_back(song.val());
}
}
/* WVB -- the thing below can be further optimized
* 1 - get all flags and pass them in one flag
*/
bool DataBase::filter(SongSelectorLogic* selector, Song *item, Song* main,
float4 limit)
{
if (main!=NULL && main->get_author()==item->get_author() &&
!main->get_author().isEmpty())
item->set_played_author_at_time(History::get_songs_played());
// song on disk ?
if (Config::limit_ondisk && !item->get_ondisk())
return false;
// song played ?
if (Config::limit_nonplayed && item->get_played())
return false;
// okay, no similar authors please..
if (Config::limit_authornonplayed &&
(signed4)(History::get_songs_played()-item->get_played_author_at_time())
< Config::get_authorDecay())
return false;
// now check the tempo stuff
if (main && (Config::limit_uprange || Config::limit_downrange))
if (!item->tempo_show(main,
Config::limit_uprange,
Config::limit_downrange)) return false;
// search
const QString lookingfor = selector -> searchLine -> text();
if (!lookingfor.isEmpty())
{
QString title=item->get_title().toUpper();
QString author=item->get_author().toUpper();
if (!title.contains(lookingfor) && !author.contains(lookingfor))
return false;
}
// obtain the distance
bool indistance = item->get_distance_to_main(limit);
if (Config::limit_indistance && !indistance)
return false;
return true;
}
int DataBase::get_unheaped_selection(SongSelectorLogic* selector, Song* main,
QSong* target)
{
// to get an appropriate selection we allocate the necessary vector
int itemcount=0;
updateCache(selector);
Song ** show = bpmdj_allocate(cache.size(),Song*);
vectorIterator<Song*> song(cache);
ITERATE_OVER(song)
bool vis = filter(selector,song.val(),main,1.0);
if (vis) show[itemcount++] = song.val();
}
return set_answer(show,itemcount,target);
}
int DataBase::set_answer(Song ** show, int itemcount, QSong* target)
{
if (itemcount)
show = bpmdj_reallocate(show, itemcount, Song*);
else
{
show = NULL;
bpmdj_deallocate(show);
}
Song* before=get_current_song();
target->setVector(show,itemcount);
set_current_song(before,true);
return itemcount;
}
int DataBase::getSelection(SongSelectorLogic* selector, QSong* target,
int count)
{
Song* main=::main_song;
// only when we have a dcolor limitation can we use the amount limitation
if (!Config::limit_indistance || count==0 || main==NULL)
return get_unheaped_selection(selector, main, target);
assert(count>0);
updateCache(selector);
SongHeap heap(count);
if (Config::limit_indistance) heap.maximum = 1.0;
vectorIterator<Song*> song(cache);
ITERATE_OVER(song)
bool vis = filter(selector,song.val(),main, heap.maximum);
if (vis) heap.add(song.val());
}
Song ** show = bpmdj_allocate(count,Song*);
int itemcount = heap.copy_to(show);
assert(itemcount<=count);
return set_answer(show,itemcount,target);
}
void DataBase::addNewSongs(SongSelectorLogic* selector, QSong* target,
vector<Song*> *newsongs)
{
/**
* Since these come from the index reader we do not want to limit the
* selection to a heap since that would slow everything down. Instead we
* simply extend the current selection if the song satisfies all other
* criteria (in tempo, proper tag etc) we also do not check cache updates
* or the likes, this means that the filter must be applied in 2 steps.
* First to check the tag, then to check its distance to the main song
*/
assert(newsongs);
if (!newsongs->size()) return;
Song* show[newsongs->size()];
unsigned int count=0;
vectorIterator<Song*> song(newsongs);
ITERATE_OVER(song)
add(song.val()); // add it to the total list
bool vis = tagFilter(song.val());
if (vis)
{
cache.push_back(song.val()); // put it in the cache if necessary
if (filter(selector,song.val(),::main_song,1.0))
show[count++]=song.val(); // put it in the current selection
}
}
assert(count<=newsongs->size());
target->addVector(show,count);
}
void DataBase::add(Song* song)
{
all.push_back(song);
if (file2song.find(song->get_file())!=file2song.end())
{
Song *song2 = file2song[song->get_file()];
Fatal("The song \n %s\n"
"occurs at least two times in different index files\n"
"The first index file is \n %s\n"
"The second index file is \n %s",
(const char*)song->get_file().toAscii().data(),
(const char*)song->get_storedin().toAscii().data(),
(const char*)song2->get_storedin().toAscii().data());
}
file2song[song->get_file()]=song;
flush_cache();
};
Song * * DataBase::closestSongs(SongSelectorLogic * selector,
Song * target1, float4 weight1,
Song * target2, float4 weight2,
SongMetriek * metriek, int maximum, int &count)
{
int i, j;
float4 * minima = bpmdj_allocate(maximum,float4);
Song * * entries = bpmdj_allocate(maximum,Song*);
count = 0;
for(i = 0 ; i < maximum ; i++)
{
entries[i]=NULL;
minima[i]=-1;
}
updateCache(selector);
vectorIterator<Song*> song(cache);
ITERATE_OVER(song)
// standard checks: tags completed and ondisk
if (!song.val()->get_ondisk()) continue;
if (!tagFilter(song.val())) continue;
// measure distance, gegeven de metriek
float8 d = song.val()->distance(target1,weight1,target2,weight2,metriek);
// find the best position to insert the item..
for (j = 0 ; j < count ; j++)
if (minima[j]>d) break;
if (j==count && count < maximum)
count++;
// insert the item
if (j<count)
{
for(int k = maximum - 1 ; k > j ; k --)
{
minima[k]=minima[k-1];
entries[k]=entries[k-1];
}
minima[j]=d;
entries[j]=song.val();
}
}
return entries;
}
#endif // __loaded__database_cpp__