libsaria: Remove old index code
It has been replaced with my new index.
This commit is contained in:
parent
1d3baa79d5
commit
26a18f331e
|
@ -48,7 +48,6 @@ static void do_load()
|
||||||
|
|
||||||
libsaria::library::rebuild_list();
|
libsaria::library::rebuild_list();
|
||||||
libsaria::library::refresh();
|
libsaria::library::refresh();
|
||||||
libsaria::library::reindex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LoadTask::LoadTask() {}
|
LoadTask::LoadTask() {}
|
||||||
|
|
|
@ -1,210 +0,0 @@
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <set>
|
|
||||||
#include <algorithm>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include <libsaria/callback.h>
|
|
||||||
#include <libsaria/format.h>
|
|
||||||
#include "library.h"
|
|
||||||
|
|
||||||
#define MAX_TRACKS 100
|
|
||||||
|
|
||||||
static map<string, set<sid_t> > substr_index;
|
|
||||||
static set<sid_t> results;
|
|
||||||
static string cur_filter;
|
|
||||||
static bool filtered;
|
|
||||||
|
|
||||||
static void add_to_index(sid_t &inode, string &key)
|
|
||||||
{
|
|
||||||
map<string, set<sid_t> >::iterator it;
|
|
||||||
|
|
||||||
it = substr_index.find(key);
|
|
||||||
if (it != substr_index.end())
|
|
||||||
it->second.insert(inode);
|
|
||||||
else {
|
|
||||||
set<sid_t> to_insert;
|
|
||||||
to_insert.insert(inode);
|
|
||||||
substr_index.insert(pair<string, set<sid_t> >(key, to_insert));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void index_word(sid_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(sid_t &inode, string tag)
|
|
||||||
{
|
|
||||||
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(inode, *it);
|
|
||||||
}
|
|
||||||
|
|
||||||
IndexTask::IndexTask() {};
|
|
||||||
IndexTask::~IndexTask() {};
|
|
||||||
|
|
||||||
void IndexTask::index_track(libsaria::Track *tag)
|
|
||||||
{
|
|
||||||
track_list.push_back(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IndexTask::run_task()
|
|
||||||
{
|
|
||||||
list<libsaria::Track *>::iterator it;
|
|
||||||
sid_t inode;
|
|
||||||
|
|
||||||
for (it = track_list.begin(); it != track_list.end(); it++) {
|
|
||||||
inode = (*it)->get_songid();
|
|
||||||
index_tag(inode, (*it)->get_title());
|
|
||||||
index_tag(inode, (*it)->get_artist());
|
|
||||||
index_tag(inode, (*it)->get_album());
|
|
||||||
}
|
|
||||||
if (cur_filter != "")
|
|
||||||
libsaria::library::filter(cur_filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void reindex_path(list<libsaria::Track> *tag_list)
|
|
||||||
{
|
|
||||||
list<libsaria::Track>::iterator it;
|
|
||||||
unsigned int i = 0, j = 0;
|
|
||||||
IndexTask *task = new IndexTask();
|
|
||||||
|
|
||||||
for (it = tag_list->begin(); it != tag_list->end(); it++) {
|
|
||||||
task->index_track(&(*it));
|
|
||||||
i++;
|
|
||||||
if (i == MAX_TRACKS) {
|
|
||||||
task->queue();
|
|
||||||
task = new IndexTask();
|
|
||||||
i = 0;
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
task->queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Thanks to: http://stackoverflow.com/questions/1773526/in-place-c-set-intersection
|
|
||||||
* for the set intersection algorithm!
|
|
||||||
*/
|
|
||||||
static void inplace_intersect(set<sid_t> *inodes)
|
|
||||||
{
|
|
||||||
set<sid_t>::iterator it1 = results.begin();
|
|
||||||
set<sid_t>::iterator it2 = inodes->begin();
|
|
||||||
|
|
||||||
while ( (it1 != results.end()) && (it2 != inodes->end()) ) {
|
|
||||||
if (*it1 < *it2)
|
|
||||||
results.erase(it1++);
|
|
||||||
else if (*it2 < *it1)
|
|
||||||
it2++;
|
|
||||||
else { /* *it1 == *it2 */
|
|
||||||
it1++;
|
|
||||||
it2++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove everything in results that wasn't in inodes */
|
|
||||||
results.erase(it1, results.end());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void do_filter(list<string> *terms)
|
|
||||||
{
|
|
||||||
list<string>::iterator it;
|
|
||||||
map<string, set<sid_t> >::iterator index_iter;
|
|
||||||
|
|
||||||
for (it = terms->begin(); it != terms->end(); it++) {
|
|
||||||
index_iter = substr_index.find(*it);
|
|
||||||
/*
|
|
||||||
* Key not found means we don't need to filter anymore
|
|
||||||
* just clear the results set and return
|
|
||||||
*/
|
|
||||||
if (index_iter == substr_index.end()) {
|
|
||||||
results.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* This is the first result, so the result set is empty.
|
|
||||||
* Taking an intersection will always give us an empty set
|
|
||||||
*/
|
|
||||||
if (it == terms->begin())
|
|
||||||
results = index_iter->second;
|
|
||||||
else
|
|
||||||
inplace_intersect(&index_iter->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace libsaria
|
|
||||||
{
|
|
||||||
|
|
||||||
void library::reindex()
|
|
||||||
{
|
|
||||||
list<LibraryPath> *path_list = get_path_list();
|
|
||||||
list<LibraryPath>::iterator it;
|
|
||||||
|
|
||||||
for (it = path_list->begin(); it != path_list->end(); it++)
|
|
||||||
reindex_path(it->get_list());
|
|
||||||
}
|
|
||||||
|
|
||||||
void library::filter(string &text)
|
|
||||||
{
|
|
||||||
list<string> *key_list;
|
|
||||||
|
|
||||||
key_list = format_text(text);
|
|
||||||
results.clear();
|
|
||||||
|
|
||||||
if (key_list->size() == 0)
|
|
||||||
filtered = false;
|
|
||||||
else {
|
|
||||||
do_filter(key_list);
|
|
||||||
filtered = true;
|
|
||||||
}
|
|
||||||
cur_filter = text;
|
|
||||||
trigger_callback(REFILTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool library::is_filtered()
|
|
||||||
{
|
|
||||||
return filtered;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool library::is_visible(sid_t &inode)
|
|
||||||
{
|
|
||||||
set<sid_t>::iterator it;
|
|
||||||
|
|
||||||
if (filtered == false)
|
|
||||||
return true;
|
|
||||||
it = results.find(inode);
|
|
||||||
return it != results.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int library::filter_size()
|
|
||||||
{
|
|
||||||
return results.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
sid_t library::index_rand()
|
|
||||||
{
|
|
||||||
set<sid_t>::iterator it = results.begin();
|
|
||||||
unsigned int index = rand() % filter_size();
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < index; i++)
|
|
||||||
it++;
|
|
||||||
return *it;
|
|
||||||
}
|
|
||||||
|
|
||||||
}; /* Namespace: libsaria */
|
|
|
@ -44,7 +44,6 @@ namespace libsaria
|
||||||
void library::refresh()
|
void library::refresh()
|
||||||
{
|
{
|
||||||
rebuild_list();
|
rebuild_list();
|
||||||
reindex();
|
|
||||||
trigger_callback(LIBRARY_REFRESH);
|
trigger_callback(LIBRARY_REFRESH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,18 +54,6 @@ class ScanTask : public IdleTask
|
||||||
void run_task();
|
void run_task();
|
||||||
};
|
};
|
||||||
|
|
||||||
class IndexTask : public IdleTask
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
list<libsaria::Track *> track_list;
|
|
||||||
|
|
||||||
public:
|
|
||||||
IndexTask();
|
|
||||||
~IndexTask();
|
|
||||||
void index_track(libsaria::Track *);
|
|
||||||
void run_task();
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace libsaria
|
namespace libsaria
|
||||||
{
|
{
|
||||||
namespace library
|
namespace library
|
||||||
|
@ -73,10 +61,6 @@ namespace libsaria
|
||||||
list<LibraryPath> *get_path_list();
|
list<LibraryPath> *get_path_list();
|
||||||
LibraryPath *get_path(string);
|
LibraryPath *get_path(string);
|
||||||
void rebuild_list();
|
void rebuild_list();
|
||||||
void reindex();
|
|
||||||
bool is_filtered();
|
|
||||||
unsigned int filter_size();
|
|
||||||
sid_t index_rand();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ static void random_next()
|
||||||
cur_track = play_list.begin();
|
cur_track = play_list.begin();
|
||||||
if (libsaria::index::is_filtered()) {
|
if (libsaria::index::is_filtered()) {
|
||||||
println("Picking random track from index");
|
println("Picking random track from index");
|
||||||
sid_t inode = libsaria::library::index_rand();
|
sid_t inode = libsaria::index::rand();
|
||||||
libsaria::library::play_id(inode);
|
libsaria::library::play_id(inode);
|
||||||
} else {
|
} else {
|
||||||
unsigned int index = rand() % play_list.size();
|
unsigned int index = rand() % play_list.size();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user