/[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.23 by sysadm, Mon May 26 03:20:39 2025 UTC Revision 1.45 by sysadm, Tue Oct 21 06:24:51 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 108  int article_block_init(const char *filen Line 95  int article_block_init(const char *filen
95                  return -1;                  return -1;
96          }          }
97    
98          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
99          {          {
100                  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);
101                  return -2;                  return -2;
# Line 236  void article_block_cleanup(void) Line 223  void article_block_cleanup(void)
223          p_article_block_pool = NULL;          p_article_block_pool = NULL;
224  }  }
225    
226    int set_article_block_shm_readonly(void)
227    {
228            int shmid;
229            void *p_shm;
230            int i;
231    
232            if (p_article_block_pool == NULL)
233            {
234                    log_error("article_block_pool not initialized\n");
235                    return -1;
236            }
237    
238            for (i = 0; i < p_article_block_pool->shm_count; i++)
239            {
240                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
241    
242                    // Remap shared memory in read-only mode
243                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
244                    if (p_shm == (void *)-1)
245                    {
246                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
247                            return -2;
248                    }
249            }
250    
251            return 0;
252    }
253    
254    int detach_article_block_shm(void)
255    {
256            int shmid;
257    
258            if (p_article_block_pool == NULL)
259            {
260                    return -1;
261            }
262    
263            for (int i = 0; i < p_article_block_pool->shm_count; i++)
264            {
265                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
266                    {
267                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
268                            return -2;
269                    }
270            }
271    
272            shmid = p_article_block_pool->shmid;
273    
274            if (shmdt(p_article_block_pool) == -1)
275            {
276                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
277                    return -3;
278            }
279    
280            p_article_block_pool = NULL;
281    
282            return 0;
283    }
284    
285  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
286  {  {
287          ARTICLE_BLOCK *p_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
# Line 340  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 452  extern int section_list_init(const char Line 503  extern int section_list_init(const char
503          return 0;          return 0;
504  }  }
505    
506    void section_list_cleanup(void)
507    {
508            int shmid;
509    
510            if (p_section_list_pool == NULL)
511            {
512                    return;
513            }
514    
515            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
516            {
517                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
518                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
519            }
520    
521            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
522            {
523                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
524                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
525            }
526    
527            shmid = p_section_list_pool->shmid;
528    
529            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
530            {
531                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
532            }
533    
534            if (shmdt(p_section_list_pool) == -1)
535            {
536                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
537            }
538    
539            if (shmctl(shmid, IPC_RMID, NULL) == -1)
540            {
541                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
542            }
543    
544            p_section_list_pool = NULL;
545    }
546    
547    int set_section_list_shm_readonly(void)
548    {
549            int shmid;
550            void *p_shm;
551    
552            if (p_section_list_pool == NULL)
553            {
554                    log_error("p_section_list_pool not initialized\n");
555                    return -1;
556            }
557    
558            shmid = p_section_list_pool->shmid;
559    
560            // Remap shared memory in read-only mode
561            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
562            if (p_shm == (void *)-1)
563            {
564                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
565                    return -3;
566            }
567    
568            p_section_list_pool = p_shm;
569    
570            return 0;
571    }
572    
573    int detach_section_list_shm(void)
574    {
575            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
576            {
577                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
578                    return -1;
579            }
580    
581            p_section_list_pool = NULL;
582    
583            return 0;
584    }
585    
586  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)
587  {  {
588          uint32_t u_sid;          uint32_t u_sid;
# Line 466  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 488  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 517  SECTION_LIST *section_list_create(int32_ Line 649  SECTION_LIST *section_list_create(int32_
649          return p_section;          return p_section;
650  }  }
651    
652  void section_list_reset_articles(SECTION_LIST *p_section)  int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
653  {  {
654          p_section->article_count = 0;          int64_t index;
         p_section->topic_count = 0;  
         p_section->visible_article_count = 0;  
         p_section->visible_topic_count = 0;  
         p_section->p_article_head = NULL;  
         p_section->p_article_tail = NULL;  
   
         p_section->page_count = 0;  
         p_section->last_page_visible_article_count = 0;  
 }  
   
 void section_list_cleanup(void)  
 {  
         int shmid;  
655    
656          if (p_section_list_pool == NULL)          if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
657          {          {
658                  return;                  log_error("NULL pointer error\n");
659                    return -1;
660          }          }
661    
662          if (p_section_list_pool->p_trie_dict_section_by_name != NULL)          index = get_section_index(p_section);
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);  
                 p_section_list_pool->p_trie_dict_section_by_name = NULL;  
         }  
663    
664          if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
665          {          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);  
                 p_section_list_pool->p_trie_dict_section_by_sid = NULL;  
         }  
666    
667          if (p_section_list_pool != NULL)          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
668            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
669    
670            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
671            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
672    
673            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
674          {          {
675                  if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)                  log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
676                  {                  return -2;
677                          log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);          }
                 }  
678    
679                  shmid = p_section_list_pool->shmid;          return 0;
680    }
681    
682                  if (shmdt(p_section_list_pool) == -1)  void section_list_reset_articles(SECTION_LIST *p_section)
683                  {  {
684                          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);          p_section->article_count = 0;
685                  }          p_section->topic_count = 0;
686            p_section->visible_article_count = 0;
687            p_section->visible_topic_count = 0;
688            p_section->p_article_head = NULL;
689            p_section->p_article_tail = NULL;
690    
691                  if (shmctl(shmid, IPC_RMID, NULL) == -1)          p_section->page_count = 0;
692                  {          p_section->last_page_visible_article_count = 0;
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
693    
694                  p_section_list_pool = NULL;          p_section->ontop_article_count = 0;
         }  
695  }  }
696    
697  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
698  {  {
699          int64_t index;          int64_t index;
700            int ret;
701    
702          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
703          {          {
# Line 584  SECTION_LIST *section_list_find_by_name( Line 705  SECTION_LIST *section_list_find_by_name(
705                  return NULL;                  return NULL;
706          }          }
707    
708          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
709            if (ret < 0)
710          {          {
711                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
712                  return NULL;                  return NULL;
713          }          }
714            else if (ret == 0)
715            {
716                    return NULL;
717            }
718    
719          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
720  }  }
# Line 596  SECTION_LIST *section_list_find_by_name( Line 722  SECTION_LIST *section_list_find_by_name(
722  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
723  {  {
724          int64_t index;          int64_t index;
725            int ret;
726          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
727    
728          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
# Line 606  SECTION_LIST *section_list_find_by_sid(i Line 733  SECTION_LIST *section_list_find_by_sid(i
733    
734          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
735    
736          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
737            if (ret < 0)
738          {          {
739                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
740                  return NULL;                  return NULL;
741          }          }
742            else if (ret == 0)
743            {
744                    return NULL;
745            }
746    
747          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
748  }  }
# Line 625  int section_list_append_article(SECTION_ Line 757  int section_list_append_article(SECTION_
757    
758          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
759          {          {
760                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
761                  return -1;                  return -1;
762          }          }
763    
# Line 750  int section_list_append_article(SECTION_ Line 882  int section_list_append_article(SECTION_
882                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
883          }          }
884    
885            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
886            {
887                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
888                                      p_section->sid, p_article->aid);
889                    return -5;
890            }
891    
892          return 0;          return 0;
893  }  }
894    
# Line 761  int section_list_set_article_visible(SEC Line 900  int section_list_set_article_visible(SEC
900    
901          if (p_section == NULL)          if (p_section == NULL)
902          {          {
903                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
904                  return -2;                  return -1;
905          }          }
906    
907          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 773  int section_list_set_article_visible(SEC Line 912  int section_list_set_article_visible(SEC
912    
913          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
914          {          {
915                  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);
916                  return -2;                  return -2;
917          }          }
918    
# Line 824  int section_list_set_article_visible(SEC Line 963  int section_list_set_article_visible(SEC
963          return affected_count;          return affected_count;
964  }  }
965    
966    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
967    {
968            int i;
969    
970            if (p_section == NULL || p_article == NULL)
971            {
972                    log_error("NULL pointer error\n");
973                    return -1;
974            }
975    
976            if (p_section->sid != p_article->sid)
977            {
978                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
979                    return -2;
980            }
981    
982            if (p_article->ontop)
983            {
984                    for (i = 0; i < p_section->ontop_article_count; i++)
985                    {
986                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
987                            {
988                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
989                                    return 0;
990                            }
991                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
992                            {
993                                    break;
994                            }
995                    }
996    
997                    // Remove the oldest one if the array of ontop articles is full
998                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
999                    {
1000                            if (i == 0) // p_article is the oldest one
1001                            {
1002                                    return 0;
1003                            }
1004                            memmove((void *)(p_section->p_ontop_articles),
1005                                            (void *)(p_section->p_ontop_articles + 1),
1006                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1007                            p_section->ontop_article_count--;
1008                            i--;
1009                    }
1010                    else
1011                    {
1012                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1013                                            (void *)(p_section->p_ontop_articles + i),
1014                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1015                    }
1016    
1017                    p_section->p_ontop_articles[i] = p_article;
1018                    p_section->ontop_article_count++;
1019    
1020                    // TODO: debug
1021            }
1022            else // ontop == 0
1023            {
1024                    for (i = 0; i < p_section->ontop_article_count; i++)
1025                    {
1026                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1027                            {
1028                                    break;
1029                            }
1030                    }
1031                    if (i == p_section->ontop_article_count) // not found
1032                    {
1033                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1034                            return 0;
1035                    }
1036    
1037                    memmove((void *)(p_section->p_ontop_articles + i),
1038                                    (void *)(p_section->p_ontop_articles + i + 1),
1039                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1040                    p_section->ontop_article_count--;
1041            }
1042    
1043            return 0;
1044    }
1045    
1046    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1047    {
1048            int page_count;
1049    
1050            if (p_section == NULL)
1051            {
1052                    log_error("NULL pointer error\n");
1053                    return -1;
1054            }
1055    
1056            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1057                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1058                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1059    
1060            return page_count;
1061    }
1062    
1063    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1064    {
1065            if (p_section == NULL)
1066            {
1067                    log_error("NULL pointer error\n");
1068                    return -1;
1069            }
1070    
1071            if (page_id < p_section->page_count - 1)
1072            {
1073                    return BBS_article_limit_per_page;
1074            }
1075            else // if (page_id >= p_section->page_count - 1)
1076            {
1077                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1078                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1079            }
1080    }
1081    
1082  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)
1083  {  {
1084          ARTICLE *p_article;          ARTICLE *p_article;
# Line 837  ARTICLE *section_list_find_article_with_ Line 1092  ARTICLE *section_list_find_article_with_
1092    
1093          if (p_section == NULL)          if (p_section == NULL)
1094          {          {
1095                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1096                  return NULL;                  return NULL;
1097          }          }
1098    
# Line 929  int section_list_calculate_page(SECTION_ Line 1184  int section_list_calculate_page(SECTION_
1184    
1185          if (p_section == NULL)          if (p_section == NULL)
1186          {          {
1187                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1188                  return -1;                  return -1;
1189          }          }
1190    
# Line 1023  int section_list_calculate_page(SECTION_ Line 1278  int section_list_calculate_page(SECTION_
1278          return 0;          return 0;
1279  }  }
1280    
1281    int32_t article_block_last_aid(void)
1282    {
1283            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1284            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1285    
1286            return last_aid;
1287    }
1288    
1289    int article_block_article_count(void)
1290    {
1291            int ret;
1292    
1293            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1294            {
1295                    return -1;
1296            }
1297    
1298            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1299                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1300    
1301            return ret;
1302    }
1303    
1304  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1305  {  {
1306          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1063  int section_list_move_topic(SECTION_LIST Line 1341  int section_list_move_topic(SECTION_LIST
1341          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1342          int move_counter;          int move_counter;
1343    
1344          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1345          {          {
1346                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1347                    return -1;
1348            }
1349    
1350            if (p_section_src->sid == p_section_dest->sid)
1351            {
1352                    log_error("section_list_move_topic() src and dest section are the same\n");
1353                  return -1;                  return -1;
1354          }          }
1355    
1356          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1357          {          {
1358                  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);
1359                  return -2;                  return -2;
1360          }          }
1361    
# Line 1112  int section_list_move_topic(SECTION_LIST Line 1396  int section_list_move_topic(SECTION_LIST
1396          {          {
1397                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1398                  {                  {
1399                          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",
1400                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1401                          return -2;                          p_article = p_article->p_topic_next;
1402                            continue;
1403                  }                  }
1404    
1405                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1140  int section_list_move_topic(SECTION_LIST Line 1425  int section_list_move_topic(SECTION_LIST
1425    
1426                  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)
1427                  {                  {
1428                          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);
1429                          return -4;                          p_article = p_article->p_topic_next;
1430                            continue;
1431                  }                  }
1432    
1433                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1219  int section_list_move_topic(SECTION_LIST Line 1505  int section_list_move_topic(SECTION_LIST
1505    
1506          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)
1507          {          {
1508                  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",
1509                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1510          }          }
1511    
# Line 1429  int section_list_rd_lock(SECTION_LIST *p Line 1715  int section_list_rd_lock(SECTION_LIST *p
1715                          timer++;                          timer++;
1716                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1717                          {                          {
1718                                  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", timer, sid);
1719                          }                          }
1720                  }                  }
1721                  else // failed                  else // failed
1722                  {                  {
1723                          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);
1724                          break;                          break;
1725                  }                  }
1726          }          }
# Line 1460  int section_list_rw_lock(SECTION_LIST *p Line 1746  int section_list_rw_lock(SECTION_LIST *p
1746                          timer++;                          timer++;
1747                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1748                          {                          {
1749                                  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", timer, sid);
1750                          }                          }
1751                  }                  }
1752                  else // failed                  else // failed
1753                  {                  {
1754                          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);
1755                          break;                          break;
1756                  }                  }
1757          }          }


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

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