--- lbbs/src/article_view_log.c 2025/06/07 07:59:38 1.1 +++ lbbs/src/article_view_log.c 2025/10/14 03:24:27 1.10 @@ -15,15 +15,14 @@ ***************************************************************************/ #include "article_view_log.h" -#include "log.h" #include "common.h" #include "database.h" +#include "log.h" #include - -#define _XOPEN_SOURCE 500 -#define _POSIX_C_SOURCE 200809L #include +ARTICLE_VIEW_LOG BBS_article_view_log; + int article_view_log_load(int uid, ARTICLE_VIEW_LOG *p_view_log, int keep_inc) { MYSQL *db; @@ -33,10 +32,25 @@ int article_view_log_load(int uid, ARTIC if (p_view_log == NULL) { - log_error("article_view_log_load() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } + p_view_log->uid = uid; + + if (uid == 0) + { + p_view_log->aid_base_cnt = 0; + p_view_log->aid_base = NULL; + + if (!keep_inc) + { + p_view_log->aid_inc_cnt = 0; + } + + return 0; + } + if ((db = db_open()) == NULL) { log_error("article_view_log_load() error: Unable to open DB\n"); @@ -76,6 +90,8 @@ int article_view_log_load(int uid, ARTIC mysql_close(db); + log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid); + if (!keep_inc) { p_view_log->aid_inc_cnt = 0; @@ -84,11 +100,11 @@ int article_view_log_load(int uid, ARTIC return 0; } -int article_view_log_unload(int uid, ARTICLE_VIEW_LOG *p_view_log) +int article_view_log_unload(ARTICLE_VIEW_LOG *p_view_log) { if (p_view_log == NULL) { - log_error("article_view_log_unload() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -102,19 +118,25 @@ int article_view_log_unload(int uid, ART return 0; } -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) { - MYSQL *db; + MYSQL *db = NULL; char sql[SQL_BUFFER_LEN]; char tuple_tmp[LINE_BUFFER_LEN]; int i; + int affected_record = 0; if (p_view_log == NULL) { - log_error("article_view_log_save_inc() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } + if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) + { + return 0; + } + if ((db = db_open()) == NULL) { log_error("article_view_log_load() error: Unable to open DB\n"); @@ -122,27 +144,28 @@ int article_view_log_save_inc(int uid, c } snprintf(sql, sizeof(sql), - "INSERT INTO view_article_log(AID, UID, dt) "); + "INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); for (i = 0; i < p_view_log->aid_inc_cnt; i++) { snprintf(tuple_tmp, sizeof(tuple_tmp), "(%d, %d, NOW())", - p_view_log->aid_inc[i], uid); + p_view_log->aid_inc[i], p_view_log->uid); strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); - 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 { - strncat(sql, " ON DUPLICATE KEY UPDATE 0 + 0", sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); - if (mysql_query(db, sql) != 0) { log_error("Add view_article_log error: %s\n", mysql_error(db)); + mysql_close(db); return -3; } + affected_record += (int)mysql_affected_rows(db); + snprintf(sql, sizeof(sql), - "INSERT INTO view_article_log(AID, UID, dt) "); + "INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); } else { @@ -150,6 +173,8 @@ int article_view_log_save_inc(int uid, c } } + log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid); + mysql_close(db); return 0; @@ -163,7 +188,7 @@ int article_view_log_merge_inc(ARTICLE_V if (p_view_log == NULL) { - log_error("article_view_log_merge_inc() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -223,7 +248,7 @@ int article_view_log_is_viewed(int32_t a if (p_view_log == NULL) { - log_error("article_view_log_is_viewed() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -272,7 +297,7 @@ int article_view_log_set_viewed(int32_t if (p_view_log == NULL) { - log_error("article_view_log_set_viewed() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -310,8 +335,15 @@ int article_view_log_set_viewed(int32_t } // Merge if Inc is full - if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) + if (p_view_log->aid_inc_cnt >= MAX_VIEWED_AID_INC_CNT) { + // Save incremental article view log + if (article_view_log_save_inc(p_view_log) < 0) + { + log_error("article_view_log_save_inc() error\n"); + return -2; + } + article_view_log_merge_inc(p_view_log); p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid;