/[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.29 by sysadm, Wed May 28 07:30:23 2025 UTC Revision 1.43 by sysadm, Tue Oct 14 05:28:15 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 77  typedef struct article_block_pool_t ARTI Line 75  typedef struct article_block_pool_t ARTI
75    
76  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77    
78  struct section_list_pool_t  SECTION_LIST_POOL *p_section_list_pool = NULL;
 {  
         int shmid;  
         SECTION_LIST sections[BBS_max_section];  
         int section_count;  
         int semid;  
         TRIE_NODE *p_trie_dict_section_by_name;  
         TRIE_NODE *p_trie_dict_section_by_sid;  
 };  
 typedef struct section_list_pool_t SECTION_LIST_POOL;  
   
 static SECTION_LIST_POOL *p_section_list_pool = NULL;  
79    
80  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
81  {  {
# Line 610  inline static void sid_to_str(int32_t si Line 597  inline static void sid_to_str(int32_t si
597          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
598  }  }
599    
600  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)
601  {  {
602          SECTION_LIST *p_section;          SECTION_LIST *p_section;
603          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
# Line 632  SECTION_LIST *section_list_create(int32_ Line 619  SECTION_LIST *section_list_create(int32_
619          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;
620    
621          p_section->sid = sid;          p_section->sid = sid;
622            p_section->ex_menu_tm = 0;
623    
624          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
625          p_section->sname[sizeof(p_section->sname) - 1] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
# Line 639  SECTION_LIST *section_list_create(int32_ Line 627  SECTION_LIST *section_list_create(int32_
627          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
628          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
629    
630          strncpy(p_section->master_list, master_name, sizeof(p_section->master_list) - 1);          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
631          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
632    
633          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)
# Line 661  SECTION_LIST *section_list_create(int32_ Line 649  SECTION_LIST *section_list_create(int32_
649          return p_section;          return p_section;
650  }  }
651    
652    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
653    {
654            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
655            {
656                    log_error("NULL pointer error\n");
657                    return -1;
658            }
659    
660            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
661            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
662    
663            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
664            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
665    
666            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
667            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
668    
669            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
670            {
671                    log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
672                    return -2;
673            }
674    
675            return 0;
676    }
677    
678  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
679  {  {
680          p_section->article_count = 0;          p_section->article_count = 0;
# Line 672  void section_list_reset_articles(SECTION Line 686  void section_list_reset_articles(SECTION
686    
687          p_section->page_count = 0;          p_section->page_count = 0;
688          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
689    
690            p_section->ontop_article_count = 0;
691  }  }
692    
693  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
# Line 737  int section_list_append_article(SECTION_ Line 753  int section_list_append_article(SECTION_
753    
754          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
755          {          {
756                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
757                  return -1;                  return -1;
758          }          }
759    
# Line 862  int section_list_append_article(SECTION_ Line 878  int section_list_append_article(SECTION_
878                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
879          }          }
880    
881            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
882            {
883                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
884                                      p_section->sid, p_article->aid);
885                    return -5;
886            }
887    
888          return 0;          return 0;
889  }  }
890    
# Line 873  int section_list_set_article_visible(SEC Line 896  int section_list_set_article_visible(SEC
896    
897          if (p_section == NULL)          if (p_section == NULL)
898          {          {
899                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
900                  return -2;                  return -1;
901          }          }
902    
903          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 908  int section_list_set_article_visible(SEC
908    
909          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
910          {          {
911                  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);
912                  return -2;                  return -2;
913          }          }
914    
# Line 936  int section_list_set_article_visible(SEC Line 959  int section_list_set_article_visible(SEC
959          return affected_count;          return affected_count;
960  }  }
961    
962    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
963    {
964            int i;
965    
966            if (p_section == NULL || p_article == NULL)
967            {
968                    log_error("NULL pointer error\n");
969                    return -1;
970            }
971    
972            if (p_section->sid != p_article->sid)
973            {
974                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
975                    return -2;
976            }
977    
978            if (p_article->ontop)
979            {
980                    for (i = 0; i < p_section->ontop_article_count; i++)
981                    {
982                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
983                            {
984                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
985                                    return 0;
986                            }
987                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
988                            {
989                                    break;
990                            }
991                    }
992    
993                    // Remove the oldest one if the array of ontop articles is full
994                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
995                    {
996                            if (i == 0) // p_article is the oldest one
997                            {
998                                    return 0;
999                            }
1000                            memmove((void *)(p_section->p_ontop_articles),
1001                                            (void *)(p_section->p_ontop_articles + 1),
1002                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1003                            p_section->ontop_article_count--;
1004                            i--;
1005                    }
1006                    else
1007                    {
1008                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1009                                            (void *)(p_section->p_ontop_articles + i),
1010                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1011                    }
1012    
1013                    p_section->p_ontop_articles[i] = p_article;
1014                    p_section->ontop_article_count++;
1015    
1016                    // TODO: debug
1017            }
1018            else // ontop == 0
1019            {
1020                    for (i = 0; i < p_section->ontop_article_count; i++)
1021                    {
1022                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1023                            {
1024                                    break;
1025                            }
1026                    }
1027                    if (i == p_section->ontop_article_count) // not found
1028                    {
1029                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1030                            return 0;
1031                    }
1032    
1033                    memmove((void *)(p_section->p_ontop_articles + i),
1034                                    (void *)(p_section->p_ontop_articles + i + 1),
1035                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1036                    p_section->ontop_article_count--;
1037            }
1038    
1039            return 0;
1040    }
1041    
1042    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1043    {
1044            int page_count;
1045    
1046            if (p_section == NULL)
1047            {
1048                    log_error("NULL pointer error\n");
1049                    return -1;
1050            }
1051    
1052            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1053                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1054                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1055    
1056            return page_count;
1057    }
1058    
1059    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1060    {
1061            if (p_section == NULL)
1062            {
1063                    log_error("NULL pointer error\n");
1064                    return -1;
1065            }
1066    
1067            if (page_id < p_section->page_count - 1)
1068            {
1069                    return BBS_article_limit_per_page;
1070            }
1071            else // if (page_id >= p_section->page_count - 1)
1072            {
1073                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1074                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1075            }
1076    }
1077    
1078  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)
1079  {  {
1080          ARTICLE *p_article;          ARTICLE *p_article;
# Line 949  ARTICLE *section_list_find_article_with_ Line 1088  ARTICLE *section_list_find_article_with_
1088    
1089          if (p_section == NULL)          if (p_section == NULL)
1090          {          {
1091                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1092                  return NULL;                  return NULL;
1093          }          }
1094    
# Line 1041  int section_list_calculate_page(SECTION_ Line 1180  int section_list_calculate_page(SECTION_
1180    
1181          if (p_section == NULL)          if (p_section == NULL)
1182          {          {
1183                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1184                  return -1;                  return -1;
1185          }          }
1186    
# Line 1200  int section_list_move_topic(SECTION_LIST Line 1339  int section_list_move_topic(SECTION_LIST
1339    
1340          if (p_section_src == NULL || p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1341          {          {
1342                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1343                  return -1;                  return -1;
1344          }          }
1345    
# Line 1572  int section_list_rd_lock(SECTION_LIST *p Line 1711  int section_list_rd_lock(SECTION_LIST *p
1711                          timer++;                          timer++;
1712                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1713                          {                          {
1714                                  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);
1715                          }                          }
1716                  }                  }
1717                  else // failed                  else // failed
1718                  {                  {
1719                          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);
1720                          break;                          break;
1721                  }                  }
1722          }          }
# Line 1603  int section_list_rw_lock(SECTION_LIST *p Line 1742  int section_list_rw_lock(SECTION_LIST *p
1742                          timer++;                          timer++;
1743                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1744                          {                          {
1745                                  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);
1746                          }                          }
1747                  }                  }
1748                  else // failed                  else // failed
1749                  {                  {
1750                          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);
1751                          break;                          break;
1752                  }                  }
1753          }          }


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

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