| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "article_view_log.h" |
#include "article_view_log.h" |
|
#include "log.h" |
|
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
#include "database.h" |
#include "database.h" |
| 20 |
|
#include "log.h" |
| 21 |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
|
|
#define _XOPEN_SOURCE 500 |
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 22 |
#include <string.h> |
#include <string.h> |
| 23 |
|
|
| 24 |
|
ARTICLE_VIEW_LOG BBS_article_view_log; |
| 25 |
|
|
| 26 |
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) |
| 27 |
{ |
{ |
| 28 |
MYSQL *db; |
MYSQL *db; |
| 32 |
|
|
| 33 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 34 |
{ |
{ |
| 35 |
log_error("article_view_log_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 36 |
return -1; |
return -1; |
| 37 |
} |
} |
| 38 |
|
|
| 39 |
|
p_view_log->uid = uid; |
| 40 |
|
|
| 41 |
if (uid == 0) |
if (uid == 0) |
| 42 |
{ |
{ |
| 43 |
p_view_log->aid_base_cnt = 0; |
p_view_log->aid_base_cnt = 0; |
| 90 |
|
|
| 91 |
mysql_close(db); |
mysql_close(db); |
| 92 |
|
|
| 93 |
|
log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid); |
| 94 |
|
|
| 95 |
if (!keep_inc) |
if (!keep_inc) |
| 96 |
{ |
{ |
| 97 |
p_view_log->aid_inc_cnt = 0; |
p_view_log->aid_inc_cnt = 0; |
| 104 |
{ |
{ |
| 105 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 106 |
{ |
{ |
| 107 |
log_error("article_view_log_unload() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 108 |
return -1; |
return -1; |
| 109 |
} |
} |
| 110 |
|
|
| 118 |
return 0; |
return 0; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
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) |
| 122 |
{ |
{ |
| 123 |
MYSQL *db; |
MYSQL *db = NULL; |
| 124 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 125 |
char tuple_tmp[LINE_BUFFER_LEN]; |
char tuple_tmp[LINE_BUFFER_LEN]; |
| 126 |
int i; |
int i; |
| 127 |
|
int affected_record = 0; |
| 128 |
|
|
| 129 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 130 |
{ |
{ |
| 131 |
log_error("article_view_log_save_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 132 |
return -1; |
return -1; |
| 133 |
} |
} |
| 134 |
|
|
| 135 |
|
if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) |
| 136 |
|
{ |
| 137 |
|
return 0; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
if ((db = db_open()) == NULL) |
if ((db = db_open()) == NULL) |
| 141 |
{ |
{ |
| 142 |
log_error("article_view_log_load() error: Unable to open DB\n"); |
log_error("article_view_log_load() error: Unable to open DB\n"); |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 147 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 148 |
|
|
| 149 |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
| 150 |
{ |
{ |
| 151 |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
| 152 |
"(%d, %d, NOW())", |
"(%d, %d, NOW())", |
| 153 |
p_view_log->aid_inc[i], uid); |
p_view_log->aid_inc[i], p_view_log->uid); |
| 154 |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
| 155 |
|
|
| 156 |
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 |
| 157 |
{ |
{ |
|
strncat(sql, " ON DUPLICATE KEY UPDATE 0 + 0", sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
|
|
|
|
| 158 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 159 |
{ |
{ |
| 160 |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
| 161 |
|
mysql_close(db); |
| 162 |
return -3; |
return -3; |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
|
affected_record += (int)mysql_affected_rows(db); |
| 166 |
|
|
| 167 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 168 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 169 |
} |
} |
| 170 |
else |
else |
| 171 |
{ |
{ |
| 173 |
} |
} |
| 174 |
} |
} |
| 175 |
|
|
| 176 |
|
log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid); |
| 177 |
|
|
| 178 |
mysql_close(db); |
mysql_close(db); |
| 179 |
|
|
| 180 |
return 0; |
return 0; |
| 188 |
|
|
| 189 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 190 |
{ |
{ |
| 191 |
log_error("article_view_log_merge_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 192 |
return -1; |
return -1; |
| 193 |
} |
} |
| 194 |
|
|
| 248 |
|
|
| 249 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 250 |
{ |
{ |
| 251 |
log_error("article_view_log_is_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 252 |
return -1; |
return -1; |
| 253 |
} |
} |
| 254 |
|
|
| 297 |
|
|
| 298 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 299 |
{ |
{ |
| 300 |
log_error("article_view_log_set_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 301 |
return -1; |
return -1; |
| 302 |
} |
} |
| 303 |
|
|
| 335 |
} |
} |
| 336 |
|
|
| 337 |
// Merge if Inc is full |
// Merge if Inc is full |
| 338 |
if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) |
if (p_view_log->aid_inc_cnt >= MAX_VIEWED_AID_INC_CNT) |
| 339 |
{ |
{ |
| 340 |
|
// Save incremental article view log |
| 341 |
|
if (article_view_log_save_inc(p_view_log) < 0) |
| 342 |
|
{ |
| 343 |
|
log_error("article_view_log_save_inc() error\n"); |
| 344 |
|
return -2; |
| 345 |
|
} |
| 346 |
|
|
| 347 |
article_view_log_merge_inc(p_view_log); |
article_view_log_merge_inc(p_view_log); |
| 348 |
|
|
| 349 |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |
| 360 |
right = left + 1; |
right = left + 1; |
| 361 |
} |
} |
| 362 |
|
|
| 363 |
for (i = p_view_log->aid_inc_cnt - 1; i >= right; i--) |
if (p_view_log->aid_inc_cnt > right) |
| 364 |
{ |
{ |
| 365 |
p_view_log->aid_inc[i + 1] = p_view_log->aid_inc[i]; |
memmove(p_view_log->aid_inc + right + 1, |
| 366 |
|
p_view_log->aid_inc + right, |
| 367 |
|
sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - right)); |
| 368 |
} |
} |
| 369 |
|
|
| 370 |
p_view_log->aid_inc[right] = aid; |
p_view_log->aid_inc[right] = aid; |