| 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 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "article_view_log.h" |
#include "article_view_log.h" |
|
#include "log.h" |
|
| 14 |
#include "common.h" |
#include "common.h" |
| 15 |
#include "database.h" |
#include "database.h" |
| 16 |
|
#include "log.h" |
| 17 |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
|
|
#define _XOPEN_SOURCE 500 |
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 18 |
#include <string.h> |
#include <string.h> |
| 19 |
|
|
| 20 |
ARTICLE_VIEW_LOG BBS_article_view_log; |
ARTICLE_VIEW_LOG BBS_article_view_log; |
| 28 |
|
|
| 29 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 30 |
{ |
{ |
| 31 |
log_error("article_view_log_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 32 |
return -1; |
return -1; |
| 33 |
} |
} |
| 34 |
|
|
| 100 |
{ |
{ |
| 101 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 102 |
{ |
{ |
| 103 |
log_error("article_view_log_unload() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 104 |
return -1; |
return -1; |
| 105 |
} |
} |
| 106 |
|
|
| 116 |
|
|
| 117 |
int article_view_log_save_inc(const ARTICLE_VIEW_LOG *p_view_log) |
int article_view_log_save_inc(const ARTICLE_VIEW_LOG *p_view_log) |
| 118 |
{ |
{ |
| 119 |
MYSQL *db; |
MYSQL *db = NULL; |
| 120 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 121 |
char tuple_tmp[LINE_BUFFER_LEN]; |
char tuple_tmp[LINE_BUFFER_LEN]; |
| 122 |
int i; |
int i; |
| 124 |
|
|
| 125 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 126 |
{ |
{ |
| 127 |
log_error("article_view_log_save_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 128 |
return -1; |
return -1; |
| 129 |
} |
} |
| 130 |
|
|
| 131 |
if (p_view_log->uid <= 0) |
if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) |
| 132 |
{ |
{ |
| 133 |
return 0; |
return 0; |
| 134 |
} |
} |
| 154 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 155 |
{ |
{ |
| 156 |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
log_error("Add view_article_log error: %s\n", mysql_error(db)); |
| 157 |
|
mysql_close(db); |
| 158 |
return -3; |
return -3; |
| 159 |
} |
} |
| 160 |
|
|
| 181 |
int32_t *aid_new; |
int32_t *aid_new; |
| 182 |
int aid_new_cnt; |
int aid_new_cnt; |
| 183 |
int i, j, k; |
int i, j, k; |
| 184 |
|
int len; |
| 185 |
|
|
| 186 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 187 |
{ |
{ |
| 188 |
log_error("article_view_log_merge_inc() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 189 |
return -1; |
return -1; |
| 190 |
} |
} |
| 191 |
|
|
| 222 |
} |
} |
| 223 |
} |
} |
| 224 |
|
|
| 225 |
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; |
| 226 |
k += (p_view_log->aid_base_cnt - i); |
if (len > 0) |
| 227 |
memcpy(aid_new + k, p_view_log->aid_inc + j, sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - j)); |
{ |
| 228 |
k += (p_view_log->aid_inc_cnt - j); |
memcpy(aid_new + k, p_view_log->aid_base + i, |
| 229 |
|
sizeof(int32_t) * (size_t)len); |
| 230 |
|
k += len; |
| 231 |
|
} |
| 232 |
|
len = p_view_log->aid_inc_cnt - j; |
| 233 |
|
if (len > 0) |
| 234 |
|
{ |
| 235 |
|
memcpy(aid_new + k, p_view_log->aid_inc + j, |
| 236 |
|
sizeof(int32_t) * (size_t)len); |
| 237 |
|
k += len; |
| 238 |
|
} |
| 239 |
|
|
| 240 |
free(p_view_log->aid_base); |
free(p_view_log->aid_base); |
| 241 |
p_view_log->aid_base = aid_new; |
p_view_log->aid_base = aid_new; |
| 255 |
|
|
| 256 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 257 |
{ |
{ |
| 258 |
log_error("article_view_log_is_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 259 |
return -1; |
return -1; |
| 260 |
} |
} |
| 261 |
|
|
| 274 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 275 |
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])) |
| 276 |
{ |
{ |
| 277 |
right = mid; |
right = mid - 1; |
| 278 |
} |
} |
| 279 |
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])) |
| 280 |
{ |
{ |
| 304 |
|
|
| 305 |
if (p_view_log == NULL) |
if (p_view_log == NULL) |
| 306 |
{ |
{ |
| 307 |
log_error("article_view_log_set_viewed() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 308 |
return -1; |
return -1; |
| 309 |
} |
} |
| 310 |
|
|
| 323 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 324 |
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])) |
| 325 |
{ |
{ |
| 326 |
right = mid; |
right = mid - 1; |
| 327 |
} |
} |
| 328 |
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])) |
| 329 |
{ |
{ |
| 342 |
} |
} |
| 343 |
|
|
| 344 |
// Merge if Inc is full |
// Merge if Inc is full |
| 345 |
if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) |
if (p_view_log->aid_inc_cnt >= MAX_VIEWED_AID_INC_CNT) |
| 346 |
{ |
{ |
| 347 |
// Save incremental article view log |
// Save incremental article view log |
| 348 |
if (article_view_log_save_inc(p_view_log) < 0) |
if (article_view_log_save_inc(p_view_log) < 0) |
| 367 |
right = left + 1; |
right = left + 1; |
| 368 |
} |
} |
| 369 |
|
|
| 370 |
for (i = p_view_log->aid_inc_cnt - 1; i >= right; i--) |
if (p_view_log->aid_inc_cnt > right) |
| 371 |
{ |
{ |
| 372 |
p_view_log->aid_inc[i + 1] = p_view_log->aid_inc[i]; |
memmove(p_view_log->aid_inc + right + 1, |
| 373 |
|
p_view_log->aid_inc + right, |
| 374 |
|
sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - right)); |
| 375 |
} |
} |
| 376 |
|
|
| 377 |
p_view_log->aid_inc[right] = aid; |
p_view_log->aid_inc[right] = aid; |