summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: fe3dc1b)
raw | patch | inline | side by side (parent: fe3dc1b)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 17 Aug 2010 13:20:59 +0000 (15:20 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 17 Aug 2010 13:21:11 +0000 (15:21 +0200) |
Since the main purpose of the data structure is to iterate over it, using
an AVL tree here is less efficient than a linked list. Also, it's easier
to read.
an AVL tree here is less efficient than a linked list. Also, it's easier
to read.
src/redis.c | patch | blob | history |
diff --git a/src/redis.c b/src/redis.c
index 12cf5840d16615450742eedaf4dc6515fb52ad12..c085963890ef28df73bc86d44e18547c69fe9a6d 100644 (file)
--- a/src/redis.c
+++ b/src/redis.c
* </Plugin>
*/
-static c_avl_tree_t *redis_tree = NULL;
-static pthread_mutex_t redis_lock = PTHREAD_MUTEX_INITIALIZER;
-
-typedef struct redis_node_s {
+struct redis_node_s;
+typedef struct redis_node_s redis_node_t;
+struct redis_node_s
+{
char name[MAX_REDIS_NODE_NAME];
char host[HOST_NAME_MAX];
int port;
int timeout;
-} redis_node_t;
-static redis_node_t *redis_node_get (const char *name, redis_node_t *rn) /* {{{ */
-{
- if (c_avl_get (redis_tree, name, (void *) rn) == 0)
- return (rn);
- else
- return (NULL);
-} /* }}} */
+ redis_node_t *next;
+};
+
+static redis_node_t *nodes_head = NULL;
static int redis_node_add (const redis_node_t *rn) /* {{{ */
{
- int status;
- redis_node_t *rn_copy = NULL;
+ redis_node_t *rn_copy;
redis_node_t *rn_ptr;
- redis_node_t rn_get;
- if (redis_tree == NULL)
+ /* Check for duplicates first */
+ for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
+ if (strcmp (rn->name, rn_ptr->name) == 0)
+ break;
+
+ if (rn_ptr != NULL)
{
- redis_tree = c_avl_create ((void *) strcmp);
- if (redis_tree == NULL)
- {
- ERROR ("redis plugin: c_avl_create failed.");
- return (-1);
- }
+ ERROR ("redis plugin: A node with the name `%s' already exists.",
+ rn->name);
+ return (-1);
}
rn_copy = malloc (sizeof (*rn_copy));
}
memcpy (rn_copy, rn, sizeof (*rn_copy));
- if (rn_copy->name[0] == 0)
- {
- /* in theory never fails */
- (void) strncpy(rn_copy->name, "default", sizeof (rn_copy->name));
- }
+ rn_copy->next = NULL;
- DEBUG ("redis plugin: adding entry `%s' to the tree.", rn_copy->name);
+ DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
- pthread_mutex_lock (&redis_lock);
-
- rn_ptr = redis_node_get (rn_copy->name, &rn_get);
- if (rn_ptr != NULL)
- {
- pthread_mutex_unlock (&redis_lock);
- ERROR ("redis plugin: A node with the name `%s' already exists.",
- rn_copy->name);
- sfree (rn_copy);
- return (-1);
- }
-
- status = c_avl_insert (redis_tree, rn_copy->name, rn_copy);
- pthread_mutex_unlock (&redis_lock);
-
- if (status != 0)
+ if (nodes_head == NULL)
+ nodes_head = rn_copy;
+ else
{
- ERROR ("redis plugin: c_avl_insert (%s) failed adding new node.",
- rn_copy->name);
- sfree (rn_copy);
- return (status);
+ rn_ptr = nodes_head;
+ while (rn_ptr->next != NULL)
+ rn_ptr = rn_ptr->next;
+ rn_ptr->next = rn_copy;
}
return (0);
" configuration. It will be ignored.", option->key);
}
- if (redis_tree == NULL)
+ if (nodes_head == NULL)
{
ERROR ("redis plugin: No valid node configuration could be found.");
return (ENOENT);
REDIS rh;
REDIS_INFO info;
- char key[64];
int status;
- c_avl_iterator_t *iter;
redis_node_t *rn;
- status = -1;
- if ( (iter = c_avl_get_iterator (redis_tree)) == NULL )
- {
- ERROR ("redis plugin: unable to iterate redis tree.");
- return (-1);
- }
-
- while (c_avl_iterator_next (iter, (void *) &key, (void *) &rn) == 0)
+ for (rn = nodes_head; rn != NULL; rn = rn->next)
{
DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
status = 0;
}
- c_avl_iterator_destroy(iter);
- if ( status != 0 )
- {
- return (-1);
- }
-
return 0;
}
/* }}} */