| 33 |
|
|
| 34 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
| 35 |
|
|
| 36 |
|
#define SID_STR_LEN 5 // 32-bit + NULL |
| 37 |
|
|
| 38 |
struct article_block_t |
struct article_block_t |
| 39 |
{ |
{ |
| 40 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 65 |
static int section_list_pool_shmid; |
static int section_list_pool_shmid; |
| 66 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 67 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 68 |
static TRIE_NODE *p_trie_dict_section_list = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 69 |
|
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
| 70 |
|
|
| 71 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 72 |
{ |
{ |
| 300 |
|
|
| 301 |
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) |
| 302 |
{ |
{ |
| 303 |
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); |
| 304 |
return NULL; |
return NULL; |
| 305 |
} |
} |
| 306 |
|
|
| 308 |
|
|
| 309 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
| 310 |
{ |
{ |
| 311 |
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); |
| 312 |
return NULL; |
return NULL; |
| 313 |
} |
} |
| 314 |
|
|
| 323 |
size_t size; |
size_t size; |
| 324 |
void *p_shm; |
void *p_shm; |
| 325 |
|
|
| 326 |
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 || p_trie_dict_section_by_sid == NULL) |
| 327 |
{ |
{ |
| 328 |
section_list_pool_cleanup(); |
section_list_pool_cleanup(); |
| 329 |
} |
} |
| 361 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 362 |
section_list_count = 0; |
section_list_count = 0; |
| 363 |
|
|
| 364 |
p_trie_dict_section_list = trie_dict_create(); |
p_trie_dict_section_by_name = trie_dict_create(); |
| 365 |
if (p_trie_dict_section_list == NULL) |
if (p_trie_dict_section_by_name == NULL) |
| 366 |
|
{ |
| 367 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 368 |
|
return -2; |
| 369 |
|
} |
| 370 |
|
|
| 371 |
|
p_trie_dict_section_by_sid = trie_dict_create(); |
| 372 |
|
if (p_trie_dict_section_by_sid == NULL) |
| 373 |
{ |
{ |
| 374 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 375 |
return -2; |
return -2; |
| 378 |
return 0; |
return 0; |
| 379 |
} |
} |
| 380 |
|
|
| 381 |
|
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 382 |
|
{ |
| 383 |
|
uint32_t u_sid; |
| 384 |
|
int i; |
| 385 |
|
|
| 386 |
|
u_sid = (uint32_t)sid; |
| 387 |
|
for (i = 0; i < SID_STR_LEN - 1; i++) |
| 388 |
|
{ |
| 389 |
|
p_sid_str[i] = (char)(u_sid % 255 + 1); |
| 390 |
|
u_sid /= 255; |
| 391 |
|
} |
| 392 |
|
p_sid_str[i] = '\0'; |
| 393 |
|
} |
| 394 |
|
|
| 395 |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
| 396 |
{ |
{ |
| 397 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 398 |
|
char sid_str[SID_STR_LEN]; |
| 399 |
|
|
| 400 |
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 || p_trie_dict_section_by_sid == NULL) |
| 401 |
{ |
{ |
| 402 |
log_error("session_list_pool not initialized\n"); |
log_error("session_list_pool not initialized\n"); |
| 403 |
return NULL; |
return NULL; |
| 409 |
return NULL; |
return NULL; |
| 410 |
} |
} |
| 411 |
|
|
| 412 |
|
sid_to_str(sid, sid_str); |
| 413 |
|
|
| 414 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool + section_list_count; |
| 415 |
|
|
| 416 |
p_section->sid = sid; |
p_section->sid = sid; |
| 424 |
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)); |
| 425 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
| 426 |
|
|
| 427 |
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) |
| 428 |
|
{ |
| 429 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count); |
| 430 |
|
return NULL; |
| 431 |
|
} |
| 432 |
|
|
| 433 |
|
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
| 434 |
{ |
{ |
| 435 |
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); |
| 436 |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
| 437 |
return NULL; |
return NULL; |
| 438 |
} |
} |
| 439 |
|
|
| 459 |
|
|
| 460 |
void section_list_pool_cleanup(void) |
void section_list_pool_cleanup(void) |
| 461 |
{ |
{ |
| 462 |
if (p_trie_dict_section_list != NULL) |
if (p_trie_dict_section_by_name != NULL) |
| 463 |
{ |
{ |
| 464 |
trie_dict_destroy(p_trie_dict_section_list); |
trie_dict_destroy(p_trie_dict_section_by_name); |
| 465 |
p_trie_dict_section_list = NULL; |
p_trie_dict_section_by_name = NULL; |
| 466 |
|
} |
| 467 |
|
|
| 468 |
|
if (p_trie_dict_section_by_sid != NULL) |
| 469 |
|
{ |
| 470 |
|
trie_dict_destroy(p_trie_dict_section_by_sid); |
| 471 |
|
p_trie_dict_section_by_sid = NULL; |
| 472 |
} |
} |
| 473 |
|
|
| 474 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 492 |
{ |
{ |
| 493 |
int64_t index; |
int64_t index; |
| 494 |
|
|
| 495 |
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) |
| 496 |
|
{ |
| 497 |
|
log_error("section_list not initialized\n"); |
| 498 |
|
return NULL; |
| 499 |
|
} |
| 500 |
|
|
| 501 |
|
if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1) |
| 502 |
|
{ |
| 503 |
|
log_error("trie_dict_get(section, %s) error\n", sname); |
| 504 |
|
return NULL; |
| 505 |
|
} |
| 506 |
|
|
| 507 |
|
return (p_section_list_pool + index); |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 511 |
|
{ |
| 512 |
|
int64_t index; |
| 513 |
|
char sid_str[SID_STR_LEN]; |
| 514 |
|
|
| 515 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == 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) |
sid_to_str(sid, sid_str); |
| 522 |
|
|
| 523 |
|
if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1) |
| 524 |
{ |
{ |
| 525 |
log_error("trie_dict_get(section_data, %s) error\n", sname); |
log_error("trie_dict_get(section, %d) error\n", sid); |
| 526 |
return NULL; |
return NULL; |
| 527 |
} |
} |
| 528 |
|
|
| 549 |
return -1; |
return -1; |
| 550 |
} |
} |
| 551 |
|
|
| 552 |
|
if (p_section->sid != p_article_src->sid) |
| 553 |
|
{ |
| 554 |
|
log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid); |
| 555 |
|
return -2; |
| 556 |
|
} |
| 557 |
|
|
| 558 |
if (p_section->article_count >= BBS_article_limit_per_section) |
if (p_section->article_count >= BBS_article_limit_per_section) |
| 559 |
{ |
{ |
| 560 |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
| 587 |
// AID of articles should be strictly ascending |
// AID of articles should be strictly ascending |
| 588 |
if (p_article_src->aid <= last_aid) |
if (p_article_src->aid <= last_aid) |
| 589 |
{ |
{ |
| 590 |
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); |
| 591 |
return -3; |
return -3; |
| 592 |
} |
} |
| 593 |
|
|
| 685 |
return -1; // Not found |
return -1; // Not found |
| 686 |
} |
} |
| 687 |
|
|
| 688 |
|
if (p_section->sid != p_article->sid) |
| 689 |
|
{ |
| 690 |
|
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 691 |
|
return -2; |
| 692 |
|
} |
| 693 |
|
|
| 694 |
if (p_article->visible == visible) |
if (p_article->visible == visible) |
| 695 |
{ |
{ |
| 696 |
return 0; // Already set |
return 0; // Already set |
| 857 |
|
|
| 858 |
if (start_aid > 0) |
if (start_aid > 0) |
| 859 |
{ |
{ |
| 860 |
|
p_article = article_block_find_by_aid(start_aid); |
| 861 |
|
if (p_article == NULL) |
| 862 |
|
{ |
| 863 |
|
return -1; // Not found |
| 864 |
|
} |
| 865 |
|
|
| 866 |
|
if (p_section->sid != p_article->sid) |
| 867 |
|
{ |
| 868 |
|
log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid); |
| 869 |
|
return -2; |
| 870 |
|
} |
| 871 |
|
|
| 872 |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
| 873 |
if (p_article == NULL) |
if (p_article == NULL) |
| 874 |
{ |
{ |
| 937 |
return 0; |
return 0; |
| 938 |
} |
} |
| 939 |
|
|
| 940 |
int section_list_count_of_topic_articles(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 941 |
{ |
{ |
| 942 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 943 |
int article_count; |
int article_count; |
| 977 |
return -1; |
return -1; |
| 978 |
} |
} |
| 979 |
|
|
| 980 |
if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL) |
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
| 981 |
{ |
{ |
| 982 |
log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid); |
log_error("section_list_move_topic() error: article %d not found in block\n", aid); |
| 983 |
|
return -2; |
| 984 |
|
} |
| 985 |
|
|
| 986 |
|
if (p_section_src->sid != p_article->sid) |
| 987 |
|
{ |
| 988 |
|
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 989 |
|
p_section_src->sid, p_article->aid, p_article->sid); |
| 990 |
return -2; |
return -2; |
| 991 |
} |
} |
| 992 |
|
|
| 998 |
|
|
| 999 |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
| 1000 |
|
|
| 1001 |
move_article_count = section_list_count_of_topic_articles(aid); |
move_article_count = article_count_of_topic(aid); |
| 1002 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1003 |
{ |
{ |
| 1004 |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
| 1018 |
|
|
| 1019 |
do |
do |
| 1020 |
{ |
{ |
| 1021 |
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
if (p_section_src->sid != p_article->sid) |
| 1022 |
{ |
{ |
| 1023 |
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1024 |
return -4; |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1025 |
|
return -2; |
| 1026 |
} |
} |
| 1027 |
|
|
| 1028 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1043 |
p_article->p_prior->p_next = p_article->p_next; |
p_article->p_prior->p_next = p_article->p_next; |
| 1044 |
p_article->p_next->p_prior = p_article->p_prior; |
p_article->p_next->p_prior = p_article->p_prior; |
| 1045 |
|
|
| 1046 |
|
// Update sid of article |
| 1047 |
|
p_article->sid = p_section_dest->sid; |
| 1048 |
|
|
| 1049 |
|
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1050 |
|
{ |
| 1051 |
|
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1052 |
|
return -4; |
| 1053 |
|
} |
| 1054 |
|
|
| 1055 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1056 |
if (p_next == NULL) // empty section |
if (p_next == NULL) // empty section |
| 1057 |
{ |
{ |
| 1117 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1118 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1119 |
{ |
{ |
| 1120 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1121 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1122 |
} |
} |
| 1123 |
|
|
| 1134 |
// Re-calculate pages of src section |
// Re-calculate pages of src section |
| 1135 |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
| 1136 |
{ |
{ |
| 1137 |
log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n", |
log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n", |
| 1138 |
p_section_src->sid, last_unaffected_aid_src, aid); |
p_section_src->sid, last_unaffected_aid_src, aid); |
| 1139 |
} |
} |
| 1140 |
|
|
| 1143 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1144 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1145 |
{ |
{ |
| 1146 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1147 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1148 |
} |
} |
| 1149 |
} |
} |