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

Diff of /lbbs/src/section_list.c

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

Revision 1.28 by sysadm, Tue May 27 07:21:43 2025 UTC Revision 1.37 by sysadm, Wed Jun 25 02:49:20 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
17  #include "log.h"  #include "log.h"
18    #include "section_list.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
20    #include <errno.h>
21    #include <signal.h>
22  #include <stdio.h>  #include <stdio.h>
23    #include <stdlib.h>
24  #include <string.h>  #include <string.h>
 #include <signal.h>  
25  #include <unistd.h>  #include <unistd.h>
26  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
27  #include <sys/param.h>  #include <sys/param.h>
28  #include <sys/sem.h>  #include <sys/sem.h>
29  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
30    
31  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
32  union semun  union semun
# Line 44  union semun Line 42  union semun
42  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
43  #define SECTION_TRY_LOCK_TIMES 10  #define SECTION_TRY_LOCK_TIMES 10
44    
45  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
46  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
47  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
48    
49  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections
50    
51  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
52    
# Line 557  void section_list_cleanup(void) Line 555  void section_list_cleanup(void)
555          p_section_list_pool = NULL;          p_section_list_pool = NULL;
556  }  }
557    
558    void section_list_ex_menu_set_cleanup(void)
559    {
560            int i;
561    
562            for (i = 0; i < p_section_list_pool->section_count; i++)
563            {
564                    if (p_section_list_pool->sections[i].ex_menu_tm > 0)
565                    {
566                            unload_menu(&(p_section_list_pool->sections[i].ex_menu_set));
567                    }
568            }
569    }
570    
571  int set_section_list_shm_readonly(void)  int set_section_list_shm_readonly(void)
572  {  {
573          int shmid;          int shmid;
# Line 610  inline static void sid_to_str(int32_t si Line 621  inline static void sid_to_str(int32_t si
621          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
622  }  }
623    
624  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
625  {  {
626          SECTION_LIST *p_section;          SECTION_LIST *p_section;
627          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
# Line 632  SECTION_LIST *section_list_create(int32_ Line 643  SECTION_LIST *section_list_create(int32_
643          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
644    
645          p_section->sid = sid;          p_section->sid = sid;
646            p_section->ex_menu_tm = 0;
647    
648          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
649          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
650    
651          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
652          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
653    
654          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);
655          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
656    
657          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
658          {          {
# Line 672  void section_list_reset_articles(SECTION Line 684  void section_list_reset_articles(SECTION
684    
685          p_section->page_count = 0;          p_section->page_count = 0;
686          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
687    
688            p_section->ontop_article_count = 0;
689  }  }
690    
691  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
# Line 862  int section_list_append_article(SECTION_ Line 876  int section_list_append_article(SECTION_
876                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
877          }          }
878    
879            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
880            {
881                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
882                                      p_section->sid, p_article->aid);
883                    return -5;
884            }
885    
886          return 0;          return 0;
887  }  }
888    
# Line 873  int section_list_set_article_visible(SEC Line 894  int section_list_set_article_visible(SEC
894    
895          if (p_section == NULL)          if (p_section == NULL)
896          {          {
897                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
898                  return -2;                  return -1;
899          }          }
900    
901          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 885  int section_list_set_article_visible(SEC Line 906  int section_list_set_article_visible(SEC
906    
907          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
908          {          {
909                  log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
910                  return -2;                  return -2;
911          }          }
912    
# Line 936  int section_list_set_article_visible(SEC Line 957  int section_list_set_article_visible(SEC
957          return affected_count;          return affected_count;
958  }  }
959    
960    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
961    {
962            int i;
963    
964            if (p_section == NULL || p_article == NULL)
965            {
966                    log_error("NULL pointer error\n");
967                    return -1;
968            }
969    
970            if (p_section->sid != p_article->sid)
971            {
972                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
973                    return -2;
974            }
975    
976            if (p_article->ontop)
977            {
978                    for (i = 0; i < p_section->ontop_article_count; i++)
979                    {
980                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
981                            {
982                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
983                                    return 0;
984                            }
985                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
986                            {
987                                    break;
988                            }
989                    }
990    
991                    // Remove the oldest one if the array of ontop articles is full
992                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
993                    {
994                            if (i == 0) // p_article is the oldest one
995                            {
996                                    return 0;
997                            }
998                            memmove((void *)(p_section->p_ontop_articles),
999                                            (void *)(p_section->p_ontop_articles + 1),
1000                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1001                            p_section->ontop_article_count--;
1002                            i--;
1003                    }
1004                    else
1005                    {
1006                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1007                                            (void *)(p_section->p_ontop_articles + i),
1008                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1009                    }
1010    
1011                    p_section->p_ontop_articles[i] = p_article;
1012                    p_section->ontop_article_count++;
1013    
1014                    // TODO: debug
1015            }
1016            else // ontop == 0
1017            {
1018                    for (i = 0; i < p_section->ontop_article_count; i++)
1019                    {
1020                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1021                            {
1022                                    break;
1023                            }
1024                    }
1025                    if (i == p_section->ontop_article_count) // not found
1026                    {
1027                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1028                            return 0;
1029                    }
1030    
1031                    memmove((void *)(p_section->p_ontop_articles + i),
1032                                    (void *)(p_section->p_ontop_articles + i + 1),
1033                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1034                    p_section->ontop_article_count--;
1035            }
1036    
1037            return 0;
1038    }
1039    
1040    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1041    {
1042            int page_count;
1043    
1044            if (p_section == NULL)
1045            {
1046                    log_error("NULL pointer error\n");
1047                    return -1;
1048            }
1049    
1050            page_count = p_section->page_count - 1 +
1051                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1052                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1053    
1054            return page_count;
1055    }
1056    
1057    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1058    {
1059            if (p_section == NULL)
1060            {
1061                    log_error("NULL pointer error\n");
1062                    return -1;
1063            }
1064    
1065            if (page_id < p_section->page_count - 1)
1066            {
1067                    return BBS_article_limit_per_page;
1068            }
1069            else // if (page_id >= p_section->page_count - 1)
1070            {
1071                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1072                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1073            }
1074    }
1075    
1076  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
1077  {  {
1078          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1572  int section_list_rd_lock(SECTION_LIST *p Line 1709  int section_list_rd_lock(SECTION_LIST *p
1709                          timer++;                          timer++;
1710                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1711                          {                          {
1712                                  log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);                                  log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer);
1713                          }                          }
1714                  }                  }
1715                  else // failed                  else // failed
1716                  {                  {
1717                          log_error("section_list_rd_lock() failed on section %d\n", sid);                          log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1718                          break;                          break;
1719                  }                  }
1720          }          }
# Line 1603  int section_list_rw_lock(SECTION_LIST *p Line 1740  int section_list_rw_lock(SECTION_LIST *p
1740                          timer++;                          timer++;
1741                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1742                          {                          {
1743                                  log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);                                  log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer);
1744                          }                          }
1745                  }                  }
1746                  else // failed                  else // failed
1747                  {                  {
1748                          log_error("acquire_section_rw_lock() failed on section %d\n", sid);                          log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1749                          break;                          break;
1750                  }                  }
1751          }          }


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

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