« Return to Thread: osync_xmlformat_validate() in format plugins

Re: osync_xmlformat_validate() in format plugins

by Bjoern Ricks :: Rate this Message:

Reply to Author | View in Thread

Hi Daniel,

I started coding to get rid of osync_xml_validate and to have only one
instance of a xml schema for each objtype. Therefore I tried to reuse
the oop singleton pattern in C via a static GList. Could you please
review my first intuition because I am more familiar with C++ than C.

best regards,
Bjoern

> Hi,
>
> i just found that osync_xmlformat_validate() is quite expensive, since it
> parses for each call the XSLT schemas from scratch (xmlSchemaParse()).
>
> Callgraph of sync with vformat plugin and the vcard_to_xmlformat converter:
> http://cryptomilch.de/~dgollub/OpenSync/opensync_profiling_xmlformat_validate.png
> (Look for the redline in on the left bottom of the callgraph - 991x stands for
> the 991 function calls.)
>
> Actually we could do this XSLT schema parsing within in the OpenSync framework
> once, and validate all xmlformats which got reproted with the same object
> from a single xmlSchemaParse() call. This would require some changes for
> osync_xmlformat_*() - i try to prepare a patch within the next days. This
> would save us 990 of 991 xmlSchemaParse() calls in mentioned example. One
> call for each used xmlformat. If someone would sync contacts and events this
> would end up in two xmlSchemParse() calls for xmlformat-event and
> xmlformat-contact....
>
> Anyway- for now i would recommend to remove osync_xmlformat_validate() calls
> from every format plugin converter function. It's O.K. for debugging tools
> like "vconvert", but the plugin format conversion routines should be fast ;)
>
> I already removed osync_xmlformat_validate() from the vformat plugin and from
> gnokii-format and palm-format plugins. AFAIK there is only opie-sync and
> sync4j-sync (is anyone still using this?) left which have
> osync_xmlformat_validate(), at least in the OpenSync SVN - didn't checked yet
> other format plugins.
>
> best regards,
> Daniel
>  
--
/Bjoern Ricks

Index: opensync_xmlformat.c
===================================================================
--- opensync_xmlformat.c (Revision 3370)
+++ opensync_xmlformat.c (Arbeitskopie)
@@ -91,6 +91,80 @@
  return p;
 }
 
+OSyncXMLFormatSchema * osync_xmlformat_schema_get_instance(OSyncXMLFormat * xmlformat, OSyncError **error) {
+ static GList * schemas = NULL;
+ OSyncXMLFormatSchema * osyncschema = NULL;
+ GList * entry;
+ const char * objtype;
+
+ osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, xmlformat, error);
+
+ osync_assert(xmlformat);
+
+ objtype = osync_xmlformat_get_objtype(xmlformat);
+ //TODO get mutex
+ // find schema for objtype
+ for ( entry = schemas; entry != NULL; entry=entry->next) { // should be fast enough for only a few objtypes
+ osyncschema = (OSyncXMLFormatSchema *) entry->data;
+ if (!strcmp(osyncschema->objtype, objtype) ) {
+ return osyncschema;
+ }
+ osyncschema = NULL;
+ }
+ if ( osyncschema == NULL ) {
+ osyncschema = osync_try_malloc0(sizeof(OSyncXMLFormatSchema), error);
+ if(!osyncschema) {
+ osync_trace(TRACE_EXIT_ERROR, "%s: %s" , __func__, osync_error_print(error));
+ //TODO release mutex
+ return NULL;
+ }
+ osyncschema->objtype = g_strdup(objtype);
+ char *schemafilepath = g_strdup_printf("%s%c%s%s%s",
+ OPENSYNC_SCHEMASDIR,
+ G_DIR_SEPARATOR,
+ "xmlformat-",
+ osyncschema->objtype,
+ ".xsd");
+
+ xmlSchemaParserCtxtPtr xmlSchemaParserCtxt;
+ xmlSchemaPtr xmlSchema;
+ xmlSchemaValidCtxtPtr xmlSchemaValidCtxt;
+
+ xmlSchemaParserCtxt = xmlSchemaNewParserCtxt(schemafilepath);
+ osyncschema->schema = xmlSchemaParse(xmlSchemaParserCtxt);
+ xmlSchemaFreeParserCtxt(xmlSchemaParserCtxt);
+
+ osyncschema->context = xmlSchemaNewValidCtxt(xmlSchema);
+ if (osyncschema->context == NULL) {
+ xmlSchemaFree(osyncschema->schema);
+ g_free(osyncschema->objtype);
+ g_free(osyncschema);
+ osyncschema = NULL;
+ //TODO set error
+ }
+ schemas = g_list_append(schemas, osyncschema);
+ }
+ //TODO release mutex
+ return osyncschema;
+
+}
+
+
+osync_bool osync_xmlformat_schema_validate(OSyncXMLFormatSchema * schema, OSyncXMLFormat * xmlformat)
+{
+ osync_assert(xmlformat);
+ osync_assert(schema);
+
+ int rc = 0;
+
+ /* Validate the document */
+ rc = xmlSchemaValidateDoc(schema->context, xmlformat->doc);
+
+ if(rc != 0)
+ return FALSE;
+ return TRUE;
+}
+
 /**
  * @brief Validate the xmlformat against its schema in inidivual path
  * @param xmlformat The pointer to a xmlformat object
@@ -399,8 +473,9 @@
 osync_bool osync_xmlformat_validate(OSyncXMLFormat *xmlformat)
 {
  osync_assert(xmlformat);
-
- return _osync_xmlformat_validate(xmlformat, NULL);
+
+ OSyncXMLFormatSchema * schema = osync_xmlformat_schema_get_instance(xmlformat, NULL);
+ return osync_xmlformat_schema_validate(schema, xmlformat);
 }
 
 /**
Index: opensync_xmlformat_internals.h
===================================================================
--- opensync_xmlformat_internals.h (Revision 3370)
+++ opensync_xmlformat_internals.h (Arbeitskopie)
@@ -45,7 +45,20 @@
 
 };
 
+struct OSyncXMLFormatSchema {
+
+ xmlSchemaPtr schema;
+
+ xmlSchemaValidCtxtPtr context;
+
+ const char *objtype
+};
+
 int _osync_xmlformat_get_points(OSyncXMLPoints points[], int* cur_pos, int basic_points, const char* fieldname);
 osync_bool _osync_xmlformat_validate(OSyncXMLFormat *xmlformat, const char *path);
 
+OSyncXMLFormatSchema * osync_xmlformat_schema_get_instance(OSyncXMLFormat * xmlformat, OSyncError **error)
+void osync_xmlformat_schema_free_instance(OSyncXMLFormatSchema *  schema, OSyncError **error);
+osync_bool osync_xmlformat_schema_validate(OSyncXMLFormatSchema * schema, OSyncXMLFormat * xmlformat);
+
 #endif /*OPENSYNC_XMLFORMAT_INTERNAL_H_*/

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Opensync-devel mailing list
Opensync-devel@...
https://lists.sourceforge.net/lists/listinfo/opensync-devel

 « Return to Thread: osync_xmlformat_validate() in format plugins

LightInTheBox - Buy quality products at wholesale price