/[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.7 by sysadm, Thu May 22 06:20:47 2025 UTC Revision 1.16 by sysadm, Sat May 24 07:32:46 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"
21  #include "trie_dict.h"  #include "trie_dict.h"
# Line 24  Line 26 
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <errno.h>  #include <errno.h>
28  #include <sys/param.h>  #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    
# Line 31  Line 34 
34  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
35  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
36    
37    #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic
38    
39    #define SID_STR_LEN 5 // 32-bit + NULL
40    
41  struct article_block_t  struct article_block_t
42  {  {
43          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
# Line 58  typedef struct article_block_pool_t ARTI Line 65  typedef struct article_block_pool_t ARTI
65    
66  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
67    
68    static int section_list_pool_shmid;
69    static int section_list_pool_semid;
70  static SECTION_LIST *p_section_list_pool = NULL;  static SECTION_LIST *p_section_list_pool = NULL;
71  static int section_list_count = 0;  static int section_list_count = 0;
72  static TRIE_NODE *p_trie_dict_section_list = NULL;  static TRIE_NODE *p_trie_dict_section_by_name = NULL;
73    static TRIE_NODE *p_trie_dict_section_by_sid = NULL;
74    
75  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
76  {  {
# Line 294  ARTICLE *article_block_find_by_index(int Line 304  ARTICLE *article_block_find_by_index(int
304    
305          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)
306          {          {
307                  log_error("section_data_find_article_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);
308                  return NULL;                  return NULL;
309          }          }
310    
# Line 302  ARTICLE *article_block_find_by_index(int Line 312  ARTICLE *article_block_find_by_index(int
312    
313          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
314          {          {
315                  log_error("section_data_find_article_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);
316                  return NULL;                  return NULL;
317          }          }
318    
319          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
320  }  }
321    
322  SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name)  extern int section_list_pool_init(const char *filename)
323  {  {
324          SECTION_LIST *p_section;          int semid;
325            int shmid;
326            int proj_id;
327            key_t key;
328            size_t size;
329            void *p_shm;
330    
331            if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)
332            {
333                    section_list_pool_cleanup();
334            }
335    
336            p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));
337          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
338          {          {
339                  p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);
340                  if (p_section_list_pool == NULL)                  return -1;
341                  {          }
                         log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);  
                         return NULL;  
                 }  
342    
343                  section_list_count = 0;          proj_id = (int)(time(NULL) % getpid());
344            key = ftok(filename, proj_id);
345            if (key == -1)
346            {
347                    log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
348                    return -3;
349          }          }
350    
351          if (p_trie_dict_section_list == NULL)          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
352            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
353            if (semid == -1)
354          {          {
355                  p_trie_dict_section_list = trie_dict_create();                  log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno);
356                  if (p_trie_dict_section_list == NULL)                  return -3;
357                  {          }
358                          log_error("trie_dict_create() OOM\n", BBS_max_section);  
359                          return NULL;          section_list_pool_semid = semid;
360                  }  
361            size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;
362            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
363            if (shmid == -1)
364            {
365                    log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno);
366                    return -3;
367            }
368            p_shm = shmat(shmid, NULL, 0);
369            if (p_shm == (void *)-1)
370            {
371                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
372                    return -3;
373            }
374    
375            section_list_pool_shmid = shmid;
376            p_section_list_pool = p_shm;
377            section_list_count = 0;
378    
379            p_trie_dict_section_by_name = trie_dict_create();
380            if (p_trie_dict_section_by_name == NULL)
381            {
382                    log_error("trie_dict_create() OOM\n", BBS_max_section);
383                    return -2;
384            }
385    
386            p_trie_dict_section_by_sid = trie_dict_create();
387            if (p_trie_dict_section_by_sid == NULL)
388            {
389                    log_error("trie_dict_create() OOM\n", BBS_max_section);
390                    return -2;
391            }
392    
393            return 0;
394    }
395    
396    inline static void sid_to_str(int32_t sid, char *p_sid_str)
397    {
398            uint32_t u_sid;
399            int i;
400    
401            u_sid = (uint32_t)sid;
402            for (i = 0; i < SID_STR_LEN - 1; i++)
403            {
404                    p_sid_str[i] = (char)(u_sid % 255 + 1);
405                    u_sid /= 255;
406            }
407            p_sid_str[i] = '\0';
408    }
409    
410    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)
411    {
412            SECTION_LIST *p_section;
413            char sid_str[SID_STR_LEN];
414    
415            if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)
416            {
417                    log_error("session_list_pool not initialized\n");
418                    return NULL;
419          }          }
420    
421          if (section_list_count >= BBS_max_section)          if (section_list_count >= BBS_max_section)
422          {          {
423                  log_error("section_list_count exceed limit %d\n", BBS_max_section);                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);
424                  return NULL;                  return NULL;
425          }          }
426    
427            sid_to_str(sid, sid_str);
428    
429          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool + section_list_count;
430    
431            p_section->sid = sid;
432    
433          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));
434          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname - 1)] = '\0';
435    
# Line 352  SECTION_LIST *section_list_create(const Line 439  SECTION_LIST *section_list_create(const
439          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));
440          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
441    
442          if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1)          if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1)
443            {
444                    log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count);
445                    return NULL;
446            }
447    
448            if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1)
449          {          {
450                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count);
451                    log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]);
452                  return NULL;                  return NULL;
453          }          }
454    
# Line 368  SECTION_LIST *section_list_create(const Line 462  SECTION_LIST *section_list_create(const
462  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
463  {  {
464          p_section->article_count = 0;          p_section->article_count = 0;
465            p_section->topic_count = 0;
466            p_section->visible_article_count = 0;
467            p_section->visible_topic_count = 0;
468          p_section->p_article_head = NULL;          p_section->p_article_head = NULL;
469          p_section->p_article_tail = NULL;          p_section->p_article_tail = NULL;
470    
471          p_section->page_count = 0;          p_section->page_count = 0;
472          p_section->last_page_article_count = 0;          p_section->last_page_visible_article_count = 0;
473  }  }
474    
475  void section_list_cleanup(void)  void section_list_pool_cleanup(void)
476  {  {
477          if (p_trie_dict_section_list != NULL)          if (p_trie_dict_section_by_name != NULL)
478          {          {
479                  trie_dict_destroy(p_trie_dict_section_list);                  trie_dict_destroy(p_trie_dict_section_by_name);
480                  p_trie_dict_section_list = NULL;                  p_trie_dict_section_by_name = NULL;
481            }
482    
483            if (p_trie_dict_section_by_sid != NULL)
484            {
485                    trie_dict_destroy(p_trie_dict_section_by_sid);
486                    p_trie_dict_section_by_sid = NULL;
487          }          }
488    
489          if (p_section_list_pool != NULL)          if (p_section_list_pool != NULL)
490          {          {
491                  free(p_section_list_pool);                  if (shmdt(p_section_list_pool) == -1)
492                    {
493                            log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);
494                    }
495                  p_section_list_pool = NULL;                  p_section_list_pool = NULL;
496    
497                    if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)
498                    {
499                            log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);
500                    }
501    
502                    if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1)
503                    {
504                            log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno);
505                    }
506          }          }
507    
508          section_list_count = 0;          section_list_count = 0;
# Line 396  SECTION_LIST *section_list_find_by_name( Line 512  SECTION_LIST *section_list_find_by_name(
512  {  {
513          int64_t index;          int64_t index;
514    
515          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)
516          {          {
517                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
518                  return NULL;                  return NULL;
519          }          }
520    
521          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1)
522          {          {
523                  log_error("trie_dict_get(section_data, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
524                    return NULL;
525            }
526    
527            return (p_section_list_pool + index);
528    }
529    
530    SECTION_LIST *section_list_find_by_sid(int32_t sid)
531    {
532            int64_t index;
533            char sid_str[SID_STR_LEN];
534    
535            if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)
536            {
537                    log_error("section_list not initialized\n");
538                    return NULL;
539            }
540    
541            sid_to_str(sid, sid_str);
542    
543            if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1)
544            {
545                    log_error("trie_dict_get(section, %d) error\n", sid);
546                  return NULL;                  return NULL;
547          }          }
548    
# Line 431  int section_list_append_article(SECTION_ Line 569  int section_list_append_article(SECTION_
569                  return -1;                  return -1;
570          }          }
571    
572            if (p_section->sid != p_article_src->sid)
573            {
574                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
575                    return -2;
576            }
577    
578            if (p_section->article_count >= BBS_article_limit_per_section)
579            {
580                    log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
581                    return -2;
582            }
583    
584          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
585                  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 >= ARTICLE_PER_BLOCK)
586          {          {
# Line 457  int section_list_append_article(SECTION_ Line 607  int section_list_append_article(SECTION_
607          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
608          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
609          {          {
610                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
611                  return -3;                  return -3;
612          }          }
613    
# Line 468  int section_list_append_article(SECTION_ Line 618  int section_list_append_article(SECTION_
618          // Copy article data          // Copy article data
619          *p_article = *p_article_src;          *p_article = *p_article_src;
620    
621            if (p_article->visible)
622            {
623                    p_section->visible_article_count++;
624            }
625    
626          // Link appended article as tail node of topic bi-directional list          // Link appended article as tail node of topic bi-directional list
627          if (p_article->tid != 0)          if (p_article->tid != 0)
628          {          {
# Line 487  int section_list_append_article(SECTION_ Line 642  int section_list_append_article(SECTION_
642          }          }
643          else          else
644          {          {
645                    p_section->topic_count++;
646    
647                    if (p_article->visible)
648                    {
649                            p_section->visible_topic_count++;
650                    }
651    
652                  p_topic_head = p_article;                  p_topic_head = p_article;
653                  p_topic_tail = p_article;                  p_topic_tail = p_article;
654          }          }
# Line 509  int section_list_append_article(SECTION_ Line 671  int section_list_append_article(SECTION_
671          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
672    
673          // Update page          // Update page
674          if (p_section->last_page_article_count % BBS_article_limit_per_page == 0)          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||
675                    p_section->article_count == 1)
676          {          {
677                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
678                  p_section->page_count++;                  p_section->page_count++;
679                  p_section->last_page_article_count = 0;                  p_section->last_page_visible_article_count = 0;
680            }
681    
682            if (p_article->visible)
683            {
684                    p_section->last_page_visible_article_count++;
685          }          }
         p_section->last_page_article_count++;  
686    
687          return 0;          return 0;
688  }  }
# Line 523  int section_list_append_article(SECTION_ Line 690  int section_list_append_article(SECTION_
690  int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible)  int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible)
691  {  {
692          ARTICLE *p_article;          ARTICLE *p_article;
693            ARTICLE *p_reply;
694            int affected_count = 0;
695    
696          if (p_section == NULL)          if (p_section == NULL)
697          {          {
# Line 536  int section_list_set_article_visible(SEC Line 705  int section_list_set_article_visible(SEC
705                  return -1; // Not found                  return -1; // Not found
706          }          }
707    
708            if (p_section->sid != p_article->sid)
709            {
710                    log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);
711                    return -2;
712            }
713    
714          if (p_article->visible == visible)          if (p_article->visible == visible)
715          {          {
716                  return 0; // Already set                  return 0; // Already set
717          }          }
718    
719            if (visible == 0) // 1 -> 0
720            {
721                    p_section->visible_article_count--;
722    
723                    if (p_article->tid == 0)
724                    {
725                            p_section->visible_topic_count--;
726    
727                            // Set related visible replies to invisible
728                            for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next)
729                            {
730                                    if (p_reply->tid != aid)
731                                    {
732                                            log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);
733                                            continue;
734                                    }
735    
736                                    if (p_reply->visible == 1)
737                                    {
738                                            p_reply->visible = 0;
739                                            p_section->visible_article_count--;
740                                            affected_count++;
741                                    }
742                            }
743                    }
744            }
745            else // 0 -> 1
746            {
747                    p_section->visible_article_count++;
748    
749                    if (p_article->tid == 0)
750                    {
751                            p_section->visible_topic_count++;
752                    }
753            }
754    
755          p_article->visible = visible;          p_article->visible = visible;
756            affected_count++;
757    
758            return affected_count;
759    }
760    
761    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)
762    {
763            ARTICLE *p_article;
764            int left;
765            int right;
766            int mid;
767    
768            *p_page = -1;
769            *p_offset = -1;
770            *pp_next = NULL;
771    
772            if (p_section == NULL)
773            {
774                    log_error("section_list_find_article_with_offset() NULL pointer error\n");
775                    return NULL;
776            }
777    
778            if (p_section->article_count == 0) // empty
779            {
780                    *p_page = 0;
781                    *p_offset = 0;
782                    return NULL;
783            }
784    
785            left = 0;
786            right = p_section->page_count;
787    
788            // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]
789            while (left < right - 1)
790            {
791                    // get page id no less than mid value of left page id and right page id
792                    mid = (left + right) / 2 + (right - left) % 2;
793    
794                    if (mid >= p_section->page_count)
795                    {
796                            log_error("page id (mid = %d) is out of boundary\n", mid);
797                            return NULL;
798                    }
799    
800                    if (aid < p_section->p_page_first_article[mid]->aid)
801                    {
802                            right = mid;
803                    }
804                    else
805                    {
806                            left = mid;
807                    }
808            }
809    
810            *p_page = left;
811    
812            p_article = p_section->p_page_first_article[*p_page];
813    
814            // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
815            right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
816    
817            // left will be the offset of article found or offset to insert
818            left = 0;
819    
820            while (aid > p_article->aid)
821            {
822                    p_article = p_article->p_next;
823                    left++;
824    
825                    if (aid == p_article->aid)
826                    {
827                            *pp_next = p_article->p_next;
828                            break;
829                    }
830    
831                    // over last article in the page
832                    if (p_article == p_section->p_article_head || p_article->aid >= right)
833                    {
834                            *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
835                            *p_offset = left;
836                            return NULL; // not found
837                    }
838            }
839    
840            if (aid < p_article->aid)
841            {
842                    *pp_next = p_article;
843                    p_article = NULL; // not found
844            }
845            else // aid == p_article->aid
846            {
847                    *pp_next = p_article->p_next;
848            }
849    
850            *p_offset = left;
851    
852            return p_article;
853    }
854    
855    int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
856    {
857            ARTICLE *p_article;
858            ARTICLE *p_next;
859            int32_t page;
860            int32_t offset;
861            int visible_article_count;
862            int page_head_set;
863    
864            if (p_section == NULL)
865            {
866                    log_error("section_list_calculate_page() NULL pointer error\n");
867                    return -1;
868            }
869    
870            if (p_section->article_count == 0) // empty
871            {
872                    p_section->page_count = 0;
873                    p_section->last_page_visible_article_count = 0;
874    
875                    return 0;
876            }
877    
878            if (start_aid > 0)
879            {
880                    p_article = article_block_find_by_aid(start_aid);
881                    if (p_article == NULL)
882                    {
883                            return -1; // Not found
884                    }
885    
886                    if (p_section->sid != p_article->sid)
887                    {
888                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
889                            return -2;
890                    }
891    
892                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
893                    if (p_article == NULL)
894                    {
895                            if (page < 0)
896                            {
897                                    return -1;
898                            }
899                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
900                                              start_aid, p_section->sid);
901                            return -2;
902                    }
903    
904                    if (offset > 0)
905                    {
906                            p_article = p_section->p_page_first_article[page];
907                    }
908            }
909            else
910            {
911                    p_article = p_section->p_article_head;
912                    page = 0;
913                    offset = 0;
914            }
915    
916            visible_article_count = 0;
917            page_head_set = 0;
918    
919            do
920            {
921                    if (!page_head_set && visible_article_count == 0)
922                    {
923                            p_section->p_page_first_article[page] = p_article;
924                            page_head_set = 1;
925                    }
926    
927                    if (p_article->visible)
928                    {
929                            visible_article_count++;
930                    }
931    
932                    p_article = p_article->p_next;
933    
934                    // skip remaining invisible articles
935                    while (p_article->visible == 0 && p_article != p_section->p_article_head)
936                    {
937                            p_article = p_article->p_next;
938                    }
939    
940                    if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
941                    {
942                            page++;
943                            visible_article_count = 0;
944                            page_head_set = 0;
945    
946                            if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
947                            {
948                                    log_error("Count of page exceed limit in section %d\n", p_section->sid);
949                                    break;
950                            }
951                    }
952            } while (p_article != p_section->p_article_head);
953    
954            p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
955            p_section->last_page_visible_article_count = visible_article_count;
956    
957            return 0;
958    }
959    
960    int article_count_of_topic(int32_t aid)
961    {
962            ARTICLE *p_article;
963            int article_count;
964    
965            p_article = article_block_find_by_aid(aid);
966            if (p_article == NULL)
967            {
968                    return 0; // Not found
969            }
970    
971            article_count = 0;
972    
973            do
974            {
975                    if (p_article->tid != 0 && p_article->tid != aid)
976                    {
977                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
978                            break;
979                    }
980    
981                    article_count++;
982                    p_article = p_article->p_topic_next;
983            } while (p_article->aid != aid);
984    
985            return article_count;
986    }
987    
988    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
989    {
990            ARTICLE *p_article;
991            ARTICLE *p_next;
992            int32_t page;
993            int32_t offset;
994            int32_t move_article_count;
995            int32_t dest_article_count_old;
996            int32_t last_unaffected_aid_src;
997            int32_t first_inserted_aid_dest;
998            int move_counter;
999    
1000            if (p_section_dest == NULL)
1001            {
1002                    log_error("section_list_move_topic() NULL pointer error\n");
1003                    return -1;
1004            }
1005    
1006            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1007            {
1008                    log_error("section_list_move_topic() error: article %d not found in block\n", aid);
1009                    return -2;
1010            }
1011    
1012            if (p_section_src->sid != p_article->sid)
1013            {
1014                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1015                                      p_section_src->sid, p_article->aid, p_article->sid);
1016                    return -2;
1017            }
1018    
1019            if (p_article->tid != 0)
1020            {
1021                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1022                    return -2;
1023            }
1024    
1025            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1026    
1027            move_article_count = article_count_of_topic(aid);
1028            if (move_article_count <= 0)
1029            {
1030                    log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);
1031                    return -2;
1032            }
1033    
1034            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1035            {
1036                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1037                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1038                    return -3;
1039            }
1040    
1041            dest_article_count_old = p_section_dest->article_count;
1042            move_counter = 0;
1043            first_inserted_aid_dest = p_article->aid;
1044    
1045            do
1046            {
1047                    if (p_section_src->sid != p_article->sid)
1048                    {
1049                            log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1050                                              p_section_src->sid, p_article->aid, p_article->sid);
1051                            return -2;
1052                    }
1053    
1054                    // Remove from bi-directional article list of src section
1055                    if (p_section_src->p_article_head == p_article)
1056                    {
1057                            p_section_src->p_article_head = p_article->p_next;
1058                    }
1059                    if (p_section_src->p_article_tail == p_article)
1060                    {
1061                            p_section_src->p_article_tail = p_article->p_prior;
1062                    }
1063                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1064                    {
1065                            p_section_src->p_article_head = NULL;
1066                            p_section_src->p_article_tail = NULL;
1067                    }
1068    
1069                    p_article->p_prior->p_next = p_article->p_next;
1070                    p_article->p_next->p_prior = p_article->p_prior;
1071    
1072                    // Update sid of article
1073                    p_article->sid = p_section_dest->sid;
1074    
1075                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1076                    {
1077                            log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1078                            return -4;
1079                    }
1080    
1081                    // Insert into bi-directional article list of dest section
1082                    if (p_next == NULL) // empty section
1083                    {
1084                            p_section_dest->p_article_head = p_article;
1085                            p_section_dest->p_article_tail = p_article;
1086                            p_article->p_prior = p_article;
1087                            p_article->p_next = p_article;
1088                    }
1089                    else
1090                    {
1091                            if (p_section_dest->p_article_head == p_next)
1092                            {
1093                                    if (p_article->aid < p_next->aid)
1094                                    {
1095                                            p_section_dest->p_article_head = p_article;
1096                                    }
1097                                    else // p_article->aid > p_next->aid
1098                                    {
1099                                            p_section_dest->p_article_tail = p_article;
1100                                    }
1101                            }
1102    
1103                            p_article->p_prior = p_next->p_prior;
1104                            p_article->p_next = p_next;
1105                            p_next->p_prior->p_next = p_article;
1106                            p_next->p_prior = p_article;
1107                    }
1108    
1109                    // Update article / topic counter of src / desc section
1110                    p_section_src->article_count--;
1111                    p_section_dest->article_count++;
1112                    if (p_article->tid == 0)
1113                    {
1114                            p_section_src->topic_count--;
1115                            p_section_dest->topic_count++;
1116                    }
1117    
1118                    // Update visible article / topic counter of src / desc section
1119                    if (p_article->visible)
1120                    {
1121                            p_section_src->visible_article_count--;
1122                            p_section_dest->visible_article_count++;
1123                            if (p_article->tid == 0)
1124                            {
1125                                    p_section_src->visible_topic_count--;
1126                                    p_section_dest->visible_topic_count++;
1127                            }
1128                    }
1129    
1130                    // Update page for empty dest section
1131                    if (p_section_dest->article_count == 1)
1132                    {
1133                            p_section_dest->p_page_first_article[0] = p_article;
1134                            p_section_dest->page_count = 1;
1135                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1136                    }
1137    
1138                    p_article = p_article->p_topic_next;
1139    
1140                    move_counter++;
1141                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1142                    {
1143                            // Re-calculate pages of desc section
1144                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1145                            {
1146                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1147                                                      p_section_dest->sid, first_inserted_aid_dest);
1148                            }
1149    
1150                            first_inserted_aid_dest = p_article->aid;
1151                    }
1152            } while (p_article->aid != aid);
1153    
1154            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1155            {
1156                    log_error("section_list_move_topic() error: count of moved articles %d != %d\n",
1157                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1158            }
1159    
1160            // Re-calculate pages of src section
1161            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1162            {
1163                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1164                                      p_section_src->sid, last_unaffected_aid_src, aid);
1165            }
1166    
1167            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1168            {
1169                    // Re-calculate pages of desc section
1170                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1171                    {
1172                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1173                                              p_section_dest->sid, first_inserted_aid_dest);
1174                    }
1175            }
1176    
1177            return move_article_count;
1178    }
1179    
1180    int get_section_index(SECTION_LIST *p_section)
1181    {
1182            int index;
1183    
1184            if (p_section_list_pool == NULL)
1185            {
1186                    log_error("get_section_index() error: uninitialized\n");
1187                    return -1;
1188            }
1189    
1190            if (p_section == NULL)
1191            {
1192                    index = BBS_max_section;
1193            }
1194            else
1195            {
1196                    index = (int)(p_section - p_section_list_pool);
1197                    if (index < 0 || index >= BBS_max_section)
1198                    {
1199                            log_error("get_section_index(%d) error: index out of range\n", index);
1200                            return -2;
1201                    }
1202            }
1203    
1204            return index;
1205    }
1206    
1207    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1208    {
1209            int index;
1210            struct sembuf sops[2];
1211            struct timespec timeout;
1212            int ret;
1213    
1214            index = get_section_index(p_section);
1215            if (index < 0)
1216            {
1217                    return -2;
1218            }
1219    
1220            sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index
1221            sops[0].sem_op = 0;                                                      // check if unlocked
1222            sops[0].sem_flg = 0;
1223    
1224            sops[1].sem_num = (unsigned short)index; // r_sem of section index
1225            sops[1].sem_op = 1;                                              // lock
1226            sops[1].sem_flg = SEM_UNDO;                              // undo on terminate
1227    
1228            timeout.tv_sec = wait_sec;
1229            timeout.tv_nsec = 0;
1230    
1231            ret = semtimedop(section_list_pool_semid, sops, 2, &timeout);
1232    
1233            return ret;
1234    }
1235    
1236    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1237    {
1238            int index;
1239            struct sembuf sops[3];
1240            struct timespec timeout;
1241            int ret;
1242    
1243            index = get_section_index(p_section);
1244            if (index < 0)
1245            {
1246                    return -2;
1247            }
1248    
1249            sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index
1250            sops[0].sem_op = 0;                                                      // check if unlocked
1251            sops[0].sem_flg = 0;
1252    
1253            sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index
1254            sops[1].sem_op = 1;                                                      // lock
1255            sops[1].sem_flg = SEM_UNDO;                                      // undo on terminate
1256    
1257            sops[2].sem_num = (unsigned short)index; // r_sem of section index
1258            sops[1].sem_op = 0;                                              // wait until unlocked
1259            sops[1].sem_flg = 0;
1260    
1261            timeout.tv_sec = wait_sec;
1262            timeout.tv_nsec = 0;
1263    
1264            ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);
1265    
1266            return ret;
1267    }
1268    
1269    int section_list_rd_unlock(SECTION_LIST *p_section)
1270    {
1271            int index;
1272            struct sembuf sops[1];
1273            int ret;
1274    
1275            index = get_section_index(p_section);
1276            if (index < 0)
1277            {
1278                    return -2;
1279            }
1280    
1281            sops[0].sem_num = (unsigned short)index; // r_sem of section index
1282            sops[0].sem_op = -1;                                     // unlock
1283            sops[0].sem_flg = IPC_NOWAIT;                    // no wait
1284    
1285            ret = semop(section_list_pool_semid, sops, 1);
1286    
1287            return ret;
1288    }
1289    
1290    int section_list_rw_unlock(SECTION_LIST *p_section)
1291    {
1292            int index;
1293            struct sembuf sops[1];
1294            int ret;
1295    
1296            index = get_section_index(p_section);
1297            if (index < 0)
1298            {
1299                    return -2;
1300            }
1301    
1302            sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index
1303            sops[0].sem_op = -1;                                             // unlock
1304            sops[0].sem_flg = IPC_NOWAIT;                            // no wait
1305    
1306          // TODO:          ret = semop(section_list_pool_semid, sops, 1);
1307    
1308          return 1;          return ret;
1309  }  }


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

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