int tractorbeam_zkrecv(tractorbeam_zkrecv_t *info) { tractorbeam_monitor_t *mh = tractorbeam_monitor_init(info->endpoint, info->path, info->timeout); if (mh == NULL) { TB_DEBUG0("error connecting to zookeeper"); return(-1); } int rc = -1; if (info->layout == ZKRECV_LAYOUT_FILE) { int dash = strcmp(info->output, "-"); FILE *file = (dash == 0) ? stdout : fopen(info->output, "w"); if (file != NULL) { rc = tractorbeam_monitor_snapshot(mh, info->path, __tbzkrcv_file_cc, file); if (dash != 0) { fclose(file); } } else { rc = -1; TB_DEBUG("could not open file: %s", info->output); } } else if (info->layout == ZKRECV_LAYOUT_FILESYSTEM) { mkdir(info->output, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); rc = tractorbeam_monitor_snapshot(mh, info->path, __tbzkrcv_filesystem_cc, info->output); } tractorbeam_monitor_term(mh); return(rc); }
Xapian::Database * Server::fetch_db() { pthread_mutex_lock(&m_db_lock); Xapian::Database *db = 0; while(db == NULL){ for(size_t i=0;i<db_pool_size;i++){ if(m_dbs_busy[i] == false){ m_dbs_busy[i] = true; db = m_dbs[i]; if(m_reload_interval>0 and (time(0)-m_dbs_last_reload_ts[i])>m_reload_interval){ db->reopen(); m_dbs_last_reload_ts[i] = time(0); } break; } } if(db == NULL){ pthread_cond_wait(&m_db_cond, &m_db_lock); } } pthread_mutex_unlock(&m_db_lock); TB_DEBUG("fetch db "<<db); return db; }
bool UserSearchSession::serialize_result(Xapian::MSet &matches, string &resp) { UserSearchResult pb_result; //numbers pb_result.set_offset(m_query.offset()); pb_result.set_total_estimated(m_qualified_matches); TB_DEBUG("matches count:"<<matches.get_matches_estimated()); TB_DEBUG("matches qualified:"<<m_qualified_matches); //matches for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) { if(i.get_weight() == 0){ break; } string data = i.get_document().get_data(); UserSearchResult_ResultItem *item = pb_result.add_result_item(); item->set_uid(*(uint64_t*)data.data()); item->set_score(i.get_weight()); item->set_link_distance(1); if(m_query.pb_query.has_debug() && m_query.pb_query.debug()){ item->set_debug_data(m_mid_data_recorder[item->uid()]); } TB_DEBUG("uid :"<<item->uid()); } pb_result.set_count(pb_result.result_item_size()); TB_DEBUG("matches in range:"<<pb_result.result_item_size()); //stats std::vector<SearchStats::pair_t> top_v; m_stats.topk(top_v,MAX_STATS_ITEMS); for(unsigned i=0;i<top_v.size();i++){ const SearchStats::term_t &key = top_v[i].first; uint32_t value = top_v[i].second; UserSearchResult_StatsItem *stats = pb_result.add_stats_item(); stats->set_stats_type(USER_STATS_NAME[m_query.task_type]); stats->set_stats_key(key); stats->set_stats_value(value); } //serialize if(false == pb_result.SerializeToString(&resp)){ TB_DEBUG("serialize response failed"); return false; } return true; }
bool JobSearchSession::serialize_result(Xapian::MSet &matches, string &resp) { JobSearchResult pb_result; //numbers pb_result.set_offset(m_query.offset()); pb_result.set_total_estimated(m_qualified_matches); TB_INFO("matches count:"<<matches.get_matches_estimated()); TB_INFO("matches qualified:"<<m_qualified_matches); //matches for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) { if(i.get_weight() == 0){ break; } string data = i.get_document().get_data(); JobSearchResult pb_; pb_.ParseFromString(data); JobSearchResult_ResultItem &item = *(pb_result.add_result_item()); item.set_jid(pb_.result_item(0).jid()); item.set_title(pb_.result_item(0).title()); item.set_location(pb_.result_item(0).location()); item.set_company_id(pb_.result_item(0).company_id()); item.set_company_name(pb_.result_item(0).company_name()); item.set_company_image(pb_.result_item(0).company_image()); item.set_department(pb_.result_item(0).department()); item.set_post_date(pb_.result_item(0).post_date()); item.set_score(i.get_weight()); if(m_query.pb_query.has_debug() && m_query.pb_query.debug()){ item.set_debug_data(m_mid_data_recorder[item.jid()]); } TB_DEBUG("jid :"<<item.jid()); } pb_result.set_count(pb_result.result_item_size()); TB_INFO("matches in range:"<<pb_result.result_item_size()); //stats std::vector<SearchStats::pair_t> top_v; m_stats.topk(top_v,MAX_STATS_ITEMS); for(unsigned i=0;i<top_v.size();i++){ const SearchStats::term_t &key = top_v[i].first; uint32_t value = top_v[i].second; JobSearchResult_StatsItem *stats = pb_result.add_stats_item(); stats->set_stats_type(JOB_STATS_NAME[m_query.task_type]); stats->set_stats_key(key); stats->set_stats_value(value); } //serialize if(false == pb_result.SerializeToString(&resp)){ TB_INFO("serialize response failed"); return false; } return true; }
void Server::release_db(Xapian::Database *db) { TB_DEBUG("release db "<<db); pthread_mutex_lock(&m_db_lock); for(size_t i=0;i<db_pool_size;i++){ if(m_dbs[i] == db){ m_dbs_busy[i] = false; break; } } pthread_mutex_unlock(&m_db_lock); pthread_cond_signal(&m_db_cond); }
Xapian::Database * Server::fetch_db() { pthread_mutex_lock(&m_db_lock); Xapian::Database *db = 0; while(db == NULL){ for(size_t i=0;i<db_pool_size;i++){ if(m_dbs_busy[i] == false){ m_dbs_busy[i] = true; db = m_dbs[i]; break; } } if(db == NULL){ pthread_cond_wait(&m_db_cond, &m_db_lock); } } pthread_mutex_unlock(&m_db_lock); TB_DEBUG("fetch db "<<db); return db; }
static int __tbzkrcv_filesystem_cc(tb_snapshot_events event, const char *ppath, const char *name, const void *contents, size_t contsize, void *data) { char *chdir = (char *) data; char *dir = NULL; char *file = NULL; FILE *fd = NULL; int rc = -1; if (event == DONE) { return(0); } else if (event != ITEM) { return(-1); } dir = tbh_join(chdir, "/", ppath, "/", name, NULL); if (dir != NULL) { mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); } if (contents == 0) { rc = 0; } else { file = tbh_join(chdir, "/", ppath, "/", name, ".data", NULL); if (file != NULL && (fd = fopen(file, "w")) != NULL) { if (fwrite(contents, sizeof(char), contsize, fd) > 0) { rc = 0; } fclose(fd); } else { TB_DEBUG("could not open file: %s/%s/%s", chdir, ppath, name); rc = -1; } } free(dir); free(file); return(rc); }