/[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.2 by sysadm, Sat Jun 7 08:07:07 2025 UTC Revision 1.3 by sysadm, Sat Jun 7 10:00:10 2025 UTC
# Line 24  Line 24 
24  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
25  #include <string.h>  #include <string.h>
26    
27    ARTICLE_VIEW_LOG BBS_article_view_log;
28    
29  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)
30  {  {
31          MYSQL *db;          MYSQL *db;
# Line 37  int article_view_log_load(int uid, ARTIC Line 39  int article_view_log_load(int uid, ARTIC
39                  return -1;                  return -1;
40          }          }
41    
42            p_view_log->uid = uid;
43    
44          if (uid == 0)          if (uid == 0)
45          {          {
46                  p_view_log->aid_base_cnt = 0;                  p_view_log->aid_base_cnt = 0;
# Line 89  int article_view_log_load(int uid, ARTIC Line 93  int article_view_log_load(int uid, ARTIC
93    
94          mysql_close(db);          mysql_close(db);
95    
96            log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid);
97    
98          if (!keep_inc)          if (!keep_inc)
99          {          {
100                  p_view_log->aid_inc_cnt = 0;                  p_view_log->aid_inc_cnt = 0;
# Line 115  int article_view_log_unload(ARTICLE_VIEW Line 121  int article_view_log_unload(ARTICLE_VIEW
121          return 0;          return 0;
122  }  }
123    
124  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)
125  {  {
126          MYSQL *db;          MYSQL *db;
127          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
128          char tuple_tmp[LINE_BUFFER_LEN];          char tuple_tmp[LINE_BUFFER_LEN];
129          int i;          int i;
130            int affected_record = 0;
131    
132          if (p_view_log == NULL)          if (p_view_log == NULL)
133          {          {
# Line 128  int article_view_log_save_inc(int uid, c Line 135  int article_view_log_save_inc(int uid, c
135                  return -1;                  return -1;
136          }          }
137    
138            if (p_view_log->uid <= 0)
139            {
140                    return 0;
141            }
142    
143          if ((db = db_open()) == NULL)          if ((db = db_open()) == NULL)
144          {          {
145                  log_error("article_view_log_load() error: Unable to open DB\n");                  log_error("article_view_log_load() error: Unable to open DB\n");
# Line 135  int article_view_log_save_inc(int uid, c Line 147  int article_view_log_save_inc(int uid, c
147          }          }
148    
149          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
150                           "INSERT INTO view_article_log(AID, UID, dt) ");                           "INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES ");
151    
152          for (i = 0; i < p_view_log->aid_inc_cnt; i++)          for (i = 0; i < p_view_log->aid_inc_cnt; i++)
153          {          {
154                  snprintf(tuple_tmp, sizeof(tuple_tmp),                  snprintf(tuple_tmp, sizeof(tuple_tmp),
155                                   "(%d, %d, NOW())",                                   "(%d, %d, NOW())",
156                                   p_view_log->aid_inc[i], uid);                                   p_view_log->aid_inc[i], p_view_log->uid);
157                  strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql)));                  strncat(sql, tuple_tmp, sizeof(sql) - 1 - strnlen(sql, sizeof(sql)));
158    
159                  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
160                  {                  {
                         strncat(sql, " ON DUPLICATE KEY UPDATE 0 + 0", sizeof(sql) - 1 - strnlen(sql, sizeof(sql)));  
   
161                          if (mysql_query(db, sql) != 0)                          if (mysql_query(db, sql) != 0)
162                          {                          {
163                                  log_error("Add view_article_log error: %s\n", mysql_error(db));                                  log_error("Add view_article_log error: %s\n", mysql_error(db));
164                                  return -3;                                  return -3;
165                          }                          }
166    
167                            affected_record += (int)mysql_affected_rows(db);
168    
169                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
170                                           "INSERT INTO view_article_log(AID, UID, dt) ");                                           "INSERT IGNORE INTO view_article_log(AID, UID, dt) VALUES ");
171                  }                  }
172                  else                  else
173                  {                  {
# Line 163  int article_view_log_save_inc(int uid, c Line 175  int article_view_log_save_inc(int uid, c
175                  }                  }
176          }          }
177    
178            log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid);
179    
180          mysql_close(db);          mysql_close(db);
181    
182          return 0;          return 0;
# Line 325  int article_view_log_set_viewed(int32_t Line 339  int article_view_log_set_viewed(int32_t
339          // Merge if Inc is full          // Merge if Inc is full
340          if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT)          if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT)
341          {          {
342                    // Save incremental article view log
343                    if (article_view_log_save_inc(p_view_log) < 0)
344                    {
345                            log_error("article_view_log_save_inc() error\n");
346                            return -2;
347                    }
348    
349                  article_view_log_merge_inc(p_view_log);                  article_view_log_merge_inc(p_view_log);
350    
351                  p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid;                  p_view_log->aid_inc[(p_view_log->aid_inc_cnt)++] = aid;


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

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