pike.git / lib / modules / Search.pmod / Utils.pmod

version» Context lines:

pike.git/lib/modules/Search.pmod/Utils.pmod:1:   // This file is part of Roxen Search   // Copyright © 2001 Roxen IS. All rights reserved.   // - // $Id: Utils.pmod,v 1.24 2001/08/13 11:56:44 anders Exp $ + // $Id: Utils.pmod,v 1.25 2001/08/14 19:46:19 nilsson Exp $      #if !constant(report_error)   #define report_error werror   #endif      public array(string) tokenize_and_normalize( string what )   //! This can be optimized quite significantly when compared to   //! tokenize( normalize( x ) ) in the future, currently it's not all   //! that much faster, but still faster.   {
pike.git/lib/modules/Search.pmod/Utils.pmod:53:    //! ProfileCache cache)    //! @param cache    //! The parent cache object.    void create(int _database_profile_id,    int _search_profile_id,    ProfileCache _my_cache) {    database_profile_id = _database_profile_id;    search_profile_id = _search_profile_id;    my_cache = _my_cache;    int last_stat = time(1); -  } +     -  //! Checks with the parent @[ProfileCache] if any dependent -  //! profile is updated. If so, the potentially stale cached -  //! values are cleared. Checks against the parent @[ProfileCache] -  //! are done with at least five minutes intervals, and never when -  //! the object is unused, i.e. this method is called from -  //! @[get_database], @[get_ranking] and @[get_stop_words]. -  void refresh() { -  // werror("Time since check: %d\n", time(1)-last_stat); -  if(time(1)-last_stat < 5*60) return; -  int check = my_cache->up_to_datep(database_profile_id); -  if(check == -1) destruct(); -  if(!check) { -  database_values = 0; -  ranking = 0; -  db = 0; +  // Prefetch.. +  get_ranking();    } -  check = my_cache->up_to_datep(search_profile_id); -  if(check == -1) destruct(); -  if(!check) { -  search_values = 0; -  ranking = 0; -  stop_words = 0; -  } +  +  //! Checks if it is time to check if the profile values are +  //! to old. +  int(0..1) check_timeout() { +  if(time(1)-last_stat < 5*60) return 0;    last_stat = time(1); -  +  return 1;    }       //! Returns the database profile value @[index].    mixed get_database_value(string index) {    if(!database_values)    database_values = my_cache->get_value_mapping(database_profile_id);    return database_values[index];    }       //! Returns the search profile value @[index].    mixed get_search_value(string index) {    if(!search_values)    search_values = my_cache->get_value_mapping(search_profile_id);    return search_values[index];    }       //! Returns a cached search database for the current database profile.    Search.Database.MySQL get_database() { -  refresh(); +     if(!db) {   #if constant(DBManager)    db = Search.Database.MySQL( DBManager.db_url( get_database_value("db_name"), 1) );   #endif    if(!db)    THROW("Could not aquire the database URL to database " +    get_database_value("db_name") + ".\n");    }    return db;    }       //! Returns a cached ranking profile for the current database and    //! search profile.    Search.RankingProfile get_ranking() { -  refresh(); +     if(!ranking)    ranking = Search.RankingProfile(get_search_value("fi_cut"),    get_search_value("px_rank"),    get_database(),    get_search_value("fi_rank"));    return ranking;    }       class ADTSet {    private mapping vals = ([]);
pike.git/lib/modules/Search.pmod/Utils.pmod:163:    case "array": return indices(vals);    case "multiset": return (multiset)indices(vals);    default:    THROW("Can not cast ADTSet to "+to+".\n");    }    }    }       //! Returns a cached array of stop words for the current search profile.    array(string) get_stop_words() { -  refresh(); +     if(!stop_words) {    ADTSet words = ADTSet();    foreach(get_search_value("sw_lists"), string fn) {    string file = Stdio.read_file(fn);    if(!fn)    report_error("Could not load %O.\n", fn);    else    words + (Array.flatten(map(file/"\n",    lambda(string in) {    return in/" ";
pike.git/lib/modules/Search.pmod/Utils.pmod:189:    }))-({""}));    stop_words = (array)words;    }    return stop_words;    }   }      //!   class ProfileCache (string db_name) {    -  private mapping(int:ProfileEntry) entry_cache = ([]); +  private mapping(string:ProfileEntry) entry_cache = ([]);    private mapping(int:mapping(string:mixed)) value_cache = ([]);    private mapping(string:int) db_profile_names = ([]);    private mapping(string:int) srh_profile_names = ([]);    private mapping(int:int) profile_stat = ([]);       private Sql.Sql get_db() {    Sql.Sql db;   #if constant(DBManager)    db = DBManager.cached_get(db_name);   #endif
pike.git/lib/modules/Search.pmod/Utils.pmod:211:    return db;    }       //! Checks if the profile @[profile_id] has been changed, and clears    //! related caches if so.    //! @returns    //! @int    //! @value -1    //! The profile is deleted.    //! @value 0 -  //! The profile is up to date. +  //! The profile is not up to date.    //! @value 1 -  +  //! The profile is up to date.    //! @endint    int(-1..1) up_to_datep(int profile_id) {    // werror("Called up-to-date...\n");    array(mapping(string:string)) res;    res = get_db()->query("SELECT altered,type FROM wf_profile WHERE id=%d", profile_id);       // The profile is deleted. In such a rare event we take the    // trouble to review all our cached values.    if(!sizeof(res)) {    array(int) existing = (array(int))get_db()->query("SELECT id FROM wf_profile")->id;       foreach(indices(value_cache), int id)    if(!has_value(existing, id))    m_delete(value_cache, id);    -  foreach(indices(entry_cache), int id) -  if(!has_value(existing, id)) +  foreach(indices(entry_cache), string id) { +  int dbp, srhp; +  sscanf(id, "%d:%d", dbp, srhp); +  if(!has_value(existing, dbp))    m_delete(entry_cache, id); -  +  if(!has_value(existing, srhp)) +  m_delete(entry_cache, id); +  }       foreach(indices(db_profile_names), string name)    if(!has_value(existing, db_profile_names[name]))    m_delete(db_profile_names, name);       foreach(indices(srh_profile_names), string name)    if(!has_value(existing, srh_profile_names[name]))    m_delete(srh_profile_names, name);       return -1;    }       // Not altered    if((int)res[0]->altered == profile_stat[profile_id]) return 1;    profile_stat[profile_id] = (int)res[0]->altered;       // Search profile    if((int)res[0]->type == 2)    {    m_delete(value_cache, profile_id); -  m_delete(entry_cache, profile_id); +  foreach(indices(entry_cache), string id) +  if(array_sscanf(id, "%d:%d")[1]==profile_id) +  m_delete(entry_cache, id);    return 0;    }       m_delete(value_cache, profile_id); -  m_delete(entry_cache, profile_id); +  foreach(indices(entry_cache), string id) +  if(array_sscanf(id, "%d:%d")[0]==profile_id) +  m_delete(entry_cache, id);    return 0;    }       //! Returns the profile number for the given database profile.    int get_db_profile_number(string name) {    int db_profile;    if(db_profile=db_profile_names[name])    return db_profile;       array res = get_db()->
pike.git/lib/modules/Search.pmod/Utils.pmod:365:    //! the rules from search profile @[srh_name].    ProfileEntry get_profile_entry(string db_name, void|string srh_name) {    // werror("Entry: %O\n", indices(entry_cache));    // werror("Value: %O\n", indices(value_cache));    // werror("Stat : %O\n", profile_stat);       int db = get_db_profile_number(db_name);    int srh = get_srh_profile_number(srh_name);       ProfileEntry entry; -  if(entry=entry_cache[srh]) -  return entry; +  if(entry=entry_cache[srh +":"+ db]) { +  if(!entry->check_timeout()) return entry; +  if(up_to_datep(db) && +  up_to_datep(srh)) return entry; +  }       entry = ProfileEntry( db, srh, this_object() ); -  return entry_cache[srh] = entry; +  return entry_cache[srh +":"+ db] = entry;    }       //! Flushes profile entry @[p] from the profile cache.    void flush_profile(int p) {    m_delete(value_cache, p);    foreach(indices(db_profile_names), string name)    if(db_profile_names[name]==p)    m_delete(db_profile_names, name);    m_delete(srh_profile_names, p); -  +  foreach(indices(entry_cache), string id) { +  array ids = array_sscanf(id, "%d:%d"); +  if(ids[0]==p || ids[1]==p) +  m_delete(entry_cache, id);    } -  +  }       //! Empty the whole cache.    void flush_cache() {    value_cache = ([]);    db_profile_names = ([]);    srh_profile_names = ([]);    }   }    -  + private mapping(string:ProfileCache) profile_cache_cache = ([]); +  + ProfileCache get_profile_cache(string db_name) { +  if(profile_cache_cache[db_name]) +  return profile_cache_cache[db_name]; +  return profile_cache_cache[db_name] = ProfileCache(db_name); + } +  + void flush_profile(int p) { +  values(profile_cache_cache)->flush_profile(p); + } +  +    //!   class Logger {       private string|Sql.Sql logdb;    private int profile;       private Sql.Sql get_db() {    Sql.Sql db;   #if constant(DBManager)    if(stringp(logdb))