libsaria: Build the substring index

This is an index of every substring in the (artist, album, title) tags
of the library.  This should be easier to work with than regular
expression based filtering since I don't need to compare every key with
every search term.
This commit is contained in:
Bryan Schumaker 2011-11-13 09:48:50 -05:00
parent 7fc4e22795
commit d5df6e134b
2 changed files with 43 additions and 8 deletions

View File

@ -1,6 +1,46 @@
#include <map>
#include <set>
using namespace std;
#include "library.h"
static map<string, set<ino_t> > substr_index;
static void add_to_index(ino_t &inode, string &key)
{
map<string, set<ino_t> >::iterator it;
it = substr_index.find(key);
if (it != substr_index.end())
it->second.insert(inode);
else {
set<ino_t> to_insert;
to_insert.insert(inode);
substr_index.insert(pair<string, set<ino_t> >(key, to_insert));
}
}
static void index_word(ino_t &inode, string &word)
{
string substr, key;
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);
add_to_index(inode, key);
}
}
}
static void index_tag(ino_t &inode, list<string> *word_list)
{
list<string>::iterator it;
for (it = word_list->begin(); it != word_list->end(); it++)
index_word(inode, *it);
}
IndexTask::IndexTask() {};
IndexTask::~IndexTask() {};
@ -9,10 +49,6 @@ void IndexTask::index_track(TrackTag *tag)
track_list.push_back(tag);
}
void IndexTask::index_list(ino_t &inode, list<string> *word_list)
{
}
void IndexTask::run_task()
{
list<TrackTag *>::iterator it;
@ -20,9 +56,9 @@ void IndexTask::run_task()
for (it = track_list.begin(); it != track_list.end(); it++) {
inode = (*it)->get_inode();
index_list(inode, (*it)->get_title_words());
index_list(inode, (*it)->get_artist_words());
index_list(inode, (*it)->get_album_words());
index_tag(inode, (*it)->get_title_words());
index_tag(inode, (*it)->get_artist_words());
index_tag(inode, (*it)->get_album_words());
}
}

View File

@ -49,7 +49,6 @@ class IndexTask : public IdleTask
{
private:
list<TrackTag *> track_list;
void index_list(ino_t &, list<string> *);
public:
IndexTask();