| 31 |
#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) |
| 32 |
#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) |
| 33 |
|
|
| 34 |
|
#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]; |
| 62 |
|
|
| 63 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 64 |
|
|
| 65 |
|
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 |
|
|
| 315 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name) |
extern int section_list_pool_init(const char *filename) |
| 319 |
{ |
{ |
| 320 |
SECTION_LIST *p_section; |
int shmid; |
| 321 |
|
int proj_id; |
| 322 |
|
key_t key; |
| 323 |
|
size_t size; |
| 324 |
|
void *p_shm; |
| 325 |
|
|
| 326 |
|
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(); |
| 329 |
|
} |
| 330 |
|
|
| 331 |
|
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
| 332 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 333 |
{ |
{ |
| 334 |
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
| 335 |
if (p_section_list_pool == NULL) |
return -1; |
| 336 |
{ |
} |
|
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
|
|
return NULL; |
|
|
} |
|
| 337 |
|
|
| 338 |
section_list_count = 0; |
proj_id = (int)(time(NULL) % getpid()); |
| 339 |
|
key = ftok(filename, proj_id); |
| 340 |
|
if (key == -1) |
| 341 |
|
{ |
| 342 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 343 |
|
return -3; |
| 344 |
} |
} |
| 345 |
|
|
| 346 |
if (p_trie_dict_section_list == NULL) |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
| 347 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 348 |
|
if (shmid == -1) |
| 349 |
{ |
{ |
| 350 |
p_trie_dict_section_list = trie_dict_create(); |
log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno); |
| 351 |
if (p_trie_dict_section_list == NULL) |
return -3; |
| 352 |
{ |
} |
| 353 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
p_shm = shmat(shmid, NULL, 0); |
| 354 |
return NULL; |
if (p_shm == (void *)-1) |
| 355 |
} |
{ |
| 356 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 357 |
|
return -3; |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
section_list_pool_shmid = shmid; |
| 361 |
|
p_section_list_pool = p_shm; |
| 362 |
|
section_list_count = 0; |
| 363 |
|
|
| 364 |
|
p_trie_dict_section_by_name = trie_dict_create(); |
| 365 |
|
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); |
| 375 |
|
return -2; |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
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) |
| 396 |
|
{ |
| 397 |
|
SECTION_LIST *p_section; |
| 398 |
|
char sid_str[SID_STR_LEN]; |
| 399 |
|
|
| 400 |
|
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"); |
| 403 |
|
return NULL; |
| 404 |
} |
} |
| 405 |
|
|
| 406 |
if (section_list_count >= BBS_max_section) |
if (section_list_count >= BBS_max_section) |
| 407 |
{ |
{ |
| 408 |
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); |
| 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; |
| 417 |
|
|
| 418 |
strncpy(p_section->sname, sname, sizeof(p_section->sname - 1)); |
strncpy(p_section->sname, sname, sizeof(p_section->sname - 1)); |
| 419 |
p_section->sname[sizeof(p_section->sname - 1)] = '\0'; |
p_section->sname[sizeof(p_section->sname - 1)] = '\0'; |
| 420 |
|
|
| 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_data, %s, %d) error\n", sname, section_list_count); |
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, %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 |
|
|
| 457 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 458 |
} |
} |
| 459 |
|
|
| 460 |
void section_list_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) |
| 475 |
{ |
{ |
| 476 |
free(p_section_list_pool); |
if (shmdt(p_section_list_pool) == -1) |
| 477 |
|
{ |
| 478 |
|
log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno); |
| 479 |
|
} |
| 480 |
p_section_list_pool = NULL; |
p_section_list_pool = NULL; |
| 481 |
|
|
| 482 |
|
if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1) |
| 483 |
|
{ |
| 484 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
| 485 |
|
} |
| 486 |
} |
} |
| 487 |
|
|
| 488 |
section_list_count = 0; |
section_list_count = 0; |
| 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 |
|
|
| 651 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 652 |
|
|
| 653 |
// Update page |
// Update page |
| 654 |
if (p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) |
if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) || |
| 655 |
|
p_section->article_count == 1) |
| 656 |
{ |
{ |
| 657 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 658 |
p_section->page_count++; |
p_section->page_count++; |
| 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 |
| 738 |
return affected_count; |
return affected_count; |
| 739 |
} |
} |
| 740 |
|
|
| 741 |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset) |
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) |
| 742 |
{ |
{ |
| 743 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 744 |
int left; |
int left; |
| 747 |
|
|
| 748 |
*p_page = -1; |
*p_page = -1; |
| 749 |
*p_offset = -1; |
*p_offset = -1; |
| 750 |
|
*pp_next = NULL; |
| 751 |
|
|
| 752 |
if (p_section == NULL) |
if (p_section == NULL) |
| 753 |
{ |
{ |
| 792 |
p_article = p_section->p_page_first_article[*p_page]; |
p_article = p_section->p_page_first_article[*p_page]; |
| 793 |
|
|
| 794 |
// p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid |
// p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid |
| 795 |
right = (*p_page == p_section->page_count - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid); |
right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid); |
| 796 |
|
|
| 797 |
// left will be the offset of article found or offset to insert |
// left will be the offset of article found or offset to insert |
| 798 |
left = 0; |
left = 0; |
| 799 |
|
|
| 800 |
while (1) |
while (aid > p_article->aid) |
| 801 |
{ |
{ |
| 802 |
if (aid == p_article->aid) // found |
p_article = p_article->p_next; |
| 803 |
{ |
left++; |
| 804 |
break; |
|
| 805 |
} |
if (aid == p_article->aid) |
|
else if (aid < p_article->aid) // not exist |
|
| 806 |
{ |
{ |
| 807 |
p_article = NULL; |
*pp_next = p_article->p_next; |
| 808 |
break; |
break; |
| 809 |
} |
} |
| 810 |
|
|
|
// aid > p_article->aid |
|
|
p_article = p_article->p_next; |
|
|
left++; |
|
|
|
|
| 811 |
// over last article in the page |
// over last article in the page |
| 812 |
if (p_article == p_section->p_article_head || p_article->aid >= right) |
if (p_article == p_section->p_article_head || p_article->aid >= right) |
| 813 |
{ |
{ |
| 814 |
p_article = NULL; |
*pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]); |
| 815 |
break; |
*p_offset = left; |
| 816 |
|
return NULL; // not found |
| 817 |
} |
} |
| 818 |
} |
} |
| 819 |
|
|
| 820 |
|
if (aid < p_article->aid) |
| 821 |
|
{ |
| 822 |
|
*pp_next = p_article; |
| 823 |
|
p_article = NULL; // not found |
| 824 |
|
} |
| 825 |
|
else // aid == p_article->aid |
| 826 |
|
{ |
| 827 |
|
*pp_next = p_article->p_next; |
| 828 |
|
} |
| 829 |
|
|
| 830 |
*p_offset = left; |
*p_offset = left; |
| 831 |
|
|
| 832 |
return p_article; |
return p_article; |
| 835 |
int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid) |
int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid) |
| 836 |
{ |
{ |
| 837 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 838 |
|
ARTICLE *p_next; |
| 839 |
int32_t page; |
int32_t page; |
| 840 |
int32_t offset; |
int32_t offset; |
| 841 |
int visible_article_count; |
int visible_article_count; |
| 847 |
return -1; |
return -1; |
| 848 |
} |
} |
| 849 |
|
|
| 850 |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset); |
if (p_section->article_count == 0) // empty |
| 851 |
if (p_article == NULL) |
{ |
| 852 |
|
p_section->page_count = 0; |
| 853 |
|
p_section->last_page_visible_article_count = 0; |
| 854 |
|
|
| 855 |
|
return 0; |
| 856 |
|
} |
| 857 |
|
|
| 858 |
|
if (start_aid > 0) |
| 859 |
{ |
{ |
| 860 |
if (page < 0) |
p_article = article_block_find_by_aid(start_aid); |
| 861 |
|
if (p_article == NULL) |
| 862 |
{ |
{ |
| 863 |
return 0; |
return -1; // Not found |
| 864 |
} |
} |
| 865 |
|
|
| 866 |
log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n", start_aid, p_section->sid); |
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 |
if (offset > 0) |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
| 873 |
|
if (p_article == NULL) |
| 874 |
|
{ |
| 875 |
|
if (page < 0) |
| 876 |
|
{ |
| 877 |
|
return -1; |
| 878 |
|
} |
| 879 |
|
log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n", |
| 880 |
|
start_aid, p_section->sid); |
| 881 |
|
return -2; |
| 882 |
|
} |
| 883 |
|
|
| 884 |
|
if (offset > 0) |
| 885 |
|
{ |
| 886 |
|
p_article = p_section->p_page_first_article[page]; |
| 887 |
|
} |
| 888 |
|
} |
| 889 |
|
else |
| 890 |
{ |
{ |
| 891 |
p_article = p_section->p_page_first_article[page]; |
p_article = p_section->p_article_head; |
| 892 |
|
page = 0; |
| 893 |
|
offset = 0; |
| 894 |
} |
} |
| 895 |
|
|
| 896 |
visible_article_count = 0; |
visible_article_count = 0; |
| 936 |
|
|
| 937 |
return 0; |
return 0; |
| 938 |
} |
} |
| 939 |
|
|
| 940 |
|
int article_count_of_topic(int32_t aid) |
| 941 |
|
{ |
| 942 |
|
ARTICLE *p_article; |
| 943 |
|
int article_count; |
| 944 |
|
|
| 945 |
|
p_article = article_block_find_by_aid(aid); |
| 946 |
|
if (p_article == NULL) |
| 947 |
|
{ |
| 948 |
|
return 0; // Not found |
| 949 |
|
} |
| 950 |
|
|
| 951 |
|
article_count = 0; |
| 952 |
|
|
| 953 |
|
do |
| 954 |
|
{ |
| 955 |
|
article_count++; |
| 956 |
|
p_article = p_article->p_topic_next; |
| 957 |
|
} while (p_article->aid != aid); |
| 958 |
|
|
| 959 |
|
return article_count; |
| 960 |
|
} |
| 961 |
|
|
| 962 |
|
int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid) |
| 963 |
|
{ |
| 964 |
|
ARTICLE *p_article; |
| 965 |
|
ARTICLE *p_next; |
| 966 |
|
int32_t page; |
| 967 |
|
int32_t offset; |
| 968 |
|
int32_t move_article_count; |
| 969 |
|
int32_t dest_article_count_old; |
| 970 |
|
int32_t last_unaffected_aid_src; |
| 971 |
|
int32_t first_inserted_aid_dest; |
| 972 |
|
int move_counter; |
| 973 |
|
|
| 974 |
|
if (p_section_dest == NULL) |
| 975 |
|
{ |
| 976 |
|
log_error("section_list_move_topic() NULL pointer error\n"); |
| 977 |
|
return -1; |
| 978 |
|
} |
| 979 |
|
|
| 980 |
|
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
| 981 |
|
{ |
| 982 |
|
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; |
| 991 |
|
} |
| 992 |
|
|
| 993 |
|
if (p_article->tid != 0) |
| 994 |
|
{ |
| 995 |
|
log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid); |
| 996 |
|
return -2; |
| 997 |
|
} |
| 998 |
|
|
| 999 |
|
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
| 1000 |
|
|
| 1001 |
|
move_article_count = article_count_of_topic(aid); |
| 1002 |
|
if (move_article_count <= 0) |
| 1003 |
|
{ |
| 1004 |
|
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
| 1005 |
|
return -2; |
| 1006 |
|
} |
| 1007 |
|
|
| 1008 |
|
if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section) |
| 1009 |
|
{ |
| 1010 |
|
log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n", |
| 1011 |
|
p_section_dest->article_count + move_article_count, p_section_dest->sid); |
| 1012 |
|
return -3; |
| 1013 |
|
} |
| 1014 |
|
|
| 1015 |
|
dest_article_count_old = p_section_dest->article_count; |
| 1016 |
|
move_counter = 0; |
| 1017 |
|
first_inserted_aid_dest = p_article->aid; |
| 1018 |
|
|
| 1019 |
|
do |
| 1020 |
|
{ |
| 1021 |
|
if (p_section_src->sid != p_article->sid) |
| 1022 |
|
{ |
| 1023 |
|
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1024 |
|
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 |
| 1029 |
|
if (p_section_src->p_article_head == p_article) |
| 1030 |
|
{ |
| 1031 |
|
p_section_src->p_article_head = p_article->p_next; |
| 1032 |
|
} |
| 1033 |
|
if (p_section_src->p_article_tail == p_article) |
| 1034 |
|
{ |
| 1035 |
|
p_section_src->p_article_tail = p_article->p_prior; |
| 1036 |
|
} |
| 1037 |
|
if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article |
| 1038 |
|
{ |
| 1039 |
|
p_section_src->p_article_head = NULL; |
| 1040 |
|
p_section_src->p_article_tail = NULL; |
| 1041 |
|
} |
| 1042 |
|
|
| 1043 |
|
p_article->p_prior->p_next = p_article->p_next; |
| 1044 |
|
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 |
| 1056 |
|
if (p_next == NULL) // empty section |
| 1057 |
|
{ |
| 1058 |
|
p_section_dest->p_article_head = p_article; |
| 1059 |
|
p_section_dest->p_article_tail = p_article; |
| 1060 |
|
p_article->p_prior = p_article; |
| 1061 |
|
p_article->p_next = p_article; |
| 1062 |
|
} |
| 1063 |
|
else |
| 1064 |
|
{ |
| 1065 |
|
if (p_section_dest->p_article_head == p_next) |
| 1066 |
|
{ |
| 1067 |
|
if (p_article->aid < p_next->aid) |
| 1068 |
|
{ |
| 1069 |
|
p_section_dest->p_article_head = p_article; |
| 1070 |
|
} |
| 1071 |
|
else // p_article->aid > p_next->aid |
| 1072 |
|
{ |
| 1073 |
|
p_section_dest->p_article_tail = p_article; |
| 1074 |
|
} |
| 1075 |
|
} |
| 1076 |
|
|
| 1077 |
|
p_article->p_prior = p_next->p_prior; |
| 1078 |
|
p_article->p_next = p_next; |
| 1079 |
|
p_next->p_prior->p_next = p_article; |
| 1080 |
|
p_next->p_prior = p_article; |
| 1081 |
|
} |
| 1082 |
|
|
| 1083 |
|
// Update article / topic counter of src / desc section |
| 1084 |
|
p_section_src->article_count--; |
| 1085 |
|
p_section_dest->article_count++; |
| 1086 |
|
if (p_article->tid == 0) |
| 1087 |
|
{ |
| 1088 |
|
p_section_src->topic_count--; |
| 1089 |
|
p_section_dest->topic_count++; |
| 1090 |
|
} |
| 1091 |
|
|
| 1092 |
|
// Update visible article / topic counter of src / desc section |
| 1093 |
|
if (p_article->visible) |
| 1094 |
|
{ |
| 1095 |
|
p_section_src->visible_article_count--; |
| 1096 |
|
p_section_dest->visible_article_count++; |
| 1097 |
|
if (p_article->tid == 0) |
| 1098 |
|
{ |
| 1099 |
|
p_section_src->visible_topic_count--; |
| 1100 |
|
p_section_dest->visible_topic_count++; |
| 1101 |
|
} |
| 1102 |
|
} |
| 1103 |
|
|
| 1104 |
|
// Update page for empty dest section |
| 1105 |
|
if (p_section_dest->article_count == 1) |
| 1106 |
|
{ |
| 1107 |
|
p_section_dest->p_page_first_article[0] = p_article; |
| 1108 |
|
p_section_dest->page_count = 1; |
| 1109 |
|
p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0); |
| 1110 |
|
} |
| 1111 |
|
|
| 1112 |
|
p_article = p_article->p_topic_next; |
| 1113 |
|
|
| 1114 |
|
move_counter++; |
| 1115 |
|
if (move_counter % CALCULATE_PAGE_THRESHOLD == 0) |
| 1116 |
|
{ |
| 1117 |
|
// Re-calculate pages of desc section |
| 1118 |
|
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1119 |
|
{ |
| 1120 |
|
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1121 |
|
p_section_dest->sid, first_inserted_aid_dest); |
| 1122 |
|
} |
| 1123 |
|
|
| 1124 |
|
first_inserted_aid_dest = p_article->aid; |
| 1125 |
|
} |
| 1126 |
|
} while (p_article->aid != aid); |
| 1127 |
|
|
| 1128 |
|
if (p_section_dest->article_count - dest_article_count_old != move_article_count) |
| 1129 |
|
{ |
| 1130 |
|
log_error("section_list_move_topic() error: count of moved articles %d != %d\n", |
| 1131 |
|
p_section_dest->article_count - dest_article_count_old, move_article_count); |
| 1132 |
|
} |
| 1133 |
|
|
| 1134 |
|
// Re-calculate pages of src section |
| 1135 |
|
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
| 1136 |
|
{ |
| 1137 |
|
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); |
| 1139 |
|
} |
| 1140 |
|
|
| 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 |
|
|
| 1151 |
|
return move_article_count; |
| 1152 |
|
} |