| 19 |
#include "log.h" |
#include "log.h" |
| 20 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 21 |
#include "user_list.h" |
#include "user_list.h" |
| 22 |
|
#include "user_stat.h" |
| 23 |
#include <errno.h> |
#include <errno.h> |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
| 25 |
#include <string.h> |
#include <string.h> |
| 54 |
USER_ONLINE_LIST user_online_list[2]; |
USER_ONLINE_LIST user_online_list[2]; |
| 55 |
USER_ONLINE_LIST *p_online_current; |
USER_ONLINE_LIST *p_online_current; |
| 56 |
USER_ONLINE_LIST *p_online_new; |
USER_ONLINE_LIST *p_online_new; |
| 57 |
|
USER_STAT_MAP user_stat_map; |
| 58 |
}; |
}; |
| 59 |
typedef struct user_list_pool_t USER_LIST_POOL; |
typedef struct user_list_pool_t USER_LIST_POOL; |
| 60 |
|
|
| 108 |
{ |
{ |
| 109 |
return 1; |
return 1; |
| 110 |
} |
} |
| 111 |
|
else if (p1->id < p2->id) |
| 112 |
|
{ |
| 113 |
|
return -1; |
| 114 |
|
} |
| 115 |
|
else if (p1->id > p2->id) |
| 116 |
|
{ |
| 117 |
|
return 1; |
| 118 |
|
} |
| 119 |
return 0; |
return 0; |
| 120 |
} |
} |
| 121 |
|
|
| 126 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 127 |
int ret = 0; |
int ret = 0; |
| 128 |
int i; |
int i; |
| 129 |
|
int j; |
| 130 |
|
int32_t last_uid; |
| 131 |
|
size_t intro_buf_offset; |
| 132 |
|
size_t intro_len; |
| 133 |
|
|
| 134 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 135 |
{ |
{ |
| 137 |
return -1; |
return -1; |
| 138 |
} |
} |
| 139 |
|
|
| 140 |
|
if (p_list->user_count > 0) |
| 141 |
|
{ |
| 142 |
|
last_uid = p_list->users[p_list->user_count - 1].uid; |
| 143 |
|
} |
| 144 |
|
else |
| 145 |
|
{ |
| 146 |
|
last_uid = -1; |
| 147 |
|
} |
| 148 |
|
|
| 149 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 150 |
"SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, " |
"SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, visit_count, " |
| 151 |
"UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) " |
"UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(last_logout_dt), " |
| 152 |
|
"UNIX_TIMESTAMP(birthday), `introduction` " |
| 153 |
"FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID " |
"FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID " |
| 154 |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
| 155 |
"WHERE enable ORDER BY username"); |
"WHERE enable ORDER BY username"); |
| 168 |
goto cleanup; |
goto cleanup; |
| 169 |
} |
} |
| 170 |
|
|
| 171 |
|
intro_buf_offset = 0; |
| 172 |
i = 0; |
i = 0; |
| 173 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 174 |
{ |
{ |
| 183 |
p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4])); |
p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4])); |
| 184 |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
| 185 |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
| 186 |
p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7])); |
p_list->users[i].visit_count = (row[7] == NULL ? 0 : atoi(row[7])); |
| 187 |
p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8])); |
p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8])); |
| 188 |
p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9])); |
p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9])); |
| 189 |
|
p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10])); |
| 190 |
// index |
p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[11])); |
| 191 |
p_list->index_uid[i].uid = p_list->users[i].uid; |
intro_len = strlen((row[12] == NULL ? "" : row[12])); |
| 192 |
p_list->index_uid[i].id = i; |
if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset) |
| 193 |
|
{ |
| 194 |
|
log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i); |
| 195 |
|
break; |
| 196 |
|
} |
| 197 |
|
memcpy(p_list->user_intro_buf + intro_buf_offset, |
| 198 |
|
(row[12] == NULL ? "" : row[12]), |
| 199 |
|
intro_len + 1); |
| 200 |
|
p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset; |
| 201 |
|
intro_buf_offset += (intro_len + 1); |
| 202 |
|
|
| 203 |
i++; |
i++; |
| 204 |
if (i >= BBS_max_user_count) |
if (i >= BBS_max_user_count) |
| 210 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 211 |
rs = NULL; |
rs = NULL; |
| 212 |
|
|
| 213 |
p_list->user_count = i; |
if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed |
| 214 |
|
{ |
| 215 |
|
// Rebuild index |
| 216 |
|
for (j = 0; j < i; j++) |
| 217 |
|
{ |
| 218 |
|
p_list->index_uid[j].uid = p_list->users[j].uid; |
| 219 |
|
p_list->index_uid[j].id = j; |
| 220 |
|
} |
| 221 |
|
|
| 222 |
// Sort index |
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 223 |
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
|
| 224 |
|
#ifdef _DEBUG |
| 225 |
|
log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid); |
| 226 |
|
#endif |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
p_list->user_count = i; |
| 230 |
|
|
| 231 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 232 |
log_error("Loaded %d users\n", p_list->user_count); |
log_error("Loaded %d users\n", p_list->user_count); |
| 245 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 246 |
int ret = 0; |
int ret = 0; |
| 247 |
int i; |
int i; |
| 248 |
|
int j; |
| 249 |
|
int user_cnt; |
| 250 |
|
int guest_cnt; |
| 251 |
|
|
| 252 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 253 |
{ |
{ |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
i = 0; |
i = 0; |
| 280 |
|
user_cnt = 0; |
| 281 |
|
guest_cnt = 0; |
| 282 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 283 |
{ |
{ |
| 284 |
|
if (atoi(row[1]) == 0) // guest |
| 285 |
|
{ |
| 286 |
|
guest_cnt++; |
| 287 |
|
continue; |
| 288 |
|
} |
| 289 |
|
else |
| 290 |
|
{ |
| 291 |
|
user_cnt++; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
p_list->users[i].id = i; |
p_list->users[i].id = i; |
| 295 |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
| 296 |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
| 297 |
|
|
| 298 |
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) < 0) |
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) <= 0) |
| 299 |
{ |
{ |
| 300 |
log_error("query_user_info(%d) error\n", atoi(row[1])); |
log_error("query_user_info_by_uid(%d) error\n", atoi(row[1])); |
| 301 |
continue; |
continue; |
| 302 |
} |
} |
|
else if (ret == 0) // Guest |
|
|
{ |
|
|
p_list->users[i].user_info.id = -1; |
|
|
p_list->users[i].user_info.uid = 0; |
|
|
strncpy(p_list->users[i].user_info.username, "guest", sizeof(p_list->users[i].user_info.username) - 1); |
|
|
p_list->users[i].user_info.username[sizeof(p_list->users[i].user_info.username) - 1] = '\0'; |
|
|
strncpy(p_list->users[i].user_info.nickname, "Guest", sizeof(p_list->users[i].user_info.nickname) - 1); |
|
|
p_list->users[i].user_info.nickname[sizeof(p_list->users[i].user_info.nickname) - 1] = '\0'; |
|
|
p_list->users[i].user_info.gender = 'M'; |
|
|
p_list->users[i].user_info.gender_pub = 0; |
|
|
p_list->users[i].user_info.life = 150; |
|
|
p_list->users[i].user_info.exp = 0; |
|
|
p_list->users[i].user_info.signup_dt = 0; |
|
|
p_list->users[i].user_info.last_login_dt = 0; |
|
|
p_list->users[i].user_info.birthday = 0; |
|
|
} |
|
| 303 |
|
|
| 304 |
strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1); |
strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1); |
| 305 |
p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0'; |
p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0'; |
| 331 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 332 |
rs = NULL; |
rs = NULL; |
| 333 |
|
|
| 334 |
p_list->user_count = i; |
if (user_cnt > 0) |
| 335 |
|
{ |
| 336 |
|
// Rebuild index |
| 337 |
|
for (j = 0; j < user_cnt; j++) |
| 338 |
|
{ |
| 339 |
|
p_list->index_uid[j].uid = p_list->users[j].user_info.uid; |
| 340 |
|
p_list->index_uid[j].id = j; |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
qsort(p_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 344 |
|
|
| 345 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 346 |
log_error("Loaded %d users\n", p_list->user_count); |
log_error("Rebuild index of %d online users\n", user_cnt); |
| 347 |
|
#endif |
| 348 |
|
} |
| 349 |
|
|
| 350 |
|
p_list->user_count = user_cnt; |
| 351 |
|
p_list->guest_count = guest_cnt; |
| 352 |
|
|
| 353 |
|
#ifdef _DEBUG |
| 354 |
|
log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count); |
| 355 |
#endif |
#endif |
| 356 |
|
|
| 357 |
cleanup: |
cleanup: |
| 360 |
return ret; |
return ret; |
| 361 |
} |
} |
| 362 |
|
|
| 363 |
int user_list_pool_init(void) |
int user_list_pool_init(const char *filename) |
| 364 |
{ |
{ |
| 365 |
int shmid; |
int shmid; |
| 366 |
int semid; |
int semid; |
| 394 |
|
|
| 395 |
// Allocate shared memory |
// Allocate shared memory |
| 396 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 397 |
key = ftok(VAR_USER_LIST_SHM, proj_id); |
key = ftok(filename, proj_id); |
| 398 |
if (key == -1) |
if (key == -1) |
| 399 |
{ |
{ |
| 400 |
log_error("ftok(%s %d) error (%d)\n", VAR_USER_LIST_SHM, proj_id, errno); |
log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); |
| 401 |
return -2; |
return -2; |
| 402 |
} |
} |
| 403 |
|
|
| 450 |
p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]); |
p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]); |
| 451 |
p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]); |
p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]); |
| 452 |
|
|
| 453 |
|
user_stat_map_init(&(p_user_list_pool->user_stat_map)); |
| 454 |
|
|
| 455 |
return 0; |
return 0; |
| 456 |
} |
} |
| 457 |
|
|
| 535 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 536 |
USER_LIST *p_tmp; |
USER_LIST *p_tmp; |
| 537 |
USER_ONLINE_LIST *p_online_tmp; |
USER_ONLINE_LIST *p_online_tmp; |
| 538 |
|
int ret = 0; |
| 539 |
|
|
| 540 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 541 |
{ |
{ |
| 555 |
if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0) |
if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0) |
| 556 |
{ |
{ |
| 557 |
log_error("user_online_list_load() error\n"); |
log_error("user_online_list_load() error\n"); |
| 558 |
return -2; |
ret = -2; |
| 559 |
|
goto cleanup; |
| 560 |
} |
} |
| 561 |
} |
} |
| 562 |
else |
else |
| 564 |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
| 565 |
{ |
{ |
| 566 |
log_error("user_list_load() error\n"); |
log_error("user_list_load() error\n"); |
| 567 |
return -2; |
ret = -2; |
| 568 |
|
goto cleanup; |
| 569 |
} |
} |
| 570 |
} |
} |
| 571 |
|
|
| 572 |
mysql_close(db); |
mysql_close(db); |
| 573 |
|
db = NULL; |
| 574 |
|
|
| 575 |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
| 576 |
{ |
{ |
| 577 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 578 |
return -3; |
ret = -3; |
| 579 |
|
goto cleanup; |
| 580 |
} |
} |
| 581 |
|
|
| 582 |
if (online_user) |
if (online_user) |
| 597 |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
| 598 |
{ |
{ |
| 599 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 600 |
return -3; |
ret = -3; |
| 601 |
|
goto cleanup; |
| 602 |
} |
} |
| 603 |
|
|
| 604 |
return 0; |
cleanup: |
| 605 |
|
mysql_close(db); |
| 606 |
|
|
| 607 |
|
return ret; |
| 608 |
} |
} |
| 609 |
|
|
| 610 |
int user_list_try_rd_lock(int semid, int wait_sec) |
int user_list_try_rd_lock(int semid, int wait_sec) |
| 769 |
return -1; |
return -1; |
| 770 |
} |
} |
| 771 |
|
|
| 772 |
|
*p_user_count = 0; |
| 773 |
|
*p_page_count = 0; |
| 774 |
|
|
| 775 |
// acquire lock of user list |
// acquire lock of user list |
| 776 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 777 |
{ |
{ |
| 825 |
return -1; |
return -1; |
| 826 |
} |
} |
| 827 |
|
|
| 828 |
|
*p_user_count = 0; |
| 829 |
|
*p_page_count = 0; |
| 830 |
|
|
| 831 |
// acquire lock of user list |
// acquire lock of user list |
| 832 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 833 |
{ |
{ |
| 871 |
return ret; |
return ret; |
| 872 |
} |
} |
| 873 |
|
|
| 874 |
|
int get_user_list_count(int *p_user_cnt) |
| 875 |
|
{ |
| 876 |
|
if (p_user_cnt == NULL) |
| 877 |
|
{ |
| 878 |
|
log_error("NULL pointer error\n"); |
| 879 |
|
return -1; |
| 880 |
|
} |
| 881 |
|
|
| 882 |
|
// acquire lock of user list |
| 883 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 884 |
|
{ |
| 885 |
|
log_error("user_list_rd_lock() error\n"); |
| 886 |
|
return -2; |
| 887 |
|
} |
| 888 |
|
|
| 889 |
|
*p_user_cnt = p_user_list_pool->p_current->user_count; |
| 890 |
|
|
| 891 |
|
// release lock of user list |
| 892 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 893 |
|
{ |
| 894 |
|
log_error("user_list_rd_unlock() error\n"); |
| 895 |
|
return -2; |
| 896 |
|
} |
| 897 |
|
|
| 898 |
|
return 0; |
| 899 |
|
} |
| 900 |
|
|
| 901 |
|
int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt) |
| 902 |
|
{ |
| 903 |
|
if (p_user_cnt == NULL || p_guest_cnt == NULL) |
| 904 |
|
{ |
| 905 |
|
log_error("NULL pointer error\n"); |
| 906 |
|
return -1; |
| 907 |
|
} |
| 908 |
|
|
| 909 |
|
// acquire lock of user list |
| 910 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 911 |
|
{ |
| 912 |
|
log_error("user_list_rd_lock() error\n"); |
| 913 |
|
return -2; |
| 914 |
|
} |
| 915 |
|
|
| 916 |
|
*p_user_cnt = p_user_list_pool->p_online_current->user_count; |
| 917 |
|
*p_guest_cnt = p_user_list_pool->p_online_current->guest_count; |
| 918 |
|
|
| 919 |
|
// release lock of user list |
| 920 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 921 |
|
{ |
| 922 |
|
log_error("user_list_rd_unlock() error\n"); |
| 923 |
|
return -2; |
| 924 |
|
} |
| 925 |
|
|
| 926 |
|
return 0; |
| 927 |
|
} |
| 928 |
|
|
| 929 |
int query_user_info(int32_t id, USER_INFO *p_user) |
int query_user_info(int32_t id, USER_INFO *p_user) |
| 930 |
{ |
{ |
| 931 |
int ret = 0; |
int ret = 0; |
| 988 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 989 |
if (uid < p_user_list_pool->p_current->index_uid[mid].uid) |
if (uid < p_user_list_pool->p_current->index_uid[mid].uid) |
| 990 |
{ |
{ |
| 991 |
right = mid; |
right = mid - 1; |
| 992 |
} |
} |
| 993 |
else if (uid > p_user_list_pool->p_current->index_uid[mid].uid) |
else if (uid > p_user_list_pool->p_current->index_uid[mid].uid) |
| 994 |
{ |
{ |
| 1004 |
if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found |
if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found |
| 1005 |
{ |
{ |
| 1006 |
id = p_user_list_pool->p_current->index_uid[left].id; |
id = p_user_list_pool->p_current->index_uid[left].id; |
| 1007 |
if ((ret = query_user_info(id, p_user)) <= 0) |
*p_user = p_user_list_pool->p_current->users[id]; |
| 1008 |
{ |
ret = 1; |
|
log_error("query_user_info(id=%d) error: %d\n", id, ret); |
|
|
} |
|
|
else |
|
|
{ |
|
|
ret = 1; |
|
|
} |
|
| 1009 |
} |
} |
| 1010 |
|
|
| 1011 |
// release lock of user list |
// release lock of user list |
| 1050 |
|
|
| 1051 |
return ret; |
return ret; |
| 1052 |
} |
} |
| 1053 |
|
|
| 1054 |
|
int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id) |
| 1055 |
|
{ |
| 1056 |
|
int left; |
| 1057 |
|
int right; |
| 1058 |
|
int mid; |
| 1059 |
|
int32_t id; |
| 1060 |
|
int ret = 0; |
| 1061 |
|
int i; |
| 1062 |
|
int user_cnt; |
| 1063 |
|
|
| 1064 |
|
if (p_users == NULL || p_user_cnt == NULL) |
| 1065 |
|
{ |
| 1066 |
|
log_error("NULL pointer error\n"); |
| 1067 |
|
return -1; |
| 1068 |
|
} |
| 1069 |
|
|
| 1070 |
|
user_cnt = *p_user_cnt; |
| 1071 |
|
*p_user_cnt = 0; |
| 1072 |
|
|
| 1073 |
|
// acquire lock of user list |
| 1074 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1075 |
|
{ |
| 1076 |
|
log_error("user_list_rd_lock() error\n"); |
| 1077 |
|
return -2; |
| 1078 |
|
} |
| 1079 |
|
|
| 1080 |
|
left = start_id; |
| 1081 |
|
right = p_user_list_pool->p_online_current->user_count - 1; |
| 1082 |
|
|
| 1083 |
|
while (left < right) |
| 1084 |
|
{ |
| 1085 |
|
mid = (left + right) / 2; |
| 1086 |
|
if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1087 |
|
{ |
| 1088 |
|
right = mid - 1; |
| 1089 |
|
} |
| 1090 |
|
else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1091 |
|
{ |
| 1092 |
|
left = mid + 1; |
| 1093 |
|
} |
| 1094 |
|
else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1095 |
|
{ |
| 1096 |
|
left = mid; |
| 1097 |
|
break; |
| 1098 |
|
} |
| 1099 |
|
} |
| 1100 |
|
|
| 1101 |
|
if (uid == p_user_list_pool->p_online_current->index_uid[left].uid) |
| 1102 |
|
{ |
| 1103 |
|
right = left; |
| 1104 |
|
left = start_id; |
| 1105 |
|
|
| 1106 |
|
while (left < right) |
| 1107 |
|
{ |
| 1108 |
|
mid = (left + right) / 2; |
| 1109 |
|
if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1110 |
|
{ |
| 1111 |
|
right = mid; |
| 1112 |
|
} |
| 1113 |
|
else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1114 |
|
{ |
| 1115 |
|
left = mid + 1; |
| 1116 |
|
} |
| 1117 |
|
} |
| 1118 |
|
|
| 1119 |
|
for (i = 0; |
| 1120 |
|
left < p_user_list_pool->p_online_current->user_count && i < user_cnt && |
| 1121 |
|
uid == p_user_list_pool->p_online_current->index_uid[left].uid; |
| 1122 |
|
left++, i++) |
| 1123 |
|
{ |
| 1124 |
|
id = p_user_list_pool->p_online_current->index_uid[left].id; |
| 1125 |
|
p_users[i] = p_user_list_pool->p_online_current->users[id]; |
| 1126 |
|
} |
| 1127 |
|
|
| 1128 |
|
if (i > 0) |
| 1129 |
|
{ |
| 1130 |
|
*p_user_cnt = i; |
| 1131 |
|
ret = 1; |
| 1132 |
|
} |
| 1133 |
|
} |
| 1134 |
|
|
| 1135 |
|
// release lock of user list |
| 1136 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1137 |
|
{ |
| 1138 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1139 |
|
ret = -1; |
| 1140 |
|
} |
| 1141 |
|
|
| 1142 |
|
return ret; |
| 1143 |
|
} |
| 1144 |
|
|
| 1145 |
|
int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid) |
| 1146 |
|
{ |
| 1147 |
|
int left; |
| 1148 |
|
int right; |
| 1149 |
|
int mid; |
| 1150 |
|
int ret = 0; |
| 1151 |
|
int i; |
| 1152 |
|
|
| 1153 |
|
if (p_uid_list == NULL || p_user_cnt == NULL) |
| 1154 |
|
{ |
| 1155 |
|
log_error("NULL pointer error\n"); |
| 1156 |
|
return -1; |
| 1157 |
|
} |
| 1158 |
|
|
| 1159 |
|
// acquire lock of user list |
| 1160 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1161 |
|
{ |
| 1162 |
|
log_error("user_list_rd_lock() error\n"); |
| 1163 |
|
return -2; |
| 1164 |
|
} |
| 1165 |
|
|
| 1166 |
|
left = 0; |
| 1167 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1168 |
|
|
| 1169 |
|
while (left < right) |
| 1170 |
|
{ |
| 1171 |
|
mid = (left + right) / 2; |
| 1172 |
|
if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid) |
| 1173 |
|
{ |
| 1174 |
|
right = mid - 1; |
| 1175 |
|
} |
| 1176 |
|
else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid) |
| 1177 |
|
{ |
| 1178 |
|
left = mid + 1; |
| 1179 |
|
} |
| 1180 |
|
else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid) |
| 1181 |
|
{ |
| 1182 |
|
left = mid; |
| 1183 |
|
break; |
| 1184 |
|
} |
| 1185 |
|
} |
| 1186 |
|
|
| 1187 |
|
for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++) |
| 1188 |
|
{ |
| 1189 |
|
p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid; |
| 1190 |
|
} |
| 1191 |
|
*p_user_cnt = i; |
| 1192 |
|
|
| 1193 |
|
// release lock of user list |
| 1194 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1195 |
|
{ |
| 1196 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1197 |
|
ret = -1; |
| 1198 |
|
} |
| 1199 |
|
|
| 1200 |
|
return ret; |
| 1201 |
|
} |
| 1202 |
|
|
| 1203 |
|
int user_stat_update(void) |
| 1204 |
|
{ |
| 1205 |
|
return user_stat_map_update(&(p_user_list_pool->user_stat_map)); |
| 1206 |
|
} |
| 1207 |
|
|
| 1208 |
|
int user_article_cnt_inc(int32_t uid, int n) |
| 1209 |
|
{ |
| 1210 |
|
return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n); |
| 1211 |
|
} |
| 1212 |
|
|
| 1213 |
|
int get_user_article_cnt(int32_t uid) |
| 1214 |
|
{ |
| 1215 |
|
const USER_STAT *p_stat; |
| 1216 |
|
int ret; |
| 1217 |
|
|
| 1218 |
|
ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat); |
| 1219 |
|
if (ret < 0) |
| 1220 |
|
{ |
| 1221 |
|
log_error("user_stat_get(uid=%d) error: %d\n", uid, ret); |
| 1222 |
|
return -1; |
| 1223 |
|
} |
| 1224 |
|
else if (ret == 0) // user not found |
| 1225 |
|
{ |
| 1226 |
|
return -1; |
| 1227 |
|
} |
| 1228 |
|
|
| 1229 |
|
return p_stat->article_count; |
| 1230 |
|
} |