Skip to content

Commit

Permalink
independent core of mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
zhblue committed Jul 24, 2017
1 parent eae8257 commit 8985c5a
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions trunk/core/judged/judged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ static char oj_redisqname[BUFFER_SIZE];
static bool STOP = false;
static int DEBUG = 0;
static int ONCE = 0;
#ifdef _mysql_h
static MYSQL *conn;
static MYSQL_RES *res;
static MYSQL_ROW row;
//static FILE *fp_log;
static char query[BUFFER_SIZE];
#endif

void call_for_exit(int s) {
STOP = true;
Expand Down Expand Up @@ -190,9 +192,11 @@ void init_mysql_conf() {


}
#ifdef _mysql_h
sprintf(query,
"SELECT solution_id FROM solution WHERE language in (%s) and result<2 and MOD(solution_id,%d)=%d ORDER BY result ASC,solution_id ASC limit %d",
oj_lang_set, oj_tot, oj_mod, max_running * 2);
#endif
sleep_tmp = sleep_time;
// fclose(fp);
}
Expand Down Expand Up @@ -233,6 +237,7 @@ void run_client(int runid, int clientid) {

//exit(0);
}
#ifdef _mysql_h
int executesql(const char * sql) {

if (mysql_real_query(conn, sql, strlen(sql))) {
Expand All @@ -244,7 +249,9 @@ int executesql(const char * sql) {
} else
return 0;
}
#endif

#ifdef _mysql_h
int init_mysql() {
if (conn == NULL) {
conn = mysql_init(NULL); // init the database connection
Expand All @@ -265,6 +272,7 @@ int init_mysql() {
return executesql("set names utf8");
}
}
#endif
FILE * read_cmd_output(const char * fmt, ...) {
char cmd[BUFFER_SIZE];

Expand Down Expand Up @@ -326,6 +334,7 @@ int _get_jobs_http(int * jobs) {
jobs[i++] = 0;
return ret;
}
#ifdef _mysql_h
int _get_jobs_mysql(int * jobs) {
if (mysql_real_query(conn, query, strlen(query))) {
if (DEBUG)
Expand All @@ -350,7 +359,7 @@ int _get_jobs_mysql(int * jobs) {
jobs[i++] = 0;
return ret;
}

#endif
int _get_jobs_redis(int * jobs){
int ret=0;
const char * cmd="redis-cli -h %s -p %d -a %s --raw rpop %s";
Expand Down Expand Up @@ -379,11 +388,16 @@ int get_jobs(int * jobs) {
if(oj_redis){
return _get_jobs_redis(jobs);
}else{
#ifdef _mysql_h
return _get_jobs_mysql(jobs);
#else
return 0;
#endif
}
}
}

#ifdef _mysql_h
bool _check_out_mysql(int solution_id, int result) {
char sql[BUFFER_SIZE];
sprintf(sql,
Expand All @@ -400,6 +414,7 @@ bool _check_out_mysql(int solution_id, int result) {
}

}
#endif

bool _check_out_http(int solution_id, int result) {
login();
Expand All @@ -416,8 +431,13 @@ bool check_out(int solution_id, int result) {
if(oj_redis) return true;
if (http_judge) {
return _check_out_http(solution_id, result);
} else
} else{
#ifdef _mysql_h
return _check_out_mysql(solution_id, result);
#else
return 0;
#endif
}

}
int work() {
Expand Down Expand Up @@ -491,11 +511,13 @@ int work() {
printf("tmp_pid = %d\n", tmp_pid);
}
if (!http_judge) {
#ifdef _mysql_h
if(res!=NULL) {
mysql_free_result(res); // free the memory
res=NULL;
}
executesql("commit");
#endif
}
if (DEBUG && retcnt)
write_log("<<%ddone!>>", retcnt);
Expand Down Expand Up @@ -594,13 +616,19 @@ int main(int argc, char** argv) {
// struct timespec final_sleep;
// final_sleep.tv_sec=0;
// final_sleep.tv_nsec=500000000;
#ifdef _mysql_h
init_mysql_conf(); // set the database info
#endif
signal(SIGQUIT, call_for_exit);
signal(SIGKILL, call_for_exit);
signal(SIGTERM, call_for_exit);
int j = 1;
while (1) { // start to run
while (j && (http_judge || !init_mysql())) {
while (j && (http_judge
#ifdef _mysql_h
|| !init_mysql()
#endif
)) {

j = work();

Expand Down

0 comments on commit 8985c5a

Please sign in to comment.