/[LeafOK_CVS]/lbbs/src/article_view_log.c
ViewVC logotype

Diff of /lbbs/src/article_view_log.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.3 by sysadm, Sat Jun 7 10:00:10 2025 UTC Revision 1.17 by sysadm, Tue Nov 4 14:58:56 2025 UTC
# Line 1  Line 1 
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     */
 /***************************************************************************  
  *                                                                         *  
  *   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;
# Line 35  int article_view_log_load(int uid, ARTIC Line 24  int article_view_log_load(int uid, ARTIC
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    
# Line 107  int article_view_log_unload(ARTICLE_VIEW Line 96  int article_view_log_unload(ARTICLE_VIEW
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    
# Line 123  int article_view_log_unload(ARTICLE_VIEW Line 112  int article_view_log_unload(ARTICLE_VIEW
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;
# Line 131  int article_view_log_save_inc(const ARTI Line 120  int article_view_log_save_inc(const ARTI
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          }          }
# Line 161  int article_view_log_save_inc(const ARTI Line 150  int article_view_log_save_inc(const ARTI
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    
# Line 187  int article_view_log_merge_inc(ARTICLE_V Line 177  int article_view_log_merge_inc(ARTICLE_V
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    
# Line 227  int article_view_log_merge_inc(ARTICLE_V Line 218  int article_view_log_merge_inc(ARTICLE_V
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;
# Line 250  int article_view_log_is_viewed(int32_t a Line 251  int article_view_log_is_viewed(int32_t a
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    
# Line 269  int article_view_log_is_viewed(int32_t a Line 270  int article_view_log_is_viewed(int32_t a
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                          {                          {
# Line 299  int article_view_log_set_viewed(int32_t Line 300  int article_view_log_set_viewed(int32_t
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    
# Line 318  int article_view_log_set_viewed(int32_t Line 319  int article_view_log_set_viewed(int32_t
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                          {                          {
# Line 337  int article_view_log_set_viewed(int32_t Line 338  int article_view_log_set_viewed(int32_t
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)
# Line 362  int article_view_log_set_viewed(int32_t Line 363  int article_view_log_set_viewed(int32_t
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;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1