libsaria: Began new index
Right now I just add songs to it and cache substrings for each word. I also print out stats during libsaria shutdown.
This commit is contained in:
parent
52a3e73023
commit
9c749e4ade
|
@ -0,0 +1,18 @@
|
|||
#ifndef LIBSARIA_INDEX_H
|
||||
#define LIBSARIA_INDEX_H
|
||||
|
||||
#include <libsaria/track.h>
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
namespace index
|
||||
{
|
||||
|
||||
void add_track(libsaria::Track &);
|
||||
void print_stats();
|
||||
|
||||
}; /* Namespace: index */
|
||||
|
||||
}; /* Namespace: libsaria */
|
||||
#endif /* LIBSARIA_INDEX_H */
|
|
@ -0,0 +1,100 @@
|
|||
|
||||
#include <libsaria/track.h>
|
||||
#include <libsaria/index.h>
|
||||
#include <libsaria/format.h>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
map<string, set<sid_t> > artist_index;
|
||||
map<string, set<sid_t> > album_index;
|
||||
map<string, set<sid_t> > title_index;
|
||||
|
||||
static map<string, set<string> > substr_cache;
|
||||
unsigned int hits;
|
||||
unsigned int misses;
|
||||
|
||||
static set<string> *gen_substrs(string &word)
|
||||
{
|
||||
string substr, key;
|
||||
set<string> substrs;
|
||||
pair< map<string, set<string> >::iterator, bool > ret;
|
||||
|
||||
for (unsigned int i = 0; i < word.size(); i++) {
|
||||
substr = word.substr(i, word.size());
|
||||
for (unsigned int j = 1; j < substr.size() + 1; j++) {
|
||||
key = substr.substr(0, j);
|
||||
substrs.insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
ret = substr_cache.insert( pair<string, set<string> >(word, substrs));
|
||||
return &(ret.first->second);
|
||||
}
|
||||
|
||||
static set<string> *find_substrs(string &word)
|
||||
{
|
||||
map<string, set<string> >::iterator it;
|
||||
it = substr_cache.find(word);
|
||||
if (it == substr_cache.end()) {
|
||||
misses++;
|
||||
return gen_substrs(word);
|
||||
} else {
|
||||
hits++;
|
||||
return &(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
static void add_to_index(sid_t &songid, string key,
|
||||
map<string, set<sid_t> >*index)
|
||||
{
|
||||
map<string, set<sid_t> >::iterator it;
|
||||
|
||||
it = index->find(key);
|
||||
if (it != index->end())
|
||||
it->second.insert(songid);
|
||||
else {
|
||||
set<sid_t> ids;
|
||||
ids.insert(songid);
|
||||
index->insert(pair<string, set<sid_t> >(key, ids));
|
||||
}
|
||||
}
|
||||
|
||||
static void index_word(sid_t &songid, string &word,
|
||||
map<string, set<sid_t> > *index)
|
||||
{
|
||||
set<string> *substrs = find_substrs(word);
|
||||
set<string>::iterator it;
|
||||
for (it = substrs->begin(); it != substrs->end(); it++)
|
||||
add_to_index(songid, *it, index);
|
||||
}
|
||||
|
||||
static void index_tag(sid_t &songid, const string &tag,
|
||||
map<string, set<sid_t> >*index)
|
||||
{
|
||||
list<string> *word_list;
|
||||
list<string>::iterator it;
|
||||
|
||||
word_list = libsaria::format_text(tag);
|
||||
for (it = word_list->begin(); it != word_list->end(); it++)
|
||||
index_word(songid, *it, index);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void index::add_track(libsaria::Track &track)
|
||||
{
|
||||
sid_t songid = track.get_songid();
|
||||
index_tag(songid, track.get_artist(), &artist_index);
|
||||
index_tag(songid, track.get_album(), &album_index);
|
||||
index_tag(songid, track.get_title(), &title_index);
|
||||
}
|
||||
|
||||
void index::print_stats()
|
||||
{
|
||||
println("Index cache hits: %u misses: %u", hits, misses);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
|
||||
#include <libsaria/library.h>
|
||||
#include <libsaria/index.h>
|
||||
#include "library.h"
|
||||
|
||||
LibraryPath::LibraryPath(InFile &in, string dir)
|
||||
|
@ -14,8 +15,10 @@ LibraryPath::LibraryPath(InFile &in, string dir)
|
|||
println("Library path dir: " + path);
|
||||
println("Library path size: %d", size);
|
||||
|
||||
for (unsigned int i = 0; i < size; i++)
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
file_list.push_back(libsaria::Track(in));
|
||||
libsaria::index::add_track(file_list.back());
|
||||
}
|
||||
}
|
||||
|
||||
void LibraryPath::save(OutFile &out)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <libsaria/print.h>
|
||||
#include <libsaria/library.h>
|
||||
#include <libsaria/format.h>
|
||||
#include <libsaria/index.h>
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
@ -27,6 +28,7 @@ namespace libsaria
|
|||
{
|
||||
println("Quitting libsaria");
|
||||
print_format_stats();
|
||||
index::print_stats();
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue