/[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.24 by sysadm, Mon May 26 04:47:45 2025 UTC Revision 1.35 by sysadm, Mon Jun 23 08:38:01 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 108  int article_block_init(const char *filen Line 106  int article_block_init(const char *filen
106                  return -1;                  return -1;
107          }          }
108    
109          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
110          {          {
111                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
112                  return -2;                  return -2;
# Line 236  void article_block_cleanup(void) Line 234  void article_block_cleanup(void)
234          p_article_block_pool = NULL;          p_article_block_pool = NULL;
235  }  }
236    
237    int set_article_block_shm_readonly(void)
238    {
239            int shmid;
240            void *p_shm;
241            int i;
242    
243            if (p_article_block_pool == NULL)
244            {
245                    log_error("article_block_pool not initialized\n");
246                    return -1;
247            }
248    
249            for (i = 0; i < p_article_block_pool->shm_count; i++)
250            {
251                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
252    
253                    // Remap shared memory in read-only mode
254                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
255                    if (p_shm == (void *)-1)
256                    {
257                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
258                            return -2;
259                    }
260            }
261    
262            return 0;
263    }
264    
265    int detach_article_block_shm(void)
266    {
267            int shmid;
268    
269            if (p_article_block_pool == NULL)
270            {
271                    return -1;
272            }
273    
274            for (int i = 0; i < p_article_block_pool->shm_count; i++)
275            {
276                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
277                    {
278                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
279                            return -2;
280                    }
281            }
282    
283            shmid = p_article_block_pool->shmid;
284    
285            if (shmdt(p_article_block_pool) == -1)
286            {
287                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
288                    return -3;
289            }
290    
291            p_article_block_pool = NULL;
292    
293            return 0;
294    }
295    
296  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
297  {  {
298          ARTICLE_BLOCK *p_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
# Line 340  ARTICLE *article_block_find_by_aid(int32 Line 397  ARTICLE *article_block_find_by_aid(int32
397                  }                  }
398          }          }
399    
400          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
401            {
402                    return NULL;
403            }
404    
405            return (p_block->articles + left); // found
406  }  }
407    
408  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 452  extern int section_list_init(const char Line 514  extern int section_list_init(const char
514          return 0;          return 0;
515  }  }
516    
517    void section_list_cleanup(void)
518    {
519            int shmid;
520    
521            if (p_section_list_pool == NULL)
522            {
523                    return;
524            }
525    
526            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
527            {
528                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
529                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
530            }
531    
532            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
533            {
534                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
535                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
536            }
537    
538            shmid = p_section_list_pool->shmid;
539    
540            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
541            {
542                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
543            }
544    
545            if (shmdt(p_section_list_pool) == -1)
546            {
547                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
548            }
549    
550            if (shmctl(shmid, IPC_RMID, NULL) == -1)
551            {
552                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
553            }
554    
555            p_section_list_pool = NULL;
556    }
557    
558    int set_section_list_shm_readonly(void)
559    {
560            int shmid;
561            void *p_shm;
562    
563            if (p_section_list_pool == NULL)
564            {
565                    log_error("p_section_list_pool not initialized\n");
566                    return -1;
567            }
568    
569            shmid = p_section_list_pool->shmid;
570    
571            // Remap shared memory in read-only mode
572            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
573            if (p_shm == (void *)-1)
574            {
575                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
576                    return -3;
577            }
578    
579            p_section_list_pool = p_shm;
580    
581            return 0;
582    }
583    
584    int detach_section_list_shm(void)
585    {
586            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
587            {
588                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
589                    return -1;
590            }
591    
592            p_section_list_pool = NULL;
593    
594            return 0;
595    }
596    
597  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
598  {  {
599          uint32_t u_sid;          uint32_t u_sid;
# Line 466  inline static void sid_to_str(int32_t si Line 608  inline static void sid_to_str(int32_t si
608          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
609  }  }
610    
611  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)
612  {  {
613          SECTION_LIST *p_section;          SECTION_LIST *p_section;
614          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
# Line 489  SECTION_LIST *section_list_create(int32_ Line 631  SECTION_LIST *section_list_create(int32_
631    
632          p_section->sid = sid;          p_section->sid = sid;
633    
634          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
635          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
636    
637          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
638          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
639    
640          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);
641          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
642    
643          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)
644          {          {
# Line 528  void section_list_reset_articles(SECTION Line 670  void section_list_reset_articles(SECTION
670    
671          p_section->page_count = 0;          p_section->page_count = 0;
672          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
   
 void section_list_cleanup(void)  
 {  
         int shmid;  
   
         if (p_section_list_pool == NULL)  
         {  
                 return;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_name != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);  
                 p_section_list_pool->p_trie_dict_section_by_name = NULL;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);  
                 p_section_list_pool->p_trie_dict_section_by_sid = NULL;  
         }  
   
         if (p_section_list_pool != NULL)  
         {  
                 if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)  
                 {  
                         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);  
                 }  
   
                 shmid = p_section_list_pool->shmid;  
   
                 if (shmdt(p_section_list_pool) == -1)  
                 {  
                         log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);  
                 }  
673    
674                  if (shmctl(shmid, IPC_RMID, NULL) == -1)          p_section->ontop_article_count = 0;
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
   
                 p_section_list_pool = NULL;  
         }  
675  }  }
676    
677  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
# Line 762  int section_list_append_article(SECTION_ Line 862  int section_list_append_article(SECTION_
862                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
863          }          }
864    
865            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
866            {
867                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
868                                      p_section->sid, p_article->aid);
869                    return -5;
870            }
871    
872          return 0;          return 0;
873  }  }
874    
# Line 773  int section_list_set_article_visible(SEC Line 880  int section_list_set_article_visible(SEC
880    
881          if (p_section == NULL)          if (p_section == NULL)
882          {          {
883                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
884                  return -2;                  return -1;
885          }          }
886    
887          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 785  int section_list_set_article_visible(SEC Line 892  int section_list_set_article_visible(SEC
892    
893          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
894          {          {
895                  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);
896                  return -2;                  return -2;
897          }          }
898    
# Line 836  int section_list_set_article_visible(SEC Line 943  int section_list_set_article_visible(SEC
943          return affected_count;          return affected_count;
944  }  }
945    
946    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
947    {
948            int i;
949    
950            if (p_section == NULL || p_article == NULL)
951            {
952                    log_error("NULL pointer error\n");
953                    return -1;
954            }
955    
956            if (p_section->sid != p_article->sid)
957            {
958                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
959                    return -2;
960            }
961    
962            if (p_article->ontop)
963            {
964                    for (i = 0; i < p_section->ontop_article_count; i++)
965                    {
966                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
967                            {
968                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
969                                    return 0;
970                            }
971                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
972                            {
973                                    break;
974                            }
975                    }
976    
977                    // Remove the oldest one if the array of ontop articles is full
978                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
979                    {
980                            if (i == 0) // p_article is the oldest one
981                            {
982                                    return 0;
983                            }
984                            memmove((void *)(p_section->p_ontop_articles),
985                                            (void *)(p_section->p_ontop_articles + 1),
986                                            sizeof(ARTICLE *) * (size_t)(i - 1));
987                            p_section->ontop_article_count--;
988                            i--;
989                    }
990                    else
991                    {
992                            memmove((void *)(p_section->p_ontop_articles + i + 1),
993                                            (void *)(p_section->p_ontop_articles + i),
994                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
995                    }
996    
997                    p_section->p_ontop_articles[i] = p_article;
998                    p_section->ontop_article_count++;
999    
1000                    // TODO: debug
1001            }
1002            else // ontop == 0
1003            {
1004                    for (i = 0; i < p_section->ontop_article_count; i++)
1005                    {
1006                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1007                            {
1008                                    break;
1009                            }
1010                    }
1011                    if (i == p_section->ontop_article_count) // not found
1012                    {
1013                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1014                            return 0;
1015                    }
1016    
1017                    memmove((void *)(p_section->p_ontop_articles + i),
1018                                    (void *)(p_section->p_ontop_articles + i + 1),
1019                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1020                    p_section->ontop_article_count--;
1021            }
1022    
1023            return 0;
1024    }
1025    
1026    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1027    {
1028            int page_count;
1029    
1030            if (p_section == NULL)
1031            {
1032                    log_error("NULL pointer error\n");
1033                    return -1;
1034            }
1035    
1036            page_count = p_section->page_count - 1 +
1037                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1038                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1039    
1040            return page_count;
1041    }
1042    
1043    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1044    {
1045            if (p_section == NULL)
1046            {
1047                    log_error("NULL pointer error\n");
1048                    return -1;
1049            }
1050    
1051            if (page_id < p_section->page_count - 1)
1052            {
1053                    return BBS_article_limit_per_page;
1054            }
1055            else // if (page_id >= p_section->page_count - 1)
1056            {
1057                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1058                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1059            }
1060    }
1061    
1062  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)
1063  {  {
1064          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1035  int section_list_calculate_page(SECTION_ Line 1258  int section_list_calculate_page(SECTION_
1258          return 0;          return 0;
1259  }  }
1260    
1261    int32_t article_block_last_aid(void)
1262    {
1263            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1264            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1265    
1266            return last_aid;
1267    }
1268    
1269    int article_block_article_count(void)
1270    {
1271            int ret;
1272    
1273            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1274            {
1275                    return -1;
1276            }
1277    
1278            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1279                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1280    
1281            return ret;
1282    }
1283    
1284  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1285  {  {
1286          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1075  int section_list_move_topic(SECTION_LIST Line 1321  int section_list_move_topic(SECTION_LIST
1321          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1322          int move_counter;          int move_counter;
1323    
1324          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1325          {          {
1326                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1327                  return -1;                  return -1;
1328          }          }
1329    
1330            if (p_section_src->sid == p_section_dest->sid)
1331            {
1332                    log_error("section_list_move_topic() src and dest section are the same\n");
1333                    return -1;
1334            }
1335    
1336          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1337          {          {
1338                  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);
1339                  return -2;                  return -2;
1340          }          }
1341    
# Line 1124  int section_list_move_topic(SECTION_LIST Line 1376  int section_list_move_topic(SECTION_LIST
1376          {          {
1377                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1378                  {                  {
1379                          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",
1380                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1381                          return -2;                          p_article = p_article->p_topic_next;
1382                            continue;
1383                  }                  }
1384    
1385                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1152  int section_list_move_topic(SECTION_LIST Line 1405  int section_list_move_topic(SECTION_LIST
1405    
1406                  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)
1407                  {                  {
1408                          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);
1409                          return -4;                          p_article = p_article->p_topic_next;
1410                            continue;
1411                  }                  }
1412    
1413                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1231  int section_list_move_topic(SECTION_LIST Line 1485  int section_list_move_topic(SECTION_LIST
1485    
1486          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)
1487          {          {
1488                  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",
1489                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1490          }          }
1491    


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

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