| 24 |
#define _POSIX_C_SOURCE 200809L |
#define _POSIX_C_SOURCE 200809L |
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
|
|
| 27 |
|
ARTICLE_VIEW_LOG BBS_article_view_log; |
| 28 |
|
|
| 29 |
int article_view_log_load(int uid, ARTICLE_VIEW_LOG *p_view_log, int keep_inc) |
int article_view_log_load(int uid, ARTICLE_VIEW_LOG *p_view_log, int keep_inc) |
| 30 |
{ |
{ |
| 31 |
MYSQL *db; |
MYSQL *db; |
| 39 |
return -1; |
return -1; |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
|
p_view_log->uid = uid; |
| 43 |
|
|
| 44 |
|
if (uid == 0) |
| 45 |
|
{ |
| 46 |
|
p_view_log->aid_base_cnt = 0; |
| 47 |
|
p_view_log->aid_base = NULL; |
| 48 |
|
|
| 49 |
|
if (!keep_inc) |
| 50 |
|
{ |
| 51 |
|
p_view_log->aid_inc_cnt = 0; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
return 0; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
if ((db = db_open()) == NULL) |
if ((db = db_open()) == NULL) |
| 58 |
{ |
{ |
| 59 |
log_error("article_view_log_load() error: Unable to open DB\n"); |
log_error("article_view_log_load() error: Unable to open DB\n"); |
| 93 |
|
|
| 94 |
mysql_close(db); |
mysql_close(db); |
| 95 |
|
|
| 96 |
|
log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid); |
| 97 |
|
|
| 98 |
if (!keep_inc) |
if (!keep_inc) |
| 99 |
{ |
{ |
| 100 |
p_view_log->aid_inc_cnt = 0; |
p_view_log->aid_inc_cnt = 0; |
| 103 |
return 0; |
return 0; |
| 104 |
} |
} |
| 105 |
|
|
| 106 |
int article_view_log_unload(int uid, ARTICLE_VIEW_LOG *p_view_log) |
int article_view_log_unload(ARTICLE_VIEW_LOG *p_view_log) |
| 107 |
{ |
{ |
| 108 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 109 |
{ |
{ |
| 121 |
return 0; |
return 0; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
int article_view_log_save_inc(int uid, const ARTICLE_VIEW_LOG *p_view_log) |
int article_view_log_save_inc(const ARTICLE_VIEW_LOG *p_view_log) |
| 125 |
{ |
{ |
| 126 |
MYSQL *db; |
MYSQL *db = NULL; |
| 127 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 128 |
char tuple_tmp[LINE_BUFFER_LEN]; |
char tuple_tmp[LINE_BUFFER_LEN]; |
| 129 |
int i; |
int i; |
| 130 |
|
int affected_record = 0; |
| 131 |
|
|
| 132 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 133 |
{ |
{ |
| 135 |
return -1; |
return -1; |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
|
if (p_view_log->uid <= 0) |
| 139 |
|
{ |
| 140 |
|
return 0; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
if ((db = db_open()) == NULL) |
if ((db = db_open()) == NULL) |
| 144 |
{ |
{ |
| 145 |
log_error("article_view_log_load() error: Unable to open DB\n"); |
log_error("article_view_log_load() error: Unable to open DB\n"); |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 150 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 151 |
|
|
| 152 |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
| 153 |
{ |
{ |
| 154 |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
| 155 |
"(%d, %d, NOW())", |
"(%d, %d, NOW())", |
| 156 |
p_view_log->aid_inc[i], uid); |
p_view_log->aid_inc[i], p_view_log->uid); |
| 157 |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
| 158 |
|
|
| 159 |
if (i % 100 == 0) // Insert 100 records per query |
if ((i + 1) % 100 == 0 || (i + 1) == p_view_log->aid_inc_cnt) // Insert 100 records per query |
| 160 |
{ |
{ |
|
strncat(sql, " ON DUPLICATE KEY UPDATE 0 + 0", sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
|
|
|
|
| 161 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 162 |
{ |
{ |
| 163 |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
| 164 |
|
mysql_close(db); |
| 165 |
return -3; |
return -3; |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
|
affected_record += (int)mysql_affected_rows(db); |
| 169 |
|
|
| 170 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 171 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 172 |
} |
} |
| 173 |
else |
else |
| 174 |
{ |
{ |
| 176 |
} |
} |
| 177 |
} |
} |
| 178 |
|
|
| 179 |
|
log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid); |
| 180 |
|
|
| 181 |
mysql_close(db); |
mysql_close(db); |
| 182 |
|
|
| 183 |
return 0; |
return 0; |
| 340 |
// Merge if Inc is full |
// Merge if Inc is full |
| 341 |
if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) |
if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) |
| 342 |
{ |
{ |
| 343 |
|
// Save incremental article view log |
| 344 |
|
if (article_view_log_save_inc(p_view_log) < 0) |
| 345 |
|
{ |
| 346 |
|
log_error("article_view_log_save_inc() error\n"); |
| 347 |
|
return -2; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
article_view_log_merge_inc(p_view_log); |
article_view_log_merge_inc(p_view_log); |
| 351 |
|
|
| 352 |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |