From 3b77fe78d9e52045c71a5a8fb4a32aec49a04427 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 19 Oct 2007 21:15:17 +0000 Subject: [PATCH] Replace the array hack for user lookup with tree --- src/admin.c | 1 - src/bouncer.h | 1 + src/loader.c | 2 -- src/main.c | 3 +++ src/objects.c | 74 +++++++++++++-------------------------------------- src/objects.h | 5 ++-- 6 files changed, 25 insertions(+), 61 deletions(-) diff --git a/src/admin.c b/src/admin.c index 01f4a40..1568d30 100644 --- a/src/admin.c +++ b/src/admin.c @@ -975,7 +975,6 @@ void admin_setup(void) user = add_user("pgbouncer", ""); if (!user) fatal("cannot create admin user?"); - create_auth_cache(); /* prepare welcome */ pktbuf_static(&msg, pool->welcome_msg, sizeof(pool->welcome_msg)); diff --git a/src/bouncer.h b/src/bouncer.h index 937d87b..86cd6c0 100644 --- a/src/bouncer.h +++ b/src/bouncer.h @@ -187,6 +187,7 @@ struct PgPool { struct PgUser { List head; List pool_list; + Node tree_node; char name[MAX_USERNAME]; char passwd[MAX_PASSWORD]; }; diff --git a/src/loader.c b/src/loader.c index 34eb690..0c5212f 100644 --- a/src/loader.c +++ b/src/loader.c @@ -455,8 +455,6 @@ bool load_auth_file(const char *fn) } free(buf); - create_auth_cache(); - return true; } diff --git a/src/main.c b/src/main.c index 42b7511..1de356a 100644 --- a/src/main.c +++ b/src/main.c @@ -467,6 +467,9 @@ int main(int argc, char *argv[]) if (optind + 1 != argc) usage(1); cf_config_file = argv[optind]; + + init_objects(); + load_config(false); /* need to do that after loading config */ diff --git a/src/objects.c b/src/objects.c index 0316376..f68a336 100644 --- a/src/objects.c +++ b/src/objects.c @@ -27,6 +27,8 @@ STATLIST(user_list); STATLIST(database_list); STATLIST(pool_list); +Tree user_tree; + /* * client and server objects will be pre-allocated * they are always in either active or free lists @@ -49,18 +51,6 @@ static int absolute_client_count = 0; /* how many server sockets are allocated */ static int absolute_server_count = 0; -/* list of users ordered by name */ -static PgUser **user_lookup = NULL; - -/* drop lookup list because it will be out of sync */ -static void reset_auth_cache(void) -{ - if (user_lookup != NULL) { - free(user_lookup); - user_lookup = NULL; - } -} - /* fast way to get number of active clients */ int get_active_client_count(void) { @@ -342,8 +332,6 @@ PgUser *add_user(const char *name, const char *passwd) { PgUser *user = find_user(name); - reset_auth_cache(); - if (user == NULL) { user = zmalloc(sizeof(*user)); if (!user) @@ -353,6 +341,8 @@ PgUser *add_user(const char *name, const char *passwd) list_init(&user->pool_list); strlcpy(user->name, name, sizeof(user->name)); put_in_order(&user->head, &user_list, cmp_user); + + tree_insert(&user_tree, (long)user->name, &user->tree_node); } strlcpy(user->passwd, passwd, sizeof(user->passwd)); return user; @@ -388,56 +378,28 @@ PgDatabase *find_database(const char *name) return NULL; } -/* compare string with PgUser->name, for usage with bsearch() */ -static int user_name_cmp(const void *namestr, const void *userptr) +/* compare string with PgUser->name, for usage with btree */ +static int user_node_cmp(long userptr, Node *node) { - const PgUser * const *user_p = userptr; - const PgUser *user = *user_p; - return strcmp(namestr, user->name); + const char *name = (const char *)userptr; + PgUser *user = container_of(node, PgUser, tree_node); + return strcmp(name, user->name); } -/* find existing user */ -PgUser *find_user(const char *name) +void init_objects(void) { - List *item; - PgUser *user; - - /* if lookup table is available, use faster method */ - if (user_lookup) { - PgUser **res; - res = bsearch(name, user_lookup, - statlist_count(&user_list), - sizeof(PgUser *), - user_name_cmp); - return res ? *res : NULL; - } - - /* slow lookup */ - statlist_for_each(item, &user_list) { - user = container_of(item, PgUser, head); - if (strcmp(user->name, name) == 0) - return user; - } - return NULL; + tree_init(&user_tree, user_node_cmp, NULL); } -/* create lookup list */ -void create_auth_cache(void) +/* find existing user */ +PgUser *find_user(const char *name) { - int i = 0; - List *item; - PgUser *user; - - reset_auth_cache(); - - user_lookup = malloc(sizeof(PgUser *) * statlist_count(&user_list)); - if (!user_lookup) - return; + PgUser *user = NULL; + Node *node; - statlist_for_each(item, &user_list) { - user = container_of(item, PgUser, head); - user_lookup[i++] = user; - } + node = tree_search(&user_tree, (long)name); + user = node ? container_of(node, PgUser, tree_node) : NULL; + return user; } /* create new pool object */ diff --git a/src/objects.h b/src/objects.h index ac421e1..8062308 100644 --- a/src/objects.h +++ b/src/objects.h @@ -17,6 +17,7 @@ */ extern StatList user_list; +extern Tree user_tree; extern StatList pool_list; extern StatList database_list; extern StatList login_client_list; @@ -61,7 +62,7 @@ int get_active_server_count(void); void tag_database_dirty(PgDatabase *db); void for_each_server(PgPool *pool, void (*func)(PgSocket *sk)); -void create_auth_cache(void); - void reuse_just_freed_objects(void); +void init_objects(void); + -- 2.40.0