From: <svn...@op...> - 2009-03-30 15:02:48
|
Author: dgollub Date: Mon Mar 30 17:02:40 2009 New Revision: 5462 URL: http://www.opensync.org/changeset/5462 Log: Introduced OSyncAnchor key=value interface. This unfortuantely breaks the API and requires all plugins using the anchor to port. Porting instruction: char *anchor_value = plugin_specific_anchor(); -osync_anchor_compare(anchor, anchor_value, &anchormatch, &error); +osync_anchor_compare(anchor, "anchor_key"1, anchor_value, &anchormatch, &error); -osync_anchor_update(anchor, anchor_value, &error); +osync_anchor_update(anchor, "anchor_key1", anchor_value, &error); -osync_anchor_retrieve(anchor, &error); -osync_anchor_retrieve(anchor, "anchor_key1", &error); This key=value pair is per ObjTypeSink. This means you can reuse the key-name for each ObjTypeSink. Dropped old and unintended anchor.db file from test fixtures for filter_destobjtype_delete testcase fixes #1090 Deleted: trunk/tests/data/destobjtype_delete/configs/group/1/anchor.db Modified: trunk/docs/examples/plugins/src/plugin.c trunk/docs/examples/plugins/src/simple_plugin.c trunk/opensync/helper/opensync_anchor.c trunk/opensync/helper/opensync_anchor.h trunk/tests/mock-plugin/mock_sync.c Modified: trunk/docs/examples/plugins/src/plugin.c ============================================================================== --- trunk/docs/examples/plugins/src/plugin.c Mon Mar 30 14:32:13 2009 (r5461) +++ trunk/docs/examples/plugins/src/plugin.c Mon Mar 30 17:02:40 2009 (r5462) @@ -41,7 +41,7 @@ OSyncAnchor *anchor = osync_objtype_sink_get_anchor(sink); osync_bool anchormatch; - if (!osync_anchor_compare(anchor, "lanchor", &anchormatch, &error)) { + if (!osync_anchor_compare(anchor, "anchor_key", "dynamic_anchor_value", &anchormatch, &error)) { /* anchor couldn't be compared */ goto error; } @@ -236,7 +236,7 @@ //If we use anchors we have to update it now. //Now you get/calculate the current anchor of the device OSyncAnchor *anchor = osync_objtype_sink_get_anchor(sink); - if (!osync_anchor_update(anchor, "lanchor", &error)) { + if (!osync_anchor_update(anchor, "anchor_key", "dynamic_anchor_value", &error)) { goto error; } Modified: trunk/docs/examples/plugins/src/simple_plugin.c ============================================================================== --- trunk/docs/examples/plugins/src/simple_plugin.c Mon Mar 30 14:32:13 2009 (r5461) +++ trunk/docs/examples/plugins/src/simple_plugin.c Mon Mar 30 17:02:40 2009 (r5462) @@ -36,7 +36,7 @@ OSyncAnchor *anchor = osync_objtype_sink_get_anchor(sink); osync_bool anchormatch; - if (!osync_anchor_compare(anchor, "lanchor", &anchormatch, &error)) { + if (!osync_anchor_compare(anchor, "anchor_key", "dynamic_anchor_value", &anchormatch, &error)) { /* anchor couldn't be compared */ goto error; } @@ -167,7 +167,7 @@ //If we use anchors we have to update it now. //Now you get/calculate the current anchor of the device OSyncAnchor *anchor = osync_objtype_sink_get_anchor(sink); - if (!osync_anchor_update(anchor, "lanchor", &error)) + if (!osync_anchor_update(anchor, "anchor_key", "dynamic_anchor_value", &error)) goto error; //Answer the call Modified: trunk/opensync/helper/opensync_anchor.c ============================================================================== --- trunk/opensync/helper/opensync_anchor.c Mon Mar 30 14:32:13 2009 (r5461) +++ trunk/opensync/helper/opensync_anchor.c Mon Mar 30 17:02:40 2009 (r5462) @@ -32,7 +32,8 @@ char *query = NULL; osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, anchor, error); - query = osync_strdup("CREATE TABLE tbl_anchor (id INTEGER PRIMARY KEY, anchor VARCHAR, objtype VARCHAR UNIQUE)"); + /* TODO: How portable to other databes is UNIQUE? */ + query = osync_strdup("CREATE TABLE tbl_anchor (id INTEGER PRIMARY KEY, key VARCHAR UNIQUE, value VARCHAR, objtype VARCHAR)"); if (!osync_db_query(anchor->db, query, error)) { osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); @@ -125,15 +126,19 @@ } } -char *osync_anchor_retrieve(OSyncAnchor *anchor, OSyncError **error) +char *osync_anchor_retrieve(OSyncAnchor *anchor, const char *key, OSyncError **error) { char *retanchor = NULL; - char *query = NULL; + char *query = NULL, *escaped_key; osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, anchor, error); osync_assert(anchor); osync_assert(anchor->db); + osync_assert(key); - query = osync_strdup_printf("SELECT anchor FROM tbl_anchor WHERE objtype='%s'", anchor->objtype ? anchor->objtype : ""); + escaped_key = osync_db_sql_escape(key); + query = osync_strdup_printf("SELECT value FROM tbl_anchor WHERE key='%s' AND objtype='%s'", + escaped_key, anchor->objtype ? anchor->objtype : ""); + osync_free(escaped_key); retanchor = osync_db_query_single_string(anchor->db, query, error); osync_free(query); @@ -151,19 +156,22 @@ return NULL; } -osync_bool osync_anchor_update(OSyncAnchor *anchor, const char *value, OSyncError **error) +osync_bool osync_anchor_update(OSyncAnchor *anchor, const char *key, const char *value, OSyncError **error) { - char *escaped_value = NULL; + char *escaped_value = NULL, *escaped_key = NULL; char *query = NULL; osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, anchor, __NULLSTR(value), error); osync_assert(anchor); osync_assert(anchor->db); + osync_assert(key); osync_assert(value); + escaped_key = osync_db_sql_escape(key); escaped_value = osync_db_sql_escape(value); - query = osync_strdup_printf("REPLACE INTO tbl_anchor (objtype, anchor) VALUES('%s', '%s')", - anchor->objtype ? anchor->objtype : "", escaped_value); + query = osync_strdup_printf("REPLACE INTO tbl_anchor (objtype, key, value) VALUES('%s', '%s', '%s')", + anchor->objtype ? anchor->objtype : "", escaped_key, escaped_value); osync_free(escaped_value); + osync_free(escaped_key); if (!osync_db_query(anchor->db, query, error)) goto error; @@ -178,23 +186,25 @@ return FALSE; } -osync_bool osync_anchor_compare(OSyncAnchor *anchor, const char *new_anchor, osync_bool *same, OSyncError **error) +osync_bool osync_anchor_compare(OSyncAnchor *anchor, const char *key, const char *value, osync_bool *same, OSyncError **error) { - char *old_anchor = NULL; + char *old_value = NULL; - osync_trace(TRACE_ENTRY, "%s(%p, %s, %p, %p)", __func__, anchor, __NULLSTR(new_anchor), same, error); + osync_trace(TRACE_ENTRY, "%s(%p, %s, %s, %p, %p)", __func__, anchor, __NULLSTR(key), __NULLSTR(value), same, error); osync_assert(anchor); + osync_assert(key); + osync_assert(value); - old_anchor = osync_anchor_retrieve(anchor, error); - if (!old_anchor) + old_value = osync_anchor_retrieve(anchor, key, error); + if (!old_value) goto error; - if (!strcmp(old_anchor, new_anchor)) + if (!strcmp(old_value, value)) *same = TRUE; else *same = FALSE; - osync_free(old_anchor); + osync_free(old_value); osync_trace(TRACE_EXIT, "%s", __func__); return TRUE; Modified: trunk/opensync/helper/opensync_anchor.h ============================================================================== --- trunk/opensync/helper/opensync_anchor.h Mon Mar 30 14:32:13 2009 (r5461) +++ trunk/opensync/helper/opensync_anchor.h Mon Mar 30 17:02:40 2009 (r5462) @@ -28,10 +28,9 @@ * @ingroup OSyncHelper * @brief Functions to deal with anchors * - * The Anchor module stores some kind of data (called an "anchor") from the - * Member in a persistent database. This "anchor" gets updated once on each - * sync. If the "anchor" doesn't match on the next sync a slow-sync is - * triggered. + * The Anchor module stores anchors and peer states from the Member in a + * persistent database (which can be flushed by the Engien anytime!). + * This "anchor" shoudl get updated once on each sync. * * Example: * A random string gets generated by the member on each sync, eg. XYZ, and is @@ -50,37 +49,40 @@ */ /*@{*/ -/** @brief Compares the value of an anchor with the supplied value +/** @brief Compares the anchor value of key with the supplied value * * @param anchor Pointer to the anchor object - * @param new_anchor the value to compare with the stored value + * @param key the key of the value to compare + * @param value the value to compare with the stored anchor value * @param issame Pointer to osync_bool to determine the comaprsion result * @param error Pointer to error struct, which get set on any error * @returns TRUE if the anchor compare completed successful, FALSE on error. Not the return value of the comparsion! * */ -OSYNC_EXPORT osync_bool osync_anchor_compare(OSyncAnchor *anchor, const char *new_anchor, osync_bool *issame, OSyncError **error); +OSYNC_EXPORT osync_bool osync_anchor_compare(OSyncAnchor *anchor, const char *key, const char *value, osync_bool *issame, OSyncError **error); -/** @brief Updates the value of an anchor +/** @brief Updates the anchor value of the key * * @param anchor Pointer to the anchor object - * @param new_anchor the new value to set + * @param key the key of the value to set + * @param value the new value to set * @param error Pointer to error struct, which get set on any error * @returns TRUE if anchor update completed successful, FALSE on error. * */ -OSYNC_EXPORT osync_bool osync_anchor_update(OSyncAnchor *anchor, const char *new_anchor, OSyncError **error); +OSYNC_EXPORT osync_bool osync_anchor_update(OSyncAnchor *anchor, const char *key, const char *value, OSyncError **error); -/** @brief Retrieves the value of an anchor +/** @brief Retrieves the anchor value of the supplied key * * @param anchor Pointer to the anchor object + * @param key The key as string of the anchor value to retrieve * @param error Pointer to error struct, which get set on any error * @returns the value of the anchor if it was found, otherwise an empty string get retruned. * Caller is responsible for freeing the return value with osync_free(). * NULL on error of retrieving the anchor information. * */ -OSYNC_EXPORT char *osync_anchor_retrieve(OSyncAnchor *anchor, OSyncError **error); +OSYNC_EXPORT char *osync_anchor_retrieve(OSyncAnchor *anchor, const char *key, OSyncError **error); /*@}*/ Modified: trunk/tests/mock-plugin/mock_sync.c ============================================================================== --- trunk/tests/mock-plugin/mock_sync.c Mon Mar 30 14:32:13 2009 (r5461) +++ trunk/tests/mock-plugin/mock_sync.c Mon Mar 30 17:02:40 2009 (r5462) @@ -87,7 +87,7 @@ */ dir->connect_done = FALSE; - osync_assert_msg(osync_anchor_compare(anchor, dir->path, &anchor_compare_match, NULL), "Not expected to fail"); + osync_assert_msg(osync_anchor_compare(anchor, "path", dir->path, &anchor_compare_match, NULL), "Not expected to fail"); if (!anchor_compare_match) osync_context_report_slowsync(ctx); @@ -563,7 +563,7 @@ if (mock_get_error(info->memberid, "SYNC_DONE_TIMEOUT")) return; - osync_assert_msg(osync_anchor_update(anchor, dir->path, NULL), "Not expected to fail!"); + osync_assert_msg(osync_anchor_update(anchor, "path", dir->path, NULL), "Not expected to fail!"); osync_context_report_success(ctx); |