| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
user_list.c - description |
/* |
| 3 |
------------------- |
* user_list |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - data model and basic operations of (online) user list |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include "database.h" |
#include "database.h" |
| 11 |
#include "log.h" |
#include "log.h" |
| 12 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 13 |
#include "user_list.h" |
#include "user_list.h" |
| 14 |
|
#include "user_stat.h" |
| 15 |
#include <errno.h> |
#include <errno.h> |
| 16 |
#include <stdlib.h> |
#include <stdlib.h> |
| 17 |
#include <string.h> |
#include <string.h> |
| 46 |
USER_ONLINE_LIST user_online_list[2]; |
USER_ONLINE_LIST user_online_list[2]; |
| 47 |
USER_ONLINE_LIST *p_online_current; |
USER_ONLINE_LIST *p_online_current; |
| 48 |
USER_ONLINE_LIST *p_online_new; |
USER_ONLINE_LIST *p_online_new; |
| 49 |
|
USER_STAT_MAP user_stat_map; |
| 50 |
|
int user_login_count; |
| 51 |
}; |
}; |
| 52 |
typedef struct user_list_pool_t USER_LIST_POOL; |
typedef struct user_list_pool_t USER_LIST_POOL; |
| 53 |
|
|
| 66 |
{"BBS_NET", "站点穿梭"}, |
{"BBS_NET", "站点穿梭"}, |
| 67 |
{"CHICKEN", "电子小鸡"}, |
{"CHICKEN", "电子小鸡"}, |
| 68 |
{"EDIT_ARTICLE", "修改文章"}, |
{"EDIT_ARTICLE", "修改文章"}, |
| 69 |
|
{"LOGIN", "进入大厅"}, |
| 70 |
{"MENU", "菜单选择"}, |
{"MENU", "菜单选择"}, |
| 71 |
{"POST_ARTICLE", "撰写文章"}, |
{"POST_ARTICLE", "撰写文章"}, |
| 72 |
{"REPLY_ARTICLE", "回复文章"}, |
{"REPLY_ARTICLE", "回复文章"}, |
| 73 |
{"USER_LIST", "查花名册"}, |
{"USER_LIST", "查花名册"}, |
| 74 |
{"USER_ONLINE", "环顾四周"}, |
{"USER_ONLINE", "环顾四周"}, |
| 75 |
{"VIEW_ARTICLE", "阅读文章"}, |
{"VIEW_ARTICLE", "阅读文章"}, |
| 76 |
{"VIEW_FILE", "查看文档"}}; |
{"VIEW_FILE", "查看文档"}, |
| 77 |
|
{"WWW", "Web浏览"}}; |
| 78 |
|
|
| 79 |
const int user_action_map_size = 11; |
const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP); |
| 80 |
|
|
| 81 |
static int user_list_try_rd_lock(int semid, int wait_sec); |
static int user_list_try_rd_lock(int semid, int wait_sec); |
| 82 |
static int user_list_try_rw_lock(int semid, int wait_sec); |
static int user_list_try_rw_lock(int semid, int wait_sec); |
| 86 |
static int user_list_rw_lock(int semid); |
static int user_list_rw_lock(int semid); |
| 87 |
|
|
| 88 |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
| 89 |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list); |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list); |
| 90 |
|
static int user_login_count_load(MYSQL *db); |
| 91 |
|
|
| 92 |
|
static int user_info_index_uid_comp(const void *ptr1, const void *ptr2) |
| 93 |
|
{ |
| 94 |
|
const USER_INFO_INDEX_UID *p1 = ptr1; |
| 95 |
|
const USER_INFO_INDEX_UID *p2 = ptr2; |
| 96 |
|
|
| 97 |
|
if (p1->uid < p2->uid) |
| 98 |
|
{ |
| 99 |
|
return -1; |
| 100 |
|
} |
| 101 |
|
else if (p1->uid > p2->uid) |
| 102 |
|
{ |
| 103 |
|
return 1; |
| 104 |
|
} |
| 105 |
|
else if (p1->id < p2->id) |
| 106 |
|
{ |
| 107 |
|
return -1; |
| 108 |
|
} |
| 109 |
|
else if (p1->id > p2->id) |
| 110 |
|
{ |
| 111 |
|
return 1; |
| 112 |
|
} |
| 113 |
|
return 0; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
| 117 |
{ |
{ |
| 120 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 121 |
int ret = 0; |
int ret = 0; |
| 122 |
int i; |
int i; |
| 123 |
|
int j; |
| 124 |
|
int32_t last_uid; |
| 125 |
|
size_t intro_buf_offset; |
| 126 |
|
size_t intro_len; |
| 127 |
|
|
| 128 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 129 |
{ |
{ |
| 131 |
return -1; |
return -1; |
| 132 |
} |
} |
| 133 |
|
|
| 134 |
|
if (p_list->user_count > 0) |
| 135 |
|
{ |
| 136 |
|
last_uid = p_list->users[p_list->user_count - 1].uid; |
| 137 |
|
} |
| 138 |
|
else |
| 139 |
|
{ |
| 140 |
|
last_uid = -1; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 144 |
"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, " |
| 145 |
"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), " |
| 146 |
|
"UNIX_TIMESTAMP(birthday), `introduction` " |
| 147 |
"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 " |
| 148 |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
| 149 |
"WHERE enable ORDER BY UID"); |
"WHERE enable ORDER BY username"); |
| 150 |
|
|
| 151 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 152 |
{ |
{ |
| 162 |
goto cleanup; |
goto cleanup; |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
|
intro_buf_offset = 0; |
| 166 |
i = 0; |
i = 0; |
| 167 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 168 |
{ |
{ |
| 169 |
|
// record |
| 170 |
|
p_list->users[i].id = i; |
| 171 |
p_list->users[i].uid = atoi(row[0]); |
p_list->users[i].uid = atoi(row[0]); |
| 172 |
strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1); |
strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1); |
| 173 |
p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0'; |
p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0'; |
| 177 |
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])); |
| 178 |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
| 179 |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
| 180 |
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])); |
| 181 |
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])); |
| 182 |
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])); |
| 183 |
|
p_list->users[i].last_logout_dt = (row[10] == NULL ? 0 : atol(row[10])); |
| 184 |
|
p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[11])); |
| 185 |
|
intro_len = strlen((row[12] == NULL ? "" : row[12])); |
| 186 |
|
if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset) |
| 187 |
|
{ |
| 188 |
|
log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i); |
| 189 |
|
break; |
| 190 |
|
} |
| 191 |
|
memcpy(p_list->user_intro_buf + intro_buf_offset, |
| 192 |
|
(row[12] == NULL ? "" : row[12]), |
| 193 |
|
intro_len + 1); |
| 194 |
|
p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset; |
| 195 |
|
intro_buf_offset += (intro_len + 1); |
| 196 |
|
|
| 197 |
i++; |
i++; |
| 198 |
if (i >= BBS_max_user_count) |
if (i >= BBS_max_user_count) |
| 204 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 205 |
rs = NULL; |
rs = NULL; |
| 206 |
|
|
| 207 |
|
if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed |
| 208 |
|
{ |
| 209 |
|
// Rebuild index |
| 210 |
|
for (j = 0; j < i; j++) |
| 211 |
|
{ |
| 212 |
|
p_list->index_uid[j].uid = p_list->users[j].uid; |
| 213 |
|
p_list->index_uid[j].id = j; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 217 |
|
|
| 218 |
|
#ifdef _DEBUG |
| 219 |
|
log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid); |
| 220 |
|
#endif |
| 221 |
|
} |
| 222 |
|
|
| 223 |
p_list->user_count = i; |
p_list->user_count = i; |
| 224 |
|
|
| 225 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 232 |
return ret; |
return ret; |
| 233 |
} |
} |
| 234 |
|
|
| 235 |
int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list) |
int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list) |
| 236 |
{ |
{ |
| 237 |
MYSQL_RES *rs = NULL; |
MYSQL_RES *rs = NULL; |
| 238 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 239 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 240 |
int ret = 0; |
int ret = 0; |
| 241 |
int i; |
int i; |
| 242 |
|
int j; |
| 243 |
|
int user_cnt; |
| 244 |
|
int guest_cnt; |
| 245 |
|
|
| 246 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_online_list == NULL) |
| 247 |
{ |
{ |
| 248 |
log_error("NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 249 |
return -1; |
return -1; |
| 271 |
} |
} |
| 272 |
|
|
| 273 |
i = 0; |
i = 0; |
| 274 |
|
user_cnt = 0; |
| 275 |
|
guest_cnt = 0; |
| 276 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 277 |
{ |
{ |
| 278 |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
if (atoi(row[1]) == 0) // guest |
| 279 |
|
{ |
| 280 |
|
guest_cnt++; |
| 281 |
|
continue; |
| 282 |
|
} |
| 283 |
|
else |
| 284 |
|
{ |
| 285 |
|
user_cnt++; |
| 286 |
|
} |
| 287 |
|
|
| 288 |
|
p_online_list->users[i].id = i; |
| 289 |
|
strncpy(p_online_list->users[i].session_id, row[0], sizeof(p_online_list->users[i].session_id) - 1); |
| 290 |
|
p_online_list->users[i].session_id[sizeof(p_online_list->users[i].session_id) - 1] = '\0'; |
| 291 |
|
|
| 292 |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_online_list->users[i].user_info), NULL, 0)) <= 0) |
|
if (query_user_info(atoi(row[1]), &(p_list->users[i].user_info)) <= 0) |
|
| 293 |
{ |
{ |
| 294 |
log_error("query_user_info(%d) error\n", atoi(row[1])); |
log_error("query_user_info_by_uid(%d) error\n", atoi(row[1])); |
| 295 |
continue; |
continue; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
strncpy(p_list->users[i].ip, row[2], sizeof(p_list->users[i].ip) - 1); |
strncpy(p_online_list->users[i].ip, row[2], sizeof(p_online_list->users[i].ip) - 1); |
| 299 |
p_list->users[i].ip[sizeof(p_list->users[i].ip) - 1] = '\0'; |
p_online_list->users[i].ip[sizeof(p_online_list->users[i].ip) - 1] = '\0'; |
| 300 |
|
|
| 301 |
strncpy(p_list->users[i].current_action, row[3], sizeof(p_list->users[i].current_action) - 1); |
strncpy(p_online_list->users[i].current_action, row[3], sizeof(p_online_list->users[i].current_action) - 1); |
| 302 |
p_list->users[i].current_action[sizeof(p_list->users[i].current_action) - 1] = '\0'; |
p_online_list->users[i].current_action[sizeof(p_online_list->users[i].current_action) - 1] = '\0'; |
| 303 |
p_list->users[i].current_action_title = NULL; |
p_online_list->users[i].current_action_title = NULL; |
| 304 |
if (p_list->users[i].current_action[0] == '\0') |
if (p_online_list->users[i].current_action[0] == '\0') |
| 305 |
{ |
{ |
| 306 |
p_list->users[i].current_action_title = "Web浏览"; |
p_online_list->users[i].current_action_title = ""; |
| 307 |
} |
} |
| 308 |
else if (trie_dict_get(p_trie_action_dict, p_list->users[i].current_action, (int64_t *)(&(p_list->users[i].current_action_title))) < 0) |
else if (trie_dict_get(p_trie_action_dict, p_online_list->users[i].current_action, (int64_t *)(&(p_online_list->users[i].current_action_title))) < 0) |
| 309 |
{ |
{ |
| 310 |
log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n", |
log_error("trie_dict_get(p_trie_action_dict, %s) error on session_id=%s\n", |
| 311 |
p_list->users[i].current_action, p_list->users[i].session_id); |
p_online_list->users[i].current_action, p_online_list->users[i].session_id); |
| 312 |
continue; |
continue; |
| 313 |
} |
} |
| 314 |
|
|
| 315 |
p_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4])); |
p_online_list->users[i].login_tm = (row[4] == NULL ? 0 : atol(row[4])); |
| 316 |
p_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5])); |
p_online_list->users[i].last_tm = (row[5] == NULL ? 0 : atol(row[5])); |
| 317 |
|
|
| 318 |
i++; |
i++; |
| 319 |
if (i >= BBS_max_user_online_count) |
if (i >= BBS_max_user_online_count) |
| 325 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 326 |
rs = NULL; |
rs = NULL; |
| 327 |
|
|
| 328 |
p_list->user_count = i; |
if (user_cnt > 0) |
| 329 |
|
{ |
| 330 |
|
// Rebuild index |
| 331 |
|
for (j = 0; j < user_cnt; j++) |
| 332 |
|
{ |
| 333 |
|
p_online_list->index_uid[j].uid = p_online_list->users[j].user_info.uid; |
| 334 |
|
p_online_list->index_uid[j].id = j; |
| 335 |
|
} |
| 336 |
|
|
| 337 |
|
qsort(p_online_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 338 |
|
|
| 339 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 340 |
log_error("Loaded %d users\n", p_list->user_count); |
log_error("Rebuild index of %d online users\n", user_cnt); |
| 341 |
|
#endif |
| 342 |
|
} |
| 343 |
|
|
| 344 |
|
p_online_list->user_count = user_cnt; |
| 345 |
|
p_online_list->guest_count = guest_cnt; |
| 346 |
|
|
| 347 |
|
#ifdef _DEBUG |
| 348 |
|
log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count); |
| 349 |
#endif |
#endif |
| 350 |
|
|
| 351 |
cleanup: |
cleanup: |
| 354 |
return ret; |
return ret; |
| 355 |
} |
} |
| 356 |
|
|
| 357 |
int user_list_pool_init(void) |
int user_login_count_load(MYSQL *db) |
| 358 |
|
{ |
| 359 |
|
MYSQL_RES *rs = NULL; |
| 360 |
|
MYSQL_ROW row; |
| 361 |
|
char sql[SQL_BUFFER_LEN]; |
| 362 |
|
|
| 363 |
|
if (db == NULL) |
| 364 |
|
{ |
| 365 |
|
log_error("NULL pointer error\n"); |
| 366 |
|
return -1; |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
snprintf(sql, sizeof(sql), |
| 370 |
|
"SELECT ID FROM user_login_log ORDER BY ID DESC LIMIT 1"); |
| 371 |
|
if (mysql_query(db, sql) != 0) |
| 372 |
|
{ |
| 373 |
|
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
| 374 |
|
return -2; |
| 375 |
|
} |
| 376 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 377 |
|
{ |
| 378 |
|
log_error("Get user_login_log data failed\n"); |
| 379 |
|
return -2; |
| 380 |
|
} |
| 381 |
|
if ((row = mysql_fetch_row(rs))) |
| 382 |
|
{ |
| 383 |
|
p_user_list_pool->user_login_count = atoi(row[0]); |
| 384 |
|
} |
| 385 |
|
mysql_free_result(rs); |
| 386 |
|
|
| 387 |
|
return 0; |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
int user_list_pool_init(const char *filename) |
| 391 |
{ |
{ |
| 392 |
int shmid; |
int shmid; |
| 393 |
int semid; |
int semid; |
| 421 |
|
|
| 422 |
// Allocate shared memory |
// Allocate shared memory |
| 423 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 424 |
key = ftok(VAR_USER_LIST_SHM, proj_id); |
key = ftok(filename, proj_id); |
| 425 |
if (key == -1) |
if (key == -1) |
| 426 |
{ |
{ |
| 427 |
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); |
| 428 |
return -2; |
return -2; |
| 429 |
} |
} |
| 430 |
|
|
| 477 |
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]); |
| 478 |
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]); |
| 479 |
|
|
| 480 |
|
user_stat_map_init(&(p_user_list_pool->user_stat_map)); |
| 481 |
|
|
| 482 |
return 0; |
return 0; |
| 483 |
} |
} |
| 484 |
|
|
| 503 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 504 |
} |
} |
| 505 |
|
|
| 506 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 507 |
{ |
{ |
| 508 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 509 |
} |
} |
| 562 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 563 |
USER_LIST *p_tmp; |
USER_LIST *p_tmp; |
| 564 |
USER_ONLINE_LIST *p_online_tmp; |
USER_ONLINE_LIST *p_online_tmp; |
| 565 |
|
int ret = 0; |
| 566 |
|
|
| 567 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 568 |
{ |
{ |
| 582 |
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) |
| 583 |
{ |
{ |
| 584 |
log_error("user_online_list_load() error\n"); |
log_error("user_online_list_load() error\n"); |
| 585 |
return -2; |
ret = -2; |
| 586 |
|
goto cleanup; |
| 587 |
|
} |
| 588 |
|
|
| 589 |
|
if (user_login_count_load(db) < 0) |
| 590 |
|
{ |
| 591 |
|
log_error("user_login_count_load() error\n"); |
| 592 |
|
ret = -2; |
| 593 |
|
goto cleanup; |
| 594 |
} |
} |
| 595 |
} |
} |
| 596 |
else |
else |
| 598 |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
| 599 |
{ |
{ |
| 600 |
log_error("user_list_load() error\n"); |
log_error("user_list_load() error\n"); |
| 601 |
return -2; |
ret = -2; |
| 602 |
|
goto cleanup; |
| 603 |
} |
} |
| 604 |
} |
} |
| 605 |
|
|
| 606 |
mysql_close(db); |
mysql_close(db); |
| 607 |
|
db = NULL; |
| 608 |
|
|
| 609 |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
| 610 |
{ |
{ |
| 611 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 612 |
return -3; |
ret = -3; |
| 613 |
|
goto cleanup; |
| 614 |
} |
} |
| 615 |
|
|
| 616 |
if (online_user) |
if (online_user) |
| 631 |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
| 632 |
{ |
{ |
| 633 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 634 |
return -3; |
ret = -3; |
| 635 |
|
goto cleanup; |
| 636 |
} |
} |
| 637 |
|
|
| 638 |
return 0; |
cleanup: |
| 639 |
|
mysql_close(db); |
| 640 |
|
|
| 641 |
|
return ret; |
| 642 |
} |
} |
| 643 |
|
|
| 644 |
int user_list_try_rd_lock(int semid, int wait_sec) |
int user_list_try_rd_lock(int semid, int wait_sec) |
| 803 |
return -1; |
return -1; |
| 804 |
} |
} |
| 805 |
|
|
| 806 |
|
*p_user_count = 0; |
| 807 |
|
*p_page_count = 0; |
| 808 |
|
|
| 809 |
// acquire lock of user list |
// acquire lock of user list |
| 810 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 811 |
{ |
{ |
| 859 |
return -1; |
return -1; |
| 860 |
} |
} |
| 861 |
|
|
| 862 |
|
*p_user_count = 0; |
| 863 |
|
*p_page_count = 0; |
| 864 |
|
|
| 865 |
// acquire lock of user list |
// acquire lock of user list |
| 866 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 867 |
{ |
{ |
| 905 |
return ret; |
return ret; |
| 906 |
} |
} |
| 907 |
|
|
| 908 |
int query_user_info(int32_t uid, USER_INFO *p_user) |
int get_user_list_count(int *p_user_cnt) |
| 909 |
|
{ |
| 910 |
|
if (p_user_cnt == NULL) |
| 911 |
|
{ |
| 912 |
|
log_error("NULL pointer error\n"); |
| 913 |
|
return -1; |
| 914 |
|
} |
| 915 |
|
|
| 916 |
|
// acquire lock of user list |
| 917 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 918 |
|
{ |
| 919 |
|
log_error("user_list_rd_lock() error\n"); |
| 920 |
|
return -2; |
| 921 |
|
} |
| 922 |
|
|
| 923 |
|
*p_user_cnt = p_user_list_pool->p_current->user_count; |
| 924 |
|
|
| 925 |
|
// release lock of user list |
| 926 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 927 |
|
{ |
| 928 |
|
log_error("user_list_rd_unlock() error\n"); |
| 929 |
|
return -2; |
| 930 |
|
} |
| 931 |
|
|
| 932 |
|
return 0; |
| 933 |
|
} |
| 934 |
|
|
| 935 |
|
int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt) |
| 936 |
|
{ |
| 937 |
|
if (p_user_cnt == NULL || p_guest_cnt == NULL) |
| 938 |
|
{ |
| 939 |
|
log_error("NULL pointer error\n"); |
| 940 |
|
return -1; |
| 941 |
|
} |
| 942 |
|
|
| 943 |
|
// acquire lock of user list |
| 944 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 945 |
|
{ |
| 946 |
|
log_error("user_list_rd_lock() error\n"); |
| 947 |
|
return -2; |
| 948 |
|
} |
| 949 |
|
|
| 950 |
|
*p_user_cnt = p_user_list_pool->p_online_current->user_count; |
| 951 |
|
*p_guest_cnt = p_user_list_pool->p_online_current->guest_count; |
| 952 |
|
|
| 953 |
|
// release lock of user list |
| 954 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 955 |
|
{ |
| 956 |
|
log_error("user_list_rd_unlock() error\n"); |
| 957 |
|
return -2; |
| 958 |
|
} |
| 959 |
|
|
| 960 |
|
return 0; |
| 961 |
|
} |
| 962 |
|
|
| 963 |
|
int get_user_login_count(int *p_login_cnt) |
| 964 |
|
{ |
| 965 |
|
if (p_login_cnt == NULL) |
| 966 |
|
{ |
| 967 |
|
log_error("NULL pointer error\n"); |
| 968 |
|
return -1; |
| 969 |
|
} |
| 970 |
|
|
| 971 |
|
*p_login_cnt = p_user_list_pool->user_login_count; |
| 972 |
|
|
| 973 |
|
return 0; |
| 974 |
|
} |
| 975 |
|
|
| 976 |
|
int query_user_info(int32_t id, USER_INFO *p_user) |
| 977 |
|
{ |
| 978 |
|
int ret = 0; |
| 979 |
|
|
| 980 |
|
if (p_user == NULL) |
| 981 |
|
{ |
| 982 |
|
log_error("NULL pointer error\n"); |
| 983 |
|
return -1; |
| 984 |
|
} |
| 985 |
|
|
| 986 |
|
// acquire lock of user list |
| 987 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 988 |
|
{ |
| 989 |
|
log_error("user_list_rd_lock() error\n"); |
| 990 |
|
return -2; |
| 991 |
|
} |
| 992 |
|
|
| 993 |
|
if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found |
| 994 |
|
{ |
| 995 |
|
*p_user = p_user_list_pool->p_current->users[id]; |
| 996 |
|
ret = 1; |
| 997 |
|
} |
| 998 |
|
|
| 999 |
|
// release lock of user list |
| 1000 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1001 |
|
{ |
| 1002 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1003 |
|
ret = -1; |
| 1004 |
|
} |
| 1005 |
|
|
| 1006 |
|
return ret; |
| 1007 |
|
} |
| 1008 |
|
|
| 1009 |
|
int query_user_info_by_uid(int32_t uid, USER_INFO *p_user, char *p_intro_buf, size_t intro_buf_len) |
| 1010 |
{ |
{ |
| 1011 |
int left; |
int left; |
| 1012 |
int right; |
int right; |
| 1013 |
int mid; |
int mid; |
| 1014 |
|
int32_t id; |
| 1015 |
int ret = 0; |
int ret = 0; |
| 1016 |
|
|
| 1017 |
if (p_user == NULL) |
if (p_user == NULL) |
| 1033 |
while (left < right) |
while (left < right) |
| 1034 |
{ |
{ |
| 1035 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 1036 |
if (uid < p_user_list_pool->p_current->users[mid].uid) |
if (uid < p_user_list_pool->p_current->index_uid[mid].uid) |
| 1037 |
{ |
{ |
| 1038 |
right = mid; |
right = mid - 1; |
| 1039 |
} |
} |
| 1040 |
else if (uid > p_user_list_pool->p_current->users[mid].uid) |
else if (uid > p_user_list_pool->p_current->index_uid[mid].uid) |
| 1041 |
{ |
{ |
| 1042 |
left = mid + 1; |
left = mid + 1; |
| 1043 |
} |
} |
| 1044 |
else // if (uid == p_user_list_pool->p_current->users[mid].uid) |
else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid) |
| 1045 |
{ |
{ |
| 1046 |
left = mid; |
left = mid; |
| 1047 |
break; |
break; |
| 1048 |
} |
} |
| 1049 |
} |
} |
| 1050 |
|
|
| 1051 |
if (uid == p_user_list_pool->p_current->users[left].uid) // Found |
if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found |
| 1052 |
{ |
{ |
| 1053 |
*p_user = p_user_list_pool->p_current->users[left]; |
id = p_user_list_pool->p_current->index_uid[left].id; |
| 1054 |
|
*p_user = p_user_list_pool->p_current->users[id]; |
| 1055 |
ret = 1; |
ret = 1; |
| 1056 |
|
|
| 1057 |
|
if (p_intro_buf != NULL) |
| 1058 |
|
{ |
| 1059 |
|
strncpy(p_intro_buf, p_user_list_pool->p_current->users[id].intro, intro_buf_len - 1); |
| 1060 |
|
p_intro_buf[intro_buf_len - 1] = '\0'; |
| 1061 |
|
p_user->intro = p_intro_buf; |
| 1062 |
|
} |
| 1063 |
} |
} |
| 1064 |
|
|
| 1065 |
// release lock of user list |
// release lock of user list |
| 1071 |
|
|
| 1072 |
return ret; |
return ret; |
| 1073 |
} |
} |
| 1074 |
|
|
| 1075 |
|
int query_user_info_by_username(const char *username_prefix, int max_user_cnt, |
| 1076 |
|
int32_t uid_list[], char username_list[][BBS_username_max_len + 1]) |
| 1077 |
|
{ |
| 1078 |
|
int left; |
| 1079 |
|
int right; |
| 1080 |
|
int mid; |
| 1081 |
|
int left_save; |
| 1082 |
|
int ret = 0; |
| 1083 |
|
size_t prefix_len; |
| 1084 |
|
int comp; |
| 1085 |
|
int i; |
| 1086 |
|
|
| 1087 |
|
if (username_prefix == NULL || uid_list == NULL || username_list == NULL) |
| 1088 |
|
{ |
| 1089 |
|
log_error("NULL pointer error\n"); |
| 1090 |
|
return -1; |
| 1091 |
|
} |
| 1092 |
|
|
| 1093 |
|
prefix_len = strlen(username_prefix); |
| 1094 |
|
|
| 1095 |
|
// acquire lock of user list |
| 1096 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1097 |
|
{ |
| 1098 |
|
log_error("user_list_rd_lock() error\n"); |
| 1099 |
|
return -2; |
| 1100 |
|
} |
| 1101 |
|
|
| 1102 |
|
left = 0; |
| 1103 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1104 |
|
|
| 1105 |
|
while (left < right) |
| 1106 |
|
{ |
| 1107 |
|
mid = (left + right) / 2; |
| 1108 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1109 |
|
if (comp < 0) |
| 1110 |
|
{ |
| 1111 |
|
right = mid - 1; |
| 1112 |
|
} |
| 1113 |
|
else if (comp > 0) |
| 1114 |
|
{ |
| 1115 |
|
left = mid + 1; |
| 1116 |
|
} |
| 1117 |
|
else // if (comp == 0) |
| 1118 |
|
{ |
| 1119 |
|
left = mid; |
| 1120 |
|
break; |
| 1121 |
|
} |
| 1122 |
|
} |
| 1123 |
|
|
| 1124 |
|
if (strncasecmp(username_prefix, p_user_list_pool->p_current->users[left].username, prefix_len) == 0) // Found |
| 1125 |
|
{ |
| 1126 |
|
#ifdef _DEBUG |
| 1127 |
|
log_error("Debug: match found, pos=%d\n", left); |
| 1128 |
|
#endif |
| 1129 |
|
|
| 1130 |
|
left_save = left; |
| 1131 |
|
right = left; |
| 1132 |
|
left = 0; |
| 1133 |
|
|
| 1134 |
|
while (left < right) |
| 1135 |
|
{ |
| 1136 |
|
mid = (left + right) / 2; |
| 1137 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1138 |
|
if (comp > 0) |
| 1139 |
|
{ |
| 1140 |
|
left = mid + 1; |
| 1141 |
|
} |
| 1142 |
|
else if (comp == 0) |
| 1143 |
|
{ |
| 1144 |
|
right = mid; |
| 1145 |
|
} |
| 1146 |
|
else // if (comp < 0) |
| 1147 |
|
{ |
| 1148 |
|
log_error("Bug: left=%d right=%d mid=%d"); |
| 1149 |
|
ret = -2; |
| 1150 |
|
goto cleanup; |
| 1151 |
|
} |
| 1152 |
|
} |
| 1153 |
|
|
| 1154 |
|
#ifdef _DEBUG |
| 1155 |
|
log_error("Debug: first match found, pos=%d\n", right); |
| 1156 |
|
#endif |
| 1157 |
|
|
| 1158 |
|
left = left_save; |
| 1159 |
|
left_save = right; |
| 1160 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1161 |
|
|
| 1162 |
|
while (left < right) |
| 1163 |
|
{ |
| 1164 |
|
mid = (left + right) / 2 + (left + right) % 2; |
| 1165 |
|
comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len); |
| 1166 |
|
if (comp < 0) |
| 1167 |
|
{ |
| 1168 |
|
right = mid - 1; |
| 1169 |
|
} |
| 1170 |
|
else if (comp == 0) |
| 1171 |
|
{ |
| 1172 |
|
left = mid; |
| 1173 |
|
} |
| 1174 |
|
else // if (comp > 0) |
| 1175 |
|
{ |
| 1176 |
|
log_error("Bug: left=%d right=%d mid=%d"); |
| 1177 |
|
ret = -2; |
| 1178 |
|
goto cleanup; |
| 1179 |
|
} |
| 1180 |
|
} |
| 1181 |
|
|
| 1182 |
|
#ifdef _DEBUG |
| 1183 |
|
log_error("Debug: last match found, pos=%d\n", left); |
| 1184 |
|
#endif |
| 1185 |
|
|
| 1186 |
|
right = left; |
| 1187 |
|
left = left_save; |
| 1188 |
|
|
| 1189 |
|
for (i = 0; i < max_user_cnt && left + i <= right; i++) |
| 1190 |
|
{ |
| 1191 |
|
uid_list[i] = p_user_list_pool->p_current->users[left + i].uid; |
| 1192 |
|
strncpy(username_list[i], p_user_list_pool->p_current->users[left + i].username, sizeof(username_list[i]) - 1); |
| 1193 |
|
username_list[i][sizeof(username_list[i]) - 1] = '\0'; |
| 1194 |
|
} |
| 1195 |
|
ret = i; |
| 1196 |
|
} |
| 1197 |
|
|
| 1198 |
|
cleanup: |
| 1199 |
|
// release lock of user list |
| 1200 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1201 |
|
{ |
| 1202 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1203 |
|
ret = -1; |
| 1204 |
|
} |
| 1205 |
|
|
| 1206 |
|
return ret; |
| 1207 |
|
} |
| 1208 |
|
|
| 1209 |
|
int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user) |
| 1210 |
|
{ |
| 1211 |
|
int ret = 0; |
| 1212 |
|
|
| 1213 |
|
if (p_user == NULL) |
| 1214 |
|
{ |
| 1215 |
|
log_error("NULL pointer error\n"); |
| 1216 |
|
return -1; |
| 1217 |
|
} |
| 1218 |
|
|
| 1219 |
|
// acquire lock of user list |
| 1220 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1221 |
|
{ |
| 1222 |
|
log_error("user_list_rd_lock() error\n"); |
| 1223 |
|
return -2; |
| 1224 |
|
} |
| 1225 |
|
|
| 1226 |
|
if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found |
| 1227 |
|
{ |
| 1228 |
|
*p_user = p_user_list_pool->p_online_current->users[id]; |
| 1229 |
|
ret = 1; |
| 1230 |
|
} |
| 1231 |
|
|
| 1232 |
|
// release lock of user list |
| 1233 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1234 |
|
{ |
| 1235 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1236 |
|
ret = -1; |
| 1237 |
|
} |
| 1238 |
|
|
| 1239 |
|
return ret; |
| 1240 |
|
} |
| 1241 |
|
|
| 1242 |
|
int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id) |
| 1243 |
|
{ |
| 1244 |
|
int left; |
| 1245 |
|
int right; |
| 1246 |
|
int mid; |
| 1247 |
|
int32_t id; |
| 1248 |
|
int ret = 0; |
| 1249 |
|
int i; |
| 1250 |
|
int user_cnt; |
| 1251 |
|
|
| 1252 |
|
if (p_users == NULL || p_user_cnt == NULL) |
| 1253 |
|
{ |
| 1254 |
|
log_error("NULL pointer error\n"); |
| 1255 |
|
return -1; |
| 1256 |
|
} |
| 1257 |
|
|
| 1258 |
|
user_cnt = *p_user_cnt; |
| 1259 |
|
*p_user_cnt = 0; |
| 1260 |
|
|
| 1261 |
|
// acquire lock of user list |
| 1262 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1263 |
|
{ |
| 1264 |
|
log_error("user_list_rd_lock() error\n"); |
| 1265 |
|
return -2; |
| 1266 |
|
} |
| 1267 |
|
|
| 1268 |
|
left = start_id; |
| 1269 |
|
right = p_user_list_pool->p_online_current->user_count - 1; |
| 1270 |
|
|
| 1271 |
|
while (left < right) |
| 1272 |
|
{ |
| 1273 |
|
mid = (left + right) / 2; |
| 1274 |
|
if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1275 |
|
{ |
| 1276 |
|
right = mid - 1; |
| 1277 |
|
} |
| 1278 |
|
else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1279 |
|
{ |
| 1280 |
|
left = mid + 1; |
| 1281 |
|
} |
| 1282 |
|
else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1283 |
|
{ |
| 1284 |
|
left = mid; |
| 1285 |
|
break; |
| 1286 |
|
} |
| 1287 |
|
} |
| 1288 |
|
|
| 1289 |
|
if (uid == p_user_list_pool->p_online_current->index_uid[left].uid) |
| 1290 |
|
{ |
| 1291 |
|
right = left; |
| 1292 |
|
left = start_id; |
| 1293 |
|
|
| 1294 |
|
while (left < right) |
| 1295 |
|
{ |
| 1296 |
|
mid = (left + right) / 2; |
| 1297 |
|
if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1298 |
|
{ |
| 1299 |
|
right = mid; |
| 1300 |
|
} |
| 1301 |
|
else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1302 |
|
{ |
| 1303 |
|
left = mid + 1; |
| 1304 |
|
} |
| 1305 |
|
} |
| 1306 |
|
|
| 1307 |
|
for (i = 0; |
| 1308 |
|
left < p_user_list_pool->p_online_current->user_count && i < user_cnt && |
| 1309 |
|
uid == p_user_list_pool->p_online_current->index_uid[left].uid; |
| 1310 |
|
left++, i++) |
| 1311 |
|
{ |
| 1312 |
|
id = p_user_list_pool->p_online_current->index_uid[left].id; |
| 1313 |
|
p_users[i] = p_user_list_pool->p_online_current->users[id]; |
| 1314 |
|
} |
| 1315 |
|
|
| 1316 |
|
if (i > 0) |
| 1317 |
|
{ |
| 1318 |
|
*p_user_cnt = i; |
| 1319 |
|
ret = 1; |
| 1320 |
|
} |
| 1321 |
|
} |
| 1322 |
|
|
| 1323 |
|
// release lock of user list |
| 1324 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1325 |
|
{ |
| 1326 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1327 |
|
ret = -1; |
| 1328 |
|
} |
| 1329 |
|
|
| 1330 |
|
return ret; |
| 1331 |
|
} |
| 1332 |
|
|
| 1333 |
|
int get_user_id_list(int32_t *p_uid_list, int *p_user_cnt, int start_uid) |
| 1334 |
|
{ |
| 1335 |
|
int left; |
| 1336 |
|
int right; |
| 1337 |
|
int mid; |
| 1338 |
|
int ret = 0; |
| 1339 |
|
int i; |
| 1340 |
|
|
| 1341 |
|
if (p_uid_list == NULL || p_user_cnt == NULL) |
| 1342 |
|
{ |
| 1343 |
|
log_error("NULL pointer error\n"); |
| 1344 |
|
return -1; |
| 1345 |
|
} |
| 1346 |
|
|
| 1347 |
|
// acquire lock of user list |
| 1348 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 1349 |
|
{ |
| 1350 |
|
log_error("user_list_rd_lock() error\n"); |
| 1351 |
|
return -2; |
| 1352 |
|
} |
| 1353 |
|
|
| 1354 |
|
left = 0; |
| 1355 |
|
right = p_user_list_pool->p_current->user_count - 1; |
| 1356 |
|
|
| 1357 |
|
while (left < right) |
| 1358 |
|
{ |
| 1359 |
|
mid = (left + right) / 2; |
| 1360 |
|
if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid) |
| 1361 |
|
{ |
| 1362 |
|
right = mid - 1; |
| 1363 |
|
} |
| 1364 |
|
else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid) |
| 1365 |
|
{ |
| 1366 |
|
left = mid + 1; |
| 1367 |
|
} |
| 1368 |
|
else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid) |
| 1369 |
|
{ |
| 1370 |
|
left = mid; |
| 1371 |
|
break; |
| 1372 |
|
} |
| 1373 |
|
} |
| 1374 |
|
|
| 1375 |
|
for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++) |
| 1376 |
|
{ |
| 1377 |
|
p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid; |
| 1378 |
|
} |
| 1379 |
|
*p_user_cnt = i; |
| 1380 |
|
|
| 1381 |
|
// release lock of user list |
| 1382 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1383 |
|
{ |
| 1384 |
|
log_error("user_list_rd_unlock() error\n"); |
| 1385 |
|
ret = -1; |
| 1386 |
|
} |
| 1387 |
|
|
| 1388 |
|
return ret; |
| 1389 |
|
} |
| 1390 |
|
|
| 1391 |
|
int user_stat_update(void) |
| 1392 |
|
{ |
| 1393 |
|
return user_stat_map_update(&(p_user_list_pool->user_stat_map)); |
| 1394 |
|
} |
| 1395 |
|
|
| 1396 |
|
int user_article_cnt_inc(int32_t uid, int n) |
| 1397 |
|
{ |
| 1398 |
|
return user_stat_article_cnt_inc(&(p_user_list_pool->user_stat_map), uid, n); |
| 1399 |
|
} |
| 1400 |
|
|
| 1401 |
|
int get_user_article_cnt(int32_t uid) |
| 1402 |
|
{ |
| 1403 |
|
const USER_STAT *p_stat; |
| 1404 |
|
int ret; |
| 1405 |
|
|
| 1406 |
|
ret = user_stat_get(&(p_user_list_pool->user_stat_map), uid, &p_stat); |
| 1407 |
|
if (ret < 0) |
| 1408 |
|
{ |
| 1409 |
|
log_error("user_stat_get(uid=%d) error: %d\n", uid, ret); |
| 1410 |
|
return -1; |
| 1411 |
|
} |
| 1412 |
|
else if (ret == 0) // user not found |
| 1413 |
|
{ |
| 1414 |
|
return -1; |
| 1415 |
|
} |
| 1416 |
|
|
| 1417 |
|
return p_stat->article_count; |
| 1418 |
|
} |