--- lbbs/src/article_view_log.c 2025/06/07 08:07:07 1.2 +++ lbbs/src/article_view_log.c 2025/12/19 06:16:26 1.19 @@ -1,29 +1,24 @@ -/*************************************************************************** - 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 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #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 +28,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"); return -1; } + p_view_log->uid = uid; + if (uid == 0) { p_view_log->aid_base_cnt = 0; @@ -52,7 +49,7 @@ int article_view_log_load(int uid, ARTIC if ((db = db_open()) == NULL) { - log_error("article_view_log_load() error: Unable to open DB\n"); + log_error("article_view_log_load() error: Unable to open DB"); return -2; } @@ -62,12 +59,12 @@ int article_view_log_load(int uid, ARTIC uid); if (mysql_query(db, sql) != 0) { - log_error("Query view_article_log error: %s\n", mysql_error(db)); + log_error("Query view_article_log error: %s", mysql_error(db)); return -3; } if ((rs = mysql_store_result(db)) == NULL) { - log_error("Get view_article_log data failed\n"); + log_error("Get view_article_log data failed"); return -3; } @@ -75,7 +72,7 @@ int article_view_log_load(int uid, ARTIC p_view_log->aid_base = malloc(sizeof(int32_t) * mysql_num_rows(rs)); if (p_view_log->aid_base == NULL) { - log_error("malloc(INT32 * %d) error: OOM\n", mysql_num_rows(rs)); + log_error("malloc(INT32 * %d) error: OOM", mysql_num_rows(rs)); mysql_free_result(rs); mysql_close(db); return -4; @@ -89,6 +86,8 @@ int article_view_log_load(int uid, ARTIC mysql_close(db); + log_common("Loaded %d view_article_log records for uid=%d", p_view_log->aid_base_cnt, uid); + if (!keep_inc) { p_view_log->aid_inc_cnt = 0; @@ -101,7 +100,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"); return -1; } @@ -115,47 +114,54 @@ 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"); 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"); + log_error("article_view_log_load() error: Unable to open DB"); return -2; } 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)); + log_error("Add view_article_log error: %s", 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 +169,8 @@ int article_view_log_save_inc(int uid, c } } + log_common("Saved %d view_article_log records for uid=%d", affected_record, p_view_log->uid); + mysql_close(db); return 0; @@ -173,10 +181,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"); return -1; } @@ -190,7 +199,7 @@ int article_view_log_merge_inc(ARTICLE_V aid_new = malloc(sizeof(int32_t) * (size_t)aid_new_cnt); if (aid_new == NULL) { - log_error("malloc(INT32 * %d) error: OOM\n", aid_new_cnt); + log_error("malloc(INT32 * %d) error: OOM", aid_new_cnt); return -2; } @@ -200,7 +209,7 @@ int article_view_log_merge_inc(ARTICLE_V { if (p_view_log->aid_base[i] == p_view_log->aid_inc[j]) { - log_error("Duplicate aid = %d found in both Base (offset = %d) and Inc (offset = %d)\n", + log_error("Duplicate aid = %d found in both Base (offset = %d) and Inc (offset = %d)", p_view_log->aid_base[i], i, j); j++; // Skip duplicate one in Inc } @@ -213,10 +222,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 +255,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"); return -1; } @@ -255,7 +274,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 +304,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"); return -1; } @@ -304,7 +323,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 +342,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"); + return -2; + } + article_view_log_merge_inc(p_view_log); p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; @@ -341,9 +367,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;