[icinga-checkins] icinga.org: icinga-core/gbeutner/ido: Merge UPDATEs for is_active='1'.

git at icinga.org git at icinga.org
Tue Apr 30 15:06:52 CEST 2013


Module: icinga-core
Branch: gbeutner/ido
Commit: 7f412bd75656832f39902eb7ef0d29685fe07ff9
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=7f412bd75656832f39902eb7ef0d29685fe07ff9

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Apr 30 15:06:45 2013 +0200

Merge UPDATEs for is_active='1'.

---

 module/idoutils/include/db.h         |    4 ++
 module/idoutils/include/dbhandlers.h |    1 +
 module/idoutils/include/ido2db.h     |    5 +++
 module/idoutils/src/db.c             |   26 ++++++++++++++++
 module/idoutils/src/dbhandlers.c     |   54 ++++++++++++++++++++++++++-------
 5 files changed, 78 insertions(+), 12 deletions(-)

diff --git a/module/idoutils/include/db.h b/module/idoutils/include/db.h
index a01470d..c749461 100644
--- a/module/idoutils/include/db.h
+++ b/module/idoutils/include/db.h
@@ -163,6 +163,10 @@ int ido2db_db_get_latest_data_time(ido2db_idi *,char *,char *,unsigned long *);
 int ido2db_db_perform_maintenance(ido2db_idi *);
 int ido2db_db_trim_data_table(ido2db_idi *,char *,char *,unsigned long);
 
+void ido2db_db_txbuf_init(ido2db_txbuf *txbuf);
+void ido2db_db_txbuf_add_id_to_activate(ido2db_txbuf *txbuf, unsigned long);
+void ido2db_db_txbuf_flush(ido2db_idi *idi, ido2db_txbuf *txbuf);
+
 int ido2db_db_tx_begin(ido2db_idi *idi);
 int ido2db_db_tx_commit(ido2db_idi *idi);
 
diff --git a/module/idoutils/include/dbhandlers.h b/module/idoutils/include/dbhandlers.h
index 9960323..a9c73ec 100644
--- a/module/idoutils/include/dbhandlers.h
+++ b/module/idoutils/include/dbhandlers.h
@@ -25,6 +25,7 @@ int ido2db_object_hashfunc(const char *,const char *,int);
 int ido2db_compare_object_hashdata(const char *,const char *,const char *,const char *);
 
 int ido2db_set_all_objects_as_inactive(ido2db_idi *);
+int ido2db_set_objects_as_active(ido2db_idi *idi, unsigned long *object_ids, int count);
 int ido2db_set_object_as_active(ido2db_idi *,unsigned long);
 
 int ido2db_handle_logentry(ido2db_idi *);
diff --git a/module/idoutils/include/ido2db.h b/module/idoutils/include/ido2db.h
index 4e7a3f9..5fb1d1a 100644
--- a/module/idoutils/include/ido2db.h
+++ b/module/idoutils/include/ido2db.h
@@ -233,6 +233,10 @@ typedef struct ido2db_dbconninfo_struct{
 	ido2db_dbobject **object_hashlist;
         }ido2db_dbconninfo;
 
+typedef struct ido2db_txbuf_struct{
+	unsigned long *ids_to_activate;
+	int ids_to_activate_count;
+	}ido2db_txbuf;
 
 typedef struct ido2db_input_data_info_struct{
 	int protocol_version;
@@ -257,6 +261,7 @@ typedef struct ido2db_input_data_info_struct{
 	char **buffered_input;
 	ido2db_mbuf mbuf[IDO2DB_MAX_MBUF_ITEMS];
 	ido2db_dbconninfo dbinfo;
+	ido2db_txbuf txbuf;
         }ido2db_idi;
 
 typedef struct ido2db_proxy_struct {
diff --git a/module/idoutils/src/db.c b/module/idoutils/src/db.c
index 7d8ee9f..c3d9b3f 100644
--- a/module/idoutils/src/db.c
+++ b/module/idoutils/src/db.c
@@ -2904,6 +2904,31 @@ int ido2db_db_perform_maintenance(ido2db_idi *idi) {
 	return IDO_OK;
 }
 
+void ido2db_db_txbuf_init(ido2db_txbuf *txbuf) {
+	txbuf->ids_to_activate = NULL;
+	txbuf->ids_to_activate_count = 0;
+}
+
+void ido2db_db_txbuf_add_id_to_activate(ido2db_txbuf *txbuf, unsigned long object_id) {
+	txbuf->ids_to_activate_count++;
+	txbuf->ids_to_activate = (unsigned long *)realloc(txbuf->ids_to_activate, txbuf->ids_to_activate_count * sizeof(unsigned long));
+	txbuf->ids_to_activate[txbuf->ids_to_activate_count - 1] = object_id;
+}
+
+void ido2db_db_txbuf_flush(ido2db_idi *idi, ido2db_txbuf *txbuf)
+{
+#ifdef USE_LIBDBI
+	if (txbuf->ids_to_activate_count > 0) {
+		ido2db_set_objects_as_active(idi, txbuf->ids_to_activate, txbuf->ids_to_activate_count);
+	}
+
+	txbuf->ids_to_activate_count = 0;
+
+	free(txbuf->ids_to_activate);
+	txbuf->ids_to_activate = NULL;
+#endif
+}
+
 int ido2db_db_tx_begin(ido2db_idi *idi) {
 #ifdef USE_LIBDBI
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_tx_begin()\n");
@@ -2914,6 +2939,7 @@ int ido2db_db_tx_begin(ido2db_idi *idi) {
 }
 
 int ido2db_db_tx_commit(ido2db_idi *idi) {
+	ido2db_db_txbuf_flush(idi, &(idi->txbuf));
 #ifdef USE_LIBDBI
 	ido2db_log_debug_info(IDO2DB_DEBUGL_PROCESSINFO, 2, "ido2db_db_tx_commit()\n");
 	return ido2db_db_query(idi, "COMMIT");
diff --git a/module/idoutils/src/dbhandlers.c b/module/idoutils/src/dbhandlers.c
index 52fdaa6..d9150c8 100644
--- a/module/idoutils/src/dbhandlers.c
+++ b/module/idoutils/src/dbhandlers.c
@@ -878,6 +878,47 @@ int ido2db_set_all_objects_as_inactive(ido2db_idi *idi) {
 	return result;
 }
 
+int ido2db_set_objects_as_active(ido2db_idi *idi, unsigned long *object_ids, int count) {
+	int i, first, result;
+	char *buf;
+
+#ifdef USE_LIBDBI
+	buf = malloc(128 + 20 * count + 2);
+
+	snprintf(buf, 128, "UPDATE %s SET is_active='1' WHERE object_id IN (", ido2db_db_tablenames[IDO2DB_DBTABLE_OBJECTS]);
+
+	first = 1;
+
+	for (i = 0; i < count; i++) {
+		if (!first)
+			strcat(buf, ",");
+		else
+			first = 0;
+
+		snprintf(buf + strlen(buf), 15, "%lu", object_ids[i]);
+	}
+
+	strcat(buf, ")");
+
+	result = ido2db_db_query(idi, buf);
+
+	dbi_result_free(idi->dbinfo.dbi_result);
+	idi->dbinfo.dbi_result = NULL;
+	free(buf);
+
+	return result;
+
+#else
+	result = IDO_OK;
+
+	for (i = 0; i < count; i++) {
+		if (ido2db_set_object_as_active(idi, object_ids[i]) != IDO_OK)
+			result = IDO_ERROR;
+	}
+#endif
+
+	return result;
+}
 
 int ido2db_set_object_as_active(ido2db_idi *idi, unsigned long object_id) {
 	int result = IDO_OK;
@@ -894,18 +935,7 @@ int ido2db_set_object_as_active(ido2db_idi *idi, unsigned long object_id) {
 
 	/* mark the object as being active */
 #ifdef USE_LIBDBI /* everything else will be libdbi */
-	if (asprintf(
-	            &buf,
-	            "UPDATE %s SET is_active='1' WHERE object_id=%lu",
-	            ido2db_db_tablenames[IDO2DB_DBTABLE_OBJECTS], object_id) == -1)
-		buf = NULL;
-
-	result = ido2db_db_query(idi, buf);
-
-	dbi_result_free(idi->dbinfo.dbi_result);
-	idi->dbinfo.dbi_result = NULL;
-	free(buf);
-
+	ido2db_db_txbuf_add_id_to_activate(&(idi->txbuf), object_id);
 #endif
 
 #ifdef USE_PGSQL /* pgsql */





More information about the icinga-checkins mailing list