/[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.27 by sysadm, Tue May 27 01:53:42 2025 UTC Revision 1.38 by sysadm, Wed Jul 23 01:15:21 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 399  ARTICLE *article_block_find_by_aid(int32 Line 386  ARTICLE *article_block_find_by_aid(int32
386                  }                  }
387          }          }
388    
389          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
390            {
391                    return NULL;
392            }
393    
394            return (p_block->articles + left); // found
395  }  }
396    
397  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 605  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 627  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';
626    
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_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
631          p_section->master_name[sizeof(p_section->master_name - 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)
634          {          {
# Line 667  void section_list_reset_articles(SECTION Line 660  void section_list_reset_articles(SECTION
660    
661          p_section->page_count = 0;          p_section->page_count = 0;
662          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
663    
664            p_section->ontop_article_count = 0;
665  }  }
666    
667  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
# Line 857  int section_list_append_article(SECTION_ Line 852  int section_list_append_article(SECTION_
852                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
853          }          }
854    
855            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
856            {
857                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
858                                      p_section->sid, p_article->aid);
859                    return -5;
860            }
861    
862          return 0;          return 0;
863  }  }
864    
# Line 868  int section_list_set_article_visible(SEC Line 870  int section_list_set_article_visible(SEC
870    
871          if (p_section == NULL)          if (p_section == NULL)
872          {          {
873                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
874                  return -2;                  return -1;
875          }          }
876    
877          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 880  int section_list_set_article_visible(SEC Line 882  int section_list_set_article_visible(SEC
882    
883          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
884          {          {
885                  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);
886                  return -2;                  return -2;
887          }          }
888    
# Line 931  int section_list_set_article_visible(SEC Line 933  int section_list_set_article_visible(SEC
933          return affected_count;          return affected_count;
934  }  }
935    
936    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
937    {
938            int i;
939    
940            if (p_section == NULL || p_article == NULL)
941            {
942                    log_error("NULL pointer error\n");
943                    return -1;
944            }
945    
946            if (p_section->sid != p_article->sid)
947            {
948                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
949                    return -2;
950            }
951    
952            if (p_article->ontop)
953            {
954                    for (i = 0; i < p_section->ontop_article_count; i++)
955                    {
956                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
957                            {
958                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
959                                    return 0;
960                            }
961                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
962                            {
963                                    break;
964                            }
965                    }
966    
967                    // Remove the oldest one if the array of ontop articles is full
968                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
969                    {
970                            if (i == 0) // p_article is the oldest one
971                            {
972                                    return 0;
973                            }
974                            memmove((void *)(p_section->p_ontop_articles),
975                                            (void *)(p_section->p_ontop_articles + 1),
976                                            sizeof(ARTICLE *) * (size_t)(i - 1));
977                            p_section->ontop_article_count--;
978                            i--;
979                    }
980                    else
981                    {
982                            memmove((void *)(p_section->p_ontop_articles + i + 1),
983                                            (void *)(p_section->p_ontop_articles + i),
984                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
985                    }
986    
987                    p_section->p_ontop_articles[i] = p_article;
988                    p_section->ontop_article_count++;
989    
990                    // TODO: debug
991            }
992            else // ontop == 0
993            {
994                    for (i = 0; i < p_section->ontop_article_count; i++)
995                    {
996                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
997                            {
998                                    break;
999                            }
1000                    }
1001                    if (i == p_section->ontop_article_count) // not found
1002                    {
1003                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1004                            return 0;
1005                    }
1006    
1007                    memmove((void *)(p_section->p_ontop_articles + i),
1008                                    (void *)(p_section->p_ontop_articles + i + 1),
1009                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1010                    p_section->ontop_article_count--;
1011            }
1012    
1013            return 0;
1014    }
1015    
1016    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1017    {
1018            int page_count;
1019    
1020            if (p_section == NULL)
1021            {
1022                    log_error("NULL pointer error\n");
1023                    return -1;
1024            }
1025    
1026            page_count = p_section->page_count - 1 +
1027                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1028                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1029    
1030            return page_count;
1031    }
1032    
1033    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1034    {
1035            if (p_section == NULL)
1036            {
1037                    log_error("NULL pointer error\n");
1038                    return -1;
1039            }
1040    
1041            if (page_id < p_section->page_count - 1)
1042            {
1043                    return BBS_article_limit_per_page;
1044            }
1045            else // if (page_id >= p_section->page_count - 1)
1046            {
1047                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1048                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1049            }
1050    }
1051    
1052  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)
1053  {  {
1054          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1193  int section_list_move_topic(SECTION_LIST Line 1311  int section_list_move_topic(SECTION_LIST
1311          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1312          int move_counter;          int move_counter;
1313    
1314          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1315          {          {
1316                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1317                  return -1;                  return -1;
1318          }          }
1319    
1320            if (p_section_src->sid == p_section_dest->sid)
1321            {
1322                    log_error("section_list_move_topic() src and dest section are the same\n");
1323                    return -1;
1324            }
1325    
1326          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1327          {          {
1328                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1329                  return -2;                  return -2;
1330          }          }
1331    
# Line 1242  int section_list_move_topic(SECTION_LIST Line 1366  int section_list_move_topic(SECTION_LIST
1366          {          {
1367                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1368                  {                  {
1369                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1370                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1371                          return -2;                          p_article = p_article->p_topic_next;
1372                            continue;
1373                  }                  }
1374    
1375                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1270  int section_list_move_topic(SECTION_LIST Line 1395  int section_list_move_topic(SECTION_LIST
1395    
1396                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1397                  {                  {
1398                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1399                          return -4;                          p_article = p_article->p_topic_next;
1400                            continue;
1401                  }                  }
1402    
1403                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1349  int section_list_move_topic(SECTION_LIST Line 1475  int section_list_move_topic(SECTION_LIST
1475    
1476          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1477          {          {
1478                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1479                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1480          }          }
1481    
# Line 1559  int section_list_rd_lock(SECTION_LIST *p Line 1685  int section_list_rd_lock(SECTION_LIST *p
1685                          timer++;                          timer++;
1686                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1687                          {                          {
1688                                  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);
1689                          }                          }
1690                  }                  }
1691                  else // failed                  else // failed
1692                  {                  {
1693                          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);
1694                          break;                          break;
1695                  }                  }
1696          }          }
# Line 1590  int section_list_rw_lock(SECTION_LIST *p Line 1716  int section_list_rw_lock(SECTION_LIST *p
1716                          timer++;                          timer++;
1717                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1718                          {                          {
1719                                  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);
1720                          }                          }
1721                  }                  }
1722                  else // failed                  else // failed
1723                  {                  {
1724                          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);
1725                          break;                          break;
1726                  }                  }
1727          }          }


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

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