/[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.45 by sysadm, Tue Oct 21 06:24:51 2025 UTC Revision 1.57 by sysadm, Sun Nov 16 00:10:48 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                             section_list.c  -  description  /*
3                                                           -------------------   * section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "log.h"  #include "log.h"
14  #include "section_list.h"  #include "section_list.h"
15  #include "trie_dict.h"  #include "trie_dict.h"
16    #include "user_list.h"
17  #include <errno.h>  #include <errno.h>
18  #include <signal.h>  #include <signal.h>
19  #include <stdio.h>  #include <stdio.h>
# Line 39  union semun Line 36  union semun
36  };  };
37  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
38    
39  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second  enum _section_list_constant_t
40  #define SECTION_TRY_LOCK_TIMES 10  {
41            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
42            SECTION_TRY_LOCK_TIMES = 10,
43    
44  #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate          ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
45  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)          ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
46  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)          ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
47    
48  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
49    
50  #define SID_STR_LEN 5 // 32-bit + NULL          SID_STR_LEN = 5, // 32-bit + NULL
51    };
52    
53  struct article_block_t  struct article_block_t
54  {  {
55          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
56          int article_count;          int article_count;
57          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
58  };  };
# Line 215  void article_block_cleanup(void) Line 215  void article_block_cleanup(void)
215                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216          }          }
217    
218          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
219          {          {
220                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221          }          }
# Line 342  ARTICLE *article_block_find_by_aid(int32 Line 342  ARTICLE *article_block_find_by_aid(int32
342          }          }
343    
344          left = 0;          left = 0;
345          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
346    
347          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of blocks[left], tail aid of blocks[right] ]
348          while (left < right - 1)          while (left < right)
349          {          {
350                  // get block offset no less than mid value of left and right block offsets                  // get block offset no less than mid value of left and right block offsets
351                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_article_block_pool->block_count)  
                 {  
                         log_error("block(mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
352    
353                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
354                  {                  {
355                          right = mid;                          right = mid - 1;
356                  }                  }
357                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
358                  {                  {
359                          left = mid;                          left = mid;
360                  }                  }
# Line 404  ARTICLE *article_block_find_by_index(int Line 398  ARTICLE *article_block_find_by_index(int
398                  return NULL;                  return NULL;
399          }          }
400    
401          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
402          {          {
403                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
404                  return NULL;                  return NULL;
405          }          }
406    
407          p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];          p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block];
408    
409          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
410          {          {
411                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
412                  return NULL;                  return NULL;
413          }          }
414    
415          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
416  }  }
417    
418  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 536  void section_list_cleanup(void) Line 530  void section_list_cleanup(void)
530                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
531          }          }
532    
533          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
534          {          {
535                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
536          }          }
# Line 780  int section_list_append_article(SECTION_ Line 774  int section_list_append_article(SECTION_
774          }          }
775    
776          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
777                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block)
778          {          {
779                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
780                  {                  {
# Line 790  int section_list_append_article(SECTION_ Line 784  int section_list_append_article(SECTION_
784    
785                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
786                  {                  {
787                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid;
788                  }                  }
789    
790                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
# Line 869  int section_list_append_article(SECTION_ Line 863  int section_list_append_article(SECTION_
863          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
864    
865          // Update page          // Update page
866          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||          if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) ||
867                  p_section->article_count == 1)                  p_section->page_count == 0)
868          {          {
869                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
870                  p_section->page_count++;                  p_section->page_count++;
# Line 925  int section_list_set_article_visible(SEC Line 919  int section_list_set_article_visible(SEC
919          {          {
920                  p_section->visible_article_count--;                  p_section->visible_article_count--;
921    
922                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
923                    {
924                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
925                    }
926    
927                  if (p_article->tid == 0)                  if (p_article->tid == 0)
928                  {                  {
929                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 943  int section_list_set_article_visible(SEC Line 942  int section_list_set_article_visible(SEC
942                                          p_reply->visible = 0;                                          p_reply->visible = 0;
943                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
944                                          affected_count++;                                          affected_count++;
945    
946                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
947                                            {
948                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
949                                            }
950                                  }                                  }
951                          }                          }
952                  }                  }
# Line 955  int section_list_set_article_visible(SEC Line 959  int section_list_set_article_visible(SEC
959                  {                  {
960                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
961                  }                  }
962    
963                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
964                    {
965                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
966                    }
967          }          }
968    
969          p_article->visible = visible;          p_article->visible = visible;
# Line 1053  int section_list_page_count_with_ontop(S Line 1062  int section_list_page_count_with_ontop(S
1062                  return -1;                  return -1;
1063          }          }
1064    
1065          page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +          page_count = p_section->page_count - 1 +
1066                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1067                                   ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);                                           BBS_article_limit_per_page;
1068    
1069            if (page_count < 0)
1070            {
1071                    page_count = 0;
1072            }
1073    
1074          return page_count;          return page_count;
1075  }  }
# Line 1074  int section_list_page_article_count_with Line 1088  int section_list_page_article_count_with
1088          }          }
1089          else // if (page_id >= p_section->page_count - 1)          else // if (page_id >= p_section->page_count - 1)
1090          {          {
1091                  return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -                  return MIN(MAX(0,
1092                                             BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));                                             (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1093                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1094                                       BBS_article_limit_per_page);
1095          }          }
1096  }  }
1097    
# Line 1104  ARTICLE *section_list_find_article_with_ Line 1120  ARTICLE *section_list_find_article_with_
1120          }          }
1121    
1122          left = 0;          left = 0;
1123          right = p_section->page_count;          right = p_section->page_count - 1;
1124    
1125          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right] ]
1126          while (left < right - 1)          while (left < right)
1127          {          {
1128                  // get page id no less than mid value of left page id and right page id                  // get page id no less than mid value of left page id and right page id
1129                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_section->page_count)  
                 {  
                         log_error("page id (mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
1130    
1131                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1132                  {                  {
1133                          right = mid;                          right = mid - 1;
1134                  }                  }
1135                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1136                  {                  {
1137                          left = mid;                          left = mid;
1138                  }                  }
# Line 1273  int section_list_calculate_page(SECTION_ Line 1283  int section_list_calculate_page(SECTION_
1283          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1284    
1285          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1286          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1287                                                                                                              ? visible_article_count
1288                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1289    
1290          return 0;          return 0;
1291  }  }
# Line 1295  int article_block_article_count(void) Line 1307  int article_block_article_count(void)
1307                  return -1;                  return -1;
1308          }          }
1309    
1310          ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +          ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1311                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1312    
1313          return ret;          return ret;
# Line 1556  int get_section_index(SECTION_LIST *p_se Line 1568  int get_section_index(SECTION_LIST *p_se
1568          return index;          return index;
1569  }  }
1570    
1571    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1572    {
1573            if (p_section == NULL)
1574            {
1575                    log_error("NULL pointer error\n");
1576                    return -1;
1577            }
1578    
1579            if (section_list_rd_lock(p_section) < 0)
1580            {
1581                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1582                    return -2;
1583            }
1584    
1585            if (sname != NULL)
1586            {
1587                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1588            }
1589            if (stitle != NULL)
1590            {
1591                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1592            }
1593            if (master_list != NULL)
1594            {
1595                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1596            }
1597    
1598            // release lock of section
1599            if (section_list_rd_unlock(p_section) < 0)
1600            {
1601                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1602                    return -2;
1603            }
1604    
1605            return 0;
1606    }
1607    
1608  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1609  {  {
1610          int index;          int index;


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

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