IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 27, 2005, 4:45:12 PM (21 years ago)
Author:
desonia
Message:

tweaked the logic in adding/allocating metadata items.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psMetadata.c

    r3765 r3779  
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.59 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2005-04-23 02:07:27 $
     14*  @version $Revision: 1.60 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2005-04-28 02:45:12 $
    1616*
    1717*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3939#include "psConstants.h"
    4040
    41 /******************************************************************************/
    42 /*  DEFINE STATEMENTS                                                         */
    43 /******************************************************************************/
    44 
    45 /** Maximum size of a string */
    46 #define MAX_STRING_LENGTH 1024
    47 
    48 /******************************************************************************/
    49 /*  TYPE DEFINITIONS                                                          */
    50 /******************************************************************************/
    51 
    52 // None
    53 
    54 /*****************************************************************************/
    55 /*  GLOBAL VARIABLES                                                         */
    56 /*****************************************************************************/
    57 
    58 // None
    59 
    60 /*****************************************************************************/
    61 /*  FILE STATIC VARIABLES                                                    */
    62 /*****************************************************************************/
    63 
    6441static psS32 metadataId = 0;
    65 
    66 /*****************************************************************************/
    67 /*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    68 /*****************************************************************************/
    6942
    7043static psMetadataItem* makeMetaMulti(psHash* table, const char* key, psMetadataItem* existing)
     
    7548    }
    7649
    77     // move any existing entry into a psList
    78     psList* newList = psListAlloc(existing);
    7950
    8051    psMetadataItem* item = psMetadataItemAlloc(key,
    8152                           PS_META_MULTI,
    82                            "",
    83                            newList);
     53                           "List of Metadata Items",
     54                           NULL);
     55
     56    psListAdd(item->data.list,PS_LIST_TAIL,existing);
    8457
    8558    if (existing != NULL) {
     
    8760    }
    8861
    89     psHashAdd(table, key, item); // put in the new entry
    90 
    91     // free local references of newly allocated items.
    92     psFree(newList);
     62    psHashAdd(table, key, item); // put in the new MULTI list entry
     63
     64    // free local references of newly allocated item.
    9365    psFree(item);
    9466
     
    174146{
    175147    psMetadataItem* metadataItem = NULL;
    176 
     148    char tmp;
     149    int nBytes;
    177150
    178151    PS_PTR_CHECK_NULL(name,NULL);
     
    185158    psMemSetDeallocator(metadataItem, (psFreeFcn) metadataItemFree);
    186159
    187     // Allocate and set metadata item comment
     160    // set metadata item comment
    188161    if (comment == NULL) {
    189162        // Per SDRS, null isn't allowed, must use "" instead
     
    200173
    201174    // Allocate and set metadata item name
    202     metadataItem->name = (char *)psAlloc(sizeof(char) * MAX_STRING_LENGTH);
     175    nBytes = vsnprintf(&tmp, 0, name, argPtr) + 1;
     176    metadataItem->name = (char *)psAlloc(sizeof(char) * nBytes);
    203177    vsprintf(metadataItem->name, name, argPtr);
    204178
     
    219193    case PS_META_STR:
    220194        // Perform copy of input strings
    221         metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
     195        metadataItem->data.V = psStringCopy(va_arg(argPtr, char *));
     196        break;
     197    case PS_META_MULTI:
     198        // MULTI needs to create a psList entry, value must be NULL
     199        metadataItem->data.list = psListAlloc(NULL);
    222200        break;
    223201    case PS_META_LIST:
     
    229207    case PS_META_ASTROM:
    230208    case PS_META_UNKNOWN:
    231     case PS_META_MULTI:
    232209        // Copy of input data not performed due to variability of data types
    233210        metadataItem->data.V = psMemIncrRefCounter(va_arg(argPtr, psPtr));
     
    281258
    282259    // See if key is already in table
    283     existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
    284 
     260    existingEntry = psMetadataLookup(md, key);
     261
     262    // if replace is set, remove any existing items of the same key
     263    if (existingEntry != NULL && (flags & PS_META_REPLACE) != 0) {
     264        psMetadataRemove(md,0,key);
     265        existingEntry = NULL;
     266    }
     267
     268    // if the metadataItem is MULTI, just add the encapsulated entries
    285269    if (metadataItem->type == PS_META_MULTI) {
    286         // the incoming entry is PS_META_MULTI
    287 
    288         // force the hash entry to be PS_META_MULTI
     270
     271        // make sure the existing entry is PS_META_MULTI
    289272        existingEntry = makeMetaMulti(mdTable,key,existingEntry);
    290273
     274        psBool status = true;
    291275        // add all the items in the incoming entry to metadata
    292276        psList* list = metadataItem->data.list;
     
    294278            psListIterator* iter = psListIteratorAlloc(list,PS_LIST_HEAD,true);
    295279            psMetadataItem* listItem;
    296             while ((listItem=(psMetadataItem*)psListGetAndIncrement(iter)) != NULL) {
    297                 psMetadataAddItem(md,listItem,location,flags);
     280            while (status && (listItem=(psMetadataItem*)psListGetAndIncrement(iter)) != NULL) {
     281                status |= psMetadataAddItem(md,listItem,location,flags);
    298282            }
    299283            psFree(iter);
    300284        }
    301285
    302         return true; // all done.
     286        return status; // all done.
    303287    }
    304288
    305289    // how the item is added to the hash depends on prior existence, flags, etc.
    306     if(existingEntry != NULL) { // prior existence
     290    if(existingEntry == NULL) { // no prior existence
     291        // Node doesn't already exist - Add new metadata item to metadata collection's hash
     292        if(!psHashAdd(mdTable, key, metadataItem)) {
     293            psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
     294            return false;
     295        }
     296    } else {
    307297        if (existingEntry->type == PS_META_MULTI || (flags & PS_META_DUPLICATE_OK) != 0) {
    308298            // duplicate entries allowed - add another entry.
    309299
    310             // make sure the existing entry is PS_META_MULTI
     300            // make sure the existing hash entry is PS_META_MULTI
    311301            existingEntry = makeMetaMulti(mdTable,key,existingEntry);
    312302
    313             // add to the hash's list of duplicate entries
     303            // add to the hash key's list of entries
    314304            if (! psListAdd(existingEntry->data.list, PS_LIST_TAIL, metadataItem) ) {
    315305                psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_COLLECTION_FAILED,key);
    316306                return false;
    317307            }
    318         } else if ((flags & PS_META_REPLACE) != 0) {
    319             // replace entry instead of creating a duplicate entry.
    320 
    321             // remove the existing entry from metadata
    322             psMetadataRemove(md,0,key);
    323 
    324             // treat as if new (added to list below)
    325             if(!psHashAdd(mdTable, key, metadataItem)) {
    326                 psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
    327                 return false;
    328             }
    329308        } else {
    330             // default is to error on duplicate entry.
     309            // error on duplicate entry.
    331310            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    332311                    PS_ERRORTEXT_psMetadata_DUPLICATE_NOT_ALLOWED);
    333312            return false;
    334313        }
    335     } else {
    336         // OK, this is a new item.
    337 
    338         // Node doesn't exist - Add new metadata item to metadata collection's hash
    339         if(!psHashAdd(mdTable, key, metadataItem)) {
    340             psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
    341             return false;
    342         }
    343     }
    344 
     314    }
     315
     316    // finally, add the metadataItem to the metadata's list.
    345317    if(!psListAdd(mdList, location, metadataItem)) {
    346318        psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_COLLECTION_FAILED,key);
Note: See TracChangeset for help on using the changeset viewer.