These three patches are mutually dependent and should be committed or
rejected as a group.

The first two patches concern osrf_cache.[ch]:

1. I declared the variables __osrfCache and __osrfCacheMaxSeconds as
static, since no other file refers to them.  Also I changed the leading
double underscores to single underscores.

2. I added the const qualifier to several parameters.

3. In particular I added the const qualifier to the first parameter of
osrfCacheInit(), which is an array of pointers.  This change requires
a corresponding change in the calling code.

Fortunately only one file calls osrfCacheInit(), namely osrf_system.c,
so I can just add the const there too.  That's why all three patches
go together.  If we ever need to pass an array of truly non-const 
pointers we can find some other workaround at that time.

I would have liked to constify several other cache functions, but I
ran into problems with the underlying memcache code.  My copy of 
memcache.h says that the second parameter to mc_set() is not const.
As a result there are certain functions that I can't readily constify
without a ghastly cast.

That parameter could almost certainly be made const.  In fact I found
one version on the Net where it was indeed const.  However these
issues with third party code are more than I want to wrestle with
right now, if ever.  So I left those functions alone.

The patch to osrf_system.c constifies a few variables in order to
prepare for the day when jsonObjectGetIndex() and 
jsonObjectGetString() return const pointers.

Scott McKellar
http://home.swbell/net/mkc9/ct/

Developer's Certificate of Origin 1.1 By making a contribution to
this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license indicated
in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source license
and I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
same open source license (unless I am permitted to submit under a
different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person
who certified (a), (b) or (c) and I have not modified it; and

(d) In the case of each of (a), (b), or (c), I understand and agree
that this project and the contribution are public and that a record
of the contribution (including all personal information I submit
with it, including my sign-off) is maintained indefinitely and may
be redistributed consistent with this project or the open source
license indicated in the file.
*** trunk/include/opensrf/osrf_cache.h	2007-10-30 20:52:42.000000000 -0600
--- trunk-mod/include/opensrf/osrf_cache.h	2007-10-30 22:28:25.000000000 -0600
***************
*** 30,36 ****
    @param maxCacheSeconds The maximum amount of time an object / string may
  	be cached.  Negative number means there is no limit
    */
! int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds );
  
  
  /**
--- 30,36 ----
    @param maxCacheSeconds The maximum amount of time an object / string may
  	be cached.  Negative number means there is no limit
    */
! int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds );
  
  
  /**
***************
*** 58,82 ****
    @param key The cache key
    @return The object (which must be freed) if it exists, otherwise returns NULL
    */
! jsonObject* osrfCacheGetObject( char* key, ... );
  
  /**
    Grabs a string from the cache.
    @param key The cache key
    @return The string (which must be freed) if it exists, otherwise returns NULL
    */
! char* osrfCacheGetString( char* key, ... );
  
  /**
    Removes the item with the given key from the cache.
    @return 0 on success, -1 on error.
    */
! int osrfCacheRemove( char* key, ... );
  
  /**
   * Sets the expire time to 'seconds' for the given key
   */
! int osrfCacheSetExpire( time_t seconds, char* key, ... );
  
  
  
--- 58,82 ----
    @param key The cache key
    @return The object (which must be freed) if it exists, otherwise returns NULL
    */
! jsonObject* osrfCacheGetObject( const char* key, ... );
  
  /**
    Grabs a string from the cache.
    @param key The cache key
    @return The string (which must be freed) if it exists, otherwise returns NULL
    */
! char* osrfCacheGetString( const char* key, ... );
  
  /**
    Removes the item with the given key from the cache.
    @return 0 on success, -1 on error.
    */
! int osrfCacheRemove( const char* key, ... );
  
  /**
   * Sets the expire time to 'seconds' for the given key
   */
! int osrfCacheSetExpire( time_t seconds, const char* key, ... );
  
  
  
*** trunk/src/libopensrf/osrf_cache.c	2007-10-30 20:52:44.000000000 -0600
--- trunk-mod/src/libopensrf/osrf_cache.c	2007-10-30 22:28:57.000000000 -0600
***************
*** 15,32 ****
  
  #include <opensrf/osrf_cache.h>
  
! struct memcache* __osrfCache = NULL;
! time_t __osrfCacheMaxSeconds = -1;
  
! int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds ) {
  	if( !(serverStrings && size > 0) ) return -1;
  
  	int i;
! 	__osrfCache = mc_new();
! 	__osrfCacheMaxSeconds = maxCacheSeconds;
  
  	for( i = 0; i < size && serverStrings[i]; i++ ) 
! 		mc_server_add4( __osrfCache, serverStrings[i] );
  
  	return 0;
  }
--- 15,32 ----
  
  #include <opensrf/osrf_cache.h>
  
! static struct memcache* _osrfCache = NULL;
! static time_t _osrfCacheMaxSeconds = -1;
  
! int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) {
  	if( !(serverStrings && size > 0) ) return -1;
  
  	int i;
! 	_osrfCache = mc_new();
! 	_osrfCacheMaxSeconds = maxCacheSeconds;
  
  	for( i = 0; i < size && serverStrings[i]; i++ ) 
! 		mc_server_add4( _osrfCache, serverStrings[i] );
  
  	return 0;
  }
***************
*** 35,60 ****
  	if( !(key && obj) ) return -1;
  	char* s = jsonObjectToJSON( obj );
  	osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object: %s", s);
! 	if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
  
! 	mc_set(__osrfCache, key, strlen(key), s, strlen(s), seconds, 0);
  	free(s);
  	return 0;
  }
  
  int osrfCachePutString( char* key, const char* value, time_t seconds ) {
  	if( !(key && value) ) return -1;
! 	if( seconds < 0 ) seconds = __osrfCacheMaxSeconds;
  	osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string: %s", value);
! 	mc_set(__osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
  	return 0;
  }
  
! jsonObject* osrfCacheGetObject( char* key, ... ) {
  	jsonObject* obj = NULL;
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		char* data = (char*) mc_aget( __osrfCache, VA_BUF, strlen(VA_BUF) );
  		if( data ) {
  			osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
  			obj = jsonParseString( data );
--- 35,60 ----
  	if( !(key && obj) ) return -1;
  	char* s = jsonObjectToJSON( obj );
  	osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object: %s", s);
! 	if( seconds < 0 ) seconds = _osrfCacheMaxSeconds;
  
! 	mc_set(_osrfCache, key, strlen(key), s, strlen(s), seconds, 0);
  	free(s);
  	return 0;
  }
  
  int osrfCachePutString( char* key, const char* value, time_t seconds ) {
  	if( !(key && value) ) return -1;
! 	if( seconds < 0 ) seconds = _osrfCacheMaxSeconds;
  	osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string: %s", value);
! 	mc_set(_osrfCache, key, strlen(key), value, strlen(value), seconds, 0);
  	return 0;
  }
  
! jsonObject* osrfCacheGetObject( const char* key, ... ) {
  	jsonObject* obj = NULL;
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		char* data = (char*) mc_aget( _osrfCache, VA_BUF, strlen(VA_BUF) );
  		if( data ) {
  			osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
  			obj = jsonParseString( data );
***************
*** 65,74 ****
  	return NULL;
  }
  
! char* osrfCacheGetString( char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		char* data = (char*) mc_aget(__osrfCache, VA_BUF, strlen(VA_BUF) );
  		osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
  		if(!data) osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
  		return data;
--- 65,74 ----
  	return NULL;
  }
  
! char* osrfCacheGetString( const char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		char* data = (char*) mc_aget(_osrfCache, VA_BUF, strlen(VA_BUF) );
  		osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data);
  		if(!data) osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF);
  		return data;
***************
*** 77,92 ****
  }
  
  
! int osrfCacheRemove( char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		return mc_delete(__osrfCache, VA_BUF, strlen(VA_BUF), 0 );
  	}
  	return -1;
  }
  
  
! int osrfCacheSetExpire( time_t seconds, char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
  		jsonObject* o = osrfCacheGetObject( VA_BUF );
--- 77,92 ----
  }
  
  
! int osrfCacheRemove( const char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
! 		return mc_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 );
  	}
  	return -1;
  }
  
  
! int osrfCacheSetExpire( time_t seconds, const char* key, ... ) {
  	if( key ) {
  		VA_LIST_TO_STRING(key);
  		jsonObject* o = osrfCacheGetObject( VA_BUF );
***************
*** 97,104 ****
  }
  
  void osrfCacheCleanup() {
!     if(__osrfCache) 
!         mc_free(__osrfCache);
  }
  
  
--- 97,104 ----
  }
  
  void osrfCacheCleanup() {
!     if(_osrfCache)
!         mc_free(_osrfCache);
  }
  
  
*** trunk/src/libopensrf/osrf_system.c	2007-10-30 20:52:44.000000000 -0600
--- trunk-mod/src/libopensrf/osrf_system.c	2007-10-30 22:27:48.000000000 -0600
***************
*** 56,62 ****
  
  		if( cacheServers->type == JSON_ARRAY ) {
  			int i;
! 			char* servers[cacheServers->size];
  			for( i = 0; i != cacheServers->size; i++ ) {
  				servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
  				osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
--- 56,62 ----
  
  		if( cacheServers->type == JSON_ARRAY ) {
  			int i;
! 			const char* servers[cacheServers->size];
  			for( i = 0; i != cacheServers->size; i++ ) {
  				servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
  				osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
***************
*** 64,70 ****
  			osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
  
  		} else {
! 			char* servers[] = { jsonObjectGetString(cacheServers) };		
  			osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
  			osrfCacheInit( servers, 1, atoi(maxCache) );
  		}
--- 64,70 ----
  			osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
  
  		} else {
! 			const char* servers[] = { jsonObjectGetString(cacheServers) };		
  			osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
  			osrfCacheInit( servers, 1, atoi(maxCache) );
  		}
***************
*** 116,122 ****
  			osrfStringArrayAdd(arr, jsonObjectGetString(apps));
  
  		} else {
! 			jsonObject* app;
  			while( (app = jsonObjectGetIndex(apps, i++)) ) 
  				osrfStringArrayAdd(arr, jsonObjectGetString(app));
  		}
--- 116,122 ----
  			osrfStringArrayAdd(arr, jsonObjectGetString(apps));
  
  		} else {
! 			const jsonObject* app;
  			while( (app = jsonObjectGetIndex(apps, i++)) ) 
  				osrfStringArrayAdd(arr, jsonObjectGetString(app));
  		}

Reply via email to