]> granicus.if.org Git - pgbouncer/commitdiff
Replace the array hack for user lookup with tree
authorMarko Kreen <markokr@gmail.com>
Fri, 19 Oct 2007 21:15:17 +0000 (21:15 +0000)
committerMarko Kreen <markokr@gmail.com>
Fri, 19 Oct 2007 21:15:17 +0000 (21:15 +0000)
src/admin.c
src/bouncer.h
src/loader.c
src/main.c
src/objects.c
src/objects.h

index 01f4a402ea8e3e718623e11f0cf23fb9041ead9e..1568d30bd4c6e3f62d839729a29d26a41ee13258 100644 (file)
@@ -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));
index 937d87b1e4bef76729130bd1f230205497976f80..86cd6c0011f8f623c2268bcf72eccf78bb9e171f 100644 (file)
@@ -187,6 +187,7 @@ struct PgPool {
 struct PgUser {
        List head;
        List pool_list;
+       Node tree_node;
        char name[MAX_USERNAME];
        char passwd[MAX_PASSWORD];
 };
index 34eb6907ac1b03fa8a670b7cfbbe5d9193e4828d..0c5212f4d915dba4012b8f715531edd704f03d8b 100644 (file)
@@ -455,8 +455,6 @@ bool load_auth_file(const char *fn)
        }
        free(buf);
 
-       create_auth_cache();
-
        return true;
 }
 
index 42b75117da3d6542384981e71dc5eb5c9bad4137..1de356a1eb983c45177127892e30ccced2d9c8a8 100644 (file)
@@ -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 */
index 0316376e210316498d9dfd4bb32753f2a428a7e8..f68a33653ac1da03abfceac5fb9736036098401e 100644 (file)
@@ -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 */
index ac421e12cf15277dddd4c6ac2c3f3cecb5c81fbb..80623083bd03d1652d714c8933bbaa605c4d19c3 100644 (file)
@@ -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);
+