[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnue/geas/src/objectcache objectcache.c
From: |
Neil Tiffin |
Subject: |
gnue/geas/src/objectcache objectcache.c |
Date: |
Thu, 07 Jun 2001 12:51:53 -0700 |
CVSROOT: /home/cvs
Module name: gnue
Changes by: Neil Tiffin <address@hidden> 01/06/07 12:51:53
Modified files:
geas/src/objectcache: objectcache.c
Log message:
Convert to GNU standards, remove compiler warnings.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/geas/src/objectcache/objectcache.c.diff?cvsroot=OldCVS&tr1=1.46&tr2=1.47&r1=text&r2=text
Patches:
Index: gnue/geas/src/objectcache/objectcache.c
diff -u gnue/geas/src/objectcache/objectcache.c:1.46
gnue/geas/src/objectcache/objectcache.c:1.47
--- gnue/geas/src/objectcache/objectcache.c:1.46 Mon May 28 07:57:51 2001
+++ gnue/geas/src/objectcache/objectcache.c Thu Jun 7 12:51:53 2001
@@ -1,6 +1,8 @@
/*
- geas - GNU Enterprise Application Server
+ objectcache.c -
+
+ Part of GNU Enterprise Application Server (GEAS)
Copyright (C) 2001 Free Software Foundation
@@ -17,6 +19,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+ $Id: objectcache.c,v 1.47 2001/06/07 19:51:53 ntiffin Exp $
*/
@@ -32,7 +36,7 @@
#include "geas-skeleton.h"
/* ie, turn off temp. debug output */
-#define OC_TEMP_DEBUG (-83) /* DEBUGLEVEL_OFF */
+#define OC_TEMP_DEBUG (-83) /* DEBUGLEVEL_OFF */
/** \file objectcache.c
* \brief Store object data in RAM
@@ -50,75 +54,81 @@
*/
static GHashTable *cachedata = NULL;
-static GHashTable *indexes = NULL;
+static GHashTable *indexes = NULL;
static int objectcache_count = 0;
static guint32 cache_max_size = 0;
static GList *flushable = NULL;
-static void oc_squeeze_cache( int spaces_to_make );
-static void oc_free_object_data(_ObjectData * o);
-static _ObjectData *oc_allocate_object_data( const char *classname, const char
*key);
-static _FieldData *oc_alloc_field_data(const char *name);
-static void oc_free_field_data(gpointer key, _FieldData * f, gpointer
userdata);
-static ObjectData *oc_objectstore_query_to_objectcache(QueryData * q);
-static void oc_remove_from_index( _CacheIndex *idx , _ObjectData *o );
-static void oc_add_object_to_index( _ObjectData *object , oc_index_identifier
*indexid );
-static void
-oc_free_index_identifier_list( GList *list );
-static oc_index_identifier *
-oc_make_index_identifier( _ObjectData *object , _odl_index *i );
-static char *
-oc_make_object_index_hash( _ObjectData *o , _odl_index *idx );
-static GList *
-oc_object_to_start_of_list( GList *l , ObjectData *obj );
-static GList *
-oc_object_to_end_of_list( GList *l , ObjectData *obj );
+static void oc_squeeze_cache (int spaces_to_make);
+static void oc_free_object_data (_ObjectData * o);
+static _ObjectData *oc_allocate_object_data (const char *classname,
+ const char *key);
+static _FieldData *oc_alloc_field_data (const char *name);
+static void oc_free_field_data (gpointer key, _FieldData * f,
+ gpointer userdata);
+static ObjectData *oc_objectstore_query_to_objectcache (QueryData * q);
+static void oc_remove_from_index (_CacheIndex * idx, _ObjectData * o);
+static void oc_add_object_to_index (_ObjectData * object,
+ oc_index_identifier * indexid);
+static void oc_free_index_identifier_list (GList * list);
+static oc_index_identifier *oc_make_index_identifier (_ObjectData * object,
+ _odl_index * i);
+static char *oc_make_object_index_hash (_ObjectData * o, _odl_index * idx);
+static GList *oc_object_to_start_of_list (GList * l, ObjectData * obj);
+static GList *oc_object_to_end_of_list (GList * l, ObjectData * obj);
+
+void _fill_values (gchar * key, _FieldData * field, GHashTable * values);
/* used to clear all data from the cache */
-void oc_empty_cache()
+void
+oc_empty_cache ()
{
- int maxlen = cache_max_size;
- oc_shrink_cache( 0 );
- cache_max_size = maxlen;
+ int maxlen = cache_max_size;
+ oc_shrink_cache (0);
+ cache_max_size = maxlen;
}
-void oc_shrink_cache( int new_max_length )
+void
+oc_shrink_cache (int new_max_length)
{
- trace_functioncall();
-
- /* this can't extend it */
- if( new_max_length >= cache_max_size )
- return;
-
- if( new_max_length < 0 )
- cache_max_size += new_max_length; /* shrink by -N spaces */
- else
- cache_max_size = new_max_length;
+ trace_functioncall ();
- /* 0 == empty cache */
- if( cache_max_size < 0 ) cache_max_size = 0;
-
- oc_squeeze_cache( 5 );
-
- /* still need space for 1 item in the cache, for reading/writing */
- if( cache_max_size < 1 ) cache_max_size = 1;
+ /* this can't extend it */
+ if (new_max_length >= cache_max_size)
+ return;
+
+ if (new_max_length < 0)
+ cache_max_size += new_max_length; /* shrink by -N spaces */
+ else
+ cache_max_size = new_max_length;
+
+ /* 0 == empty cache */
+ if (cache_max_size < 0)
+ cache_max_size = 0;
+
+ oc_squeeze_cache (5);
+
+ /* still need space for 1 item in the cache, for reading/writing */
+ if (cache_max_size < 1)
+ cache_max_size = 1;
}
-void oc_extend_cache( int new_max_length )
+void
+oc_extend_cache (int new_max_length)
{
- trace_functioncall();
+ trace_functioncall ();
- /* this can't shrink it */
- if( new_max_length <= cache_max_size )
- return;
+ /* this can't shrink it */
+ if (new_max_length <= cache_max_size)
+ return;
+
+ if (new_max_length < 0)
+ cache_max_size -= new_max_length; /* extend by - N spaces */
+ else
+ cache_max_size = new_max_length;
- if( new_max_length < 0 )
- cache_max_size -= new_max_length; /* extend by - N spaces */
- else
- cache_max_size = new_max_length;
-
- cache_max_size = new_max_length;
- /* only allocates more space, no need to remove any objects */
+ cache_max_size = new_max_length;
+ /* only allocates more space, no need to remove any objects */
}
/** \brief make space in caxche
@@ -126,895 +136,976 @@
* the cache before running out of space, assuming no other objects
* are deleted
*/
-static void oc_squeeze_cache( int spaces_to_make )
+static void
+oc_squeeze_cache (int spaces_to_make)
{
- _ObjectData *obj;
- GList *last;
- gboolean squeezed = FALSE;
-
- trace_functioncall();
-
- spaces_to_make = 1;
- if( spaces_to_make > cache_max_size )
- spaces_to_make = cache_max_size;
-
- while( (spaces_to_make+objectcache_count) > cache_max_size && flushable )
- {
- squeezed = TRUE;
-
- /* grab one off the tail of the flush list and flush it */
- last = g_list_last( flushable );
- if( last )
- {
- obj = last->data;
- /* message( "flushing object %08lx > %08lx/%08lx" , obj ,
obj->classname , obj->key );
- fflush( NULL ); */
- /* message( "flushing object %s/%s" , obj->classname , obj->key );
- fflush( NULL ); */
-
- /* write to database, if needed */
- if( obj->indatabase == 0 )
- oc_flush_object_to_store(obj);
-
- /* remove from cache (has to be in database now, but check anyway)
*/
- if( obj->indatabase != 0 )
- oc_remove_object( obj );
- else
- break;
- }
- }
-
- if( objectcache_count > cache_max_size )
- {
- errormsg( "The cache has grown to %d entries, with a maximum of %d, and
none can be flushed." , objectcache_count , cache_max_size );
- }
- /* if( squeezed ) message( "New cache size: %d objects" , objectcache_count
); */
+ _ObjectData *obj;
+ GList *last;
+ gboolean squeezed = FALSE;
+
+ trace_functioncall ();
+
+ spaces_to_make = 1;
+ if (spaces_to_make > cache_max_size)
+ {
+ spaces_to_make = cache_max_size;
+ }
+ while ((spaces_to_make + objectcache_count) > cache_max_size && flushable)
+ {
+ squeezed = TRUE;
+
+ /* grab one off the tail of the flush list and flush it */
+ last = g_list_last (flushable);
+ if (last)
+ {
+ obj = last->data;
+#if 0
+ message ("flushing object %08lx > %08lx/%08lx", obj,
+ obj->classname, obj->key);
+ fflush (NULL);
+ message ("flushing object %s/%s", obj->classname, obj->key);
+ fflush (NULL);
+#endif
+ /* write to database, if needed */
+ if (obj->indatabase == 0)
+ {
+ oc_flush_object_to_store (obj);
+ }
+ /* remove from cache (has to be in database now, but check anyway) */
+ if (obj->indatabase != 0)
+ {
+ oc_remove_object (obj);
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+ if (objectcache_count > cache_max_size)
+ {
+ errormsg
+ ("The cache has grown to %d entries, with a maximum of %d, and none
can be flushed.",
+ objectcache_count, cache_max_size);
+ }
+#if 0
+ if (squeezed)
+ {
+ message ("New cache size: %d objects", objectcache_count);
+ }
+#endif
}
/* allocate/free cache */
void
-create_object_cache(guint32 max_length)
- {
- trace_functioncall();
-
- if (cachedata)
- free_object_cache();
-
- cache_max_size = max_length;
-
- cachedata = g_hash_table_new(g_str_hash, g_str_equal);
- indexes = g_hash_table_new(g_str_hash, g_str_equal);
- objectcache_count = 0;
- }
+create_object_cache (guint32 max_length)
+{
+ trace_functioncall ();
+
+ if (cachedata)
+ {
+ free_object_cache ();
+ }
+ cache_max_size = max_length;
+ cachedata = g_hash_table_new (g_str_hash, g_str_equal);
+ indexes = g_hash_table_new (g_str_hash, g_str_equal);
+ objectcache_count = 0;
+}
void
-free_object_cache()
- {
- /* TODO */
- fatal_error("Not yet implemented");
- }
+free_object_cache ()
+{
+ /* TODO */
+ fatal_error ("Not yet implemented");
+}
/* Searching */
ObjectData *
-oc_find_object_by_key(const char *classname, const char *key)
- {
- ObjectData *obj;
- QueryData *q;
-
- trace_functioncall();
- obj = oc_find_cached_object_by_key(classname,key);
- if (obj)
- return (obj);
+oc_find_object_by_key (const char *classname, const char *key)
+{
+ ObjectData *obj;
+ QueryData *q;
- /* not in cache, so load from database */
- q = oql_load_object_by_key(configdata, classname, key);
- if (q)
- obj = oc_objectstore_query_to_objectcache(q);
- oql_free_query(q);
- return (obj);
- }
+ trace_functioncall ();
+ obj = oc_find_cached_object_by_key (classname, key);
+ if (obj)
+ {
+ return (obj);
+ }
+ /* not in cache, so load from database */
+ q = oql_load_object_by_key (configdata, classname, key);
+ if (q)
+ {
+ obj = oc_objectstore_query_to_objectcache (q);
+ }
+ oql_free_query (q);
+ return (obj);
+}
ObjectData *
-oc_find_cached_object_by_key(const char *classname, const char *key)
- {
- ObjectData *obj;
- char *lookup;
- char *p;
-
- trace_functioncall();
- if( strncmp(classname,"root::",6) == 0 ) p = (char *)&classname[6];
- else p = (char *)classname;
-
- /* find the classname's hash table - if none object can't be in the */
- /* cache yet */
- lookup = g_strdup_printf( "%s-%s" , p , key );
- obj = g_hash_table_lookup(cachedata, lookup);
- g_free( lookup );
-
- return (obj);
- }
+oc_find_cached_object_by_key (const char *classname, const char *key)
+{
+ ObjectData *obj;
+ char *lookup;
+ char *p;
+
+ trace_functioncall ();
+ if (strncmp (classname, "root::", 6) == 0)
+ {
+ p = (char *) &classname[6];
+ }
+ else
+ {
+ p = (char *) classname;
+ }
+ /* find the classname's hash table - if none object can't be in the */
+ /* cache yet */
+ lookup = g_strdup_printf ("%s-%s", p, key);
+ obj = g_hash_table_lookup (cachedata, lookup);
+ g_free (lookup);
+ return (obj);
+}
/* Object management */
ObjectData *
-oc_add_empty_object(const char *classname, const char *key)
- {
- ObjectData *obj = NULL;
- char *oid = g_strdup(key);
- char *p;
-
- trace_functioncall();
-
- /* if there's still any '-' characters, remove them */
- if( strchr(key,'-') != NULL ) {
- cleanup_oid( oid );
- }
-
- if( strncmp(classname,"root::",6) == 0 ) p = (char *)&classname[6];
- else p = (char *)classname;
-
- /* if the object already exists, return it */
- obj = oc_find_cached_object_by_key(p, oid);
- if (obj) {
- g_free( oid );
+oc_add_empty_object (const char *classname, const char *key)
+{
+ ObjectData *obj = NULL;
+ char *oid = g_strdup (key);
+ char *p;
+
+ trace_functioncall ();
+
+ /* if there's still any '-' characters, remove them */
+ if (strchr (key, '-') != NULL)
+ {
+ cleanup_oid (oid);
+ }
+
+ if (strncmp (classname, "root::", 6) == 0)
+ {
+ p = (char *) &classname[6];
+ }
+ else
+ {
+ p = (char *) classname;
+ }
+ /* if the object already exists, return it */
+ obj = oc_find_cached_object_by_key (p, oid);
+ if (obj)
+ {
+ g_free (oid);
return (obj);
- }
+ }
- /* make sure the cache doesn't have too many entries */
- oc_squeeze_cache( 5 );
+ /* make sure the cache doesn't have too many entries */
+ oc_squeeze_cache (5);
- /* create a new object */
- obj = oc_allocate_object_data(p, oid);
- g_free( oid );
- oid = NULL;
- if (!obj) {
+ /* create a new object */
+ obj = oc_allocate_object_data (p, oid);
+ g_free (oid);
+ oid = NULL;
+ if (!obj)
+ {
return (NULL);
- }
- /* printf( "created: %08lx > %08lx/%08lx (%s/%s)\n", obj,
- obj->classname,obj->key , obj->classname,obj->key ); */
- /* fflush( NULL ); */
-
- /* printf( "adding %08lx to flushable\n" , obj ); */
- flushable = g_list_prepend( flushable , obj );
-
- /* store it */
- /* printf( "add %s/%s\n" , classname , key ); */
- g_hash_table_insert(cachedata, obj->hashkey, obj);
- objectcache_count+=1;
- /* printf( "objectcache_count = %d\n" , objectcache_count ); */
-/* printf( "now has %d entries\n" , objectcache_count ); */
-
- /* done */
- return (obj);
- }
+ }
+ /* printf( "created: %08lx > %08lx/%08lx (%s/%s)\n", obj,
+ obj->classname,obj->key , obj->classname,obj->key ); */
+ /* fflush( NULL ); */
+
+ /* printf( "adding %08lx to flushable\n" , obj ); */
+ flushable = g_list_prepend (flushable, obj);
+
+ /* store it */
+ /* printf( "add %s/%s\n" , classname , key ); */
+ g_hash_table_insert (cachedata, obj->hashkey, obj);
+ objectcache_count += 1;
+#if 0
+ printf ("objectcache_count = %d\n", objectcache_count);
+ printf ("now has %d entries\n", objectcache_count);
+#endif
+ /* done */
+ return (obj);
+}
gboolean
-oc_remove_object(ObjectData * object)
- {
- _ObjectData *obj;
- GList *idxs,*ids;
- odl_class *c;
-
- trace_functioncall();
-
- obj = g_hash_table_lookup(cachedata, object->hashkey);
- if (!obj)
- {
+oc_remove_object (ObjectData * object)
+{
+ _ObjectData *obj;
+ /* GList *idxs, *ids; */
+ /* odl_class *c; */
+
+ trace_functioncall ();
+
+ obj = g_hash_table_lookup (cachedata, object->hashkey);
+ if (!obj)
+ {
/* object wasn't in the cache */
return (FALSE);
- }
- /* if( g_list_find(flushable,object) ) printf( "in flushable list: %08lx
(good)\n" , object ); */
- flushable = g_list_remove( flushable , object );
- if( g_list_find(flushable,object) ) {
- printf( "still in flushable list: %08lx (bad 0)\n" , object );
- abort();
- }
- /* remove it */
- g_hash_table_remove(cachedata, obj->hashkey);
- oc_free_object_data(object);
- objectcache_count-=1;
- /* printf( "objectcache_count = %d\n" , objectcache_count ); */
+ }
+#if 0
+ if (g_list_find (flushable, object))
+ {
+ printf ("in flushable list: %px (good)\n", object);
+ }
+#endif
+ flushable = g_list_remove (flushable, object);
+ if (g_list_find (flushable, object))
+ {
+ printf ("still in flushable list: %px (bad 0)\n", object);
+ abort ();
+ }
+ /* remove it */
+ g_hash_table_remove (cachedata, obj->hashkey);
+ oc_free_object_data (object);
+ objectcache_count -= 1;
+ /* printf( "objectcache_count = %d\n" , objectcache_count ); */
- return (TRUE);
- }
+ return (TRUE);
+}
gboolean
-oc_delete_object(const char *classname, const char *keystr)
- {
- _ObjectData *obj;
- struct query_result *result;
- int err;
- char *errmsg;
-
- trace_functioncall();
-
- /* remove from cache */
- obj = oc_find_cached_object_by_key(classname, keystr);
- if (obj)
- oc_remove_object(obj);
-
- /* remove from database */
- result = delete_from_objectstore(classname, keystr, &err, &errmsg);
- if (result)
- free_query_result(result);
- if (errmsg)
- {
- errormsg(errmsg);
- g_free(errmsg);
+oc_delete_object (const char *classname, const char *keystr)
+{
+ _ObjectData *obj;
+ struct query_result *result;
+ int err;
+ char *errmsg;
+
+ trace_functioncall ();
+
+ /* remove from cache */
+ obj = oc_find_cached_object_by_key (classname, keystr);
+ if (obj)
+ oc_remove_object (obj);
+
+ /* remove from database */
+ result = delete_from_objectstore (classname, keystr, &err, &errmsg);
+ if (result)
+ free_query_result (result);
+ if (errmsg)
+ {
+ errormsg (errmsg);
+ g_free (errmsg);
return (FALSE);
- }
+ }
- return (TRUE);
- }
+ return (TRUE);
+}
const char *
-oc_get_object_class(ObjectData * object)
- {
- return (object->classname);
- }
+oc_get_object_class (ObjectData * object)
+{
+ return (object->classname);
+}
const char *
-oc_get_object_key(ObjectData * object)
- {
- return (object->key);
- }
+oc_get_object_key (ObjectData * object)
+{
+ return (object->key);
+}
gboolean
-oc_validate_object(const char *classname, const char *keystr)
- {
- trace_functioncall();
- if( oc_find_object_by_key(classname,keystr) )
- return (TRUE);
- else
- return (FALSE);
- }
+oc_validate_object (const char *classname, const char *keystr)
+{
+ trace_functioncall ();
+ if (oc_find_object_by_key (classname, keystr))
+ return (TRUE);
+ else
+ return (FALSE);
+}
void
-_fill_values (gchar *key, _FieldData *field, GHashTable *values)
+_fill_values (gchar * key, _FieldData * field, GHashTable * values)
{
g_hash_table_insert (values, key, field->value);
}
void
-oc_flush_object_to_store(ObjectData * obj)
- {
- struct query_result *r = NULL;
- GHashTable *values;
-
- trace_functioncall();
- /* TODO: error checking, updating object flags */
-
- /* create hash with pure name-values pairs */
- values = g_hash_table_new (g_str_hash, g_str_equal);
- g_hash_table_foreach (obj->fields, _fill_values, values);
-
- r = write_to_objectstore (obj->classname, obj->key, values,
- obj->indatabase, NULL, NULL);
-
- g_hash_table_destroy (values);
-
- if( r == NULL || r->success != TRUE )
- {
- free_query_result(r);
- return;
- }
+oc_flush_object_to_store (ObjectData * obj)
+{
+ struct query_result *r = NULL;
+ GHashTable *values;
- free_query_result(r);
- flushable = oc_object_to_end_of_list( flushable , obj );
+ trace_functioncall ();
+ /* TODO: error checking, updating object flags */
- oc_set_object_flag(obj, of_indatabase, FLAG_ON);
- if (oc_get_object_flag(obj, of_indatabase) != FLAG_ON)
- {
- printf("TODO: error handler at %s/%d\n" , __FILE__ , __LINE__);
- exit(0);
- }
- }
+ /* create hash with pure name-values pairs */
+ values = g_hash_table_new (g_str_hash, g_str_equal);
+ g_hash_table_foreach (obj->fields, (GHFunc) _fill_values, values);
+
+ r = write_to_objectstore (obj->classname, obj->key, values,
+ obj->indatabase, NULL, NULL);
+ g_hash_table_destroy (values);
+ if (r == NULL || r->success != TRUE)
+ {
+ free_query_result (r);
+ return;
+ }
+ free_query_result (r);
+ flushable = oc_object_to_end_of_list (flushable, obj);
+ oc_set_object_flag (obj, of_indatabase, FLAG_ON);
+ if (oc_get_object_flag (obj, of_indatabase) != FLAG_ON)
+ {
+ printf ("TODO: error handler at %s/%d\n", __FILE__, __LINE__);
+ exit (0);
+ }
+}
/* Object flags */
void
-oc_set_object_flag(ObjectData * object, enum object_flag flag, gboolean state)
- {
- trace_functioncall();
- switch (flag)
- {
- case of_indatabase:
+oc_set_object_flag (ObjectData * object, enum object_flag flag,
+ gboolean state)
+{
+ trace_functioncall ();
+ switch (flag)
+ {
+ case of_indatabase:
object->indatabase = (state == FLAG_ON ? 1 : 0);
break;
- case of_deleted:
+ case of_deleted:
object->deleted = (state == FLAG_ON ? 1 : 0);
break;
- }
+ }
- }
+}
gboolean
-oc_get_object_flag(ObjectData * object, enum object_flag flag)
- {
- trace_functioncall();
- switch (flag)
- {
- case of_indatabase:
+oc_get_object_flag (ObjectData * object, enum object_flag flag)
+{
+ trace_functioncall ();
+ switch (flag)
+ {
+ case of_indatabase:
return object->indatabase ? FLAG_ON : FLAG_OFF;
- case of_deleted:
+ case of_deleted:
return object->deleted ? FLAG_ON : FLAG_OFF;
- }
- return (FLAG_OFF);
- }
+ }
+ return (FLAG_OFF);
+}
/* Data access */
char *
-oc_get_object_field(ObjectData * object, const char *fieldname)
- {
- _FieldData *f = NULL;
- odl_class *cl = NULL;
- odl_field *ft = NULL;
- QueryData *q = NULL;
- ObjectData *o = NULL;
-
- trace_functioncall();
-
- flushable = oc_object_to_start_of_list( flushable , object );
-
- /* special case */
- if (g_strcasecmp("objectid", fieldname) == 0)
- return (g_strdup(object->key));
-
- /* look for field name in object */
- f = g_hash_table_lookup(object->fields, fieldname);
- if (f)
- {
+oc_get_object_field (ObjectData * object, const char *fieldname)
+{
+ _FieldData *f = NULL;
+ odl_class *cl = NULL;
+ odl_field *ft = NULL;
+ QueryData *q = NULL;
+ /* ObjectData *o = NULL; */
+
+ trace_functioncall ();
+
+ flushable = oc_object_to_start_of_list (flushable, object);
+
+ /* special case */
+ if (g_strcasecmp ("objectid", fieldname) == 0)
+ {
+ return (g_strdup (object->key));
+ }
+ /* look for field name in object */
+ f = g_hash_table_lookup (object->fields, fieldname);
+ if (f)
+ {
/* found in cache, returnm it */
- return (g_strdup(f->value));
- }
-
- /* get class and field info */
- cl = odl_find_class(all_classes, object->classname, NULL);
- if (!cl)
- return (NULL);
- ft = odl_class_get_field(cl, fieldname);
- if (!ft)
- return (NULL);
-
- switch (odl_field_get_type(ft))
- {
- case FT_basic:
+ return (g_strdup (f->value));
+ }
+ /* get class and field info */
+ cl = odl_find_class (all_classes, object->classname, NULL);
+ if (!cl)
+ return (NULL);
+ ft = odl_class_get_field (cl, fieldname);
+ if (!ft)
+ return (NULL);
+ switch (odl_field_get_type (ft))
+ {
+ case FT_basic:
/* basic field in this class - attempt to reload it */
- q = oql_load_object_field_by_key(configdata, object->classname,
- fieldname, object->key);
+ q = oql_load_object_field_by_key (configdata, object->classname,
+ fieldname, object->key);
if (q)
- {
- object = oc_objectstore_query_to_objectcache(q);
- oql_free_query(q);
- if (object)
+ {
+ object = oc_objectstore_query_to_objectcache (q);
+ oql_free_query (q);
+ if (object)
{
- f = g_hash_table_lookup(object->fields, fieldname);
- if (f)
- {
- /* found in cache, returnm it */
- return (g_strdup(f->value));
- }
- /* still not found? damn.. */
- return (NULL);
+ f = g_hash_table_lookup (object->fields, fieldname);
+ if (f)
+ {
+ /* found in cache, returnm it */
+ return (g_strdup (f->value));
+ }
+ /* still not found? damn.. */
+ return (NULL);
}
- }
+ }
break;
- case FT_lookup:
+ case FT_lookup:
/* lookup field not found - load it */
- {
- GList *s, *t;
- char *keystr, *value, *targetfield, *errmsg;
- odl_class *c;
- char *loadclass = (char *)odl_field_get_sourceclass(ft);
- ObjectData *o;
- int idfield;
- struct query_result *result;
-
- q = create_base_query(configdata, loadclass);
- if (!q)
+ {
+ GList *s, *t;
+ /* char *keystr; */
+ char *value;
+ /* char *targetfield; */
+ /* char *errmsg; */
+ odl_class *c;
+ char *loadclass = (char *) odl_field_get_sourceclass (ft);
+ ObjectData *o;
+ /* int idfield; */
+ /* struct query_result *result; */
+
+ q = create_base_query (configdata, loadclass);
+ if (!q)
+ {
return (NULL);
-
- /* add constraints to find just members of this lookup */
- /* ... WHERE loadclass.t1 = this.s1 AND loadclass.t2 = this.s2
- ... */
- s = odl_field_get_source_fields(ft);
- t = odl_field_get_this_fields(ft);
- while (s && t)
- {
- c = odl_field_defined_in(cl, (const char *)t->data);
+ }
+ /* add constraints to find just members of this lookup */
+ /* ... WHERE loadclass.t1 = this.s1 AND loadclass.t2 = this.s2
+ ... */
+ s = odl_field_get_source_fields (ft);
+ t = odl_field_get_this_fields (ft);
+ while (s && t)
+ {
+ c = odl_field_defined_in (cl, (const char *) t->data);
if (!c)
- {
- oql_free_query(q);
- return (NULL);
- }
- o = oc_find_object_by_key(odl_class_get_full_name(c), object->key);
+ {
+ oql_free_query (q);
+ return (NULL);
+ }
+ o =
+ oc_find_object_by_key (odl_class_get_full_name (c),
+ object->key);
if (!o)
- {
- oql_free_query(q);
- return (NULL);
- }
- value = (char *)oc_get_object_field(o, t->data);
+ {
+ oql_free_query (q);
+ return (NULL);
+ }
+ value = (char *) oc_get_object_field (o, t->data);
if (!oql_add_query_constraint
- (q, NULL, (const char *)value, "=", loadclass,
- (const char *)s->data))
- {
- oql_free_query(q);
- return (NULL);
- }
- s = g_list_next(s);
- t = g_list_next(t);
- }
- if (s != NULL || t != NULL)
- {
+ (q, NULL, (const char *) value, "=", loadclass,
+ (const char *) s->data))
+ {
+ oql_free_query (q);
+ return (NULL);
+ }
+ s = g_list_next (s);
+ t = g_list_next (t);
+ }
+ if (s != NULL || t != NULL)
+ {
/* serious error in class def */
- criticalerror("%s.%s has unbalanced field lists",
- odl_class_get_full_name(cl), fieldname);
- oql_free_query(q);
+ criticalerror ("%s.%s has unbalanced field lists",
+ odl_class_get_full_name (cl), fieldname);
+ oql_free_query (q);
return (NULL);
- }
- if (q)
- {
- o = oc_objectstore_query_to_objectcache(q);
- oql_free_query(q);
+ }
+ if (q)
+ {
+ o = oc_objectstore_query_to_objectcache (q);
+ oql_free_query (q);
if (o)
- {
- char *retval;
- retval = oc_get_object_field(o,odl_field_get_sourcefield(ft));
- f =
g_hash_table_lookup(o->fields,odl_field_get_sourcefield(ft));
- if (f)
- f->readonly = TRUE;
- return( retval );
- }
- }
- }
+ {
+ char *retval;
+ retval =
+ oc_get_object_field (o, odl_field_get_sourcefield (ft));
+ f =
+ g_hash_table_lookup (o->fields,
+ odl_field_get_sourcefield (ft));
+ if (f)
+ {
+ f->readonly = TRUE;
+ }
+ return (retval);
+ }
+ }
+ }
break;
- default:
+ default:
/* no other types handled yet */
break;
- }
+ }
- /* didn't find it */
- return (NULL);
- }
+ /* didn't find it */
+ return (NULL);
+}
gboolean
-oc_set_object_field(ObjectData * object, const char *fieldname,
- const char *value, gboolean readonly)
- {
- _FieldData *f;
- GList *idxs,*l,*ids;
- odl_class *c;
-
- trace_functioncall();
- if (g_strcasecmp("objectid", fieldname) == 0)
- return( FALSE );
-
- f = g_hash_table_lookup(object->fields, fieldname);
- if (!f)
- {
- f = oc_alloc_field_data(fieldname);
+oc_set_object_field (ObjectData * object, const char *fieldname,
+ const char *value, gboolean readonly)
+{
+ _FieldData *f;
+ GList *idxs, *l, *ids;
+ odl_class *c;
+
+ trace_functioncall ();
+ if (g_strcasecmp ("objectid", fieldname) == 0)
+ {
+ return (FALSE);
+ }
+ f = g_hash_table_lookup (object->fields, fieldname);
+ if (!f)
+ {
+ f = oc_alloc_field_data (fieldname);
if (f)
- {
- g_hash_table_insert(object->fields, f->name, f);
- }
- }
+ {
+ g_hash_table_insert (object->fields, f->name, f);
+ }
+ }
- if (f)
- {
+ if (f)
+ {
/* the first time this is called with 'readonly' set to true */
/* the object's readonly field is set to true */
/* effectively, this allows write-once behaviour so that */
/* a readonly field can be set the first time */
if (f->readonly)
- return (FALSE);
+ {
+ return (FALSE);
+ }
if (readonly)
- f->readonly = TRUE;
-
+ {
+ f->readonly = TRUE;
+ }
/* make a list of all indexes that this field is attached to */
idxs = NULL;
ids = NULL;
- c = odl_find_class( all_classes , object->classname , NULL );
- if( c )
- {
- GList *l = c->indexes;
- while( l )
- {
- _odl_index *i = (_odl_index *)l->data;
- if( !i->primary &&
odl_namelist_contains(i->fields,fieldname,FALSE) )
- {
- idxs = g_list_append( idxs , i );
- }
- l = g_list_next( l );
- }
- }
- if( idxs )
- {
- l = idxs;
- while( l )
- {
- oc_index_identifier *identifier = NULL;
- _odl_index *i = (_odl_index *)l->data;
-
- identifier = oc_make_index_identifier( object , i );
- ids = g_list_append( ids , identifier );
-
- l = l->next;
- }
- g_list_free( idxs );
- }
+ c = odl_find_class (all_classes, object->classname, NULL);
+ if (c)
+ {
+ GList *l = c->indexes;
+ while (l)
+ {
+ _odl_index *i = (_odl_index *) l->data;
+ if (!i->primary
+ && odl_namelist_contains (i->fields, fieldname, FALSE))
+ {
+ idxs = g_list_append (idxs, i);
+ }
+ l = g_list_next (l);
+ }
+ }
+ if (idxs)
+ {
+ l = idxs;
+ while (l)
+ {
+ oc_index_identifier *identifier = NULL;
+ _odl_index *i = (_odl_index *) l->data;
- /* remove the object from any indexes in the 'ids' list */
- if( ids )
- {
- GList *l = object->indexes;
- while( l )
- {
- _CacheIndex *i = l->data;
- if( odl_namelist_contains(ids,i->hashkey,TRUE) )
- {
- oc_remove_from_index( i , object );
- }
- l = g_list_next( l );
- }
- }
+ identifier = oc_make_index_identifier (object, i);
+ ids = g_list_append (ids, identifier);
+ l = l->next;
+ }
+ g_list_free (idxs);
+ }
+
+ /* remove the object from any indexes in the 'ids' list */
+ if (ids)
+ {
+ GList *l = object->indexes;
+ while (l)
+ {
+ _CacheIndex *i = l->data;
+ if (odl_namelist_contains (ids, i->hashkey, TRUE))
+ {
+ oc_remove_from_index (i, object);
+ }
+ l = g_list_next (l);
+ }
+ }
/* update the value */
if (f->value)
- g_free(f->value);
- f->value = g_strdup(value);
-
+ {
+ g_free (f->value);
+ }
+ f->value = g_strdup (value);
/* add the object to any indexes in the 'ids' list */
- if( ids )
- {
- GList *l = ids;
- while( l )
- {
- /* verify that all NOT NULL fields in the index have data */
- oc_add_object_to_index( object , l->data );
- l = g_list_next( l );
- }
- }
+ if (ids)
+ {
+ GList *l = ids;
+ while (l)
+ {
+ /* verify that all NOT NULL fields in the index have data */
+ oc_add_object_to_index (object, l->data);
+ l = g_list_next (l);
+ }
+ }
/* free list of index ids */
- if( ids )
- oc_free_index_identifier_list( ids );
-
- flushable = oc_object_to_start_of_list( flushable , object );
-
- return (TRUE);
- }
- return (FALSE);
- }
+ if (ids)
+ {
+ oc_free_index_identifier_list (ids);
+ }
+ flushable = oc_object_to_start_of_list (flushable, object);
+ return (TRUE);
+ }
+ return (FALSE);
+}
static void
-oc_free_object_data(_ObjectData * o)
- {
- GList*l;
-
- trace_functioncall();
- /* printf( "freeing: %08lx\n" , o ); */
- if( g_list_find(flushable,o) ) {
- printf( "Still in flushable list : %08lx (very bad)\n" , o );
- abort();
- }
- if (o)
- {
- if( o->indexes )
- {
- l = o->indexes;
- while( l )
- {
- oc_remove_from_index( l->data , o );
- l = g_list_next( l );
- }
- g_list_free( o->indexes );
- o->indexes = NULL;
- }
+oc_free_object_data (_ObjectData * o)
+{
+ GList *l;
- if( o->hashkey )
- g_free( o->hashkey );
+ trace_functioncall ();
+ /* printf( "freeing: %08lx\n" , o ); */
+ if (g_list_find (flushable, o))
+ {
+ printf ("Still in flushable list : %px (very bad)\n", o);
+ abort ();
+ }
+ if (o)
+ {
+ if (o->indexes)
+ {
+ l = o->indexes;
+ while (l)
+ {
+ oc_remove_from_index (l->data, o);
+ l = g_list_next (l);
+ }
+ g_list_free (o->indexes);
+ o->indexes = NULL;
+ }
+ if (o->hashkey)
+ {
+ g_free (o->hashkey);
+ }
o->hashkey = NULL;
if (o->key)
- g_free(o->key);
+ {
+ g_free (o->key);
+ }
o->key = NULL;
- if( o->classname )
- g_free( o->classname );
+ if (o->classname)
+ {
+ g_free (o->classname);
+ }
o->classname = NULL;
- if (o->fields) {
- g_hash_table_foreach(o->fields, (GHFunc) oc_free_field_data,
- NULL);
- g_hash_table_destroy(o->fields);
- }
+ if (o->fields)
+ {
+ g_hash_table_foreach (o->fields, (GHFunc) oc_free_field_data, NULL);
+ g_hash_table_destroy (o->fields);
+ }
o->fields = NULL;
-
- g_free(o);
- }
- }
+ g_free (o);
+ }
+}
static _ObjectData *
-oc_allocate_object_data(const char *classname, const char *key)
- {
- _ObjectData *o;
-
- trace_functioncall();
-
- o = g_malloc( sizeof(_ObjectData) );
- if (o)
- {
+oc_allocate_object_data (const char *classname, const char *key)
+{
+ _ObjectData *o;
+
+ trace_functioncall ();
+
+ o = g_malloc (sizeof (_ObjectData));
+ if (o)
+ {
/* store object ID info */
- o->classname = g_strdup(classname);
- o->key = g_strdup(key);
- o->hashkey = g_strdup_printf( "%s-%s" , classname , key );
+ o->classname = g_strdup (classname);
+ o->key = g_strdup (key);
+ o->hashkey = g_strdup_printf ("%s-%s", classname, key);
/* field storage */
- o->fields = g_hash_table_new(g_str_hash, g_str_equal);
+ o->fields = g_hash_table_new (g_str_hash, g_str_equal);
/* clear flags */
o->deleted = 0;
o->indatabase = 0;
if (!o->classname || !o->key || !o->fields || !o->hashkey)
- {
- oc_free_object_data(o);
- o = NULL;
- }
+ {
+ oc_free_object_data (o);
+ o = NULL;
+ }
/* new objects aren't in any indexes */
o->indexes = NULL;
- }
- return (o);
- }
+ }
+ return (o);
+}
static _FieldData *
-oc_alloc_field_data(const char *name)
- {
- _FieldData *f = g_new0(_FieldData, 1);
+oc_alloc_field_data (const char *name)
+{
+ _FieldData *f = g_new0 (_FieldData, 1);
- trace_functioncall();
- if (f)
- {
+ trace_functioncall ();
+ if (f)
+ {
f->readonly = FALSE;
- f->name = g_strdup(name);
+ f->name = g_strdup (name);
f->value = NULL;
if (!f->name)
- {
- oc_free_field_data(NULL, f, NULL);
- f = NULL;
- }
- }
- return (f);
- }
+ {
+ oc_free_field_data (NULL, f, NULL);
+ f = NULL;
+ }
+ }
+ return (f);
+}
static void
-oc_free_field_data(gpointer key, _FieldData * f, gpointer userdata)
- {
- trace_functioncall();
- if (f)
- {
+oc_free_field_data (gpointer key, _FieldData * f, gpointer userdata)
+{
+ trace_functioncall ();
+ if (f)
+ {
if (f->name)
- g_free(f->name);
+ {
+ g_free (f->name);
+ }
if (f->value)
- g_free(f->value);
- g_free(f);
- }
- }
+ {
+ g_free (f->value);
+ }
+ g_free (f);
+ }
+}
static ObjectData *
-oc_objectstore_query_to_objectcache(QueryData * q)
- {
- ObjectData *first = NULL;
- struct query_result *result;
- ObjectData *o;
- DatabaseResultRow r;
- int row, field, idfield;
-
- trace_functioncall();
- /* perform query */
- result = query_objectstore(q, NULL, NULL);
- if (!result)
+oc_objectstore_query_to_objectcache (QueryData * q)
+{
+ ObjectData *first = NULL;
+ struct query_result *result;
+ ObjectData *o;
+ DatabaseResultRow r;
+ int row, field, idfield;
+
+ trace_functioncall ();
+ /* perform query */
+ result = query_objectstore (q, NULL, NULL);
+ if (!result)
+ {
return (NULL);
- if (!result->success || result->rows_affected == 0)
- {
- free_query_result(result);
+ }
+ if (!result->success || result->rows_affected == 0)
+ {
+ free_query_result (result);
return (NULL);
- }
-
- /* any objects get added to the cache */
- idfield = oql_query_get_field_position(q, "objectid");
- if (idfield == ( -1))
- {
- errormsg("no objectid in result from query %s\n",
- oql_query_as_sql(q,OQL_DBTYPE_CACHEONLY));
- free_query_result(result);
+ }
+ /* any objects get added to the cache */
+ idfield = oql_query_get_field_position (q, "objectid");
+ if (idfield == (-1))
+ {
+ errormsg ("no objectid in result from query %s\n",
+ oql_query_as_sql (q, OQL_DBTYPE_CACHEONLY));
+ free_query_result (result);
return (NULL);
- }
- for (row = 0; row < result->rows_affected; row++)
- {
- r = get_result_row(result, row);
- o = oc_add_empty_object(get_result_class_name(result),
- get_field_in_row(r, idfield));
+ }
+ for (row = 0; row < result->rows_affected; row++)
+ {
+ r = get_result_row (result, row);
+ o = oc_add_empty_object (get_result_class_name (result),
+ get_field_in_row (r, idfield));
if (!o)
- {
- errormsg("%s/%d: A TODO, really :)", __FILE__, __LINE__);
- exit(0);
- }
+ {
+ errormsg ("%s/%d: A TODO, really :)", __FILE__, __LINE__);
+ exit (0);
+ }
if (first == NULL)
- first = o;
+ {
+ first = o;
+ }
for (field = 0; field < result->field_count; field++)
- {
- if( field != idfield ) {
-/* printf( "setting field %s to %s\n" , get_result_field_name(result,
field) , get_field_in_row(r, field) ); */
- oc_set_object_field(o, get_result_field_name(result, field),
- get_field_in_row(r, field), FALSE);
- }
- }
- oc_set_object_flag(o, of_indatabase, FLAG_ON);
- }
- free_query_result(result);
-
- /* return the first object found */
- return (first);
- }
+ {
+ if (field != idfield)
+ {
+/* printf( "setting field %s to %s\n" , get_result_field_name(result,
field) , get_field_in_row(r, field) ); */
+ oc_set_object_field (o,
+ get_result_field_name (result,
+ field),
+ get_field_in_row (r, field), FALSE);
+ }
+ }
+ oc_set_object_flag (o, of_indatabase, FLAG_ON);
+ }
+ free_query_result (result);
+ /* return the first object found */
+ return (first);
+}
static oc_index_identifier *
-oc_make_index_identifier( _ObjectData *object , _odl_index *i )
+oc_make_index_identifier (_ObjectData * object, _odl_index * i)
{
- oc_index_identifier *id = g_new0( oc_index_identifier , 1 );
- trace_functioncall();
- if( id )
- {
- GList *f;
- GString *buf;
-
- buf = g_string_new(object->classname);
- g_string_append( buf , "/" );
-
- f = i->fields;
-
- while( f ) {
- g_string_append( buf , f->data );
- if( f->next ) g_string_append( buf , "," );
- f = f->next;
- }
- id->classname = g_strdup( object->classname );
- id->indexid = buf->str;
- id->index = i;
-
- buf->str = NULL;
- g_string_free( buf , TRUE );
- }
- return( id );
+ oc_index_identifier *id = g_new0 (oc_index_identifier, 1);
+ trace_functioncall ();
+ if (id)
+ {
+ GList *f;
+ GString *buf;
+
+ buf = g_string_new (object->classname);
+ g_string_append (buf, "/");
+ f = i->fields;
+ while (f)
+ {
+ g_string_append (buf, f->data);
+ if (f->next)
+ g_string_append (buf, ",");
+ f = f->next;
+ }
+ id->classname = g_strdup (object->classname);
+ id->indexid = buf->str;
+ id->index = i;
+ buf->str = NULL;
+ g_string_free (buf, TRUE);
+ }
+ return (id);
}
static void
-oc_free_index_identifier_list( GList *list )
+oc_free_index_identifier_list (GList * list)
{
- GList *l;
- oc_index_identifier *i;
- trace_functioncall();
-
- l = list;
- while( l )
- {
- i = list->data;
-
- if( i )
- {
- if( i->classname ) g_free( i->classname );
- if( i->indexid ) g_free( i->indexid );
- g_free( i );
- }
-
- l = g_list_next( l );
- }
- if( list ) g_list_free( list );
-}
-
-static void
-oc_remove_from_index( _CacheIndex *idx , _ObjectData *o )
-{
- _CacheIndexLink *link;
- char *objhash = NULL;
- GString *buf = NULL;
- GList *l;
- trace_functioncall();
-
- /* remove object 'o' from index 'idx' */
- objhash = oc_make_object_index_hash( o , idx->index );
- /* printf( "This object (%s/%s) is in index table '%s' (%s) (remove it
here)\n" ,
- o->classname , o->key , idx->hashkey , objhash ); */
-
- /* remove it from the index in idx */
- link = g_hash_table_lookup( idx->hashtable , objhash );
- if( link )
- {
- /* printf( "Found the link\n" ); */
- l = link->objects;
-
- while( l )
- {
- ObjectData *o2;
-
- o2 = l->data;
- if( strcmp(o2->key,o->key) == 0 )
- {
- link->objects = g_list_remove( link->objects , o2 );
- /* printf( "Remove done\n" ); */
- l = NULL;
- }
- else
- l = g_list_next( l );
- }
-
- if( link->objects == NULL )
- {
- g_hash_table_remove( idx->hashtable , objhash );
- g_free( link->hashkey );
- g_free( link );
- /* printf( "Complete remove done\n" ); */
- }
- }
- g_free( objhash );
+ GList *l;
+ oc_index_identifier *i;
+ trace_functioncall ();
+
+ l = list;
+ while (l)
+ {
+ i = list->data;
+ if (i)
+ {
+ if (i->classname)
+ {
+ g_free (i->classname);
+ }
+ if (i->indexid)
+ {
+ g_free (i->indexid);
+ }
+ g_free (i);
+ }
+
+ l = g_list_next (l);
+ }
+ if (list)
+ {
+ g_list_free (list);
+ }
}
static void
-oc_add_object_to_index( _ObjectData *object , oc_index_identifier *indexid )
+oc_remove_from_index (_CacheIndex * idx, _ObjectData * o)
{
- _CacheIndex *idx = NULL;
- _CacheIndexLink *link;
- char *objhash = NULL;
- GString *buf = NULL;
- GList *l;
- trace_functioncall();
-
- /* insert object object into index table */
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: add object %s/%s to
index %s\n" , object->classname , object->key , indexid->indexid );
-
- objhash = oc_make_object_index_hash( object , indexid->index );
- if( !objhash ) return;
-
- /* find the index, if it exists */
- idx = g_hash_table_lookup(indexes,indexid->indexid);
- if( !idx )
- {
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: adding new index
'%s'\n" , indexid->indexid );
-
- /* nope, so create a new index */
- idx = g_new0( _CacheIndex , 1 );
- if( !idx )
- return;
-
- idx->hashkey = g_strdup( indexid->indexid );
- idx->hashtable = g_hash_table_new(g_str_hash, g_str_equal);
- idx->index = indexid->index;
-
- g_hash_table_insert( indexes , idx->hashkey , idx );
- }
-
- /* find the index link that should hold this object */
- link = g_hash_table_lookup( idx->hashtable , objhash );
- if( !link )
- {
- /* first time an objexct with this key was placed in the index */
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: adding new link\n"
);
- link = g_new0( _CacheIndexLink , 1 );
- if( !link ) {
- g_free( objhash );
- return;
- }
-
- link->hashkey = g_strdup(objhash);
- link->objects = NULL;
-
- g_hash_table_insert( idx->hashtable , link->hashkey , link );
- }
- g_free( objhash );
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: link hash = %s\n" ,
link->hashkey );
-
- /* add the object to the link */
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: placing object in
index\n" );
- link->objects = g_list_append( link->objects , object );
+ _CacheIndexLink *link;
+ char *objhash = NULL;
+ /* GString *buf = NULL; */
+ GList *l;
+ trace_functioncall ();
+
+ /* remove object 'o' from index 'idx' */
+ objhash = oc_make_object_index_hash (o, idx->index);
+ /* printf( "This object (%s/%s) is in index table '%s' (%s) (remove it
here)\n" ,
+ o->classname , o->key , idx->hashkey , objhash ); */
+
+ /* remove it from the index in idx */
+ link = g_hash_table_lookup (idx->hashtable, objhash);
+ if (link)
+ {
+ /* printf( "Found the link\n" ); */
+ l = link->objects;
+ while (l)
+ {
+ ObjectData *o2;
- object->indexes = g_list_append( object->indexes , idx );
+ o2 = l->data;
+ if (strcmp (o2->key, o->key) == 0)
+ {
+ link->objects = g_list_remove (link->objects, o2);
+ /* printf( "Remove done\n" ); */
+ l = NULL;
+ }
+ else
+ {
+ l = g_list_next (l);
+ }
+ }
+ if (link->objects == NULL)
+ {
+ g_hash_table_remove (idx->hashtable, objhash);
+ g_free (link->hashkey);
+ g_free (link);
+ /* printf( "Complete remove done\n" ); */
+ }
+ }
+ g_free (objhash);
}
+static void
+oc_add_object_to_index (_ObjectData * object, oc_index_identifier * indexid)
+{
+ _CacheIndex *idx = NULL;
+ _CacheIndexLink *link;
+ char *objhash = NULL;
+ /* GString *buf = NULL; */
+ /* GList *l; */
+
+ trace_functioncall ();
+
+ /* insert object object into index table */
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: add object %s/%s to index %s\n",
+ object->classname, object->key, indexid->indexid);
+ }
+ objhash = oc_make_object_index_hash (object, indexid->index);
+ if (!objhash)
+ {
+ return;
+ }
+ /* find the index, if it exists */
+ idx = g_hash_table_lookup (indexes, indexid->indexid);
+ if (!idx)
+ {
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: adding new index '%s'\n", indexid->indexid);
+ }
+ /* nope, so create a new index */
+ idx = g_new0 (_CacheIndex, 1);
+ if (!idx)
+ {
+ return;
+ }
+ idx->hashkey = g_strdup (indexid->indexid);
+ idx->hashtable = g_hash_table_new (g_str_hash, g_str_equal);
+ idx->index = indexid->index;
+ g_hash_table_insert (indexes, idx->hashkey, idx);
+ }
+ /* find the index link that should hold this object */
+ link = g_hash_table_lookup (idx->hashtable, objhash);
+ if (!link)
+ {
+ /* first time an objexct with this key was placed in the index */
+ if (debuglevel == OC_TEMP_DEBUG)
+ printf ("OCCACHE: adding new link\n");
+ link = g_new0 (_CacheIndexLink, 1);
+ if (!link)
+ {
+ g_free (objhash);
+ return;
+ }
+ link->hashkey = g_strdup (objhash);
+ link->objects = NULL;
+ g_hash_table_insert (idx->hashtable, link->hashkey, link);
+ }
+ g_free (objhash);
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: link hash = %s\n", link->hashkey);
+ }
+ /* add the object to the link */
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: placing object in index\n");
+ }
+ link->objects = g_list_append (link->objects, object);
+ object->indexes = g_list_append (object->indexes, idx);
+}
+
/**
* Use the query to determine an appropriate index to search for the
* requested object.
@@ -1022,209 +1113,280 @@
* of an object matching the query
*/
ObjectData *
-oc_search_for_single_object( _QueryData *q )
+oc_search_for_single_object (_QueryData * q)
{
- _CacheIndex *idx = NULL;
- _CacheIndexLink *link;
- char *classname;
- GString *buf,*hash;
- GList *l;
- gboolean needcomma = TRUE;
- char *key;
+ _CacheIndex *idx = NULL;
+ _CacheIndexLink *link;
+ char *classname;
+ GString *buf;
+ /* GString *hash; */
+ GList *l;
+ /* gboolean needcomma = TRUE; */
+ char *key;
- trace_functioncall();
+ trace_functioncall ();
// return( NULL ); /* XYZ */
- if( !q ) return( NULL );
+ if (!q)
+ {
+ return (NULL);
+ }
+ /* name of class to search for */
+ if (!q->classes)
+ {
+ return (NULL);
+ }
+ classname = q->classes->data;
+ if (!classname)
+ {
+ return (NULL);
+ }
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: Searching for single object in cache\n");
+ }
+ /* if just 'objectID' field, use main index */
+ if (g_list_length (q->fields) == 1)
+ {
+ if (g_strcasecmp ("objectid", q->fields->data) == 0)
+ {
+ if (debuglevel == OC_TEMP_DEBUG)
+ printf ("OCCACHE: use objectID to search main cache\n");
+ }
+ }
+
+ /* fields to match */
+ buf = g_string_new (classname);
+ g_string_append (buf, "/");
+ l = q->conditions;
+ while (l)
+ {
+ _QueryCondition *c = l->data;
+
+ if (g_strcasecmp (c->targetclass, classname) == 0 &&
+ c->relation[0] == '=')
+ {
+ g_string_append (buf, c->field);
+ if (l->next)
+ {
+ g_string_append (buf, ",");
+ }
+ if (strcmp (c->field, "objectid") == 0)
+ {
+ key = c->value;
+ }
+ }
+ l = l->next;
+ }
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: Look in cache '%s'\n", buf->str);
+ }
+ idx = g_hash_table_lookup (indexes, buf->str);
+ if (!idx)
+ {
+ if (strcmp (&buf->str[strlen (classname) + 1], "objectid") == 0)
+ {
+ /* look in main table */
+ g_string_free (buf, TRUE);
+ return (oc_find_cached_object_by_key (classname, key));
+ }
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("could not find index %s\n", buf->str);
+ }
+ g_string_free (buf, TRUE);
+ return (NULL);
+ }
+ g_string_free (buf, TRUE);
+ /* make appropriate object hash key for finding 'classname' in 'idx' */
+ buf = g_string_new ("");
+ l = idx->index->fields;
+ while (l)
+ {
+ GList *cl;
+ char *field;
+ _QueryCondition *c;
+
+ /* find condition 'classname.(l->data) = value' */
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: index field: %s\n", (char *) (l->data));
+ }
+ cl = q->conditions;
+ field = NULL;
+ while (cl)
+ {
+ c = cl->data;
+ if (g_strcasecmp (c->field, l->data) == 0)
+ {
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: value: %s\n", c->value);
+ }
+ field = c->value;
+ cl = NULL;
+ }
+ else
+ cl = g_list_next (cl);
+ }
+
+ if (field)
+ {
+ g_string_append (buf, field);
+ if (l->next)
+ {
+ g_string_append (buf, ":");
+ }
+ }
+ else
+ {
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: failed\n");
+ }
+ g_string_free (buf, TRUE);
+ return (NULL);
+ }
+
+ l = g_list_next (l);
+ }
+
+ /* found an index, now look for the object in that index */
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: looking for '%s' in index\n", buf->str);
+ }
+ link = g_hash_table_lookup (idx->hashtable, buf->str);
+ g_string_free (buf, TRUE);
- /* name of class to search for */
- if( !q->classes ) return( NULL );
- classname = q->classes->data;
- if( !classname ) return( NULL );
-
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: Searching for single
object in cache\n" );
-
- /* if just 'objectID' field, use main index */
- if( g_list_length(q->fields) == 1 )
- {
- if( g_strcasecmp("objectid",q->fields->data) == 0 )
- {
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: use objectID
to search main cache\n" );
- }
- }
-
- /* fields to match */
- buf = g_string_new(classname);
- g_string_append( buf , "/" );
- l = q->conditions;
- while( l )
- {
- _QueryCondition *c = l->data;
-
- if( g_strcasecmp(c->targetclass,classname) == 0 &&
- c->relation[0]=='=' )
- {
- g_string_append( buf , c->field );
- if( l->next ) g_string_append( buf , "," );
-
- if( strcmp(c->field,"objectid") == 0 )
- key = c->value;
- }
- l = l->next;
- }
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: Look in cache '%s'\n" ,
buf->str );
- idx = g_hash_table_lookup(indexes,buf->str);
- if( !idx ) {
- if( strcmp( &buf->str[strlen(classname)+1] , "objectid" ) == 0 )
- {
- /* look in main table */
- g_string_free( buf , TRUE );
- return( oc_find_cached_object_by_key(classname,key) );
- }
- if( debuglevel == OC_TEMP_DEBUG ) printf( "could not find index %s\n" ,
buf->str );
- g_string_free( buf , TRUE );
- return( NULL );
- }
- g_string_free( buf , TRUE );
-
- /* make appropriate object hash key for finding 'classname' in 'idx' */
- buf = g_string_new( "" );
- l = idx->index->fields;
- while( l )
- {
- GList *cl;
- char *field;
- _QueryCondition *c;
-
- /* find condition 'classname.(l->data) = value' */
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: index field: %s\n"
, l->data );
- cl = q->conditions;
- field = NULL;
- while( cl )
- {
- c = cl->data;
- if( g_strcasecmp( c->field , l->data ) == 0 )
- {
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: value:
%s\n" , c->value );
- field = c->value;
- cl = NULL;
- }
- else
- cl = g_list_next( cl );
- }
-
- if( field )
- {
- g_string_append( buf , field );
- if( l->next ) g_string_append( buf , ":" );
- }
- else
- {
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: failed\n" );
- g_string_free( buf , TRUE );
- return( NULL );
- }
-
- l = g_list_next( l );
- }
-
- /* found an index, now look for the object in that index */
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: looking for '%s' in
index\n" , buf->str );
- link = g_hash_table_lookup( idx->hashtable , buf->str );
- g_string_free( buf , TRUE );
-
- if( !link ) return( NULL );
- if( !link->objects ) return( NULL );
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: found\n" );
- fflush(NULL);
- return( link->objects->data );
+ if (!link)
+ {
+ return (NULL);
+ }
+ if (!link->objects)
+ {
+ return (NULL);
+ }
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: found\n");
+ }
+ fflush (NULL);
+ return (link->objects->data);
}
GList *
-oc_search_for_objects( _QueryData *q )
+oc_search_for_objects (_QueryData * q)
{
- /* This is not yet implemented */
- fatal_error( "Not implemented: Searching for multiple objects in cache\n" );
- return( NULL );
+ /* This is not yet implemented */
+ fatal_error ("Not implemented: Searching for multiple objects in cache\n");
+ return (NULL);
}
static char *
-oc_make_object_index_hash( _ObjectData *object , _odl_index *idx )
+oc_make_object_index_hash (_ObjectData * object, _odl_index * idx)
{
- GList *l;
- GString *buf;
- char *retval = NULL;
- odl_class *c;
- odl_field *f;
-
- trace_functioncall();
- /* get the contents of the object's fields and add them to the object hash
*/
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: creating object's index
hash key\n" );
- l = idx->fields;
- buf = g_string_new( "" );
- c = odl_find_class( all_classes , object->classname , NULL );
- while( l )
- {
- /* TODO add a quote character to fields to quote any : characters */
- char *field;
-
- field = oc_get_object_field( object , l->data );
- f = odl_class_get_field( c , l->data );
- if( field )
- {
- g_string_append( buf , field );
- }
- else if( (f->properties & ODL_PROP_NOTNULL) != ODL_PROP_NOTNULL )
- {
- /* NULL field, allowed */
- g_string_append( buf , "(null)" );
- }
- else
- {
- /* NULL field, with NOT NULL property set */
- g_string_free( buf , TRUE );
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: NOT NULL field
(%s) is NULL - can not index\n" , l->data );
- return( NULL );
- }
- if( l->next ) g_string_append( buf , ":" );
- l = g_list_next( l );
- }
- retval = buf->str;
- buf->str = NULL;
- g_string_free( buf , FALSE );
- if( debuglevel == OC_TEMP_DEBUG ) printf( "OCCACHE: object hash key: %s\n"
, retval );
- return( retval );
+ GList *l;
+ GString *buf;
+ char *retval = NULL;
+ odl_class *c;
+ odl_field *f;
+
+ trace_functioncall ();
+ /* get the contents of the object's fields and add them to the object hash */
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: creating object's index hash key\n");
+ }
+ l = idx->fields;
+ buf = g_string_new ("");
+ c = odl_find_class (all_classes, object->classname, NULL);
+ while (l)
+ {
+ /* TODO add a quote character to fields to quote any : characters */
+ char *field;
+
+ field = oc_get_object_field (object, l->data);
+ f = odl_class_get_field (c, l->data);
+ if (field)
+ {
+ g_string_append (buf, field);
+ }
+ else if ((f->properties & ODL_PROP_NOTNULL) != ODL_PROP_NOTNULL)
+ {
+ /* NULL field, allowed */
+ g_string_append (buf, "(null)");
+ }
+ else
+ {
+ /* NULL field, with NOT NULL property set */
+ g_string_free (buf, TRUE);
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf
+ ("OCCACHE: NOT NULL field (%s) is NULL - can not index\n",
+ (char *) (l->data));
+ }
+ return (NULL);
+ }
+ if (l->next)
+ {
+ g_string_append (buf, ":");
+ }
+ l = g_list_next (l);
+ }
+ retval = buf->str;
+ buf->str = NULL;
+ g_string_free (buf, FALSE);
+ if (debuglevel == OC_TEMP_DEBUG)
+ {
+ printf ("OCCACHE: object hash key: %s\n", retval);
+ }
+ return (retval);
}
static GList *
-oc_object_to_start_of_list( GList *l , ObjectData *obj )
+oc_object_to_start_of_list (GList * l, ObjectData * obj)
{
- if( g_list_find(l,obj) )
- {
- l = g_list_remove( l , obj );
- if( g_list_find(l,obj) ) {
- printf( "%08lx is still in list (bad)\n" , obj );
- abort();
- }
- l = g_list_prepend( l , obj );
- /* if( g_list_find(l,obj) ) printf( "%08lx is back in list (good)\n" ,
obj ); */
- }
- return( l );
+ if (g_list_find (l, obj))
+ {
+ l = g_list_remove (l, obj);
+ if (g_list_find (l, obj))
+ {
+ printf ("%px is still in list (bad)\n", obj);
+ abort ();
+ }
+ l = g_list_prepend (l, obj);
+#if 0
+ if (g_list_find (l, obj))
+ {
+ printf ("%px is back in list (good)\n", obj);
+ }
+#endif
+ }
+ return (l);
}
static GList *
-oc_object_to_end_of_list( GList *l , ObjectData *obj )
+oc_object_to_end_of_list (GList * l, ObjectData * obj)
{
- if( g_list_find(l,obj) )
- {
- l = g_list_remove( l , obj );
- if( g_list_find(l,obj) ) {
- printf( "%08lx is still in list (bad)\n" , obj );
- abort();
- }
- l = g_list_append( l , obj );
- /* if( g_list_find(l,obj) ) printf( "%08lx is back in list (good)\n" ,
obj ); */
- }
- return( l );
+ if (g_list_find (l, obj))
+ {
+ l = g_list_remove (l, obj);
+ if (g_list_find (l, obj))
+ {
+ printf ("%px is still in list (bad)\n", obj);
+ abort ();
+ }
+ l = g_list_append (l, obj);
+#if 0
+ if (g_list_find (l, obj))
+ {
+ printf ("%08lx is back in list (good)\n", obj);
+ }
+#endif
+ }
+ return (l);
}
- gnue/geas/src/objectcache objectcache.c,
Neil Tiffin <=