| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#define _GNU_SOURCE |
|
|
|
|
|
#include "section_list.h" |
|
| 17 |
#include "log.h" |
#include "log.h" |
| 18 |
|
#include "section_list.h" |
| 19 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 20 |
|
#include "user_list.h" |
| 21 |
|
#include <errno.h> |
| 22 |
|
#include <signal.h> |
| 23 |
#include <stdio.h> |
#include <stdio.h> |
| 24 |
|
#include <stdlib.h> |
| 25 |
#include <string.h> |
#include <string.h> |
|
#include <signal.h> |
|
| 26 |
#include <unistd.h> |
#include <unistd.h> |
| 27 |
#include <stdlib.h> |
#include <sys/ipc.h> |
|
#include <errno.h> |
|
| 28 |
#include <sys/param.h> |
#include <sys/param.h> |
| 29 |
#include <sys/sem.h> |
#include <sys/sem.h> |
| 30 |
#include <sys/shm.h> |
#include <sys/shm.h> |
|
#include <sys/ipc.h> |
|
| 31 |
|
|
| 32 |
#ifdef _SEM_SEMUN_UNDEFINED |
#ifdef _SEM_SEMUN_UNDEFINED |
| 33 |
union semun |
union semun |
| 43 |
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
| 44 |
#define SECTION_TRY_LOCK_TIMES 10 |
#define SECTION_TRY_LOCK_TIMES 10 |
| 45 |
|
|
| 46 |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
#define ARTICLE_BLOCK_PER_SHM 1000 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 47 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 48 |
#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) |
| 49 |
|
|
| 50 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections |
| 51 |
|
|
| 52 |
#define SID_STR_LEN 5 // 32-bit + NULL |
#define SID_STR_LEN 5 // 32-bit + NULL |
| 53 |
|
|
| 76 |
|
|
| 77 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 78 |
|
|
| 79 |
struct section_list_pool_t |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
|
{ |
|
|
int shmid; |
|
|
SECTION_LIST sections[BBS_max_section]; |
|
|
int section_count; |
|
|
int semid; |
|
|
TRIE_NODE *p_trie_dict_section_by_name; |
|
|
TRIE_NODE *p_trie_dict_section_by_sid; |
|
|
}; |
|
|
typedef struct section_list_pool_t SECTION_LIST_POOL; |
|
|
|
|
|
static SECTION_LIST_POOL *p_section_list_pool = NULL; |
|
| 80 |
|
|
| 81 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 82 |
{ |
{ |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
left = 0; |
left = 0; |
| 346 |
right = p_article_block_pool->block_count; |
right = p_article_block_pool->block_count - 1; |
| 347 |
|
|
| 348 |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ] |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right] ] |
| 349 |
while (left < right - 1) |
while (left < right) |
| 350 |
{ |
{ |
| 351 |
// get block offset no less than mid value of left and right block offsets |
// get block offset no less than mid value of left and right block offsets |
| 352 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_article_block_pool->block_count) |
|
|
{ |
|
|
log_error("block(mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 353 |
|
|
| 354 |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
| 355 |
{ |
{ |
| 356 |
right = mid; |
right = mid - 1; |
| 357 |
} |
} |
| 358 |
else |
else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid) |
| 359 |
{ |
{ |
| 360 |
left = mid; |
left = mid; |
| 361 |
} |
} |
| 614 |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
| 615 |
|
|
| 616 |
p_section->sid = sid; |
p_section->sid = sid; |
| 617 |
|
p_section->ex_menu_tm = 0; |
| 618 |
|
|
| 619 |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 620 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 644 |
return p_section; |
return p_section; |
| 645 |
} |
} |
| 646 |
|
|
| 647 |
|
int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list) |
| 648 |
|
{ |
| 649 |
|
int64_t index; |
| 650 |
|
|
| 651 |
|
if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL) |
| 652 |
|
{ |
| 653 |
|
log_error("NULL pointer error\n"); |
| 654 |
|
return -1; |
| 655 |
|
} |
| 656 |
|
|
| 657 |
|
index = get_section_index(p_section); |
| 658 |
|
|
| 659 |
|
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 660 |
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 661 |
|
|
| 662 |
|
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 663 |
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 664 |
|
|
| 665 |
|
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 666 |
|
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 667 |
|
|
| 668 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0) |
| 669 |
|
{ |
| 670 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, index); |
| 671 |
|
return -2; |
| 672 |
|
} |
| 673 |
|
|
| 674 |
|
return 0; |
| 675 |
|
} |
| 676 |
|
|
| 677 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 678 |
{ |
{ |
| 679 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 685 |
|
|
| 686 |
p_section->page_count = 0; |
p_section->page_count = 0; |
| 687 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 688 |
|
|
| 689 |
|
p_section->ontop_article_count = 0; |
| 690 |
} |
} |
| 691 |
|
|
| 692 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 752 |
|
|
| 753 |
if (p_section == NULL || p_article_src == NULL) |
if (p_section == NULL || p_article_src == NULL) |
| 754 |
{ |
{ |
| 755 |
log_error("section_list_append_article() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 756 |
return -1; |
return -1; |
| 757 |
} |
} |
| 758 |
|
|
| 877 |
p_section->last_page_visible_article_count++; |
p_section->last_page_visible_article_count++; |
| 878 |
} |
} |
| 879 |
|
|
| 880 |
|
if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0) |
| 881 |
|
{ |
| 882 |
|
log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n", |
| 883 |
|
p_section->sid, p_article->aid); |
| 884 |
|
return -5; |
| 885 |
|
} |
| 886 |
|
|
| 887 |
return 0; |
return 0; |
| 888 |
} |
} |
| 889 |
|
|
| 895 |
|
|
| 896 |
if (p_section == NULL) |
if (p_section == NULL) |
| 897 |
{ |
{ |
| 898 |
log_error("section_list_set_article_visible() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 899 |
return -2; |
return -1; |
| 900 |
} |
} |
| 901 |
|
|
| 902 |
p_article = article_block_find_by_aid(aid); |
p_article = article_block_find_by_aid(aid); |
| 907 |
|
|
| 908 |
if (p_section->sid != p_article->sid) |
if (p_section->sid != p_article->sid) |
| 909 |
{ |
{ |
| 910 |
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 911 |
return -2; |
return -2; |
| 912 |
} |
} |
| 913 |
|
|
| 920 |
{ |
{ |
| 921 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 922 |
|
|
| 923 |
|
if (user_article_cnt_inc(p_article->uid, -1) < 0) |
| 924 |
|
{ |
| 925 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid); |
| 926 |
|
} |
| 927 |
|
|
| 928 |
if (p_article->tid == 0) |
if (p_article->tid == 0) |
| 929 |
{ |
{ |
| 930 |
p_section->visible_topic_count--; |
p_section->visible_topic_count--; |
| 943 |
p_reply->visible = 0; |
p_reply->visible = 0; |
| 944 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 945 |
affected_count++; |
affected_count++; |
| 946 |
|
|
| 947 |
|
if (user_article_cnt_inc(p_reply->uid, -1) < 0) |
| 948 |
|
{ |
| 949 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid); |
| 950 |
|
} |
| 951 |
} |
} |
| 952 |
} |
} |
| 953 |
} |
} |
| 960 |
{ |
{ |
| 961 |
p_section->visible_topic_count++; |
p_section->visible_topic_count++; |
| 962 |
} |
} |
| 963 |
|
|
| 964 |
|
if (user_article_cnt_inc(p_article->uid, 1) < 0) |
| 965 |
|
{ |
| 966 |
|
log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid); |
| 967 |
|
} |
| 968 |
} |
} |
| 969 |
|
|
| 970 |
p_article->visible = visible; |
p_article->visible = visible; |
| 973 |
return affected_count; |
return affected_count; |
| 974 |
} |
} |
| 975 |
|
|
| 976 |
|
int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article) |
| 977 |
|
{ |
| 978 |
|
int i; |
| 979 |
|
|
| 980 |
|
if (p_section == NULL || p_article == NULL) |
| 981 |
|
{ |
| 982 |
|
log_error("NULL pointer error\n"); |
| 983 |
|
return -1; |
| 984 |
|
} |
| 985 |
|
|
| 986 |
|
if (p_section->sid != p_article->sid) |
| 987 |
|
{ |
| 988 |
|
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 989 |
|
return -2; |
| 990 |
|
} |
| 991 |
|
|
| 992 |
|
if (p_article->ontop) |
| 993 |
|
{ |
| 994 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 995 |
|
{ |
| 996 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 997 |
|
{ |
| 998 |
|
log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid); |
| 999 |
|
return 0; |
| 1000 |
|
} |
| 1001 |
|
else if (p_section->p_ontop_articles[i]->aid > p_article->aid) |
| 1002 |
|
{ |
| 1003 |
|
break; |
| 1004 |
|
} |
| 1005 |
|
} |
| 1006 |
|
|
| 1007 |
|
// Remove the oldest one if the array of ontop articles is full |
| 1008 |
|
if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section) |
| 1009 |
|
{ |
| 1010 |
|
if (i == 0) // p_article is the oldest one |
| 1011 |
|
{ |
| 1012 |
|
return 0; |
| 1013 |
|
} |
| 1014 |
|
memmove((void *)(p_section->p_ontop_articles), |
| 1015 |
|
(void *)(p_section->p_ontop_articles + 1), |
| 1016 |
|
sizeof(ARTICLE *) * (size_t)(i - 1)); |
| 1017 |
|
p_section->ontop_article_count--; |
| 1018 |
|
i--; |
| 1019 |
|
} |
| 1020 |
|
else |
| 1021 |
|
{ |
| 1022 |
|
memmove((void *)(p_section->p_ontop_articles + i + 1), |
| 1023 |
|
(void *)(p_section->p_ontop_articles + i), |
| 1024 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i)); |
| 1025 |
|
} |
| 1026 |
|
|
| 1027 |
|
p_section->p_ontop_articles[i] = p_article; |
| 1028 |
|
p_section->ontop_article_count++; |
| 1029 |
|
|
| 1030 |
|
// TODO: debug |
| 1031 |
|
} |
| 1032 |
|
else // ontop == 0 |
| 1033 |
|
{ |
| 1034 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 1035 |
|
{ |
| 1036 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 1037 |
|
{ |
| 1038 |
|
break; |
| 1039 |
|
} |
| 1040 |
|
} |
| 1041 |
|
if (i == p_section->ontop_article_count) // not found |
| 1042 |
|
{ |
| 1043 |
|
log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid); |
| 1044 |
|
return 0; |
| 1045 |
|
} |
| 1046 |
|
|
| 1047 |
|
memmove((void *)(p_section->p_ontop_articles + i), |
| 1048 |
|
(void *)(p_section->p_ontop_articles + i + 1), |
| 1049 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1)); |
| 1050 |
|
p_section->ontop_article_count--; |
| 1051 |
|
} |
| 1052 |
|
|
| 1053 |
|
return 0; |
| 1054 |
|
} |
| 1055 |
|
|
| 1056 |
|
int section_list_page_count_with_ontop(SECTION_LIST *p_section) |
| 1057 |
|
{ |
| 1058 |
|
int page_count; |
| 1059 |
|
|
| 1060 |
|
if (p_section == NULL) |
| 1061 |
|
{ |
| 1062 |
|
log_error("NULL pointer error\n"); |
| 1063 |
|
return -1; |
| 1064 |
|
} |
| 1065 |
|
|
| 1066 |
|
page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) + |
| 1067 |
|
(p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page + |
| 1068 |
|
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1); |
| 1069 |
|
|
| 1070 |
|
return page_count; |
| 1071 |
|
} |
| 1072 |
|
|
| 1073 |
|
int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id) |
| 1074 |
|
{ |
| 1075 |
|
if (p_section == NULL) |
| 1076 |
|
{ |
| 1077 |
|
log_error("NULL pointer error\n"); |
| 1078 |
|
return -1; |
| 1079 |
|
} |
| 1080 |
|
|
| 1081 |
|
if (page_id < p_section->page_count - 1) |
| 1082 |
|
{ |
| 1083 |
|
return BBS_article_limit_per_page; |
| 1084 |
|
} |
| 1085 |
|
else // if (page_id >= p_section->page_count - 1) |
| 1086 |
|
{ |
| 1087 |
|
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1088 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
| 1089 |
|
} |
| 1090 |
|
} |
| 1091 |
|
|
| 1092 |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
| 1093 |
{ |
{ |
| 1094 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1102 |
|
|
| 1103 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1104 |
{ |
{ |
| 1105 |
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1106 |
return NULL; |
return NULL; |
| 1107 |
} |
} |
| 1108 |
|
|
| 1114 |
} |
} |
| 1115 |
|
|
| 1116 |
left = 0; |
left = 0; |
| 1117 |
right = p_section->page_count; |
right = p_section->page_count - 1; |
| 1118 |
|
|
| 1119 |
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
// aid in the range [ head aid of pages[left], tail aid of pages[right] ] |
| 1120 |
while (left < right - 1) |
while (left < right) |
| 1121 |
{ |
{ |
| 1122 |
// get page id no less than mid value of left page id and right page id |
// get page id no less than mid value of left page id and right page id |
| 1123 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_section->page_count) |
|
|
{ |
|
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 1124 |
|
|
| 1125 |
if (aid < p_section->p_page_first_article[mid]->aid) |
if (aid < p_section->p_page_first_article[mid]->aid) |
| 1126 |
{ |
{ |
| 1127 |
right = mid; |
right = mid - 1; |
| 1128 |
} |
} |
| 1129 |
else |
else // if (aid < p_section->p_page_first_article[mid]->aid) |
| 1130 |
{ |
{ |
| 1131 |
left = mid; |
left = mid; |
| 1132 |
} |
} |
| 1188 |
|
|
| 1189 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1190 |
{ |
{ |
| 1191 |
log_error("section_list_calculate_page() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1192 |
return -1; |
return -1; |
| 1193 |
} |
} |
| 1194 |
|
|
| 1347 |
|
|
| 1348 |
if (p_section_src == NULL || p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1349 |
{ |
{ |
| 1350 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1351 |
return -1; |
return -1; |
| 1352 |
} |
} |
| 1353 |
|
|
| 1719 |
timer++; |
timer++; |
| 1720 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1721 |
{ |
{ |
| 1722 |
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid); |
| 1723 |
} |
} |
| 1724 |
} |
} |
| 1725 |
else // failed |
else // failed |
| 1726 |
{ |
{ |
| 1727 |
log_error("section_list_rd_lock() failed on section %d\n", sid); |
log_error("section_list_try_rd_lock() failed on section %d\n", sid); |
| 1728 |
break; |
break; |
| 1729 |
} |
} |
| 1730 |
} |
} |
| 1750 |
timer++; |
timer++; |
| 1751 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1752 |
{ |
{ |
| 1753 |
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid); |
| 1754 |
} |
} |
| 1755 |
} |
} |
| 1756 |
else // failed |
else // failed |
| 1757 |
{ |
{ |
| 1758 |
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
log_error("section_list_try_rw_lock() failed on section %d\n", sid); |
| 1759 |
break; |
break; |
| 1760 |
} |
} |
| 1761 |
} |
} |