[open-ils-commits] r13501 - trunk/Open-ILS/src/c-apps (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Jul 5 07:09:14 EDT 2009


Author: scottmk
Date: 2009-07-05 07:09:14 -0400 (Sun, 05 Jul 2009)
New Revision: 13501

Modified:
   trunk/Open-ILS/src/c-apps/oils_cstore.c
Log:
Several tweaks to oils_cstore.c:

1. Added a utility function single_hash() to create simple WHERE
clauses instead of calling jsonParseStringFmt().  This change
avoids the overhead of creating and then parsing a JSON string.

2. In SELECT(), processing an ORDER BY clause: traverse a
JSON_ARRAY with an index instead of an iterator.  This change
avoids some mallocs and frees -- and also plugs a memory leak,
since we weren't freeing the iterator.

3. Also in SELECT(), processing an ORDER BY clause: free the
iterator that we use for traversing the hash of classes.


Modified: trunk/Open-ILS/src/c-apps/oils_cstore.c
===================================================================
--- trunk/Open-ILS/src/c-apps/oils_cstore.c	2009-07-05 05:28:40 UTC (rev 13500)
+++ trunk/Open-ILS/src/c-apps/oils_cstore.c	2009-07-05 11:09:14 UTC (rev 13501)
@@ -84,6 +84,7 @@
 #ifdef PCRUD
 static jsonObject* verifyUserPCRUD( osrfMethodContext* );
 static int verifyObjectPCRUD( osrfMethodContext*, const jsonObject* );
+static jsonObject* single_hash( const char* key, const char* value );
 #endif
 
 static int child_initialized = 0;   /* boolean */
@@ -1021,7 +1022,7 @@
         }
 
         if (fetch) {
-            jsonObject* _tmp_params = jsonParseStringFmt("{\"%s\":\"%s\"}", pkey, pkey_value);
+			jsonObject* _tmp_params = single_hash( pkey, pkey_value );
 			jsonObject* _list = doFieldmapperSearch(
 					ctx, class, _tmp_params, NULL, &err );
 
@@ -1096,11 +1097,7 @@
                     char* foreign_pkey = osrfHashGet(fcontext, "field");
                     char* foreign_pkey_value = oilsFMGetString(param, osrfHashGet(fcontext, "fkey"));
 
-                    jsonObject* _tmp_params = jsonParseStringFmt(
-                        "{\"%s\":\"%s\"}",
-                        foreign_pkey,
-                        foreign_pkey_value
-                    );
+					jsonObject* _tmp_params = single_hash( foreign_pkey, foreign_pkey_value );
 
 					jsonObject* _list = doFieldmapperSearch(
 						ctx, osrfHashGet( oilsIDL(), class_name ), _tmp_params, NULL, &err );
@@ -1119,14 +1116,10 @@
 
                             osrfHash* foreign_link_hash = oilsIDLFindPath( "/%s/links/%s", _fparam->classname, flink );
 
-                            foreign_pkey_value = oilsFMGetString(_fparam, flink);
-                            foreign_pkey = osrfHashGet( foreign_link_hash, "key" );
+							foreign_pkey_value = oilsFMGetString(_fparam, flink);
+							foreign_pkey = osrfHashGet( foreign_link_hash, "key" );
 
-                            _tmp_params = jsonParseStringFmt(
-                                "{\"%s\":\"%s\"}",
-                                foreign_pkey,
-                                foreign_pkey_value
-                            );
+							_tmp_params = single_hash( foreign_pkey, foreign_pkey_value );
 
 							_list = doFieldmapperSearch(
 								ctx,
@@ -1301,6 +1294,24 @@
 
     return OK;
 }
+
+/*
+Utility function: create a JSON_HASH with a single key/value pair.
+This function is equivalent to:
+
+	jsonParseStringFmt( "{\"%s\":\"%s\"}", key, value )
+
+...but faster because it doesn't create and parse a JSON string.
+*/
+static jsonObject* single_hash( const char* key, const char* value ) {
+	// Sanity checks
+	if( ! key ) key = "";
+	if( ! value ) value = "";
+
+	jsonObject* hash = jsonNewObjectType( JSON_HASH );
+	jsonObjectSetKey( hash, key, jsonNewObject( value ) );
+	return hash;
+}
 #endif
 
 
@@ -3683,13 +3694,13 @@
 						}
 
 					} // end while
-    	            jsonIteratorFree(order_itr);
+					jsonIteratorFree(order_itr);
 
 				} else if ( snode->type == JSON_ARRAY ) {
 
 					// Array is a list of fields from the current class
-					jsonIterator* order_itr = jsonNewIterator( snode );
-					while ( (onode = jsonIteratorNext( order_itr )) ) {
+					unsigned long order_idx = 0;
+					while(( onode = jsonObjectGetIndex( snode, order_idx++ ) )) {
 
 						const char* _f = jsonObjectGetString( onode );
 
@@ -3705,7 +3716,6 @@
 									ctx->request,
 									"Invalid field in ORDER BY clause -- see error log for more details"
 								);
-							jsonIteratorFree( order_itr );
 							jsonIteratorFree( class_itr );
 							buffer_free( order_buf );
 							free(core_class);
@@ -3725,7 +3735,6 @@
 									ctx->request,
 									"Virtual field in ORDER BY clause -- see error log for more details"
 								);
-							jsonIteratorFree( order_itr );
 							jsonIteratorFree( class_itr );
 							buffer_free( order_buf );
 							free(core_class);
@@ -3744,9 +3753,7 @@
 						buffer_fadd( order_buf, "\"%s\".%s", class_itr->key, _f);
 
 					} // end while
-				// jsonIteratorFree(order_itr);
-	
-	
+
 				// IT'S THE OOOOOOOOOOOLD STYLE!
 				} else {
 					osrfLogError(OSRF_LOG_MARK, 
@@ -3771,6 +3778,7 @@
 					return NULL;
 				}
 			} // end while
+			jsonIteratorFree( class_itr );
 		} else {
 			osrfLogError(OSRF_LOG_MARK,
 				"%s: Malformed ORDER BY clause; expected JSON_HASH or JSON_ARRAY, found %s",
@@ -3791,7 +3799,6 @@
 			if (defaultselhash) jsonObjectFree(defaultselhash);
 			return NULL;
 		}
-		// jsonIteratorFree(class_itr);
 
 		if( order_buf )
 			order_by_list = buffer_release( order_buf );



More information about the open-ils-commits mailing list