/[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.25 by sysadm, Mon May 26 23:38:11 2025 UTC Revision 1.32 by sysadm, Tue Jun 17 13:18:55 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _GNU_SOURCE  
   
17  #include "section_list.h"  #include "section_list.h"
18  #include "log.h"  #include "log.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
# Line 48  union semun Line 46  union semun
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 200 // 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 530  void section_list_reset_articles(SECTION Line 672  void section_list_reset_articles(SECTION
672          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
673  }  }
674    
 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);  
                 }  
   
                 if (shmctl(shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
   
                 p_section_list_pool = NULL;  
         }  
 }  
   
675  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
676  {  {
677          int64_t index;          int64_t index;
# Line 1043  int32_t article_block_last_aid(void) Line 1141  int32_t article_block_last_aid(void)
1141          return last_aid;          return last_aid;
1142  }  }
1143    
1144    int article_block_article_count(void)
1145    {
1146            int ret;
1147    
1148            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1149            {
1150                    return -1;
1151            }
1152    
1153            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1154                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1155    
1156            return ret;
1157    }
1158    
1159  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1160  {  {
1161          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1083  int section_list_move_topic(SECTION_LIST Line 1196  int section_list_move_topic(SECTION_LIST
1196          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1197          int move_counter;          int move_counter;
1198    
1199          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1200          {          {
1201                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1202                  return -1;                  return -1;
1203          }          }
1204    
1205            if (p_section_src->sid == p_section_dest->sid)
1206            {
1207                    log_error("section_list_move_topic() src and dest section are the same\n");
1208                    return -1;
1209            }
1210    
1211          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1212          {          {
1213                  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);
1214                  return -2;                  return -2;
1215          }          }
1216    
# Line 1132  int section_list_move_topic(SECTION_LIST Line 1251  int section_list_move_topic(SECTION_LIST
1251          {          {
1252                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1253                  {                  {
1254                          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",
1255                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1256                          return -2;                          p_article = p_article->p_topic_next;
1257                            continue;
1258                  }                  }
1259    
1260                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1160  int section_list_move_topic(SECTION_LIST Line 1280  int section_list_move_topic(SECTION_LIST
1280    
1281                  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)
1282                  {                  {
1283                          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);
1284                          return -4;                          p_article = p_article->p_topic_next;
1285                            continue;
1286                  }                  }
1287    
1288                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1239  int section_list_move_topic(SECTION_LIST Line 1360  int section_list_move_topic(SECTION_LIST
1360    
1361          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)
1362          {          {
1363                  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",
1364                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1365          }          }
1366    


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

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