--- lbbs/src/article_view_log.c 2025/06/07 08:07:07 1.2 +++ lbbs/src/article_view_log.c 2025/11/04 13:49:51 1.16 @@ -1,29 +1,20 @@ -/*************************************************************************** - article_view_log.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * article_view_log + * - data persistence and query of article view log + * + * Copyright (C) 2004-2025 by Leaflet + */ #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 +24,12 @@ 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; @@ -89,6 +82,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; @@ -101,7 +96,7 @@ int article_view_log_unload(ARTICLE_VIEW { if (p_view_log == NULL) { - log_error("article_view_log_unload() error: NULL pointer\n"); + log_error("NULL pointer error\n"); return -1; } @@ -115,19 +110,25 @@ int article_view_log_unload(ARTICLE_VIEW 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"); @@ -135,27 +136,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 { @@ -163,6 +165,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; @@ -173,10 +177,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; } @@ -213,10 +218,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; @@ -236,7 +251,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; } @@ -255,7 +270,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])) { @@ -285,7 +300,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; } @@ -304,7 +319,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])) { @@ -323,8 +338,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; @@ -341,9 +363,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;