--- lbbs/src/article_view_log.c 2025/06/07 07:59:38 1.1 +++ lbbs/src/article_view_log.c 2025/10/24 02:07:01 1.15 @@ -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; @@ -160,10 +185,11 @@ int article_view_log_merge_inc(ARTICLE_V int32_t *aid_new; int aid_new_cnt; int i, j, k; + int len; if (p_view_log == NULL) { - log_error("article_view_log_merge_inc() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -200,10 +226,20 @@ int article_view_log_merge_inc(ARTICLE_V } } - memcpy(aid_new + k, p_view_log->aid_base + i, sizeof(int32_t) * (size_t)(p_view_log->aid_base_cnt - i)); - k += (p_view_log->aid_base_cnt - i); - memcpy(aid_new + k, p_view_log->aid_inc + j, sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - j)); - k += (p_view_log->aid_inc_cnt - j); + len = p_view_log->aid_base_cnt - i; + if (len > 0) + { + memcpy(aid_new + k, p_view_log->aid_base + i, + sizeof(int32_t) * (size_t)len); + k += len; + } + len = p_view_log->aid_inc_cnt - j; + if (len > 0) + { + memcpy(aid_new + k, p_view_log->aid_inc + j, + sizeof(int32_t) * (size_t)len); + k += len; + } free(p_view_log->aid_base); p_view_log->aid_base = aid_new; @@ -223,7 +259,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; } @@ -242,7 +278,7 @@ int article_view_log_is_viewed(int32_t a mid = (left + right) / 2; if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { - right = mid; + right = mid - 1; } else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { @@ -272,7 +308,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; } @@ -291,7 +327,7 @@ int article_view_log_set_viewed(int32_t mid = (left + right) / 2; if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { - right = mid; + right = mid - 1; } else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { @@ -310,8 +346,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; @@ -328,9 +371,11 @@ int article_view_log_set_viewed(int32_t right = left + 1; } - for (i = p_view_log->aid_inc_cnt - 1; i >= right; i--) + if (p_view_log->aid_inc_cnt > right) { - p_view_log->aid_inc[i + 1] = p_view_log->aid_inc[i]; + memmove(p_view_log->aid_inc + right + 1, + p_view_log->aid_inc + right, + sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - right)); } p_view_log->aid_inc[right] = aid;