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

Diff of /lbbs/src/section_list_loader.c

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

Revision 1.5 by sysadm, Tue May 27 07:21:43 2025 UTC Revision 1.26 by sysadm, Tue Jun 17 13:18:55 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "section_list_loader.h"  #include "section_list_loader.h"
18    #include "article_cache.h"
19    #include "bbs.h"
20  #include "log.h"  #include "log.h"
21  #include "database.h"  #include "database.h"
22  #include "menu.h"  #include "menu.h"
# Line 23  Line 25 
25  #include <errno.h>  #include <errno.h>
26  #include <signal.h>  #include <signal.h>
27  #include <stdlib.h>  #include <stdlib.h>
28    #include <string.h>
29  #include <strings.h>  #include <strings.h>
30  #include <unistd.h>  #include <unistd.h>
31    
 #define SECTION_LIST_LOAD_INTERVAL 10 // second  
   
32  int section_list_loader_pid;  int section_list_loader_pid;
33  int last_article_op_log_mid;  int last_article_op_log_mid;
34    
35  int load_section_config_from_db(void)  int load_section_config_from_db(void)
36  {  {
37          MYSQL *db;          MYSQL *db = NULL;
38          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
39          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
40          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
41          int32_t sid;          int32_t sid;
42          char master_name[BBS_username_max_len + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
43          SECTION_LIST *p_section;          SECTION_LIST *p_section;
44          int ret;          int ret = 0;
45    
46          db = db_open();          db = db_open();
47          if (db == NULL)          if (db == NULL)
48          {          {
49                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
50                  return -2;                  ret = -1;
51                    goto cleanup;
52          }          }
53    
54          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 58  int load_section_config_from_db(void) Line 60  int load_section_config_from_db(void)
60          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
61          {          {
62                  log_error("Query section_list error: %s\n", mysql_error(db));                  log_error("Query section_list error: %s\n", mysql_error(db));
63                  return -3;                  ret = -2;
64                    goto cleanup;
65          }          }
66          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
67          {          {
68                  log_error("Get section_list data failed\n");                  log_error("Get section_list data failed\n");
69                  return -3;                  ret = -2;
70                    goto cleanup;
71          }          }
72    
73          ret = 0;          ret = 0;
# Line 76  int load_section_config_from_db(void) Line 80  int load_section_config_from_db(void)
80                                   "SELECT username FROM section_master "                                   "SELECT username FROM section_master "
81                                   "INNER JOIN user_list ON section_master.UID = user_list.UID "                                   "INNER JOIN user_list ON section_master.UID = user_list.UID "
82                                   "WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "                                   "WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
83                                   "ORDER BY major DESC LIMIT 1",                                   "ORDER BY major DESC, begin_dt ASC LIMIT 3",
84                                   sid);                                   sid);
85    
86                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
# Line 91  int load_section_config_from_db(void) Line 95  int load_section_config_from_db(void)
95                          ret = -3;                          ret = -3;
96                          break;                          break;
97                  }                  }
98                  if ((row2 = mysql_fetch_row(rs2)))  
99                  {                  master_list[0] = '\0';
100                          strncpy(master_name, row2[0], sizeof(master_name) - 1);                  while ((row2 = mysql_fetch_row(rs2)))
                         master_name[sizeof(master_name) - 1] = '\0';  
                 }  
                 else  
101                  {                  {
102                          master_name[0] = '\0';                          strncat(master_list, row2[0], sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
103                            strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
104                  }                  }
105                  mysql_free_result(rs2);                  mysql_free_result(rs2);
106                    rs2 = NULL;
107    
108                  p_section = section_list_find_by_sid(sid);                  p_section = section_list_find_by_sid(sid);
109    
110                  if (p_section == NULL)                  if (p_section == NULL)
111                  {                  {
112                          p_section = section_list_create(sid, row[1], row[2], "");                          p_section = section_list_create(sid, row[1], row[2], master_list);
113                          if (p_section == NULL)                          if (p_section == NULL)
114                          {                          {
115                                  log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);                                  log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);
# Line 134  int load_section_config_from_db(void) Line 137  int load_section_config_from_db(void)
137                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
138                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
139                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
140                          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1);                          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
141                          p_section->master_name[sizeof(p_section->master_name) - 1] = '\0';                          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
142                  }                  }
143    
144                  p_section->class_id = atoi(row[3]);                  p_section->class_id = atoi(row[3]);
# Line 150  int load_section_config_from_db(void) Line 153  int load_section_config_from_db(void)
153                          break;                          break;
154                  }                  }
155          }          }
         mysql_free_result(rs);  
156    
157    cleanup:
158            mysql_free_result(rs);
159          mysql_close(db);          mysql_close(db);
160    
161          return ret;          return ret;
162  }  }
163    
164  int append_articles_from_db(int32_t start_aid, int global_lock)  int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
165  {  {
166          MYSQL *db;          MYSQL *db = NULL;
167          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
168          MYSQL_ROW row;          MYSQL_ROW row;
169          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
170          ARTICLE article;          ARTICLE article;
171          ARTICLE *p_topic;          ARTICLE *p_topic;
172          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
173          int32_t last_sid = 0;          int32_t last_sid = 0;
174            char sub_ip[IP_ADDR_LEN];
175            int article_count = 0;
176          int ret = 0;          int ret = 0;
177          int i;          int i;
178    
# Line 174  int append_articles_from_db(int32_t star Line 180  int append_articles_from_db(int32_t star
180          if (db == NULL)          if (db == NULL)
181          {          {
182                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
183                  return -2;                  ret = -1;
184                    goto cleanup;
185          }          }
186    
187          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
188                           "SELECT AID, TID, SID, CID, UID, visible, excerption, ontop, `lock`, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
189                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
190                           "FROM bbs WHERE AID >= %d ORDER BY AID",                           "sub_ip, bbs_content.content "
191                           start_aid);                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
192                             "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
193                             start_aid, article_count_limit);
194    
195          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
196          {          {
197                  log_error("Query article list error: %s\n", mysql_error(db));                  log_error("Query article list error: %s\n", mysql_error(db));
198                  return -3;                  ret = -2;
199                    goto cleanup;
200          }          }
201          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
202          {          {
203                  log_error("Get article list data failed\n");                  log_error("Get article list data failed\n");
204                  return -3;                  ret = -2;
205                    goto cleanup;
206          }          }
207    
208          // acquire global lock          // acquire global lock
209          if (global_lock)          if (global_lock && (ret = section_list_rw_lock(NULL)) < 0)
210          {          {
211                  if ((ret = section_list_rw_lock(NULL)) < 0)                  log_error("section_list_rw_lock(sid = 0) error\n");
212                  {                  goto cleanup;
                         log_error("section_list_rw_lock(sid = 0) error\n");  
                         goto cleanup;  
                 }  
213          }          }
214    
215          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
216          {          {
217                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
218    
219                  // copy data of article                  // copy data of article
220                  i = 0;                  i = 0;
# Line 231  int append_articles_from_db(int32_t star Line 239  int append_articles_from_db(int32_t star
239    
240                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
241    
242                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
243                    sub_ip[sizeof(sub_ip) - 1] = '\0';
244                    ip_mask(sub_ip, 1, '*');
245    
246                  // release lock of last section if different from current one                  // release lock of last section if different from current one
247                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
248                  {                  {
249                          if ((ret = section_list_rw_unlock(p_section)) < 0)                          if ((ret = section_list_rw_unlock(p_section)) < 0)
250                          {                          {
251                                  log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                                  log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
252                                  break;                                  break;
253                          }                          }
254                  }                  }
# Line 265  int append_articles_from_db(int32_t star Line 277  int append_articles_from_db(int32_t star
277                  {                  {
278                          if ((ret = section_list_rw_lock(p_section)) < 0)                          if ((ret = section_list_rw_lock(p_section)) < 0)
279                          {                          {
280                                  log_error("section_list_rw_lock(sid = 0) error\n");                                  log_error("section_list_rw_lock(sid=0) error\n");
281                                  break;                                  break;
282                          }                          }
283                  }                  }
# Line 275  int append_articles_from_db(int32_t star Line 287  int append_articles_from_db(int32_t star
287    
288                  if (section_list_append_article(p_section, &article) < 0)                  if (section_list_append_article(p_section, &article) < 0)
289                  {                  {
290                          log_error("section_list_append_article(sid = %d, aid = %d) error\n",                          log_error("section_list_append_article(sid=%d, aid=%d) error\n",
291                                            p_section->sid, article.aid);                                            p_section->sid, article.aid);
292                          ret = -3;                          ret = -3;
293                          break;                          break;
294                  }                  }
295    
296                    article_count++;
297    
298                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
299                    {
300                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
301                            ret = -4;
302                            break;
303                    }
304          }          }
305    
306          // release lock of last section          // release lock of last section
# Line 287  int append_articles_from_db(int32_t star Line 308  int append_articles_from_db(int32_t star
308          {          {
309                  if ((ret = section_list_rw_unlock(p_section)) < 0)                  if ((ret = section_list_rw_unlock(p_section)) < 0)
310                  {                  {
311                          log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                          log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
312                  }                  }
313          }          }
314    
315          // release global lock          // release global lock
316          if (global_lock)          if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0)
317          {          {
318                  if ((ret = section_list_rw_unlock(NULL)) < 0)                  log_error("section_list_rw_unlock(sid=0) error\n");
                 {  
                         log_error("section_list_rw_unlock(sid = 0) error\n");  
                 }  
319          }          }
320    
321  cleanup:  cleanup:
322          mysql_free_result(rs);          mysql_free_result(rs);
   
323          mysql_close(db);          mysql_close(db);
324    
325          return ret;          return (ret < 0 ? ret : article_count);
326  }  }
327    
328  int set_last_article_op_log_from_db(void)  int set_last_article_op_log_from_db(void)
329  {  {
330          MYSQL *db;          MYSQL *db = NULL;
331          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
332          MYSQL_ROW row;          MYSQL_ROW row;
333          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
334            int ret = 0;
335    
336          db = db_open();          db = db_open();
337          if (db == NULL)          if (db == NULL)
338          {          {
339                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
340                  return -1;                  ret = -1;
341                    goto cleanup;
342          }          }
343    
344          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 328  int set_last_article_op_log_from_db(void Line 347  int set_last_article_op_log_from_db(void
347          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
348          {          {
349                  log_error("Query article op error: %s\n", mysql_error(db));                  log_error("Query article op error: %s\n", mysql_error(db));
350                  return -2;                  ret = -2;
351                    goto cleanup;
352          }          }
353          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
354          {          {
355                  log_error("Get article op data failed\n");                  log_error("Get article op data failed\n");
356                  return -2;                  ret = -2;
357                    goto cleanup;
358          }          }
359    
360          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 341  int set_last_article_op_log_from_db(void Line 362  int set_last_article_op_log_from_db(void
362                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
363          }          }
364    
365    cleanup:
366          mysql_free_result(rs);          mysql_free_result(rs);
   
367          mysql_close(db);          mysql_close(db);
368    
369          return last_article_op_log_mid;          return (ret < 0 ? ret : last_article_op_log_mid);
370  }  }
371    
372  int apply_article_op_log_from_db(void)  int apply_article_op_log_from_db(int op_count_limit)
373  {  {
374          MYSQL *db;          MYSQL *db = NULL;
375          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
376          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
377          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
378          ARTICLE *p_article;          ARTICLE *p_article;
379          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
380          SECTION_LIST *p_section_dest;          SECTION_LIST *p_section_dest;
381            char sub_ip[IP_ADDR_LEN];
382          int32_t last_sid = 0;          int32_t last_sid = 0;
383          int32_t sid_dest;          int32_t sid_dest;
384            int op_count = 0;
385          int ret = 0;          int ret = 0;
386    
387          db = db_open();          db = db_open();
388          if (db == NULL)          if (db == NULL)
389          {          {
390                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
391                  return -3;                  ret = -1;
392                    goto cleanup;
393          }          }
394    
395          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
396                           "SELECT MID, AID, type FROM bbs_article_op "                           "SELECT MID, AID, type FROM bbs_article_op "
397                           "WHERE MID > %d AND type NOT IN ('A', 'M') ORDER BY MID",                           "WHERE MID > %d AND type NOT IN ('A') "
398                           last_article_op_log_mid);                           "ORDER BY MID LIMIT %d",
399                             last_article_op_log_mid, op_count_limit);
400    
401          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
402          {          {
403                  log_error("Query article log error: %s\n", mysql_error(db));                  log_error("Query article log error: %s\n", mysql_error(db));
404                  return -3;                  ret = -2;
405                    goto cleanup;
406          }          }
407          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
408          {          {
409                  log_error("Get article log data failed\n");                  log_error("Get article log data failed\n");
410                  return -3;                  ret = -2;
411                    goto cleanup;
412          }          }
413    
414          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
# Line 429  int apply_article_op_log_from_db(void) Line 456  int apply_article_op_log_from_db(void)
456                          break;                          break;
457                  case 'D': // Delete article                  case 'D': // Delete article
458                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
459                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
460                          {                          {
461                                  // Set articles in the topic to be invisible                                  log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1]));
462                                  do                          }
463                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
464                                          p_article = p_article->p_topic_next;                          {
465                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
466                          }                          }
467                          break;                          break;
468                  case 'S': // Restore article                  case 'S': // Restore article
469                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
470                            {
471                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
472                            }
473                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
474                            {
475                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
476                            }
477                          break;                          break;
478                  case 'L': // Lock article                  case 'L': // Lock article
479                          p_article->lock = 1;                          p_article->lock = 1;
# Line 450  int apply_article_op_log_from_db(void) Line 482  int apply_article_op_log_from_db(void)
482                          p_article->lock = 0;                          p_article->lock = 0;
483                          break;                          break;
484                  case 'M': // Modify article                  case 'M': // Modify article
485                          log_error("Operation type=M should not be found\n");                          snprintf(sql, sizeof(sql),
486                                             "SELECT bbs.CID, sub_ip, bbs_content.content "
487                                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
488                                             "WHERE bbs.AID = %d",
489                                             p_article->aid);
490    
491                            if (mysql_query(db, sql) != 0)
492                            {
493                                    log_error("Query article error: %s\n", mysql_error(db));
494                                    ret = -3;
495                                    break;
496                            }
497                            if ((rs2 = mysql_use_result(db)) == NULL)
498                            {
499                                    log_error("Get article data failed\n");
500                                    ret = -3;
501                                    break;
502                            }
503                            if ((row2 = mysql_fetch_row(rs2)))
504                            {
505                                    p_article->cid = atoi(row2[0]);
506    
507                                    strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
508                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
509                                    ip_mask(sub_ip, 1, '*');
510    
511                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
512                                    {
513                                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
514                                            ret = -4;
515                                    }
516                            }
517                            else
518                            {
519                                    p_article->cid = 0;
520                                    ret = -4;
521                            }
522                            mysql_free_result(rs2);
523                            rs2 = NULL;
524    
525                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
526                          break;                          break;
527                  case 'T': // Move article                  case 'T': // Move article
528                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 479  int apply_article_op_log_from_db(void) Line 551  int apply_article_op_log_from_db(void)
551                                  ret = -4;                                  ret = -4;
552                          }                          }
553                          mysql_free_result(rs2);                          mysql_free_result(rs2);
554                            rs2 = NULL;
555    
556                          if (sid_dest > 0 && sid_dest != p_article->sid)                          if (sid_dest > 0 && sid_dest != p_article->sid)
557                          {                          {
# Line 488  int apply_article_op_log_from_db(void) Line 561  int apply_article_op_log_from_db(void)
561                                          ret = ERR_UNKNOWN_SECTION;                                          ret = ERR_UNKNOWN_SECTION;
562                                          break;                                          break;
563                                  }                                  }
564                                    // acquire lock of dest section
565                                    if ((ret = section_list_rw_lock(p_section_dest)) < 0)
566                                    {
567                                            log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest);
568                                            break;
569                                    }
570                                  // Move topic                                  // Move topic
571                                  if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)                                  if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)
572                                  {                                  {
573                                          break;                                          log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n",
574                                                              p_section->sid, p_section_dest->sid, p_article->aid, ret);
575                                    }
576                                    // release lock of dest section
577                                    if (section_list_rw_unlock(p_section_dest) < 0)
578                                    {
579                                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest);
580                                            ret = -1;
581                                  }                                  }
582                          }                          }
583                          break;                          break;
# Line 522  int apply_article_op_log_from_db(void) Line 608  int apply_article_op_log_from_db(void)
608    
609                  // Update MID with last successfully proceeded article_op_log                  // Update MID with last successfully proceeded article_op_log
610                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
611    
612                    op_count++;
613          }          }
614    
615          // release lock of last section          // release lock of last section
# Line 533  int apply_article_op_log_from_db(void) Line 621  int apply_article_op_log_from_db(void)
621                  }                  }
622          }          }
623    
624    cleanup:
625            mysql_free_result(rs2);
626          mysql_free_result(rs);          mysql_free_result(rs);
   
627          mysql_close(db);          mysql_close(db);
628    
629          return ret;          return (ret < 0 ? ret : op_count);
630  }  }
631    
632  int section_list_loader_launch(void)  int section_list_loader_launch(void)
# Line 562  int section_list_loader_launch(void) Line 651  int section_list_loader_launch(void)
651          {          {
652                  SYS_child_process_count++;                  SYS_child_process_count++;
653                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
654                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
655                  return 0;                  return 0;
656          }          }
657          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 598  int section_list_loader_launch(void) Line 687  int section_list_loader_launch(void)
687                  }                  }
688    
689                  // Load section articles                  // Load section articles
                 last_aid = article_block_last_aid();  
690                  article_count = article_block_article_count();                  article_count = article_block_article_count();
691    
692                  if ((ret = append_articles_from_db(last_aid + 1, 0)) < 0)                  do
693                  {                  {
694                          log_error("append_articles_from_db(%d, 0) error\n", last_aid + 1);                          last_aid = article_block_last_aid();
695    
696                          if (ret == ERR_UNKNOWN_SECTION)                          if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
697                          {                          {
698                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
699    
700                                    if (ret == ERR_UNKNOWN_SECTION)
701                                    {
702                                            SYS_section_list_reload = 1; // Force reload section_list
703                                    }
704                          }                          }
705                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
706    
707                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
   
708                  if (load_count > 0)                  if (load_count > 0)
709                  {                  {
710                          log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());                          log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
711                  }                  }
712    
713                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 626  int section_list_loader_launch(void) Line 718  int section_list_loader_launch(void)
718                  // Load article_op log                  // Load article_op log
719                  last_mid = last_article_op_log_mid;                  last_mid = last_article_op_log_mid;
720    
721                  if ((ret = apply_article_op_log_from_db()) < 0)                  do
722                  {                  {
723                          log_error("apply_article_op_log_from_db() error\n");                          if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
   
                         if (ret == ERR_UNKNOWN_SECTION)  
724                          {                          {
725                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("apply_article_op_log_from_db() error\n");
726    
727                                    if (ret == ERR_UNKNOWN_SECTION)
728                                    {
729                                            SYS_section_list_reload = 1; // Force reload section_list
730                                    }
731                          }                          }
732                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
733    
734                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
735                  {                  {
736                          log_std("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);                          log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
737                  }                  }
738    
739                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 646  int section_list_loader_launch(void) Line 741  int section_list_loader_launch(void)
741                          continue;                          continue;
742                  }                  }
743    
744                  for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++)                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)
745                  {                  {
746                          sleep(1);                          sleep(1);
747                  }                  }
# Line 659  int section_list_loader_launch(void) Line 754  int section_list_loader_launch(void)
754          detach_article_block_shm();          detach_article_block_shm();
755          detach_trie_dict_shm();          detach_trie_dict_shm();
756    
757          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
758          log_end();          log_end();
759    
760          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 685  int section_list_loader_reload(void) Line 780  int section_list_loader_reload(void)
780    
781          return 0;          return 0;
782  }  }
783    
784    int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
785    {
786            ARTICLE *p_article;
787            ARTICLE *p_next_page_first_article;
788            int ret = 0;
789    
790            if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
791            {
792                    log_error("query_section_articles() NULL pointer error\n");
793                    return -1;
794            }
795    
796            // acquire lock of section
797            if ((ret = section_list_rd_lock(p_section)) < 0)
798            {
799                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
800                    return -2;
801            }
802    
803            *p_page_count = p_section->page_count;
804    
805            if (p_section->visible_article_count == 0)
806            {
807                    *p_article_count = 0;
808            }
809            else if (page_id < 0 || page_id >= p_section->page_count)
810            {
811                    log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
812                    ret = -3;
813            }
814            else
815            {
816                    ret = page_id;
817                    p_article = p_section->p_page_first_article[page_id];
818                    p_next_page_first_article =
819                            (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
820                    *p_article_count = 0;
821    
822                    do
823                    {
824                            if (p_article->visible)
825                            {
826                                    p_articles[*p_article_count] = p_article;
827                                    (*p_article_count)++;
828                            }
829                            p_article = p_article->p_next;
830                    } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
831    
832                    if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
833                    {
834                            log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
835                    }
836            }
837    
838            // release lock of section
839            if (section_list_rd_unlock(p_section) < 0)
840            {
841                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
842                    ret = -2;
843            }
844    
845            return ret;
846    }
847    
848    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step,
849                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
850    {
851            const ARTICLE *p_article = NULL;
852            ARTICLE *p_tmp;
853            int32_t aid = 0;
854            int page_id = 0;
855            int offset = 0;
856            int ret = 0;
857            int i;
858    
859            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
860            {
861                    log_error("locate_article_in_section() NULL pointer error\n");
862                    return -1;
863            }
864    
865            // acquire lock of section
866            if ((ret = section_list_rd_lock(p_section)) < 0)
867            {
868                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
869                    return -2;
870            }
871    
872            if (direction == 0)
873            {
874                    aid = p_article_cur->aid;
875            }
876            else if (direction == 1)
877            {
878                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_next->aid > p_article_cur->aid; p_article = p_article->p_topic_next)
879                    {
880                            if (p_article->visible)
881                            {
882                                    step--;
883                            }
884                    }
885    
886                    aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0);
887            }
888            else if (direction == -1)
889            {
890                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_prior->aid < p_article_cur->aid; p_article = p_article->p_topic_prior)
891                    {
892                            if (p_article->visible)
893                            {
894                                    step--;
895                            }
896                    }
897    
898                    aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0);
899            }
900    
901            p_article = NULL;
902    
903            if (aid > 0)
904            {
905                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
906                    if (p_article != NULL)
907                    {
908                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
909    
910                            p_article = p_section->p_page_first_article[page_id];
911                            for (i = 0; i < *p_article_count && p_article != NULL;)
912                            {
913                                    if (p_article->visible)
914                                    {
915                                            if (p_article->aid == aid)
916                                            {
917                                                    *p_page_id = page_id;
918                                                    *p_visible_offset = i;
919                                                    break;
920                                            }
921                                            i++;
922                                            if (i >= *p_article_count)
923                                            {
924                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
925                                                    p_article = NULL;
926                                                    break;
927                                            }
928                                    }
929                                    p_article = p_article->p_next;
930                            }
931                    }
932            }
933    
934            // release lock of section
935            if (section_list_rd_unlock(p_section) < 0)
936            {
937                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
938                    ret = -2;
939            }
940    
941            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
942    }


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

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