| 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; |
ARTICLE_VIEW_LOG BBS_article_view_log; |
| 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 |
|
|
| 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 |
|
|
| 112 |
|
|
| 113 |
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) |
| 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; |
| 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) |
if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) |
| 128 |
{ |
{ |
| 129 |
return 0; |
return 0; |
| 130 |
} |
} |
| 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 |
|
|
| 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 |
// Save incremental article view log |
| 344 |
if (article_view_log_save_inc(p_view_log) < 0) |
if (article_view_log_save_inc(p_view_log) < 0) |
| 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; |