[icinga-checkins] icinga.org: icinga-core/mfriedrich/cgis: idoutils: change hasfunc to use sdbm algorithm for hashing precached objects #2792

git at icinga.org git at icinga.org
Wed Aug 1 03:00:45 CEST 2012


Module: icinga-core
Branch: mfriedrich/cgis
Commit: fff330e16a9a3f18d19c1e856457af5e8d6510c9
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=fff330e16a9a3f18d19c1e856457af5e8d6510c9

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Fri Jul  6 18:47:14 2012 +0200

idoutils: change hasfunc to use sdbm algorithm for hashing precached objects #2792

this will probably speedup the calculation of the precached objects

refs #2792

---

 Changelog                        |    1 +
 module/idoutils/src/dbhandlers.c |   34 ++++++++++++++++++++++++----------
 2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/Changelog b/Changelog
index e90697d..981ee71 100644
--- a/Changelog
+++ b/Changelog
@@ -12,6 +12,7 @@ ENHANCEMENTS
 * core: handle passive checks via external commands the same way as active checks, bypassing the temp check result file and reaper (Andreas Ericsson) #2687
 * core: use binary search when looking up macro names, instead of insane strcmp() loops (Andreas Ericsson) #2675
 
+* idoutils: change hasfunc to use sdbm algorithm for hashing precached objects #2792
 * icinga.spec: add devel package #2634
 
 FIXES
diff --git a/module/idoutils/src/dbhandlers.c b/module/idoutils/src/dbhandlers.c
index c868d25..85ff849 100644
--- a/module/idoutils/src/dbhandlers.c
+++ b/module/idoutils/src/dbhandlers.c
@@ -740,25 +740,35 @@ int ido2db_add_cached_object_id(ido2db_idi *idi, int object_type, char *n1, char
 	return result;
 }
 
+/*
+ * hash functions
+ */
+static unsigned long ido2db_object_sdbm(const char *str) {
+        unsigned long hash = 0;
+        int c;
+
+        while ((c = *str++) != '\0')
+                hash = c + (hash << 6) + (hash << 16) - hash;
+
+        return hash;
+}
+
 int ido2db_object_hashfunc(const char *name1, const char *name2, int hashslots) {
-	unsigned int i, result;
+        unsigned int result = 0;
 
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_object_hashfunc() start\n");
 
-	result = 0;
-	if (name1)
-		for (i = 0; i < strlen(name1); i++)
-			result += name1[i];
+        if (name1)
+                result += ido2db_object_sdbm(name1);
 
-	if (name2)
-		for (i = 0; i < strlen(name2); i++)
-			result += name2[i];
+        if (name2)
+                result += ido2db_object_sdbm(name2);
 
-	result = result % hashslots;
+        result = result % hashslots;
 
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_object_hashfunc() end\n");
 
-	return result;
+        return result;
 }
 
 int ido2db_compare_object_hashdata(const char *val1a, const char *val1b,
@@ -794,6 +804,9 @@ int ido2db_compare_object_hashdata(const char *val1a, const char *val1b,
 	return result;
 }
 
+/*
+ * free cached object ids
+ */
 int ido2db_free_cached_object_ids(ido2db_idi *idi) {
 	int x = 0;
 	ido2db_dbobject *temp_object = NULL;
@@ -825,6 +838,7 @@ int ido2db_free_cached_object_ids(ido2db_idi *idi) {
 	return IDO_OK;
 }
 
+
 int ido2db_set_all_objects_as_inactive(ido2db_idi *idi) {
 	int result = IDO_OK;
 #ifdef USE_LIBDBI





More information about the icinga-checkins mailing list