/[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.8 by sysadm, Wed May 28 07:30:23 2025 UTC Revision 1.20 by sysadm, Sat Jun 14 11:57:52 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 29  Line 31 
31  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
32  #include <string.h>  #include <string.h>
33    
 #define SECTION_LIST_LOAD_INTERVAL 10 // second  
   
34  int section_list_loader_pid;  int section_list_loader_pid;
35  int last_article_op_log_mid;  int last_article_op_log_mid;
36    
# Line 158  int load_section_config_from_db(void) Line 158  int load_section_config_from_db(void)
158          return ret;          return ret;
159  }  }
160    
161  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)
162  {  {
163          MYSQL *db;          MYSQL *db;
164          MYSQL_RES *rs;          MYSQL_RES *rs;
# Line 168  int append_articles_from_db(int32_t star Line 168  int append_articles_from_db(int32_t star
168          ARTICLE *p_topic;          ARTICLE *p_topic;
169          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
170          int32_t last_sid = 0;          int32_t last_sid = 0;
171            char sub_ip[IP_ADDR_LEN];
172            int article_count = 0;
173          int ret = 0;          int ret = 0;
174          int i;          int i;
175    
# Line 179  int append_articles_from_db(int32_t star Line 181  int append_articles_from_db(int32_t star
181          }          }
182    
183          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
184                           "SELECT AID, TID, SID, CID, UID, visible, excerption, ontop, `lock`, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
185                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
186                           "FROM bbs WHERE AID >= %d ORDER BY AID",                           "sub_ip, bbs_content.content "
187                           start_aid);                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
188                             "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
189                             start_aid, article_count_limit);
190    
191          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
192          {          {
# Line 232  int append_articles_from_db(int32_t star Line 236  int append_articles_from_db(int32_t star
236    
237                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
238    
239                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
240                    sub_ip[sizeof(sub_ip) - 1] = '\0';
241                    ip_mask(sub_ip, 1, '*');
242    
243                  // release lock of last section if different from current one                  // release lock of last section if different from current one
244                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
245                  {                  {
246                          if ((ret = section_list_rw_unlock(p_section)) < 0)                          if ((ret = section_list_rw_unlock(p_section)) < 0)
247                          {                          {
248                                  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);
249                                  break;                                  break;
250                          }                          }
251                  }                  }
# Line 266  int append_articles_from_db(int32_t star Line 274  int append_articles_from_db(int32_t star
274                  {                  {
275                          if ((ret = section_list_rw_lock(p_section)) < 0)                          if ((ret = section_list_rw_lock(p_section)) < 0)
276                          {                          {
277                                  log_error("section_list_rw_lock(sid = 0) error\n");                                  log_error("section_list_rw_lock(sid=0) error\n");
278                                  break;                                  break;
279                          }                          }
280                  }                  }
# Line 276  int append_articles_from_db(int32_t star Line 284  int append_articles_from_db(int32_t star
284    
285                  if (section_list_append_article(p_section, &article) < 0)                  if (section_list_append_article(p_section, &article) < 0)
286                  {                  {
287                          log_error("section_list_append_article(sid = %d, aid = %d) error\n",                          log_error("section_list_append_article(sid=%d, aid=%d) error\n",
288                                            p_section->sid, article.aid);                                            p_section->sid, article.aid);
289                          ret = -3;                          ret = -3;
290                          break;                          break;
291                  }                  }
292    
293                    article_count++;
294    
295                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
296                    {
297                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
298                            ret = -4;
299                            break;
300                    }
301          }          }
302    
303          // release lock of last section          // release lock of last section
# Line 288  int append_articles_from_db(int32_t star Line 305  int append_articles_from_db(int32_t star
305          {          {
306                  if ((ret = section_list_rw_unlock(p_section)) < 0)                  if ((ret = section_list_rw_unlock(p_section)) < 0)
307                  {                  {
308                          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);
309                  }                  }
310          }          }
311    
# Line 297  int append_articles_from_db(int32_t star Line 314  int append_articles_from_db(int32_t star
314          {          {
315                  if ((ret = section_list_rw_unlock(NULL)) < 0)                  if ((ret = section_list_rw_unlock(NULL)) < 0)
316                  {                  {
317                          log_error("section_list_rw_unlock(sid = 0) error\n");                          log_error("section_list_rw_unlock(sid=0) error\n");
318                  }                  }
319          }          }
320    
# Line 306  cleanup: Line 323  cleanup:
323    
324          mysql_close(db);          mysql_close(db);
325    
326          return ret;          return (ret < 0 ? ret : article_count);
327  }  }
328    
329  int set_last_article_op_log_from_db(void)  int set_last_article_op_log_from_db(void)
# Line 349  int set_last_article_op_log_from_db(void Line 366  int set_last_article_op_log_from_db(void
366          return last_article_op_log_mid;          return last_article_op_log_mid;
367  }  }
368    
369  int apply_article_op_log_from_db(void)  int apply_article_op_log_from_db(int op_count_limit)
370  {  {
371          MYSQL *db;          MYSQL *db;
372          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs, *rs2;
# Line 358  int apply_article_op_log_from_db(void) Line 375  int apply_article_op_log_from_db(void)
375          ARTICLE *p_article;          ARTICLE *p_article;
376          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
377          SECTION_LIST *p_section_dest;          SECTION_LIST *p_section_dest;
378            char sub_ip[IP_ADDR_LEN];
379          int32_t last_sid = 0;          int32_t last_sid = 0;
380          int32_t sid_dest;          int32_t sid_dest;
381            int op_count = 0;
382          int ret = 0;          int ret = 0;
383    
384          db = db_open();          db = db_open();
# Line 371  int apply_article_op_log_from_db(void) Line 390  int apply_article_op_log_from_db(void)
390    
391          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
392                           "SELECT MID, AID, type FROM bbs_article_op "                           "SELECT MID, AID, type FROM bbs_article_op "
393                           "WHERE MID > %d AND type NOT IN ('A', 'M') ORDER BY MID",                           "WHERE MID > %d AND type NOT IN ('A') "
394                           last_article_op_log_mid);                           "ORDER BY MID LIMIT %d",
395                             last_article_op_log_mid, op_count_limit);
396    
397          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
398          {          {
# Line 430  int apply_article_op_log_from_db(void) Line 450  int apply_article_op_log_from_db(void)
450                          break;                          break;
451                  case 'D': // Delete article                  case 'D': // Delete article
452                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
453                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
454                          {                          {
455                                  // 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]));
456                                  do                          }
457                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
458                                          p_article = p_article->p_topic_next;                          {
459                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
460                          }                          }
461                          break;                          break;
462                  case 'S': // Restore article                  case 'S': // Restore article
463                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
464                            {
465                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
466                            }
467                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
468                            {
469                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
470                            }
471                          break;                          break;
472                  case 'L': // Lock article                  case 'L': // Lock article
473                          p_article->lock = 1;                          p_article->lock = 1;
# Line 451  int apply_article_op_log_from_db(void) Line 476  int apply_article_op_log_from_db(void)
476                          p_article->lock = 0;                          p_article->lock = 0;
477                          break;                          break;
478                  case 'M': // Modify article                  case 'M': // Modify article
479                          log_error("Operation type=M should not be found\n");                          snprintf(sql, sizeof(sql),
480                                             "SELECT bbs.CID, sub_ip, bbs_content.content "
481                                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
482                                             "WHERE bbs.AID = %d",
483                                             p_article->aid);
484    
485                            if (mysql_query(db, sql) != 0)
486                            {
487                                    log_error("Query article error: %s\n", mysql_error(db));
488                                    ret = -3;
489                                    break;
490                            }
491                            if ((rs2 = mysql_use_result(db)) == NULL)
492                            {
493                                    log_error("Get article data failed\n");
494                                    ret = -3;
495                                    break;
496                            }
497                            if ((row2 = mysql_fetch_row(rs2)))
498                            {
499                                    p_article->cid = atoi(row2[0]);
500    
501                                    strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
502                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
503                                    ip_mask(sub_ip, 1, '*');
504    
505                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
506                                    {
507                                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
508                                            ret = -4;
509                                    }
510                            }
511                            else
512                            {
513                                    p_article->cid = 0;
514                                    ret = -4;
515                            }
516                            mysql_free_result(rs2);
517                          break;                          break;
518                  case 'T': // Move article                  case 'T': // Move article
519                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 536  int apply_article_op_log_from_db(void) Line 598  int apply_article_op_log_from_db(void)
598    
599                  // Update MID with last successfully proceeded article_op_log                  // Update MID with last successfully proceeded article_op_log
600                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
601    
602                    op_count++;
603          }          }
604    
605          // release lock of last section          // release lock of last section
# Line 551  int apply_article_op_log_from_db(void) Line 615  int apply_article_op_log_from_db(void)
615    
616          mysql_close(db);          mysql_close(db);
617    
618          return ret;          return (ret < 0 ? ret : op_count);
619  }  }
620    
621  int section_list_loader_launch(void)  int section_list_loader_launch(void)
# Line 576  int section_list_loader_launch(void) Line 640  int section_list_loader_launch(void)
640          {          {
641                  SYS_child_process_count++;                  SYS_child_process_count++;
642                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
643                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
644                  return 0;                  return 0;
645          }          }
646          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 612  int section_list_loader_launch(void) Line 676  int section_list_loader_launch(void)
676                  }                  }
677    
678                  // Load section articles                  // Load section articles
                 last_aid = article_block_last_aid();  
679                  article_count = article_block_article_count();                  article_count = article_block_article_count();
680    
681                  if ((ret = append_articles_from_db(last_aid + 1, 0)) < 0)                  do
682                  {                  {
683                          log_error("append_articles_from_db(%d, 0) error\n", last_aid + 1);                          last_aid = article_block_last_aid();
684    
685                          if (ret == ERR_UNKNOWN_SECTION)                          if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
686                          {                          {
687                                  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);
688    
689                                    if (ret == ERR_UNKNOWN_SECTION)
690                                    {
691                                            SYS_section_list_reload = 1; // Force reload section_list
692                                    }
693                          }                          }
694                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
695    
696                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
   
697                  if (load_count > 0)                  if (load_count > 0)
698                  {                  {
699                          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());
700                  }                  }
701    
702                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 640  int section_list_loader_launch(void) Line 707  int section_list_loader_launch(void)
707                  // Load article_op log                  // Load article_op log
708                  last_mid = last_article_op_log_mid;                  last_mid = last_article_op_log_mid;
709    
710                  if ((ret = apply_article_op_log_from_db()) < 0)                  do
711                  {                  {
712                          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)  
713                          {                          {
714                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("apply_article_op_log_from_db() error\n");
715    
716                                    if (ret == ERR_UNKNOWN_SECTION)
717                                    {
718                                            SYS_section_list_reload = 1; // Force reload section_list
719                                    }
720                          }                          }
721                  }                  } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
722    
723                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
724                  {                  {
725                          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);
726                  }                  }
727    
728                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 660  int section_list_loader_launch(void) Line 730  int section_list_loader_launch(void)
730                          continue;                          continue;
731                  }                  }
732    
733                  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++)
734                  {                  {
735                          sleep(1);                          sleep(1);
736                  }                  }
# Line 673  int section_list_loader_launch(void) Line 743  int section_list_loader_launch(void)
743          detach_article_block_shm();          detach_article_block_shm();
744          detach_trie_dict_shm();          detach_trie_dict_shm();
745    
746          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
747          log_end();          log_end();
748    
749          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 700  int section_list_loader_reload(void) Line 770  int section_list_loader_reload(void)
770          return 0;          return 0;
771  }  }
772    
773  int query_section_articles(SECTION_LIST *p_section, int32_t page_id, ARTICLE *p_articles[], int32_t *p_article_count)  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
774  {  {
775          ARTICLE *p_article;          ARTICLE *p_article;
776          ARTICLE *p_next_page_first_article;          ARTICLE *p_next_page_first_article;
777          int ret = 0;          int ret = 0;
778    
779          if (p_section == NULL || p_articles == NULL || p_article_count == NULL)          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
780          {          {
781                  log_error("query_section_articles() NULL pointer error\n");                  log_error("query_section_articles() NULL pointer error\n");
782                  return -1;                  return -1;
# Line 719  int query_section_articles(SECTION_LIST Line 789  int query_section_articles(SECTION_LIST
789                  return -2;                  return -2;
790          }          }
791    
792          if (page_id < 0 || page_id >= p_section->page_count)          *p_page_count = p_section->page_count;
793    
794            if (p_section->visible_article_count == 0)
795            {
796                    *p_article_count = 0;
797            }
798            else if (page_id < 0 || page_id >= p_section->page_count)
799          {          {
800                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
801                  ret = -3;                  ret = -3;
# Line 757  int query_section_articles(SECTION_LIST Line 833  int query_section_articles(SECTION_LIST
833    
834          return ret;          return ret;
835  }  }
836    
837    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,
838                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
839    {
840            const ARTICLE *p_article = NULL;
841            ARTICLE *p_tmp;
842            int32_t aid = 0;
843            int page_id;
844            int offset;
845            int ret = 0;
846            int i;
847    
848            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
849            {
850                    log_error("locate_article_in_section() NULL pointer error\n");
851                    return -1;
852            }
853    
854            // acquire lock of section
855            if ((ret = section_list_rd_lock(p_section)) < 0)
856            {
857                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
858                    return -2;
859            }
860    
861            if (direction == 0)
862            {
863                    aid = p_article_cur->aid;
864            }
865            else if (direction == 1)
866            {
867                    p_article = p_article_cur;
868                    do
869                    {
870                            p_article = p_article->p_topic_next;
871                    } while (p_article != p_article_cur && p_article->visible == 0);
872    
873                    aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0);
874            }
875            else if (direction == -1)
876            {
877                    p_article = p_article_cur;
878                    do
879                    {
880                            p_article = p_article->p_topic_prior;
881                    } while (p_article != p_article_cur && p_article->visible == 0);
882    
883                    aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0);
884            }
885    
886            p_article = NULL;
887    
888            if (aid > 0)
889            {
890                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
891                    if (p_article != NULL)
892                    {
893                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
894    
895                            p_article = p_section->p_page_first_article[page_id];
896                            for (i = 0; i < *p_article_count;)
897                            {
898                                    if (p_article->visible)
899                                    {
900                                            if (p_article->aid == aid)
901                                            {
902                                                    *p_page_id = page_id;
903                                                    *p_visible_offset = i;
904                                                    break;
905                                            }
906                                            i++;
907                                            if (i >= *p_article_count)
908                                            {
909                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
910                                                    p_article = NULL;
911                                            }
912                                    }
913                                    p_article = p_article->p_next;
914                            }
915                    }
916            }
917    
918            // release lock of section
919            if (section_list_rd_unlock(p_section) < 0)
920            {
921                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
922                    ret = -2;
923            }
924    
925            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
926    }


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

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