From 08edf9d6445f3962bf31d5f3c9fa5c9cb363c30d Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Wed, 15 Jul 2009 15:17:53 -0400 Subject: COLLECTION Add remove item functions The remove functions extract and remove items, they act differently from the way the delete_property function works. The new functions allow deletion with the disposition while the delete_property only deletes specified property. The delete_property function is left as is since there are some use cases when it is more efficient to use it rather than new remove_item_xxx ones. --- common/collection/collection.c | 62 ++++++++++++++++++++++++++++++-- common/collection/collection.h | 76 +++++++++++++++++++++++++++------------ common/collection/collection_ut.c | 28 +++++++++++++-- common/elapi/elapi_event.c | 11 +++--- 4 files changed, 146 insertions(+), 31 deletions(-) (limited to 'common') diff --git a/common/collection/collection.c b/common/collection/collection.c index 69475b048..113d9f037 100644 --- a/common/collection/collection.c +++ b/common/collection/collection.c @@ -683,7 +683,7 @@ int col_extract_item_from_current(struct collection_item *collection, *ret_ref = current->next; current->next = (*ret_ref)->next; /* If we removed the last element adjust header */ - if(current->next == NULL) header->last = parent; + if(current->next == NULL) header->last = current; } else { TRACE_ERROR_STRING("Property is last in the list", refprop); @@ -817,7 +817,7 @@ int col_extract_item(struct collection_item *collection, type, ret_ref); if (error) { - TRACE_ERROR_NUMBER("Failed extract item into current collection", error); + TRACE_ERROR_NUMBER("Failed to extract item from the current collection", error); return error; } @@ -826,6 +826,64 @@ int col_extract_item(struct collection_item *collection, } +/* Remove item (property) from collection.*/ +int col_remove_item(struct collection_item *ci, + const char *subcollection, + int disposition, + const char *refprop, + int idx, + int type) +{ + int error = EOK; + struct collection_item *ret_ref = NULL; + + TRACE_FLOW_STRING("col_remove_item", "Exit"); + + /* Extract from the current collection */ + error = col_extract_item(ci, + subcollection, + disposition, + refprop, + idx, + type, + &ret_ref); + if (error) { + TRACE_ERROR_NUMBER("Failed to extract item from the collection", error); + return error; + } + + col_delete_item(ret_ref); + + TRACE_FLOW_STRING("col_remove_item", "Exit"); + return EOK; +} + +/* Remove item (property) from current collection. + * Just a simple wropper. + */ +int col_remove_item_from_current(struct collection_item *ci, + int disposition, + const char *refprop, + int idx, + int type) +{ + int error = EOK; + + TRACE_FLOW_STRING("col_remove_item_from_current", "Exit"); + + /* Remove item from current collection */ + error = col_remove_item(ci, + NULL, + disposition, + refprop, + idx, + type); + + TRACE_FLOW_NUMBER("col_remove_item_from_current. Exit. Returning", error); + return error; +} + + /* Insert the item into the collection or subcollection */ int col_insert_item(struct collection_item *collection, const char *subcollection, diff --git a/common/collection/collection.h b/common/collection/collection.h index 5483c1381..73ac7b34c 100644 --- a/common/collection/collection.h +++ b/common/collection/collection.h @@ -502,7 +502,19 @@ int col_modify_double_item(struct collection_item *item, const char *property, double number); -/* Delete property from the collection */ +/* Delete property from the collection. */ +/* It is recomended to use a more efficient function + * col_remove_item() for the same purpose if + * the property is unique or if the collection + * has a known structure. + * This function has some advantage only + * if it is not known where propery + * resides and what is the structure of the collection. + * In this case "foo.bar.baz" notation + * can be used in the property_to_find argument to find + * and delete the property "baz" that is in sub collection "bar" + * which is in turn a part of collection "foo". + */ int col_delete_property(struct collection_item *ci, /* A collection of items */ const char *property_to_find, /* Name to match */ int type, /* Type filter */ @@ -614,7 +626,7 @@ int col_extract_item(struct collection_item *ci, /* Top collection */ const char *subcollection, /* Sub collection */ int disposition, /* Which to extract */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to extract. See notes. */ + int idx, /* Index of the property to extract. See notes. */ int type, /* Type filter */ struct collection_item **ret_ref); /* Returns the reference back */ @@ -622,10 +634,30 @@ int col_extract_item(struct collection_item *ci, /* Top collection */ int col_extract_item_from_current(struct collection_item *ci, /* Top collection */ int disposition, /* Which to extract */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to extract. See notes. */ + int idx, /* Index of the property to extract. See notes. */ int type, /* Type filter */ struct collection_item **ret_ref); /* Returns the reference back */ +/* Remove item (property) from collection. + * It is similar to delete_property function but allows more specific + * information about what item (property) to remove. + * The header will not be considered for deletion. + */ +int col_remove_item(struct collection_item *ci, /* Top collection */ + const char *subcollection, /* Sub collection */ + int disposition, /* Which to remove */ + const char *refprop, /* Property to relate to */ + int idx, /* Index of the property to remove. See notes. */ + int type); /* Type filter */ + +/* Similar function as above just considers only one level. */ +int col_remove_item_from_current(struct collection_item *ci, /* Top collection */ + int disposition, /* Which to remove */ + const char *refprop, /* Property to relate to */ + int idx, /* Index of the property to remove. See notes. */ + int type); /* Type filter */ + + /* Insert item to the collection */ /* WARNING: Only use this function to insert items * that were extracted using extract_item(). @@ -645,7 +677,7 @@ int col_insert_item(struct collection_item *collection, /* Top collection */ struct collection_item *item, /* Item to insert */ int disposition, /* What should be the position of the item */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to extract. See notes. */ + int idx, /* Index of the property to extract. See notes. */ unsigned flags); /* Flags that control naming issues */ /* Insert the item into the top level collection (similar to the function above) @@ -655,7 +687,7 @@ int col_insert_item_into_current(struct collection_item *collection, struct collection_item *item, int disposition, const char *refprop, - int index, + int idx, unsigned flags); @@ -762,7 +794,7 @@ int col_insert_property_with_ref(struct collection_item *ci, /* A collect const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ int type, /* Data type */ @@ -778,7 +810,7 @@ int col_insert_str_property(struct collection_item *ci, /* A collection o const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ const char *string, /* String */ @@ -788,7 +820,7 @@ int col_insert_binary_property(struct collection_item *ci, /* A collectio const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ void *binary_data, /* Binary data */ @@ -799,7 +831,7 @@ int col_insert_int_property(struct collection_item *ci, /* A collection o const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ int number); /* Integer */ @@ -809,7 +841,7 @@ int col_insert_unsinged_property(struct collection_item *ci, /* A collect const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned number); /* Unsigned */ @@ -819,7 +851,7 @@ int col_insert_long_property(struct collection_item *ci, /* A collection const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ long number); /* Long */ @@ -828,7 +860,7 @@ int col_insert_ulong_property(struct collection_item *ci, /* A collection const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned long number); /* Unsigned long */ @@ -837,7 +869,7 @@ int col_insert_double_property(struct collection_item *ci, /* A collectio const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ double number); /* Double */ @@ -846,7 +878,7 @@ int col_insert_bool_property(struct collection_item *ci, /* A collection const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned char logical); /* Bool */ @@ -857,7 +889,7 @@ int col_insert_str_property_with_ref(struct collection_item *ci, /* A col const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ char *string, /* String */ @@ -868,7 +900,7 @@ int col_insert_binary_property_with_ref(struct collection_item *ci, /* A const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ void *binary_data, /* Binary data */ @@ -880,7 +912,7 @@ int col_insert_int_property_with_ref(struct collection_item *ci, /* A col const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ int number, /* Integer */ @@ -891,7 +923,7 @@ int col_insert_unsinged_property_with_ref(struct collection_item *ci, /* const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned number, /* Unsigned */ @@ -901,7 +933,7 @@ int col_insert_long_property_with_ref(struct collection_item *ci, /* A co const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ long number, /* Long */ @@ -911,7 +943,7 @@ int col_insert_ulong_property_with_ref(struct collection_item *ci, /* A c const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned long number, /* Unsigned long */ @@ -921,7 +953,7 @@ int col_insert_double_property_with_ref(struct collection_item *ci, /* A const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ double number, /* Double */ @@ -931,7 +963,7 @@ int col_insert_bool_property_with_ref(struct collection_item *ci, /* A co const char *subcollection, /* Sub collection */ int disposition, /* Where to insert */ const char *refprop, /* Property to relate to */ - int index, /* Index of the property to add */ + int idx, /* Index of the property to add */ unsigned flags, /* Flags that control naming issues */ const char *property, /* Name */ unsigned char logical, /* Bool */ diff --git a/common/collection/collection_ut.c b/common/collection/collection_ut.c index c50a83847..af7746c40 100644 --- a/common/collection/collection_ut.c +++ b/common/collection/collection_ut.c @@ -1194,7 +1194,7 @@ int delete_test(void) struct collection_item *col; int error = EOK; - printf("\n\n==== DELETION TEST ====\n\n"); + printf("\n\n==== DELETION TEST 1====\n\n"); if ((error = col_create_collection(&col, "test", 0)) || (error = col_add_int_property(col, NULL, "tt", 1)) || @@ -1214,11 +1214,35 @@ int delete_test(void) } col_debug_collection(col, COL_TRAVERSE_DEFAULT); + col_destroy_collection(col); - printf("\n\n==== DELETION TEST END ====\n\n"); + printf("\n\n==== DELETION TEST 1 END ====\n\n"); + printf("\n\n==== DELETION TEST 2====\n\n"); + + if ((error = col_create_collection(&col, "test2", 0)) || + (error = col_add_int_property(col, NULL, "tt", 1)) || + (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT)) || + (error = col_add_int_property(col, NULL, "test", 1)) || + (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT)) || + (error = col_remove_item(col, NULL, COL_DSP_END, NULL, 0, COL_TYPE_ANY)) || + (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT)) || + (error = col_add_int_property(col, NULL, "test", 1)) || + (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT)) || + (error = col_remove_item_from_current(col, COL_DSP_AFTER, "tt", 0, COL_TYPE_ANY)) || + (error = col_debug_collection(col, COL_TRAVERSE_DEFAULT)) || + (error = col_add_int_property(col, NULL, "test", 1))) { + printf("Error in delete test %d\n", error); + col_destroy_collection(col); + return error; + } + + col_debug_collection(col, COL_TRAVERSE_DEFAULT); + + printf("\n\n==== DELETION TEST 2 END ====\n\n"); col_destroy_collection(col); + return error; } diff --git a/common/elapi/elapi_event.c b/common/elapi/elapi_event.c index 9322f8217..fe3b3a5e3 100644 --- a/common/elapi/elapi_event.c +++ b/common/elapi/elapi_event.c @@ -732,11 +732,12 @@ static int process_arg_list(struct collection_item *col, else { /* Remove case */ while (error != ENOENT) { - error = col_delete_property(col, - propcopy, - COL_TYPE_ANY, - COL_TRAVERSE_DEFAULT); - + error = col_remove_item(col, + NULL, + COL_DSP_FIRSTDUP, + propcopy, + 0, + COL_TYPE_ANY); if ((error) && (error != ENOENT)) { TRACE_ERROR_STRING("Error deleting property", propcopy); free(propcopy); -- cgit