Changeset 203

Show
Ignore:
Timestamp:
10/29/06 15:29:18 (2 years ago)
Author:
ug
Message:

Improved support for tagging

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo/Makefile

    r196 r203  
    1717        g++ -Wall -g -O0 -o cpptest -lsqlite3 -lboost_filesystem cpptest.cc search.cc sgfparser.cc abstractboard.cc 
    1818 
     19fast: 
     20        g++ -O3 -o cpptest -lsqlite3 -lboost_filesystem cpptest.cc search.cc sgfparser.cc abstractboard.cc 
     21 
    1922clean: 
    2023        rm search.o abstractboard.o sgfparser.o cpptest 
  • 06/libkombilo/cpptest.cc

    r200 r203  
    106106 
    107107  // ------------------- resetFormat ------------------------------------------ 
     108  printf("reset db\n"); 
    108109  gl.resetFormat("pb"); 
    109110  vector<string> res = gl.currentEntriesAsStrings(0, 40); 
  • 06/libkombilo/search.cc

    r202 r203  
    33403340    throw DBError(); 
    33413341  } 
     3342  rc = sqlite3_busy_timeout(db, 200); 
     3343  if (rc) throw DBError(); 
     3344  rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
     3345  if (rc) throw DBError(); 
     3346  rc = sqlite3_exec(db, "pragma cache_size = 700000;", 0, 0, 0); 
     3347  if (rc) throw DBError(); 
    33423348 
    33433349  rc = sqlite3_exec(db, "create table if not exists db_info ( info text );", 0, 0, 0); 
     
    34803486  //   throw DBError(); 
    34813487  // } 
    3482   rc = sqlite3_busy_timeout(db, 200); 
    3483   if (rc) throw DBError(); 
    3484   rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
    3485   if (rc) throw DBError(); 
    3486   rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 
    3487   if (rc) throw DBError(); 
    34883488  rc = sqlite3_exec(db, "begin transaction;", 0, 0, 0); 
    34893489  if (rc) throw DBError(); 
     
    36743674} 
    36753675 
     3676void GameList::tagsearch(int tag) throw(DBError) { 
     3677  char sql[200]; 
     3678   
     3679  if (!tag) return; 
     3680  if (tag > 0) { 
     3681    sprintf(sql, "select games.id from games join game_tags on games.id = game_tags.game_id where game_tags.tag_id = %d order by games.id", tag); 
     3682  } else { 
     3683    sprintf(sql, "select games.id from games except select games.id from games join game_tags on games.id = game_tags.game_id where game_tags.tag_id = %d order by games.id;", -tag); 
     3684  } 
     3685  gisearch(sql, 1); 
     3686} 
     3687 
     3688void GameList::setTag(int tag, int start, int end) throw(DBError) { 
     3689  if (end==0) end = currentList->size(); 
     3690  if (start>end || end > (int)currentList->size()) return; 
     3691  int rc = sqlite3_exec(db, "begin transaction", 0, 0, 0); 
     3692  if (rc != SQLITE_OK) throw DBError(); 
     3693  for(int i = start; i < end; i++) { 
     3694    if (getTags(i, tag).size()) continue; 
     3695    char sql[200]; 
     3696    sprintf(sql, "insert into game_tags (game_id, tag_id) values (%d, %d)", (*all)[(*currentList)[i].second]->id, tag); 
     3697    rc = sqlite3_exec(db, sql, 0, 0, 0); 
     3698    if (rc != SQLITE_OK) throw DBError(); 
     3699  } 
     3700  rc = sqlite3_exec(db, "commit", 0, 0, 0); 
     3701  if (rc != SQLITE_OK) throw DBError(); 
     3702} 
     3703 
     3704void GameList::deleteTag(int tag, int i) throw(DBError) { 
     3705  char sql[200]; 
     3706  if (i == -1) sprintf(sql, "delete from game_tags where tag_id=%d", tag); 
     3707  else sprintf(sql, "delete from game_tags where game_id=%d and tag_id=%d", (*all)[(*currentList)[i].second]->id, tag); 
     3708  int rc = sqlite3_exec(db, sql, 0, 0, 0); 
     3709  if (rc != SQLITE_OK) throw DBError(); 
     3710} 
     3711 
     3712int gettags_callback(void *res, int argc, char **argv, char **azColName) { 
     3713  if (!argc) return 1; 
     3714  ((vector<int>*)res)->push_back(atoi(argv[0])); 
     3715  return 0; 
     3716} 
     3717 
     3718vector<int> GameList::getTags(int i, int tag) throw(DBError) { 
     3719  vector<int> result; 
     3720  char sql[200]; 
     3721  if (tag==0) sprintf(sql, "select tag_id from game_tags where game_id=%d", (*all)[(*currentList)[i].second]->id); 
     3722  else sprintf(sql, "select tag_id from game_tags where game_id=%d and tag_id=%d", (*all)[(*currentList)[i].second]->id, tag); 
     3723  int rc = sqlite3_exec(db, sql, gettags_callback, &result, 0); 
     3724  if (rc != SQLITE_OK) throw DBError(); 
     3725  return result; 
     3726} 
     3727 
    36763728int gis_callback(void *gl, int argc, char **argv, char **azColName) { 
    3677   if (!argc) throw DBError()
     3729  if (!argc) return 1
    36783730  ((GameList*)gl)->makeIndexHit(atoi(argv[0]), 0); 
    36793731  return 0; 
    36803732} 
    36813733 
    3682 void GameList::gisearch(char* sql) throw(DBError) { 
     3734void GameList::gisearch(char* sql, int complete) throw(DBError) { 
    36833735  bool dbWasOpen = false; 
    36843736  if (!db) { 
     
    36973749      if (rc) throw DBError(); 
    36983750    } 
    3699     string query = "select id from games where "; 
     3751    string query; 
     3752    if (!complete) query = "select id from games where "; 
    37003753    query += sql; 
    3701     query += " order by id"; 
     3754    if (!complete) query += " order by id"; 
     3755    // printf("%s\n", query.c_str()); 
    37023756    int rc = sqlite3_exec(db, query.c_str(), gis_callback, this, 0); 
    37033757    if( rc!=SQLITE_OK ) throw DBError(); 
  • 06/libkombilo/search.h

    r201 r203  
    576576    std::vector<GameListEntry* > * all; 
    577577    std::vector<std::pair<int,int> > * currentList; // pair of game id and position within all 
    578                                           // (usually sorted w.r.t. second 
    579                                           // component) 
     578                                                    // (usually sorted w.r.t. second component) 
    580579    std::vector<std::pair<int,int> > * oldList; 
    581580    int current; 
     
    586585 
    587586    // the following methods provide the user interface 
     587 
     588    // p_options will be copied by GameList, so the caller has to free the pointer 
    588589    GameList(char* DBNAME, std::string ORDERBY="", std::string FORMAT="", ProcessOptions* p_options=0) throw(DBError); 
    589     // p_options will be copied by GameList, so the caller has to free the pointer 
    590  
     590 
     591    void start_processing(int PROCESSVARIATIONS=-1) throw(DBError); 
     592    int process(const char* sgf, const char* path, const char* fn, const char* DBTREE = 0) throw(SGFError,DBError); 
     593    void finalize_processing() throw(DBError); 
     594     
     595    // options is copied in the search method (if != 0), so the caller has to free the pointer 
    591596    void search(Pattern& pattern, SearchOptions* options = 0) throw(DBError); 
    592     // options is copied in the search method (if != 0), so the caller has to free the pointer 
    593      
     597    char lookupLabel(char x, char y); 
     598    Continuation lookupContinuation(char x, char y); 
     599 
     600    void gisearch(char* sql, int complete=0) throw(DBError); 
     601 
     602    void tagsearch(int tag) throw(DBError); 
     603    void setTag(int tag, int start=0, int end=0) throw(DBError); 
     604    void deleteTag(int tag, int i = -1) throw(DBError); 
     605    std::vector<int> getTags(int i, int tag=0) throw(DBError); // note the order of arguments! 
     606 
    594607    void reset(); // reset currentList to all 
    595608    void resetFormat(std::string ORDERBY="", std::string FORMAT=""); 
    596     void gisearch(char* sql) throw(DBError); 
    597609    int size(); 
     610    int numHits(); 
    598611    std::string resultsStr(GameListEntry* gle); 
    599612    std::string currentEntryAsString(int i); 
     
    601614    std::string getSGF(int i) throw(DBError); 
    602615    std::string getCurrentProperty(int i, std::string tag) throw (DBError); 
    603     int numHits(); 
    604  
    605     void start_processing(int PROCESSVARIATIONS=-1) throw(DBError); 
    606     int process(const char* sgf, const char* path, const char* fn, const char* DBTREE = 0) throw(SGFError,DBError); 
    607     void finalize_processing() throw(DBError); 
    608      
     616 
    609617    // internal methods (called from the algorithm classes) 
    610618    ~GameList(); 
     
    619627    void makeIndexCandidate(int index, std::vector<Candidate* > *candidates); 
    620628    void makeIndexHit(int index, std::vector<Hit* > *hits); 
    621     char lookupLabel(char x, char y); 
    622     Continuation lookupContinuation(char x, char y); 
    623629 
    624630  private: