Retain the CDB struct between lookups, only freeing when reopening the
authorMahlon E. Smith <mahlon@martini.nu>
Mon, 07 Nov 2011 10:43:09 -0800
changeset 17 bd746609ba46
parent 16 e6a640ad2cc2
child 18 d4ce82194b64
Retain the CDB struct between lookups, only freeing when reopening the db. Big, big speed boost.
db.c
main.c
volta.h
--- a/db.c	Mon Nov 07 10:43:06 2011 -0800
+++ b/db.c	Mon Nov 07 10:43:09 2011 -0800
@@ -44,6 +44,7 @@
 	time_t now = time( NULL );
 	if ( v.timer.db_lastcheck > 0 ) {
 		if ( now - v.timer.db_lastcheck >= 10 ) {
+			cdb_free( &v.db );
 			close( v.db_fd );
 		}
 		else {
@@ -65,6 +66,8 @@
 	}
 
 	v.timer.db_lastcheck = now;
+	cdb_init( &v.db, v.db_fd );
+
 	return( 0 );
 }
 
@@ -151,7 +154,6 @@
 {
 	if ( key == NULL ) return( NULL );
 
-	struct cdb cdb;
 	struct cdb_find cdbf; /* structure to hold current find position */
 
 	parsed *rule = NULL;
@@ -160,21 +162,19 @@
 
 	/* initialize search structs */
 	if ( db_attach() == -1 ) return( NULL );
-	cdb_init( &cdb, v.db_fd );
-	cdb_findinit( &cdbf, &cdb, key, (int)strlen(key) );
+	cdb_findinit( &cdbf, &v.db, key, (int)strlen(key) );
 
 	while ( cdb_findnext( &cdbf ) > 0 ) {
-		vpos = cdb_datapos( &cdb );
-		vlen = cdb_datalen( &cdb );
+		vpos = cdb_datapos( &v.db );
+		vlen = cdb_datalen( &v.db );
 
 		/* pull the value from the db */
 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
 					strerror(errno) );
-			cdb_free( &cdb );
 			return( NULL );
 		}
-		cdb_read( &cdb, val, vlen, vpos );
+		cdb_read( &v.db, val, vlen, vpos );
 
 		/* check it against the request */
 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
@@ -190,7 +190,6 @@
 		}
 	}
 
-	cdb_free( &cdb );
 	return( rule );
 }
 
--- a/main.c	Mon Nov 07 10:43:06 2011 -0800
+++ b/main.c	Mon Nov 07 10:43:09 2011 -0800
@@ -131,6 +131,7 @@
 void
 shutdown_actions( void )
 {
+	cdb_free( &v.db );
 	close( v.db_fd );
 	report_speed();
 }
--- a/volta.h	Mon Nov 07 10:43:06 2011 -0800
+++ b/volta.h	Mon Nov 07 10:43:09 2011 -0800
@@ -84,6 +84,7 @@
 	unsigned short int debugmode; /* debug level */
 	char dbname[128];             /* path to database file */
 	short int db_fd;              /* opened db file descriptor */
+	struct cdb db;                /* the cdb struct */
 
 	struct {
 		time_t start;             /* start time */