Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hasmap to find comment data #374 #375

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/naemon/comments.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@
#include "events.h"
#include "globals.h"
#include "nm_alloc.h"
#include <glib.h>

comment *comment_list = NULL;
int defer_comment_sorting = 0;
comment **comment_hashlist = NULL;
static GHashTable *comment_hashtable;


/******************************************************************/
/**************** INITIALIZATION/CLEANUP FUNCTIONS ****************/
/******************************************************************/

int initialize_comment_hashmap(void)
{
if (comment_hashtable == NULL){
comment_hashtable = g_hash_table_new(g_direct_hash, g_direct_equal);
}
return OK;
}

/* initializes comment data */
int initialize_comment_data(void)
{
comment *temp_comment = NULL;
initialize_comment_hashmap();

/* find the new starting index for comment id if its missing*/
if (next_comment_id == 0L) {
Expand Down Expand Up @@ -149,15 +160,9 @@ int delete_comment(int type, unsigned long comment_id)
comment *last_hash = NULL;

/* find the comment we should remove */
for (this_comment = comment_list, last_comment = comment_list; this_comment != NULL; this_comment = next_comment) {
next_comment = this_comment->next;

/* we found the comment we should delete */
if (this_comment->comment_id == comment_id && this_comment->comment_type == type)
break;
this_comment = g_hash_table_lookup(comment_hashtable, GINT_TO_POINTER(comment_id));
next_comment = this_comment->next;

last_comment = this_comment;
}

if (this_comment == NULL)
return ERROR;
Expand All @@ -166,6 +171,7 @@ int delete_comment(int type, unsigned long comment_id)

/* remove the comment from the list in memory */
/* first remove from chained hash list */
g_hash_table_remove(comment_hashtable, GINT_TO_POINTER(this_comment->comment_id));
hashslot = hashfunc(this_comment->host_name, NULL, COMMENT_HASHSLOTS);
last_hash = NULL;
for (this_hash = comment_hashlist[hashslot]; this_hash; this_hash = this_hash->nexthash) {
Expand Down Expand Up @@ -431,6 +437,8 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des
result = ERROR;
}

g_hash_table_insert(comment_hashtable, GINT_TO_POINTER(new_comment->comment_id), new_comment);

/* handle errors */
if (result == ERROR) {
nm_free(new_comment->comment_data);
Expand Down Expand Up @@ -527,6 +535,9 @@ void free_comment_data(void)
comment *this_comment = NULL;
comment *next_comment = NULL;

g_hash_table_destroy(comment_hashtable);
comment_hashtable = NULL;

/* free memory for the comment list */
for (this_comment = comment_list; this_comment != NULL; this_comment = next_comment) {
next_comment = this_comment->next;
Expand Down Expand Up @@ -641,10 +652,10 @@ comment *find_comment(unsigned long comment_id, int comment_type)
{
comment *temp_comment = NULL;

for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) {
if (temp_comment->comment_id == comment_id && (temp_comment->comment_type & comment_type))
return temp_comment;
}
temp_comment = g_hash_table_lookup(comment_hashtable, GINT_TO_POINTER(comment_id));
if (temp_comment && (temp_comment->comment_type & comment_type))
return temp_comment;

return NULL;

}
1 change: 1 addition & 0 deletions src/naemon/comments.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct comment {

extern struct comment *comment_list;

int initialize_comment_hashmap(void);
int initialize_comment_data(void); /* initializes comment data */
int add_new_comment(int, int, char *, char *, time_t, char *, char *, int, int, int, time_t, unsigned long *); /* adds a new host or service comment */
int add_new_host_comment(int, char *, time_t, char *, char *, int, int, int, time_t, unsigned long *); /* adds a new host comment */
Expand Down
1 change: 1 addition & 0 deletions src/naemon/naemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ int main(int argc, char **argv)
timing_point("Initialized retention data\n");

timing_point("Reading initial state information\n");
initialize_comment_hashmap();
read_initial_state_information();
timing_point("Read initial state information\n");

Expand Down