| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
article_view_log.c - description |
/* |
| 3 |
------------------- |
* article_view_log |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - data persistence and query of article view log |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* 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. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "article_view_log.h" |
#include "article_view_log.h" |
|
#include "log.h" |
|
| 10 |
#include "common.h" |
#include "common.h" |
| 11 |
#include "database.h" |
#include "database.h" |
| 12 |
|
#include "log.h" |
| 13 |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
|
|
#define _XOPEN_SOURCE 500 |
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 14 |
#include <string.h> |
#include <string.h> |
| 15 |
|
|
| 16 |
|
ARTICLE_VIEW_LOG BBS_article_view_log; |
| 17 |
|
|
| 18 |
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) |
| 19 |
{ |
{ |
| 20 |
MYSQL *db; |
MYSQL *db; |
| 24 |
|
|
| 25 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 26 |
{ |
{ |
| 27 |
log_error("article_view_log_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 28 |
return -1; |
return -1; |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
|
p_view_log->uid = uid; |
| 32 |
|
|
| 33 |
if (uid == 0) |
if (uid == 0) |
| 34 |
{ |
{ |
| 35 |
p_view_log->aid_base_cnt = 0; |
p_view_log->aid_base_cnt = 0; |
| 82 |
|
|
| 83 |
mysql_close(db); |
mysql_close(db); |
| 84 |
|
|
| 85 |
|
log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid); |
| 86 |
|
|
| 87 |
if (!keep_inc) |
if (!keep_inc) |
| 88 |
{ |
{ |
| 89 |
p_view_log->aid_inc_cnt = 0; |
p_view_log->aid_inc_cnt = 0; |
| 96 |
{ |
{ |
| 97 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 98 |
{ |
{ |
| 99 |
log_error("article_view_log_unload() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 100 |
return -1; |
return -1; |
| 101 |
} |
} |
| 102 |
|
|
| 110 |
return 0; |
return 0; |
| 111 |
} |
} |
| 112 |
|
|
| 113 |
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) |
| 114 |
{ |
{ |
| 115 |
MYSQL *db; |
MYSQL *db = NULL; |
| 116 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 117 |
char tuple_tmp[LINE_BUFFER_LEN]; |
char tuple_tmp[LINE_BUFFER_LEN]; |
| 118 |
int i; |
int i; |
| 119 |
|
int affected_record = 0; |
| 120 |
|
|
| 121 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 122 |
{ |
{ |
| 123 |
log_error("article_view_log_save_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 124 |
return -1; |
return -1; |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
|
if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) |
| 128 |
|
{ |
| 129 |
|
return 0; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
if ((db = db_open()) == NULL) |
if ((db = db_open()) == NULL) |
| 133 |
{ |
{ |
| 134 |
log_error("article_view_log_load() error: Unable to open DB\n"); |
log_error("article_view_log_load() error: Unable to open DB\n"); |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 139 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 140 |
|
|
| 141 |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
for (i = 0; i < p_view_log->aid_inc_cnt; i++) |
| 142 |
{ |
{ |
| 143 |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
snprintf(tuple_tmp, sizeof(tuple_tmp), |
| 144 |
"(%d, %d, NOW())", |
"(%d, %d, NOW())", |
| 145 |
p_view_log->aid_inc[i], uid); |
p_view_log->aid_inc[i], p_view_log->uid); |
| 146 |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
| 147 |
|
|
| 148 |
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 |
| 149 |
{ |
{ |
|
strncat(sql, " ON DUPLICATE KEY UPDATE 0 + 0", sizeof(sql) - 1 - strnlen(sql, sizeof(sql))); |
|
|
|
|
| 150 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 151 |
{ |
{ |
| 152 |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
| 153 |
|
mysql_close(db); |
| 154 |
return -3; |
return -3; |
| 155 |
} |
} |
| 156 |
|
|
| 157 |
|
affected_record += (int)mysql_affected_rows(db); |
| 158 |
|
|
| 159 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 160 |
"INSERT INTO view_article_log(AID, UID, dt) "); |
"INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES "); |
| 161 |
} |
} |
| 162 |
else |
else |
| 163 |
{ |
{ |
| 165 |
} |
} |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
|
log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid); |
| 169 |
|
|
| 170 |
mysql_close(db); |
mysql_close(db); |
| 171 |
|
|
| 172 |
return 0; |
return 0; |
| 177 |
int32_t *aid_new; |
int32_t *aid_new; |
| 178 |
int aid_new_cnt; |
int aid_new_cnt; |
| 179 |
int i, j, k; |
int i, j, k; |
| 180 |
|
int len; |
| 181 |
|
|
| 182 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 183 |
{ |
{ |
| 184 |
log_error("article_view_log_merge_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 185 |
return -1; |
return -1; |
| 186 |
} |
} |
| 187 |
|
|
| 218 |
} |
} |
| 219 |
} |
} |
| 220 |
|
|
| 221 |
memcpy(aid_new + k, p_view_log->aid_base + i, sizeof(int32_t) * (size_t)(p_view_log->aid_base_cnt - i)); |
len = p_view_log->aid_base_cnt - i; |
| 222 |
k += (p_view_log->aid_base_cnt - i); |
if (len > 0) |
| 223 |
memcpy(aid_new + k, p_view_log->aid_inc + j, sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - j)); |
{ |
| 224 |
k += (p_view_log->aid_inc_cnt - j); |
memcpy(aid_new + k, p_view_log->aid_base + i, |
| 225 |
|
sizeof(int32_t) * (size_t)len); |
| 226 |
|
k += len; |
| 227 |
|
} |
| 228 |
|
len = p_view_log->aid_inc_cnt - j; |
| 229 |
|
if (len > 0) |
| 230 |
|
{ |
| 231 |
|
memcpy(aid_new + k, p_view_log->aid_inc + j, |
| 232 |
|
sizeof(int32_t) * (size_t)len); |
| 233 |
|
k += len; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
free(p_view_log->aid_base); |
free(p_view_log->aid_base); |
| 237 |
p_view_log->aid_base = aid_new; |
p_view_log->aid_base = aid_new; |
| 251 |
|
|
| 252 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 253 |
{ |
{ |
| 254 |
log_error("article_view_log_is_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 255 |
return -1; |
return -1; |
| 256 |
} |
} |
| 257 |
|
|
| 270 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 271 |
if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
| 272 |
{ |
{ |
| 273 |
right = mid; |
right = mid - 1; |
| 274 |
} |
} |
| 275 |
else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
| 276 |
{ |
{ |
| 300 |
|
|
| 301 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 302 |
{ |
{ |
| 303 |
log_error("article_view_log_set_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 304 |
return -1; |
return -1; |
| 305 |
} |
} |
| 306 |
|
|
| 319 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 320 |
if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
| 321 |
{ |
{ |
| 322 |
right = mid; |
right = mid - 1; |
| 323 |
} |
} |
| 324 |
else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) |
| 325 |
{ |
{ |
| 338 |
} |
} |
| 339 |
|
|
| 340 |
// Merge if Inc is full |
// Merge if Inc is full |
| 341 |
if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) |
if (p_view_log->aid_inc_cnt >= MAX_VIEWED_AID_INC_CNT) |
| 342 |
{ |
{ |
| 343 |
|
// Save incremental article view log |
| 344 |
|
if (article_view_log_save_inc(p_view_log) < 0) |
| 345 |
|
{ |
| 346 |
|
log_error("article_view_log_save_inc() error\n"); |
| 347 |
|
return -2; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
article_view_log_merge_inc(p_view_log); |
article_view_log_merge_inc(p_view_log); |
| 351 |
|
|
| 352 |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |
p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid; |
| 363 |
right = left + 1; |
right = left + 1; |
| 364 |
} |
} |
| 365 |
|
|
| 366 |
for (i = p_view_log->aid_inc_cnt - 1; i >= right; i--) |
if (p_view_log->aid_inc_cnt > right) |
| 367 |
{ |
{ |
| 368 |
p_view_log->aid_inc[i + 1] = p_view_log->aid_inc[i]; |
memmove(p_view_log->aid_inc + right + 1, |
| 369 |
|
p_view_log->aid_inc + right, |
| 370 |
|
sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - right)); |
| 371 |
} |
} |
| 372 |
|
|
| 373 |
p_view_log->aid_inc[right] = aid; |
p_view_log->aid_inc[right] = aid; |