/[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.1 by sysadm, Wed May 21 04:07:42 2025 UTC Revision 1.20 by sysadm, Sun May 25 08:15:03 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _GNU_SOURCE
18    
19  #include "section_list.h"  #include "section_list.h"
20  #include "log.h"  #include "log.h"
 #include "io.h"  
21  #include "trie_dict.h"  #include "trie_dict.h"
22  #include <stdio.h>  #include <stdio.h>
23  #include <string.h>  #include <string.h>
# Line 24  Line 25 
25  #include <unistd.h>  #include <unistd.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <errno.h>  #include <errno.h>
28    #include <sys/param.h>
29    #include <sys/sem.h>
30  #include <sys/shm.h>  #include <sys/shm.h>
31  #include <sys/ipc.h>  #include <sys/ipc.h>
32    
33  #define ARTICLE_BLOCK_PER_SHM 50 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #ifdef _SEM_SEMUN_UNDEFINED
34  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256  union semun
35    {
36            int val;                           /* Value for SETVAL */
37            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
38            unsigned short *array; /* Array for GETALL, SETALL */
39            struct seminfo *__buf; /* Buffer for IPC_INFO
40                                                              (Linux-specific) */
41    };
42    #endif // #ifdef _SEM_SEMUN_UNDEFINED
43    
44    #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
45    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // 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)
47    
48    #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic
49    
50    #define SID_STR_LEN 5 // 32-bit + NULL
51    
52    struct article_block_t
53    {
54            ARTICLE articles[ARTICLE_PER_BLOCK];
55            int32_t article_count;
56            struct article_block_t *p_next_block;
57    };
58    typedef struct article_block_t ARTICLE_BLOCK;
59    
60  struct article_block_shm_t  struct article_block_shm_t
61  {  {
# Line 37  struct article_block_shm_t Line 64  struct article_block_shm_t
64  };  };
65  typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;
66    
67  static ARTICLE_BLOCK_SHM *p_article_block_shm_pool;  struct article_block_pool_t
68  static int article_block_shm_count;  {
69  static ARTICLE_BLOCK *p_article_block_free_list;          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
70            int shm_count;
71  static SECTION_DATA *p_section_data_pool;          ARTICLE_BLOCK *p_block_free_list;
72  static int section_data_count;          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
73  static TRIE_NODE *p_trie_dict_section_data;          int32_t block_count;
74    };
75    typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
76    
77    static int article_block_pool_shmid;
78    static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
79    
80    static int section_list_pool_semid;
81    static int section_list_pool_shmid;
82    static SECTION_LIST *p_section_list_pool = NULL;
83    
84  int section_data_pool_init(const char *filename, int article_block_count)  static int section_list_count = 0;
85    static TRIE_NODE *p_trie_dict_section_by_name = NULL;
86    static TRIE_NODE *p_trie_dict_section_by_sid = NULL;
87    
88    int article_block_init(const char *filename, int block_count)
89  {  {
90          int shmid;          int shmid;
91          int proj_id;          int proj_id;
# Line 53  int section_data_pool_init(const char *f Line 93  int section_data_pool_init(const char *f
93          size_t size;          size_t size;
94          void *p_shm;          void *p_shm;
95          int i;          int i;
96          int article_block_count_in_shm;          int block_count_in_shm;
97          ARTICLE_BLOCK *p_article_block_in_shm;          ARTICLE_BLOCK *p_block_in_shm;
98          ARTICLE_BLOCK **pp_article_block_next;          ARTICLE_BLOCK **pp_block_next;
99    
100          if (p_article_block_shm_pool != NULL ||          if (p_article_block_pool != NULL)
                 p_article_block_free_list != NULL ||  
                 p_section_data_pool != NULL ||  
                 p_trie_dict_section_data != NULL)  
101          {          {
102                  log_error("section_data_pool already initialized\n");                  log_error("article_block_pool already initialized\n");
103                  return -1;                  return -1;
104          }          }
105    
106          if (article_block_count > ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)          if (block_count > ARTICLE_BLOCK_PER_POOL)
107          {          {
108                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
109                  return -2;                  return -2;
110          }          }
111    
112          p_article_block_shm_pool = calloc((size_t)article_block_count / ARTICLE_BLOCK_PER_SHM + 1, sizeof(ARTICLE_BLOCK_SHM));          // Allocate shared memory
113          if (p_article_block_shm_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
114            key = ftok(filename, proj_id);
115            if (key == -1)
116          {          {
117                  log_error("calloc(%d ARTICLE_BLOCK_SHM) OOM\n", article_block_count / ARTICLE_BLOCK_PER_SHM + 1);                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
118                  return -2;                  return -3;
119          }          }
120    
121          p_section_data_pool = calloc(BBS_max_section, sizeof(SECTION_DATA));          size = sizeof(ARTICLE_BLOCK_POOL);
122          if (p_section_data_pool == NULL)          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
123            if (shmid == -1)
124          {          {
125                  log_error("calloc(%d SECTION_DATA) OOM\n", BBS_max_section);                  log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
126                  return -2;                  return -3;
127          }          }
128          section_data_count = 0;          p_shm = shmat(shmid, NULL, 0);
129            if (p_shm == (void *)-1)
         p_trie_dict_section_data = trie_dict_create();  
         if (p_trie_dict_section_data == NULL)  
130          {          {
131                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
132                  return -2;                  return -3;
133          }          }
134    
135          // Allocate shared memory          article_block_pool_shmid = shmid;
136          article_block_shm_count = 0;          p_article_block_pool = p_shm;
137          pp_article_block_next = &p_article_block_free_list;  
138            p_article_block_pool->shm_count = 0;
139            pp_block_next = &(p_article_block_pool->p_block_free_list);
140    
141          while (article_block_count > 0)          while (block_count > 0)
142          {          {
143                  article_block_count_in_shm =                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
144                          (article_block_count < ARTICLE_BLOCK_PER_SHM ? article_block_count : ARTICLE_BLOCK_PER_SHM);                  block_count -= block_count_in_shm;
                 article_block_count -= article_block_count_in_shm;  
145    
146                  proj_id = getpid() + article_block_shm_count;                  proj_id = p_article_block_pool->shm_count;
147                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
148                  if (key == -1)                  if (key == -1)
149                  {                  {
150                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
151                          section_data_pool_cleanup();                          article_block_cleanup();
152                          return -3;                          return -3;
153                  }                  }
154    
155                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)article_block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
156                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
157                  if (shmid == -1)                  if (shmid == -1)
158                  {                  {
159                          log_error("shmget(shm_index = %d, size = %d) error (%d)\n", article_block_shm_count, size, errno);                          log_error("shmget(shm_index = %d, size = %d) error (%d)\n", p_article_block_pool->shm_count, size, errno);
160                          section_data_pool_cleanup();                          article_block_cleanup();
161                          return -3;                          return -3;
162                  }                  }
163                  p_shm = shmat(shmid, NULL, 0);                  p_shm = shmat(shmid, NULL, 0);
164                  if (p_shm == (void *)-1)                  if (p_shm == (void *)-1)
165                  {                  {
166                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
167                          section_data_pool_cleanup();                          article_block_cleanup();
168                          return -3;                          return -3;
169                  }                  }
170    
171                  (p_article_block_shm_pool + article_block_shm_count)->shmid = shmid;                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid;
172                  (p_article_block_shm_pool + article_block_shm_count)->p_shm = p_shm;                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm;
173                  article_block_shm_count++;                  p_article_block_pool->shm_count++;
174    
175                  p_article_block_in_shm = p_shm;                  p_block_in_shm = p_shm;
176                  *pp_article_block_next = p_article_block_in_shm;                  *pp_block_next = p_block_in_shm;
177    
178                  for (i = 0; i < article_block_count_in_shm; i++)                  for (i = 0; i < block_count_in_shm; i++)
179                  {                  {
180                          if (i < article_block_count_in_shm - 1)                          if (i < block_count_in_shm - 1)
181                          {                          {
182                                  (p_article_block_in_shm + i)->p_next_block = (p_article_block_in_shm + i + 1);                                  (p_block_in_shm + i)->p_next_block = (p_block_in_shm + i + 1);
183                          }                          }
184                          else                          else
185                          {                          {
186                                  (p_article_block_in_shm + i)->p_next_block = NULL;                                  (p_block_in_shm + i)->p_next_block = NULL;
187                                  pp_article_block_next = &((p_article_block_in_shm + i)->p_next_block);                                  pp_block_next = &((p_block_in_shm + i)->p_next_block);
188                          }                          }
189                  }                  }
190          }          }
191    
192            p_article_block_pool->block_count = 0;
193    
194          return 0;          return 0;
195  }  }
196    
197  void section_data_pool_cleanup(void)  void article_block_cleanup(void)
198  {  {
199          int i;          if (p_article_block_pool == NULL)
200            {
201                    return;
202            }
203    
204            for (int i = 0; i < p_article_block_pool->shm_count; i++)
205            {
206                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
207                    {
208                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
209                    }
210    
211          if (p_trie_dict_section_data != NULL)                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
212                    {
213                            log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
214                    }
215            }
216    
217            if (shmdt(p_article_block_pool) == -1)
218            {
219                    log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno);
220            }
221    
222            if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1)
223            {
224                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno);
225            }
226    
227            p_article_block_pool = NULL;
228    }
229    
230    inline static ARTICLE_BLOCK *pop_free_article_block(void)
231    {
232            ARTICLE_BLOCK *p_block = NULL;
233    
234            if (p_article_block_pool->p_block_free_list != NULL)
235            {
236                    p_block = p_article_block_pool->p_block_free_list;
237                    p_article_block_pool->p_block_free_list = p_block->p_next_block;
238                    p_block->p_next_block = NULL;
239                    p_block->article_count = 0;
240            }
241    
242            return p_block;
243    }
244    
245    inline static void push_free_article_block(ARTICLE_BLOCK *p_block)
246    {
247            p_block->p_next_block = p_article_block_pool->p_block_free_list;
248            p_article_block_pool->p_block_free_list = p_block;
249    }
250    
251    int article_block_reset(void)
252    {
253            ARTICLE_BLOCK *p_block;
254    
255            if (p_article_block_pool == NULL)
256            {
257                    log_error("article_block_pool not initialized\n");
258                    return -1;
259            }
260    
261            while (p_article_block_pool->block_count > 0)
262          {          {
263                  trie_dict_destroy(p_trie_dict_section_data);                  p_article_block_pool->block_count--;
264                  p_trie_dict_section_data = NULL;                  p_block = p_article_block_pool->p_block[p_article_block_pool->block_count];
265                  section_data_count = 0;                  push_free_article_block(p_block);
266          }          }
267    
268          if (p_section_data_pool != NULL)          return 0;
269    }
270    
271    ARTICLE *article_block_find_by_aid(int32_t aid)
272    {
273            ARTICLE_BLOCK *p_block;
274            int left;
275            int right;
276            int mid;
277    
278            if (p_article_block_pool == NULL)
279          {          {
280                  free(p_section_data_pool);                  log_error("article_block_pool not initialized\n");
281                  p_section_data_pool = NULL;                  return NULL;
282          }          }
283    
284          if (p_article_block_free_list != NULL)          if (p_article_block_pool->block_count == 0) // empty
285          {          {
286                  p_article_block_free_list = NULL;                  return NULL;
287          }          }
288    
289          if (p_article_block_shm_pool != NULL)          left = 0;
290            right = p_article_block_pool->block_count;
291    
292            // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]
293            while (left < right - 1)
294          {          {
295                  for (i = 0; i < article_block_shm_count; i++)                  // get block offset no less than mid value of left and right block offsets
296                    mid = (left + right) / 2 + (right - left) % 2;
297    
298                    if (mid >= p_article_block_pool->block_count)
299                  {                  {
300                          if (shmdt((p_article_block_shm_pool + i)->p_shm) == -1)                          log_error("block(mid = %d) is out of boundary\n", mid);
301                          {                          return NULL;
302                                  log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_shm_pool + i)->shmid, errno);                  }
                         }  
303    
304                          if (shmctl((p_article_block_shm_pool + i)->shmid, IPC_RMID, NULL) == -1)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
305                          {                  {
306                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_shm_pool + i)->shmid, errno);                          right = mid;
307                          }                  }
308                    else
309                    {
310                            left = mid;
311                    }
312            }
313    
314            p_block = p_article_block_pool->p_block[left];
315    
316            left = 0;
317            right = p_block->article_count - 1;
318    
319            // aid in the range [ aid of articles[left], aid of articles[right] ]
320            while (left < right)
321            {
322                    mid = (left + right) / 2;
323    
324                    if (aid <= p_block->articles[mid].aid)
325                    {
326                            right = mid;
327                    }
328                    else
329                    {
330                            left = mid + 1;
331                  }                  }
332            }
333    
334            return (p_block->articles + left);
335    }
336    
337                  p_article_block_shm_pool = NULL;  ARTICLE *article_block_find_by_index(int index)
338                  article_block_shm_count = 0;  {
339            ARTICLE_BLOCK *p_block;
340    
341            if (p_article_block_pool == NULL)
342            {
343                    log_error("article_block_pool not initialized\n");
344                    return NULL;
345            }
346    
347            if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)
348            {
349                    log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
350                    return NULL;
351          }          }
352    
353            p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];
354    
355            if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
356            {
357                    log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
358                    return NULL;
359            }
360    
361            return (p_block->articles + (index % ARTICLE_PER_BLOCK));
362  }  }
363    
364  inline static ARTICLE_BLOCK *pop_free_article_block(void)  extern int section_list_init(const char *filename)
365  {  {
366          ARTICLE_BLOCK *p_article_block = NULL;          int semid;
367            int shmid;
368            int proj_id;
369            key_t key;
370            size_t size;
371            void *p_shm;
372            union semun arg;
373            int i;
374    
375            if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)
376            {
377                    section_list_cleanup();
378            }
379    
380            proj_id = (int)(time(NULL) % getpid());
381            key = ftok(filename, proj_id);
382            if (key == -1)
383            {
384                    log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
385                    return -3;
386            }
387    
388            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
389            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
390            if (semid == -1)
391            {
392                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
393                    return -3;
394            }
395    
396            // Initialize sem value to 0
397            arg.val = 0;
398            for (i = 0; i < size; i++)
399            {
400                    if (semctl(semid, i, SETVAL, arg) == -1)
401                    {
402                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
403                            return -3;
404                    }
405            }
406    
407            section_list_pool_semid = semid;
408    
409            size = sizeof(SECTION_LIST) * BBS_max_section;
410            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
411            if (shmid == -1)
412            {
413                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
414                    return -3;
415            }
416            p_shm = shmat(shmid, NULL, 0);
417            if (p_shm == (void *)-1)
418            {
419                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
420                    return -3;
421            }
422    
423            section_list_pool_shmid = shmid;
424            p_section_list_pool = p_shm;
425            section_list_count = 0;
426    
427            p_trie_dict_section_by_name = trie_dict_create();
428            if (p_trie_dict_section_by_name == NULL)
429            {
430                    log_error("trie_dict_create() OOM\n", BBS_max_section);
431                    return -2;
432            }
433    
434          if (p_article_block_free_list != NULL)          p_trie_dict_section_by_sid = trie_dict_create();
435            if (p_trie_dict_section_by_sid == NULL)
436          {          {
437                  p_article_block = p_article_block_free_list;                  log_error("trie_dict_create() OOM\n", BBS_max_section);
438                  p_article_block_free_list = p_article_block_free_list->p_next_block;                  return -2;
439          }          }
440    
441          return p_article_block;          return 0;
442  }  }
443    
444  inline static void push_free_article_block(ARTICLE_BLOCK *p_article_block)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
445  {  {
446          p_article_block->p_next_block = p_article_block_free_list;          uint32_t u_sid;
447          p_article_block_free_list = p_article_block;          int i;
448    
449            u_sid = (uint32_t)sid;
450            for (i = 0; i < SID_STR_LEN - 1; i++)
451            {
452                    p_sid_str[i] = (char)(u_sid % 255 + 1);
453                    u_sid /= 255;
454            }
455            p_sid_str[i] = '\0';
456  }  }
457    
458  SECTION_DATA *section_data_create(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_name)
459  {  {
460          SECTION_DATA *p_section;          SECTION_LIST *p_section;
461          int index;          char sid_str[SID_STR_LEN];
462    
463          if (p_section_data_pool == NULL || p_trie_dict_section_data == NULL)          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)
464          {          {
465                  log_error("section_data not initialized\n");                  log_error("session_list_pool not initialized\n");
466                  return NULL;                  return NULL;
467          }          }
468    
469          if (section_data_count >= BBS_max_section)          if (section_list_count >= BBS_max_section)
470          {          {
471                  log_error("section_data_count exceed limit %d\n", BBS_max_section);                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);
472                  return NULL;                  return NULL;
473          }          }
474    
475          index = section_data_count;          sid_to_str(sid, sid_str);
476          p_section = p_section_data_pool + index;  
477            p_section = p_section_list_pool + section_list_count;
478    
479            p_section->sid = sid;
480    
481          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));
482          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname - 1)] = '\0';
# Line 243  SECTION_DATA *section_data_create(const Line 487  SECTION_DATA *section_data_create(const
487          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));
488          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
489    
490          p_section->p_head_block = NULL;          if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1)
491          p_section->p_tail_block = NULL;          {
492          p_section->block_count = 0;                  log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count);
493          p_section->article_count = 0;                  return NULL;
494          p_section->delete_count = 0;          }
495    
496          if (trie_dict_set(p_trie_dict_section_data, sname, index) != 1)          if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1)
497          {          {
498                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, index);                  log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count);
499                  return NULL;                  return NULL;
500          }          }
501    
502          section_data_count++;          section_list_reset_articles(p_section);
503    
504            section_list_count++;
505    
506          return p_section;          return p_section;
507  }  }
508    
509  int section_data_free_block(SECTION_DATA *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
510  {  {
511          ARTICLE_BLOCK *p_block;          p_section->article_count = 0;
512            p_section->topic_count = 0;
513            p_section->visible_article_count = 0;
514            p_section->visible_topic_count = 0;
515            p_section->p_article_head = NULL;
516            p_section->p_article_tail = NULL;
517    
518          if (p_section == NULL)          p_section->page_count = 0;
519            p_section->last_page_visible_article_count = 0;
520    }
521    
522    void section_list_cleanup(void)
523    {
524            if (p_trie_dict_section_by_name != NULL)
525          {          {
526                  log_error("section_data_free_block() NULL pointer error\n");                  trie_dict_destroy(p_trie_dict_section_by_name);
527                  return -1;                  p_trie_dict_section_by_name = NULL;
528          }          }
529    
530          if (p_section_data_pool == NULL)          if (p_trie_dict_section_by_sid != NULL)
531          {          {
532                  log_error("section_data not initialized\n");                  trie_dict_destroy(p_trie_dict_section_by_sid);
533                  return -1;                  p_trie_dict_section_by_sid = NULL;
534          }          }
535    
536          while (p_section->p_head_block != NULL)          if (p_section_list_pool != NULL)
537          {          {
538                  p_block = p_section->p_head_block;                  if (shmdt(p_section_list_pool) == -1)
539                  p_section->p_head_block = p_block->p_next_block;                  {
540                  push_free_article_block(p_block);                          log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);
541                    }
542                    p_section_list_pool = NULL;
543    
544                    if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)
545                    {
546                            log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);
547                    }
548    
549                    if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1)
550                    {
551                            log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno);
552                    }
553          }          }
554    
555          p_section->p_tail_block = NULL;          section_list_count = 0;
556          p_section->block_count = 0;  }
         p_section->article_count = 0;  
         p_section->delete_count = 0;  
557    
558          return 0;  SECTION_LIST *section_list_find_by_name(const char *sname)
559    {
560            int64_t index;
561    
562            if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)
563            {
564                    log_error("section_list not initialized\n");
565                    return NULL;
566            }
567    
568            if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1)
569            {
570                    log_error("trie_dict_get(section, %s) error\n", sname);
571                    return NULL;
572            }
573    
574            return (p_section_list_pool + index);
575  }  }
576    
577  SECTION_DATA *section_data_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
578  {  {
579          int64_t index;          int64_t index;
580            char sid_str[SID_STR_LEN];
581    
582          if (p_section_data_pool == NULL || p_trie_dict_section_data == NULL)          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)
583          {          {
584                  log_error("section_data not initialized\n");                  log_error("section_list not initialized\n");
585                  return NULL;                  return NULL;
586          }          }
587    
588          if (trie_dict_get(p_trie_dict_section_data, sname, &index) != 1)          sid_to_str(sid, sid_str);
589    
590            if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1)
591          {          {
592                  log_error("trie_dict_get(section_data, %s) error\n", sname);                  log_error("trie_dict_get(section, %d) error\n", sid);
593                  return NULL;                  return NULL;
594          }          }
595    
596          return (p_section_data_pool + index);          return (p_section_list_pool + index);
597  }  }
598    
599  int section_data_append_article(SECTION_DATA *p_section, const ARTICLE *p_article)  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
600  {  {
601          ARTICLE_BLOCK *p_block;          ARTICLE_BLOCK *p_block;
602          int32_t last_aid = 0;          int32_t last_aid = 0;
603            ARTICLE *p_article;
604          ARTICLE *p_topic_head;          ARTICLE *p_topic_head;
605          ARTICLE *p_topic_tail;          ARTICLE *p_topic_tail;
606    
607          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article_src == NULL)
608          {          {
609                  log_error("section_data_append_article() NULL pointer error\n");                  log_error("section_list_append_article() NULL pointer error\n");
610                  return -1;                  return -1;
611          }          }
612    
613          if (p_section_data_pool == NULL)          if (p_article_block_pool == NULL)
614          {          {
615                  log_error("section_data not initialized\n");                  log_error("article_block_pool not initialized\n");
616                  return -1;                  return -1;
617          }          }
618    
619          if (p_section->p_tail_block == NULL || p_section->p_tail_block->article_count >= BBS_article_limit_per_block)          if (p_section->sid != p_article_src->sid)
620          {          {
621                  if (p_section->block_count >= BBS_article_block_limit_per_section)                  log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
622                  {                  return -2;
623                          log_error("section block count %d reach limit\n", p_section->block_count);          }
                         return -2;  
                 }  
624    
625            if (p_section->article_count >= BBS_article_limit_per_section)
626            {
627                    log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
628                    return -2;
629            }
630    
631            if (p_article_block_pool->block_count == 0 ||
632                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)
633            {
634                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
635                  {                  {
636                          log_error("pop_free_article_block() error\n");                          log_error("pop_free_article_block() error\n");
637                          return -2;                          return -2;
638                  }                  }
639    
640                  p_block->article_count = 0;                  if (p_article_block_pool->block_count > 0)
                 p_block->p_next_block = NULL;  
   
                 if (p_section->p_tail_block == NULL)  
                 {  
                         p_section->p_head_block = p_block;  
                         last_aid = 0;  
                 }  
                 else  
641                  {                  {
642                          p_section->p_tail_block->p_next_block = p_block;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;
                         last_aid = p_section->p_tail_block->articles[BBS_article_limit_per_block - 1].aid;  
643                  }                  }
644                  p_section->p_tail_block = p_block;  
645                  p_section->p_block[p_section->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
646                  p_section->block_count++;                  p_article_block_pool->block_count++;
647          }          }
648          else          else
649          {          {
650                  p_block = p_section->p_tail_block;                  p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
651                  last_aid = p_block->articles[p_block->article_count - 1].aid;                  last_aid = p_block->articles[p_block->article_count - 1].aid;
652          }          }
653    
654          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
655          if (p_article->aid <= last_aid)          if (p_article_src->aid <= last_aid)
656          {          {
657                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
658                  return -3;                  return -3;
659          }          }
660    
661          if (p_block->article_count == 0)          p_article = (p_block->articles + p_block->article_count);
662            p_block->article_count++;
663            p_section->article_count++;
664    
665            // Copy article data
666            *p_article = *p_article_src;
667    
668            if (p_article->visible)
669          {          {
670                  p_section->block_head_aid[p_section->block_count - 1] = p_article->aid;                  p_section->visible_article_count++;
671          }          }
672    
673            // Link appended article as tail node of topic bi-directional list
674          if (p_article->tid != 0)          if (p_article->tid != 0)
675          {          {
676                  p_topic_head = section_data_search_article(p_section, p_article->tid);                  p_topic_head = article_block_find_by_aid(p_article->tid);
677                  if (p_topic_head == NULL)                  if (p_topic_head == NULL)
678                  {                  {
679                          log_error("search head of topic (aid=%d) error\n", p_article->tid);                          log_error("search head of topic (aid=%d) error\n", p_article->tid);
680                          return -4;                          return -4;
681                  }                  }
682    
683                  p_topic_tail = section_data_search_article(p_section, p_topic_head->prior_aid);                  p_topic_tail = p_topic_head->p_topic_prior;
684                  if (p_topic_tail == NULL)                  if (p_topic_tail == NULL)
685                  {                  {
686                          log_error("search tail of topic (aid=%d) error\n", p_topic_head->prior_aid);                          log_error("tail of topic (aid=%d) is NULL\n", p_article->tid);
687                          return -4;                          return -4;
688                  }                  }
689          }          }
690          else          else
691          {          {
692                  p_topic_head = &(p_block->articles[p_block->article_count]);                  p_section->topic_count++;
693                  p_topic_tail = p_topic_head;  
694                    if (p_article->visible)
695                    {
696                            p_section->visible_topic_count++;
697                    }
698    
699                    p_topic_head = p_article;
700                    p_topic_tail = p_article;
701          }          }
702    
703          // Copy article data          p_article->p_topic_prior = p_topic_tail;
704          p_block->articles[p_block->article_count] = *p_article;          p_article->p_topic_next = p_topic_head;
705            p_topic_head->p_topic_prior = p_article;
706            p_topic_tail->p_topic_next = p_article;
707    
708          // Link appended article as tail node of topic bi-directional list;          // Link appended article as tail node of article bi-directional list
709          p_block->articles[p_block->article_count].prior_aid = p_topic_tail->aid;          if (p_section->p_article_head == NULL)
710          p_block->articles[p_block->article_count].next_aid = p_topic_head->aid;          {
711          p_topic_head->prior_aid = p_article->aid;                  p_section->p_article_head = p_article;
712          p_topic_tail->next_aid = p_article->aid;                  p_section->p_article_tail = p_article;
713            }
714            p_article->p_prior = p_section->p_article_tail;
715            p_article->p_next = p_section->p_article_head;
716            p_section->p_article_head->p_prior = p_article;
717            p_section->p_article_tail->p_next = p_article;
718            p_section->p_article_tail = p_article;
719    
720          p_block->article_count++;          // Update page
721          p_section->article_count++;          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||
722                    p_section->article_count == 1)
723            {
724                    p_section->p_page_first_article[p_section->page_count] = p_article;
725                    p_section->page_count++;
726                    p_section->last_page_visible_article_count = 0;
727            }
728    
729            if (p_article->visible)
730            {
731                    p_section->last_page_visible_article_count++;
732            }
733    
734          return 0;          return 0;
735  }  }
736    
737  ARTICLE *section_data_search_article(SECTION_DATA *p_section, int32_t aid)  int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible)
738    {
739            ARTICLE *p_article;
740            ARTICLE *p_reply;
741            int affected_count = 0;
742    
743            if (p_section == NULL)
744            {
745                    log_error("section_list_set_article_visible() NULL pointer error\n");
746                    return -2;
747            }
748    
749            p_article = article_block_find_by_aid(aid);
750            if (p_article == NULL)
751            {
752                    return -1; // Not found
753            }
754    
755            if (p_section->sid != p_article->sid)
756            {
757                    log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);
758                    return -2;
759            }
760    
761            if (p_article->visible == visible)
762            {
763                    return 0; // Already set
764            }
765    
766            if (visible == 0) // 1 -> 0
767            {
768                    p_section->visible_article_count--;
769    
770                    if (p_article->tid == 0)
771                    {
772                            p_section->visible_topic_count--;
773    
774                            // Set related visible replies to invisible
775                            for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next)
776                            {
777                                    if (p_reply->tid != aid)
778                                    {
779                                            log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);
780                                            continue;
781                                    }
782    
783                                    if (p_reply->visible == 1)
784                                    {
785                                            p_reply->visible = 0;
786                                            p_section->visible_article_count--;
787                                            affected_count++;
788                                    }
789                            }
790                    }
791            }
792            else // 0 -> 1
793            {
794                    p_section->visible_article_count++;
795    
796                    if (p_article->tid == 0)
797                    {
798                            p_section->visible_topic_count++;
799                    }
800            }
801    
802            p_article->visible = visible;
803            affected_count++;
804    
805            return affected_count;
806    }
807    
808    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)
809  {  {
810          ARTICLE *p_article;          ARTICLE *p_article;
         ARTICLE_BLOCK *p_block;  
811          int left;          int left;
812          int right;          int right;
813          int mid;          int mid;
814    
815            *p_page = -1;
816            *p_offset = -1;
817            *pp_next = NULL;
818    
819          if (p_section == NULL)          if (p_section == NULL)
820          {          {
821                  log_error("section_data_append_article() NULL pointer error\n");                  log_error("section_list_find_article_with_offset() NULL pointer error\n");
822                  return NULL;                  return NULL;
823          }          }
824    
825          if (p_section->block_count == 0) // empty section          if (p_section->article_count == 0) // empty
826          {          {
827                    *p_page = 0;
828                    *p_offset = 0;
829                  return NULL;                  return NULL;
830          }          }
831    
832          left = 0;          left = 0;
833          right = p_section->block_count;          right = p_section->page_count;
834    
835          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]
836          while (left < right - 1)          while (left < right - 1)
837          {          {
838                  // get block offset no less than mid value of left and right block offsets                  // get page id no less than mid value of left page id and right page id
839                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (right - left) % 2;
840                    
841                  if (mid >= BBS_article_block_limit_per_section)                  if (mid >= p_section->page_count)
842                  {                  {
843                          log_error("block_m(%d) is out of boundary\n", mid);                          log_error("page id (mid = %d) is out of boundary\n", mid);
844                          return NULL;                          return NULL;
845                  }                  }
846    
847                  if (aid < p_section->block_head_aid[mid])                  if (aid < p_section->p_page_first_article[mid]->aid)
848                  {                  {
849                          right = mid;                          right = mid;
850                  }                  }
# Line 459  ARTICLE *section_data_search_article(SEC Line 854  ARTICLE *section_data_search_article(SEC
854                  }                  }
855          }          }
856    
857          p_block = p_section->p_block[left];          *p_page = left;
858    
859            p_article = p_section->p_page_first_article[*p_page];
860    
861            // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
862            right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
863    
864            // left will be the offset of article found or offset to insert
865          left = 0;          left = 0;
         right = p_block->article_count - 1;  
866    
867          // aid in the range [ aid of articles[left], aid of articles[right] ]          while (aid > p_article->aid)
         while (left < right)  
868          {          {
869                  mid = (left + right) / 2;                  p_article = p_article->p_next;
870                    left++;
871    
872                  if (aid <= p_block->articles[mid].aid)                  if (aid == p_article->aid)
873                  {                  {
874                          right = mid;                          *pp_next = p_article->p_next;
875                            break;
876                  }                  }
877                  else  
878                    // over last article in the page
879                    if (p_article == p_section->p_article_head || p_article->aid >= right)
880                  {                  {
881                          left = mid + 1;                          *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
882                            *p_offset = left;
883                            return NULL; // not found
884                  }                  }
885          }          }
886    
887          p_article = &(p_block->articles[left]);          if (aid < p_article->aid)
888            {
889                    *pp_next = p_article;
890                    p_article = NULL; // not found
891            }
892            else // aid == p_article->aid
893            {
894                    *pp_next = p_article->p_next;
895            }
896    
897            *p_offset = left;
898    
899          return p_article;          return p_article;
900  }  }
901    
902  int section_data_mark_del_article(SECTION_DATA *p_section, int32_t aid)  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
903    {
904            ARTICLE *p_article;
905            ARTICLE *p_next;
906            int32_t page;
907            int32_t offset;
908            int visible_article_count;
909            int page_head_set;
910    
911            if (p_section == NULL)
912            {
913                    log_error("section_list_calculate_page() NULL pointer error\n");
914                    return -1;
915            }
916    
917            if (p_section->article_count == 0) // empty
918            {
919                    p_section->page_count = 0;
920                    p_section->last_page_visible_article_count = 0;
921    
922                    return 0;
923            }
924    
925            if (start_aid > 0)
926            {
927                    p_article = article_block_find_by_aid(start_aid);
928                    if (p_article == NULL)
929                    {
930                            return -1; // Not found
931                    }
932    
933                    if (p_section->sid != p_article->sid)
934                    {
935                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
936                            return -2;
937                    }
938    
939                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
940                    if (p_article == NULL)
941                    {
942                            if (page < 0)
943                            {
944                                    return -1;
945                            }
946                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
947                                              start_aid, p_section->sid);
948                            return -2;
949                    }
950    
951                    if (offset > 0)
952                    {
953                            p_article = p_section->p_page_first_article[page];
954                    }
955            }
956            else
957            {
958                    p_article = p_section->p_article_head;
959                    page = 0;
960                    offset = 0;
961            }
962    
963            visible_article_count = 0;
964            page_head_set = 0;
965    
966            do
967            {
968                    if (!page_head_set && visible_article_count == 0)
969                    {
970                            p_section->p_page_first_article[page] = p_article;
971                            page_head_set = 1;
972                    }
973    
974                    if (p_article->visible)
975                    {
976                            visible_article_count++;
977                    }
978    
979                    p_article = p_article->p_next;
980    
981                    // skip remaining invisible articles
982                    while (p_article->visible == 0 && p_article != p_section->p_article_head)
983                    {
984                            p_article = p_article->p_next;
985                    }
986    
987                    if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
988                    {
989                            page++;
990                            visible_article_count = 0;
991                            page_head_set = 0;
992    
993                            if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
994                            {
995                                    log_error("Count of page exceed limit in section %d\n", p_section->sid);
996                                    break;
997                            }
998                    }
999            } while (p_article != p_section->p_article_head);
1000    
1001            p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1002            p_section->last_page_visible_article_count = visible_article_count;
1003    
1004            return 0;
1005    }
1006    
1007    int article_count_of_topic(int32_t aid)
1008  {  {
1009          ARTICLE *p_article;          ARTICLE *p_article;
1010            int article_count;
1011    
1012          p_article = section_data_search_article(p_section, aid);          p_article = article_block_find_by_aid(aid);
1013          if (p_article == NULL)          if (p_article == NULL)
1014          {          {
1015                  return -1; // Not found                  return 0; // Not found
1016            }
1017    
1018            article_count = 0;
1019    
1020            do
1021            {
1022                    if (p_article->tid != 0 && p_article->tid != aid)
1023                    {
1024                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1025                            break;
1026                    }
1027    
1028                    article_count++;
1029                    p_article = p_article->p_topic_next;
1030            } while (p_article->aid != aid);
1031    
1032            return article_count;
1033    }
1034    
1035    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1036    {
1037            ARTICLE *p_article;
1038            ARTICLE *p_next;
1039            int32_t page;
1040            int32_t offset;
1041            int32_t move_article_count;
1042            int32_t dest_article_count_old;
1043            int32_t last_unaffected_aid_src;
1044            int32_t first_inserted_aid_dest;
1045            int move_counter;
1046    
1047            if (p_section_dest == NULL)
1048            {
1049                    log_error("section_list_move_topic() NULL pointer error\n");
1050                    return -1;
1051            }
1052    
1053            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1054            {
1055                    log_error("section_list_move_topic() error: article %d not found in block\n", aid);
1056                    return -2;
1057            }
1058    
1059            if (p_section_src->sid != p_article->sid)
1060            {
1061                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1062                                      p_section_src->sid, p_article->aid, p_article->sid);
1063                    return -2;
1064          }          }
1065    
1066          if (p_article->visible == 0)          if (p_article->tid != 0)
1067          {          {
1068                  return 0; // Already deleted                  log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1069                    return -2;
1070            }
1071    
1072            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1073    
1074            move_article_count = article_count_of_topic(aid);
1075            if (move_article_count <= 0)
1076            {
1077                    log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);
1078                    return -2;
1079          }          }
1080    
1081          p_article->visible = 0;          if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1082          p_section->delete_count++;          {
1083                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1084                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1085                    return -3;
1086            }
1087    
1088            dest_article_count_old = p_section_dest->article_count;
1089            move_counter = 0;
1090            first_inserted_aid_dest = p_article->aid;
1091    
1092            do
1093            {
1094                    if (p_section_src->sid != p_article->sid)
1095                    {
1096                            log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1097                                              p_section_src->sid, p_article->aid, p_article->sid);
1098                            return -2;
1099                    }
1100    
1101                    // Remove from bi-directional article list of src section
1102                    if (p_section_src->p_article_head == p_article)
1103                    {
1104                            p_section_src->p_article_head = p_article->p_next;
1105                    }
1106                    if (p_section_src->p_article_tail == p_article)
1107                    {
1108                            p_section_src->p_article_tail = p_article->p_prior;
1109                    }
1110                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1111                    {
1112                            p_section_src->p_article_head = NULL;
1113                            p_section_src->p_article_tail = NULL;
1114                    }
1115    
1116                    p_article->p_prior->p_next = p_article->p_next;
1117                    p_article->p_next->p_prior = p_article->p_prior;
1118    
1119                    // Update sid of article
1120                    p_article->sid = p_section_dest->sid;
1121    
1122                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1123                    {
1124                            log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1125                            return -4;
1126                    }
1127    
1128                    // Insert into bi-directional article list of dest section
1129                    if (p_next == NULL) // empty section
1130                    {
1131                            p_section_dest->p_article_head = p_article;
1132                            p_section_dest->p_article_tail = p_article;
1133                            p_article->p_prior = p_article;
1134                            p_article->p_next = p_article;
1135                    }
1136                    else
1137                    {
1138                            if (p_section_dest->p_article_head == p_next)
1139                            {
1140                                    if (p_article->aid < p_next->aid)
1141                                    {
1142                                            p_section_dest->p_article_head = p_article;
1143                                    }
1144                                    else // p_article->aid > p_next->aid
1145                                    {
1146                                            p_section_dest->p_article_tail = p_article;
1147                                    }
1148                            }
1149    
1150                            p_article->p_prior = p_next->p_prior;
1151                            p_article->p_next = p_next;
1152                            p_next->p_prior->p_next = p_article;
1153                            p_next->p_prior = p_article;
1154                    }
1155    
1156                    // Update article / topic counter of src / desc section
1157                    p_section_src->article_count--;
1158                    p_section_dest->article_count++;
1159                    if (p_article->tid == 0)
1160                    {
1161                            p_section_src->topic_count--;
1162                            p_section_dest->topic_count++;
1163                    }
1164    
1165                    // Update visible article / topic counter of src / desc section
1166                    if (p_article->visible)
1167                    {
1168                            p_section_src->visible_article_count--;
1169                            p_section_dest->visible_article_count++;
1170                            if (p_article->tid == 0)
1171                            {
1172                                    p_section_src->visible_topic_count--;
1173                                    p_section_dest->visible_topic_count++;
1174                            }
1175                    }
1176    
1177                    // Update page for empty dest section
1178                    if (p_section_dest->article_count == 1)
1179                    {
1180                            p_section_dest->p_page_first_article[0] = p_article;
1181                            p_section_dest->page_count = 1;
1182                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1183                    }
1184    
1185                    p_article = p_article->p_topic_next;
1186    
1187                    move_counter++;
1188                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1189                    {
1190                            // Re-calculate pages of desc section
1191                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1192                            {
1193                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1194                                                      p_section_dest->sid, first_inserted_aid_dest);
1195                            }
1196    
1197                            first_inserted_aid_dest = p_article->aid;
1198                    }
1199            } while (p_article->aid != aid);
1200    
1201            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1202            {
1203                    log_error("section_list_move_topic() error: count of moved articles %d != %d\n",
1204                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1205            }
1206    
1207            // Re-calculate pages of src section
1208            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1209            {
1210                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1211                                      p_section_src->sid, last_unaffected_aid_src, aid);
1212            }
1213    
1214            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1215            {
1216                    // Re-calculate pages of desc section
1217                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1218                    {
1219                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1220                                              p_section_dest->sid, first_inserted_aid_dest);
1221                    }
1222            }
1223    
1224            return move_article_count;
1225    }
1226    
1227    int get_section_index(SECTION_LIST *p_section)
1228    {
1229            int index;
1230    
1231            if (p_section_list_pool == NULL)
1232            {
1233                    log_error("get_section_index() error: uninitialized\n");
1234                    return -1;
1235            }
1236    
1237            if (p_section == NULL)
1238            {
1239                    index = BBS_max_section;
1240            }
1241            else
1242            {
1243                    index = (int)(p_section - p_section_list_pool);
1244                    if (index < 0 || index >= BBS_max_section)
1245                    {
1246                            log_error("get_section_index(%d) error: index out of range\n", index);
1247                            return -2;
1248                    }
1249            }
1250    
1251            return index;
1252    }
1253    
1254    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1255    {
1256            int index;
1257            struct sembuf sops[4];
1258            struct timespec timeout;
1259            int ret;
1260    
1261            index = get_section_index(p_section);
1262            if (index < 0)
1263            {
1264                    return -2;
1265            }
1266    
1267            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1268            sops[0].sem_op = 0;                                                                // wait until unlocked
1269            sops[0].sem_flg = 0;
1270    
1271            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1272            sops[1].sem_op = 1;                                                        // lock
1273            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1274    
1275            // Read lock on any specific section will also acquire single read lock on "all section"
1276            // so that write lock on all section only need to acquire single write on on "all section"
1277            // rather than to acquire multiple write locks on all the available sections.
1278            if (index == BBS_max_section)
1279            {
1280                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1281                    sops[2].sem_op = 0;                                                // wait until unlocked
1282                    sops[2].sem_flg = 0;
1283    
1284                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1285                    sops[3].sem_op = 1;                                        // lock
1286                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1287            }
1288    
1289            timeout.tv_sec = wait_sec;
1290            timeout.tv_nsec = 0;
1291    
1292            ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout);
1293            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1294            {
1295                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1296            }
1297    
1298            return ret;
1299    }
1300    
1301    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1302    {
1303            int index;
1304            struct sembuf sops[3];
1305            struct timespec timeout;
1306            int ret;
1307    
1308            index = get_section_index(p_section);
1309            if (index < 0)
1310            {
1311                    return -2;
1312            }
1313    
1314            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1315            sops[0].sem_op = 0;                                                                // wait until unlocked
1316            sops[0].sem_flg = 0;
1317    
1318            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1319            sops[1].sem_op = 1;                                                                // lock
1320            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1321    
1322            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1323            sops[2].sem_op = 0;                                                        // wait until unlocked
1324            sops[2].sem_flg = 0;
1325    
1326            timeout.tv_sec = wait_sec;
1327            timeout.tv_nsec = 0;
1328    
1329            ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);
1330            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1331            {
1332                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1333            }
1334    
1335            return ret;
1336    }
1337    
1338    int section_list_rd_unlock(SECTION_LIST *p_section)
1339    {
1340            int index;
1341            struct sembuf sops[2];
1342            int ret;
1343    
1344            index = get_section_index(p_section);
1345            if (index < 0)
1346            {
1347                    return -2;
1348            }
1349    
1350            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1351            sops[0].sem_op = -1;                                               // unlock
1352            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1353    
1354            // The same reason as section_list_try_rd_lock()
1355            if (index == BBS_max_section)
1356            {
1357                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1358                    sops[1].sem_op = -1;                                     // unlock
1359                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1360            }
1361    
1362            ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1));
1363            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1364            {
1365                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1366            }
1367    
1368            return ret;
1369    }
1370    
1371    int section_list_rw_unlock(SECTION_LIST *p_section)
1372    {
1373            int index;
1374            struct sembuf sops[1];
1375            int ret;
1376    
1377            index = get_section_index(p_section);
1378            if (index < 0)
1379            {
1380                    return -2;
1381            }
1382    
1383            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1384            sops[0].sem_op = -1;                                                       // unlock
1385            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1386    
1387            ret = semop(section_list_pool_semid, sops, 1);
1388            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1389            {
1390                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1391            }
1392    
1393          return 1;          return ret;
1394  }  }


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

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