Skip to content

Commit

Permalink
add a cheeky random query for the lulz
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Jul 17, 2009
1 parent 1e5ad33 commit ce1fa81
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
13 changes: 13 additions & 0 deletions resolvers/local/library.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ Library::get_album_id(int artistid, const string& name_orig)
return id; return id;
} }


int
Library::get_random_fid()
{
boost::mutex::scoped_lock lock(m_mut);
string sql = "SELECT id FROM file WHERE size > 0 AND rowid > (abs(random()) % (SELECT max(rowid) FROM file)) LIMIT 1";
sqlite3pp::query qry(m_db, sql.c_str());
for (sqlite3pp::query::iterator i = qry.begin(); i != qry.end(); ++i)
{
return (*i).get<int>(0);
}
return -1;
}

vector<scorepair> vector<scorepair>
Library::search_catalogue(string table, string name_orig) Library::search_catalogue(string table, string name_orig)
{ {
Expand Down
2 changes: 2 additions & 0 deletions resolvers/local/library.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Library
int num_artists(); int num_artists();
int num_albums(); int num_albums();
int num_tracks(); int num_tracks();

int get_random_fid();


bool build_index(std::string); bool build_index(std::string);
static std::string sortname(const std::string& name); static std::string sortname(const std::string& name);
Expand Down
61 changes: 41 additions & 20 deletions resolvers/local/rs_local_library.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -101,24 +101,50 @@ local::run()
void void
local::process( rq_ptr rq ) local::process( rq_ptr rq )
{ {
//Ignore this if it's missing artist+track fields
if( !rq->param_exists( "artist" )
|| rq->param("artist").type() != str_type
|| !rq->param_exists( "track" )
|| rq->param("track").type() != str_type )
{
return;
}
vector< json_spirit::Object > final_results; vector< json_spirit::Object > final_results;
// get candidates (rough potential matches):
vector<scorepair> candidates = find_candidates(rq, 10); // check if this is a special "random" query
// now do the "real" scoring of candidate results: if( rq->param("artist").get_str() == "*" &&
string reason; // for scoring debug. rq->param("track").get_str() == "*" )
BOOST_FOREACH(scorepair &sp, candidates) {
int fid = m_library->get_random_fid();
if( fid == -1 ) return;
json_spirit::Object js;
ResolvedItemBuilder::createFromFid( *m_library, fid, js );
js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) );
js.push_back( json_spirit::Pair( "source", m_pap->hostname()) );
js.push_back( json_spirit::Pair( "score", 0.99) );
// dangerous, will propagate forever? muhahah
final_results.push_back( js );
}
else // end special random query check
{ {
// multiple files in our collection may have matching metadata. // get candidates (rough potential matches):
// add them all to the results. vector<scorepair> candidates = find_candidates(rq, 10);
vector<int> fids = m_library->get_fids_for_tid(sp.id); // now do the "real" scoring of candidate results:
BOOST_FOREACH(int fid, fids) string reason; // for scoring debug.
BOOST_FOREACH(scorepair &sp, candidates)
{ {
json_spirit::Object js; // multiple files in our collection may have matching metadata.
js.reserve(12); // add them all to the results.
ResolvedItemBuilder::createFromFid( *m_library, fid, js ); vector<int> fids = m_library->get_fids_for_tid(sp.id);
js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) ); BOOST_FOREACH(int fid, fids)
js.push_back( json_spirit::Pair( "source", m_pap->hostname()) ); {
final_results.push_back( js ); json_spirit::Object js;
js.reserve(12);
ResolvedItemBuilder::createFromFid( *m_library, fid, js );
js.push_back( json_spirit::Pair( "sid", m_pap->gen_uuid()) );
js.push_back( json_spirit::Pair( "source", m_pap->hostname()) );
final_results.push_back( js );
}
} }
} }
if(final_results.size()) if(final_results.size())
Expand All @@ -139,11 +165,6 @@ local::find_candidates(rq_ptr rq, unsigned int limit)
vector<scorepair> candidates; vector<scorepair> candidates;
float maxartscore = 0; float maxartscore = 0;


//Ignore this request_query - nothing that this can resolve from.
if( !rq->param_exists( "artist" ) ||
!rq->param_exists( "track" ))
return candidates;

vector<scorepair> artistresults = vector<scorepair> artistresults =
m_library->search_catalogue("artist", rq->param( "artist" ).get_str()); m_library->search_catalogue("artist", rq->param( "artist" ).get_str());
BOOST_FOREACH( scorepair & sp, artistresults ) BOOST_FOREACH( scorepair & sp, artistresults )
Expand Down

0 comments on commit ce1fa81

Please sign in to comment.