| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "section_list.h" |
#include "section_list.h" |
| 18 |
|
#include "trie_dict.h" |
| 19 |
#include "bbs.h" |
#include "bbs.h" |
| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <unistd.h> |
#include <unistd.h> |
| 23 |
#include <errno.h> |
#include <errno.h> |
| 24 |
|
|
| 25 |
|
#define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat" |
| 26 |
|
#define SECTION_LIST_SHM_FILE "~section_list_shm.dat" |
| 27 |
|
#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" |
| 28 |
|
|
| 29 |
const char *sname[] = { |
const char *sname[] = { |
| 30 |
"Test", |
"Test", |
| 31 |
"ABCDEFG", |
"ABCDEFG", |
| 52 |
ARTICLE article; |
ARTICLE article; |
| 53 |
int block_count; |
int block_count; |
| 54 |
int i, j; |
int i, j; |
| 55 |
|
int sid; |
| 56 |
int last_aid; |
int last_aid; |
| 57 |
int current_tid; |
int current_tid; |
| 58 |
int section_first_aid; |
int section_first_aid; |
| 62 |
int32_t page; |
int32_t page; |
| 63 |
int32_t offset; |
int32_t offset; |
| 64 |
int affected_count; |
int affected_count; |
| 65 |
|
FILE *fp; |
| 66 |
|
|
| 67 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
| 68 |
{ |
{ |
| 70 |
return -1; |
return -1; |
| 71 |
} |
} |
| 72 |
|
|
| 73 |
log_std_redirect(STDOUT_FILENO); |
log_common_redir(STDOUT_FILENO); |
| 74 |
log_err_redirect(STDERR_FILENO); |
log_error_redir(STDERR_FILENO); |
| 75 |
|
|
| 76 |
// - 1 to make blocks allocated is less than required, to trigger error handling |
// - 1 to make blocks allocated is less than required, to trigger error handling |
| 77 |
block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK; |
block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK; |
| 78 |
|
|
| 79 |
if (article_block_init("../conf/menu.conf", block_count) < 0) |
if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL) |
| 80 |
|
{ |
| 81 |
|
log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE); |
| 82 |
|
return -1; |
| 83 |
|
} |
| 84 |
|
fclose(fp); |
| 85 |
|
|
| 86 |
|
if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL) |
| 87 |
|
{ |
| 88 |
|
log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE); |
| 89 |
|
return -1; |
| 90 |
|
} |
| 91 |
|
fclose(fp); |
| 92 |
|
|
| 93 |
|
if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL) |
| 94 |
|
{ |
| 95 |
|
log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE); |
| 96 |
|
return -1; |
| 97 |
|
} |
| 98 |
|
fclose(fp); |
| 99 |
|
|
| 100 |
|
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) |
| 101 |
|
{ |
| 102 |
|
printf("trie_dict_init failed\n"); |
| 103 |
|
return -1; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0) |
| 107 |
|
{ |
| 108 |
|
log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count); |
| 109 |
|
return -2; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
if (section_list_init(SECTION_LIST_SHM_FILE) < 0) |
| 113 |
{ |
{ |
| 114 |
log_error("section_data_pool_init() error\n"); |
log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE); |
| 115 |
return -2; |
return -2; |
| 116 |
} |
} |
| 117 |
|
|
| 119 |
|
|
| 120 |
last_aid = 0; |
last_aid = 0; |
| 121 |
|
|
| 122 |
|
if (section_list_rw_lock(NULL) < 0) |
| 123 |
|
{ |
| 124 |
|
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 125 |
|
} |
| 126 |
|
|
| 127 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 128 |
{ |
{ |
| 129 |
p_section[i] = section_list_create(i + 1, |
sid = i * 3 + 1; |
| 130 |
|
p_section[i] = section_list_create(sid, |
| 131 |
sname[i % section_conf_count], |
sname[i % section_conf_count], |
| 132 |
stitle[i % section_conf_count], |
stitle[i % section_conf_count], |
| 133 |
master_name[i % section_conf_count]); |
master_name[i % section_conf_count]); |
| 134 |
if (p_section[i] == NULL) |
if (p_section[i] == NULL) |
| 135 |
{ |
{ |
| 136 |
log_error("section_data_create(i=%d) error\n", i); |
printf("section_list_create(i = %d) error\n", i); |
| 137 |
|
return -3; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
if (get_section_index(p_section[i]) != i) |
| 141 |
|
{ |
| 142 |
|
printf("get_section_index(i = %d) error\n", i); |
| 143 |
return -3; |
return -3; |
| 144 |
} |
} |
| 145 |
} |
} |
| 148 |
{ |
{ |
| 149 |
if (section_list_find_by_name(sname[i]) == NULL) |
if (section_list_find_by_name(sname[i]) == NULL) |
| 150 |
{ |
{ |
| 151 |
printf("section_data_find_section_by_name(%s) error\n", sname[i]); |
printf("section_list_find_by_name(%s) error\n", sname[i]); |
| 152 |
return -3; |
return -3; |
| 153 |
} |
} |
| 154 |
} |
} |
| 155 |
|
|
| 156 |
|
for (i = 0; i < section_count; i++) |
| 157 |
|
{ |
| 158 |
|
sid = i * 3 + 1; |
| 159 |
|
if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid) |
| 160 |
|
{ |
| 161 |
|
printf("section_list_find_by_sid(%d) error\n", sid); |
| 162 |
|
return -3; |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
if (section_list_rw_unlock(NULL) < 0) |
| 167 |
|
{ |
| 168 |
|
printf("section_list_rw_unlock(sid = %d) error\n", 0); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
for (j = 0; j < BBS_article_limit_per_section; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 172 |
{ |
{ |
| 173 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 176 |
|
|
| 177 |
// Set article data |
// Set article data |
| 178 |
article.aid = last_aid; |
article.aid = last_aid; |
|
article.cid = article.aid; |
|
| 179 |
article.tid = 0; |
article.tid = 0; |
| 180 |
article.uid = 1; // TODO: randomize |
article.sid = i * 3 + 1; |
| 181 |
|
article.cid = article.aid; |
| 182 |
|
article.uid = 1; |
| 183 |
article.visible = 1; |
article.visible = 1; |
| 184 |
article.excerption = 0; |
article.excerption = 0; |
| 185 |
article.ontop = 0; |
article.ontop = 0; |
| 186 |
article.lock = 0; |
article.lock = 0; |
| 187 |
|
article.transship = 0; |
| 188 |
|
|
| 189 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 190 |
|
{ |
| 191 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 192 |
|
break; |
| 193 |
|
} |
| 194 |
|
|
| 195 |
if (section_list_append_article(p_section[i], &article) < 0) |
if (section_list_append_article(p_section[i], &article) < 0) |
| 196 |
{ |
{ |
| 197 |
printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j); |
printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j); |
| 198 |
break; |
break; |
| 199 |
} |
} |
| 200 |
|
|
| 201 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 202 |
|
{ |
| 203 |
|
printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno); |
| 204 |
|
break; |
| 205 |
|
} |
| 206 |
} |
} |
| 207 |
} |
} |
| 208 |
|
|
| 211 |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 212 |
} |
} |
| 213 |
|
|
| 214 |
last_aid = 0; |
if (last_aid != article_block_last_aid()) |
| 215 |
|
{ |
| 216 |
|
printf("last_aid != %d\n", article_block_last_aid()); |
| 217 |
|
} |
| 218 |
|
|
| 219 |
for (i = 0; i < section_count; i++) |
if (article_block_article_count() != section_count * BBS_article_limit_per_section) |
| 220 |
{ |
{ |
| 221 |
if (p_section[i]->article_count == 0) |
printf("article_block_article_count() error %d != %d * %d\n", |
| 222 |
{ |
article_block_article_count(), section_count, BBS_article_limit_per_section); |
| 223 |
continue; |
} |
| 224 |
} |
|
| 225 |
|
last_aid = 0; |
| 226 |
|
|
| 227 |
for (j = 0; j < p_section[i]->article_count; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 228 |
|
{ |
| 229 |
|
for (i = 0; i < section_count; i++) |
| 230 |
{ |
{ |
| 231 |
last_aid++; |
last_aid++; |
| 232 |
|
|
| 236 |
printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
| 237 |
} |
} |
| 238 |
|
|
| 239 |
p_article = article_block_find_by_index(last_aid - 1); |
if (section_list_rw_lock(p_section[i]) < 0) |
|
if (p_article == NULL || p_article->aid != last_aid) |
|
| 240 |
{ |
{ |
| 241 |
printf("article_block_find_by_index() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 242 |
|
break; |
| 243 |
} |
} |
| 244 |
|
|
| 245 |
if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1) |
if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1) |
| 246 |
{ |
{ |
| 247 |
printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid); |
printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid); |
| 248 |
} |
} |
| 249 |
|
|
| 250 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 251 |
|
{ |
| 252 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 253 |
|
break; |
| 254 |
|
} |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
// printf("Verified %d articles in section %d\n", p_section[i]->article_count, i); |
// printf("Verified %d articles in section %d\n", p_section[i]->article_count, i); |
| 259 |
|
|
| 260 |
printf("Testing #2 ...\n"); |
printf("Testing #2 ...\n"); |
| 261 |
|
|
| 262 |
|
if (section_list_rw_lock(NULL) < 0) |
| 263 |
|
{ |
| 264 |
|
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 265 |
|
} |
| 266 |
|
|
| 267 |
if (article_block_reset() != 0) |
if (article_block_reset() != 0) |
| 268 |
{ |
{ |
| 269 |
log_error("section_data_free_block(i=%d) error\n", i); |
log_error("article_block_reset() error\n"); |
| 270 |
return -4; |
return -4; |
| 271 |
} |
} |
| 272 |
|
|
| 275 |
section_list_reset_articles(p_section[i]); |
section_list_reset_articles(p_section[i]); |
| 276 |
} |
} |
| 277 |
|
|
| 278 |
|
if (section_list_rw_unlock(NULL) < 0) |
| 279 |
|
{ |
| 280 |
|
printf("section_list_rw_unlock(sid = %d) error\n", 0); |
| 281 |
|
} |
| 282 |
|
|
| 283 |
last_aid = 0; |
last_aid = 0; |
| 284 |
|
|
| 285 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 286 |
{ |
{ |
| 287 |
section_first_aid = last_aid + 1; |
section_first_aid = last_aid + 1; |
| 288 |
|
|
| 289 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 290 |
|
{ |
| 291 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 292 |
|
break; |
| 293 |
|
} |
| 294 |
|
|
| 295 |
for (j = 0; j < BBS_article_limit_per_section; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 296 |
{ |
{ |
| 297 |
last_aid++; |
last_aid++; |
| 298 |
|
|
| 299 |
// Set article data |
// Set article data |
| 300 |
article.aid = last_aid; |
article.aid = last_aid; |
|
article.cid = article.aid; |
|
| 301 |
// Group articles into group_count topics |
// Group articles into group_count topics |
| 302 |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
| 303 |
article.uid = 1; // TODO: randomize |
article.sid = i * 3 + 1; |
| 304 |
|
article.cid = article.aid; |
| 305 |
|
article.uid = 1; |
| 306 |
article.visible = 1; |
article.visible = 1; |
| 307 |
article.excerption = 0; |
article.excerption = 0; |
| 308 |
article.ontop = 0; |
article.ontop = 0; |
| 309 |
article.lock = 0; |
article.lock = 0; |
| 310 |
|
article.transship = 0; |
| 311 |
|
|
| 312 |
if (section_list_append_article(p_section[i], &article) < 0) |
if (section_list_append_article(p_section[i], &article) < 0) |
| 313 |
{ |
{ |
| 316 |
} |
} |
| 317 |
} |
} |
| 318 |
|
|
| 319 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 320 |
|
{ |
| 321 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 322 |
|
break; |
| 323 |
|
} |
| 324 |
|
|
| 325 |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 326 |
} |
} |
| 327 |
|
|
| 335 |
article_count = 0; |
article_count = 0; |
| 336 |
last_aid = 0; |
last_aid = 0; |
| 337 |
|
|
| 338 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 339 |
|
{ |
| 340 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 341 |
|
break; |
| 342 |
|
} |
| 343 |
|
|
| 344 |
p_article = p_section[i]->p_article_head; |
p_article = p_section[i]->p_article_head; |
| 345 |
|
|
| 346 |
do |
do |
| 363 |
break; |
break; |
| 364 |
} |
} |
| 365 |
|
|
| 366 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 367 |
|
{ |
| 368 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 369 |
|
break; |
| 370 |
|
} |
| 371 |
|
|
| 372 |
// printf("Verified %d articles in section %d\n", group_count, i); |
// printf("Verified %d articles in section %d\n", group_count, i); |
| 373 |
} |
} |
| 374 |
|
|
| 379 |
continue; |
continue; |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 383 |
|
{ |
| 384 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 385 |
|
break; |
| 386 |
|
} |
| 387 |
|
|
| 388 |
if (p_section[i]->topic_count != group_count) |
if (p_section[i]->topic_count != group_count) |
| 389 |
{ |
{ |
| 390 |
printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count); |
printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count); |
| 446 |
} |
} |
| 447 |
} |
} |
| 448 |
|
|
| 449 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 450 |
|
{ |
| 451 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 452 |
|
break; |
| 453 |
|
} |
| 454 |
|
|
| 455 |
// printf("Verified %d topics in section %d\n", group_count, i); |
// printf("Verified %d topics in section %d\n", group_count, i); |
| 456 |
} |
} |
| 457 |
|
|
| 461 |
{ |
{ |
| 462 |
last_aid = 0; |
last_aid = 0; |
| 463 |
|
|
| 464 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 465 |
|
{ |
| 466 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 467 |
|
break; |
| 468 |
|
} |
| 469 |
|
|
| 470 |
for (j = 0; j < p_section[i]->page_count; j++) |
for (j = 0; j < p_section[i]->page_count; j++) |
| 471 |
{ |
{ |
| 472 |
if (p_section[i]->p_page_first_article[j]->aid <= last_aid) |
if (p_section[i]->p_page_first_article[j]->aid <= last_aid) |
| 491 |
} |
} |
| 492 |
} |
} |
| 493 |
} |
} |
| 494 |
|
|
| 495 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 496 |
|
{ |
| 497 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 498 |
|
break; |
| 499 |
|
} |
| 500 |
} |
} |
| 501 |
|
|
| 502 |
printf("Testing #4 ...\n"); |
printf("Testing #4 ...\n"); |
| 503 |
|
|
| 504 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 505 |
{ |
{ |
| 506 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 507 |
|
{ |
| 508 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 509 |
|
break; |
| 510 |
|
} |
| 511 |
|
|
| 512 |
step = i % 10 + 1; |
step = i % 10 + 1; |
| 513 |
for (j = group_count; j < BBS_article_limit_per_section; j += step) |
for (j = group_count; j < BBS_article_limit_per_section; j += step) |
| 514 |
{ |
{ |
| 638 |
p_section[i]->last_page_visible_article_count); |
p_section[i]->last_page_visible_article_count); |
| 639 |
break; |
break; |
| 640 |
} |
} |
| 641 |
|
|
| 642 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 643 |
|
{ |
| 644 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 645 |
|
break; |
| 646 |
|
} |
| 647 |
} |
} |
| 648 |
|
|
| 649 |
for (i = 0; i < BBS_max_section; i++) |
for (i = 0; i < BBS_max_section; i++) |
| 650 |
{ |
{ |
| 651 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 652 |
|
{ |
| 653 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 654 |
|
break; |
| 655 |
|
} |
| 656 |
|
|
| 657 |
affected_count = 0; |
affected_count = 0; |
| 658 |
|
|
| 659 |
for (j = 0; j < BBS_article_limit_per_section; j += 1) |
for (j = 0; j < BBS_article_limit_per_section; j += 1) |
| 707 |
p_section[i]->last_page_visible_article_count); |
p_section[i]->last_page_visible_article_count); |
| 708 |
break; |
break; |
| 709 |
} |
} |
| 710 |
|
|
| 711 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 712 |
|
{ |
| 713 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 714 |
|
break; |
| 715 |
|
} |
| 716 |
} |
} |
| 717 |
|
|
| 718 |
printf("Testing #5 ...\n"); |
printf("Testing #5 ...\n"); |
| 719 |
|
|
| 720 |
|
if (section_list_rw_lock(NULL) < 0) |
| 721 |
|
{ |
| 722 |
|
printf("section_list_rw_lock(sid = %d) error\n", 0); |
| 723 |
|
} |
| 724 |
|
|
| 725 |
if (article_block_reset() != 0) |
if (article_block_reset() != 0) |
| 726 |
{ |
{ |
| 727 |
log_error("section_data_free_block(i=%d) error\n", i); |
log_error("article_block_reset() error\n"); |
| 728 |
return -4; |
return -4; |
| 729 |
} |
} |
| 730 |
|
|
| 733 |
section_list_reset_articles(p_section[i]); |
section_list_reset_articles(p_section[i]); |
| 734 |
} |
} |
| 735 |
|
|
| 736 |
|
if (section_list_rw_unlock(NULL) < 0) |
| 737 |
|
{ |
| 738 |
|
printf("section_list_rw_unlock(sid = %d) error\n", 0); |
| 739 |
|
} |
| 740 |
|
|
| 741 |
last_aid = 0; |
last_aid = 0; |
| 742 |
|
|
| 743 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 744 |
{ |
{ |
| 745 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 746 |
|
{ |
| 747 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 748 |
|
break; |
| 749 |
|
} |
| 750 |
|
|
| 751 |
section_first_aid = last_aid + 1; |
section_first_aid = last_aid + 1; |
| 752 |
|
|
| 753 |
for (j = 0; j < BBS_article_limit_per_section; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 756 |
|
|
| 757 |
// Set article data |
// Set article data |
| 758 |
article.aid = last_aid; |
article.aid = last_aid; |
|
article.cid = article.aid; |
|
| 759 |
// Group articles into group_count topics |
// Group articles into group_count topics |
| 760 |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
| 761 |
article.uid = 1; // TODO: randomize |
article.sid = i * 3 + 1; |
| 762 |
|
article.cid = article.aid; |
| 763 |
|
article.uid = 1; |
| 764 |
article.visible = 1; |
article.visible = 1; |
| 765 |
article.excerption = 0; |
article.excerption = 0; |
| 766 |
article.ontop = 0; |
article.ontop = 0; |
| 767 |
article.lock = 0; |
article.lock = 0; |
| 768 |
|
article.transship = 0; |
| 769 |
|
|
| 770 |
if (section_list_append_article(p_section[i], &article) < 0) |
if (section_list_append_article(p_section[i], &article) < 0) |
| 771 |
{ |
{ |
| 774 |
} |
} |
| 775 |
} |
} |
| 776 |
|
|
| 777 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 778 |
|
{ |
| 779 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 780 |
|
break; |
| 781 |
|
} |
| 782 |
|
|
| 783 |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 784 |
} |
} |
| 785 |
|
|
| 786 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 787 |
{ |
{ |
| 788 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 789 |
|
{ |
| 790 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 791 |
|
break; |
| 792 |
|
} |
| 793 |
|
|
| 794 |
section_first_aid = p_section[i]->p_article_head->aid; |
section_first_aid = p_section[i]->p_article_head->aid; |
| 795 |
|
|
| 796 |
for (j = 0; j < group_count; j += 2) |
for (j = 0; j < group_count; j += 2) |
| 808 |
printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid); |
printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid); |
| 809 |
} |
} |
| 810 |
} |
} |
| 811 |
|
|
| 812 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 813 |
|
{ |
| 814 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 815 |
|
break; |
| 816 |
|
} |
| 817 |
} |
} |
| 818 |
|
|
| 819 |
for (i = 0; i < section_count / 2; i++) |
for (i = 0; i < section_count / 2; i++) |
| 820 |
{ |
{ |
| 821 |
|
if (section_list_rw_lock(p_section[i]) < 0) |
| 822 |
|
{ |
| 823 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid); |
| 824 |
|
break; |
| 825 |
|
} |
| 826 |
|
|
| 827 |
|
if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0) |
| 828 |
|
{ |
| 829 |
|
printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid); |
| 830 |
|
|
| 831 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 832 |
|
{ |
| 833 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 834 |
|
} |
| 835 |
|
break; |
| 836 |
|
} |
| 837 |
|
|
| 838 |
section_first_aid = p_section[i]->p_article_head->aid; |
section_first_aid = p_section[i]->p_article_head->aid; |
| 839 |
|
|
| 840 |
for (j = 0; j < group_count; j++) |
for (j = 0; j < group_count; j++) |
| 855 |
// break; |
// break; |
| 856 |
} |
} |
| 857 |
} |
} |
| 858 |
|
|
| 859 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 860 |
|
{ |
| 861 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 862 |
|
break; |
| 863 |
|
} |
| 864 |
|
|
| 865 |
|
if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0) |
| 866 |
|
{ |
| 867 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid); |
| 868 |
|
|
| 869 |
|
if (section_list_rw_unlock(p_section[i]) < 0) |
| 870 |
|
{ |
| 871 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 872 |
|
} |
| 873 |
|
break; |
| 874 |
|
} |
| 875 |
} |
} |
| 876 |
|
|
| 877 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 878 |
{ |
{ |
| 879 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 880 |
|
{ |
| 881 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 882 |
|
break; |
| 883 |
|
} |
| 884 |
|
|
| 885 |
if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count)) |
if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count)) |
| 886 |
{ |
{ |
| 887 |
printf("Topic count error in section %d, %d != %d\n", i, |
printf("Topic count error in section %d, %d != %d\n", i, |
| 916 |
p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page)); |
p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page)); |
| 917 |
break; |
break; |
| 918 |
} |
} |
| 919 |
|
|
| 920 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 921 |
|
{ |
| 922 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 923 |
|
break; |
| 924 |
|
} |
| 925 |
|
} |
| 926 |
|
|
| 927 |
|
printf("Testing #6 ...\n"); |
| 928 |
|
|
| 929 |
|
for (i = 0; i < section_count; i++) |
| 930 |
|
{ |
| 931 |
|
if (section_list_rd_lock(p_section[i]) < 0) |
| 932 |
|
{ |
| 933 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 934 |
|
break; |
| 935 |
|
} |
| 936 |
|
} |
| 937 |
|
|
| 938 |
|
printf("Try rw_lock for 5 sec...\n"); |
| 939 |
|
if (section_list_try_rw_lock(NULL, 5) == 0) |
| 940 |
|
{ |
| 941 |
|
printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid); |
| 942 |
|
} |
| 943 |
|
|
| 944 |
|
for (i = 0; i < section_count; i++) |
| 945 |
|
{ |
| 946 |
|
if (section_list_rd_unlock(p_section[i]) < 0) |
| 947 |
|
{ |
| 948 |
|
printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid); |
| 949 |
|
break; |
| 950 |
|
} |
| 951 |
|
} |
| 952 |
|
|
| 953 |
|
if (section_list_try_rw_lock(NULL, 5) < 0) |
| 954 |
|
{ |
| 955 |
|
printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid); |
| 956 |
|
} |
| 957 |
|
|
| 958 |
|
for (i = 0; i < section_count; i++) |
| 959 |
|
{ |
| 960 |
|
if (section_list_try_rd_lock(p_section[i], 0) == 0) |
| 961 |
|
{ |
| 962 |
|
printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid); |
| 963 |
|
break; |
| 964 |
|
} |
| 965 |
|
} |
| 966 |
|
|
| 967 |
|
if (section_list_rw_unlock(NULL) < 0) |
| 968 |
|
{ |
| 969 |
|
printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid); |
| 970 |
} |
} |
| 971 |
|
|
| 972 |
printf("Press ENTER to exit..."); |
printf("Press ENTER to exit..."); |
| 973 |
getchar(); |
getchar(); |
| 974 |
|
|
| 975 |
|
section_list_cleanup(); |
| 976 |
article_block_cleanup(); |
article_block_cleanup(); |
| 977 |
|
trie_dict_cleanup(); |
| 978 |
|
|
| 979 |
|
if (unlink(TRIE_DICT_SHM_FILE) < 0) |
| 980 |
|
{ |
| 981 |
|
log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); |
| 982 |
|
return -1; |
| 983 |
|
} |
| 984 |
|
|
| 985 |
|
if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0) |
| 986 |
|
{ |
| 987 |
|
log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE); |
| 988 |
|
return -1; |
| 989 |
|
} |
| 990 |
|
|
| 991 |
|
if (unlink(SECTION_LIST_SHM_FILE) < 0) |
| 992 |
|
{ |
| 993 |
|
log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE); |
| 994 |
|
return -1; |
| 995 |
|
} |
| 996 |
|
|
| 997 |
log_end(); |
log_end(); |
| 998 |
|
|