stats: use one db_walk() to obtain stats

Don't use dbUtils.h functions.  This reduces 4 full database walks to
just one.
This commit is contained in:
Max Kellermann 2009-01-18 15:40:50 +01:00
parent 91fb2a29de
commit 14ca99b224
3 changed files with 47 additions and 76 deletions

View File

@ -50,16 +50,6 @@ typedef struct _SearchStats {
unsigned long playTime;
} SearchStats;
static int
countSongsInDirectory(struct directory *directory, void *data)
{
int *count = (int *)data;
*count += directory->songs.nr;
return 0;
}
static int
printDirectoryInDirectory(struct directory *directory, void *data)
{
@ -237,43 +227,12 @@ directoryPrintSongInfo(struct song *song, void *data)
return 0;
}
static int
sumSongTime(struct song *song, void *data)
{
unsigned long *sum_time = (unsigned long *)data;
if (song->tag && song->tag->time >= 0)
*sum_time += song->tag->time;
return 0;
}
int printInfoForAllIn(struct client *client, const char *name)
{
return db_walk(name, directoryPrintSongInfo,
printDirectoryInDirectory, client);
}
int countSongsIn(const char *name)
{
int count = 0;
void *ptr = (void *)&count;
db_walk(name, NULL, countSongsInDirectory, ptr);
return count;
}
unsigned long sumSongTimesIn(const char *name)
{
unsigned long dbPlayTime = 0;
void *ptr = (void *)&dbPlayTime;
db_walk(name, sumSongTime, NULL, ptr);
return dbPlayTime;
}
static ListCommandItem *newListCommandItem(int tagType, int numConditionals,
LocateTagItem * conditionals)
{

View File

@ -40,8 +40,6 @@ int findSongsIn(struct client *client, const char *name,
int searchStatsForSongsIn(struct client *client, const char *name,
int numItems, LocateTagItem * items);
int countSongsIn(const char *name);
unsigned long sumSongTimesIn(const char *name);
int listAllUniqueTags(struct client *client, int type, int numConditiionals,

View File

@ -24,7 +24,6 @@
#include "client.h"
#include "player_control.h"
#include "strset.h"
#include "dbUtils.h"
struct stats stats;
@ -34,50 +33,65 @@ void stats_global_init(void)
}
struct visit_data {
enum tag_type type;
struct strset *set;
struct strset *artists;
struct strset *albums;
};
static int
visit_tag_items(struct song *song, void *_data)
static void
visit_tag(struct visit_data *data, const struct tag *tag)
{
const struct visit_data *data = _data;
unsigned i;
if (tag->time > 0)
stats.song_duration += tag->time;
if (song->tag == NULL)
return 0;
for (unsigned i = 0; i < tag->numOfItems; ++i) {
const struct tag_item *item = tag->items[i];
for (i = 0; i < (unsigned)song->tag->numOfItems; ++i) {
const struct tag_item *item = song->tag->items[i];
if (item->type == data->type)
strset_add(data->set, item->value);
switch (item->type) {
case TAG_ITEM_ARTIST:
strset_add(data->artists, item->value);
break;
case TAG_ITEM_ALBUM:
strset_add(data->albums, item->value);
break;
default:
break;
}
}
}
static int
stats_collect_song(struct song *song, void *_data)
{
struct visit_data *data = _data;
++stats.song_count;
if (song->tag != NULL)
visit_tag(data, song->tag);
return 0;
}
static unsigned int
getNumberOfTagItems(enum tag_type type)
{
struct visit_data data = {
.type = type,
.set = strset_new(),
};
unsigned int ret;
db_walk(NULL, visit_tag_items, NULL, &data);
ret = strset_size(data.set);
strset_free(data.set);
return ret;
}
void stats_update(void)
{
stats.song_count = countSongsIn(NULL);
stats.song_duration = sumSongTimesIn(NULL);
stats.artist_count = getNumberOfTagItems(TAG_ITEM_ARTIST);
stats.album_count = getNumberOfTagItems(TAG_ITEM_ALBUM);
struct visit_data data;
stats.song_count = 0;
stats.song_duration = 0;
stats.artist_count = 0;
data.artists = strset_new();
data.albums = strset_new();
db_walk(NULL, stats_collect_song, NULL, &data);
stats.artist_count = strset_size(data.artists);
stats.album_count = strset_size(data.albums);
strset_free(data.artists);
strset_free(data.albums);
}
int stats_print(struct client *client)