gtpc2mbzC/C++ Language Support User's Guide

TO2_atKey-Return the Specified Element by Key

This function searches the specified collection for the specified key and, if found, returns the associated element value. If the collection contains duplicate keys equal to the specified key, the first one found will be returned.

Note:
This function does not support all collections. See Table 47 for collections that are supported for this function.

Format

#include <c$to2.h>
TO2_BUF_PTR  TO2_atKey (const TO2_PID_PTR  pid_ptr,
                              TO2_ENV_PTR  env_ptr,
                        const void        *key,
                        const long        *keyLength);

pid_ptr
The pointer to the persistent identifier (PID) assigned to the collection.

env_ptr
The pointer to the environment as returned by the TO2_createEnv function.

key
The pointer to the value that will be used as the key to locate the specific entry.

keyLength
The pointer to the length of the key. The maximum length is 248 bytes. For key sorted set collections, the key length can be shorter than the defined key to allow a locate by partial key. The key comparison is always from the first character of the key for the specified length. For all other keyed collections, the key length must equal the maximum defined key length of the collection.

Normal Return

The normal return is a pointer (TO2_BUF_PTR) to a structure (buffer) of type TO2_BUF_HDR (see Type Definitions).

Error Return

An error return is indicated by a NULL pointer. When a NULL pointer is returned, use the TO2_getErrorCode function to determine the specific error code. For more information, see Error Handling.

The following error codes are common for this function:

TO2_ERROR_EMPTY

TO2_ERROR_ENV

TO2_ERROR_LOCATOR_LGH

TO2_ERROR_LOCATOR_NOT_FOUND

TO2_ERROR_METHOD

TO2_ERROR_NOT_INIT

TO2_ERROR_PID

TO2_ERROR_REREAD

TO2_ERROR_ZERO_PID

Programming Considerations

Examples

The following example searches a keyed collection to determine if the collection contains an element with the specified key. If an element with that key is found, a buffer is obtained with a copy of the element.

#include <c$to2.h>                    /* Needed for TO2 API functions   */
#include <stdio.h>                    /* APIs for standard I/O functions*/
 
TO2_BUF_PTR         elem_ptr;
TO2_PID             keyb;             /* will set to KeyBag PID         */
TO2_ENV_PTR         env_ptr;          /* Pointer to the TO2 environment */
char                keya[] = "KeyA";  /* key to search for              */
long                keylength;
char               *dataField;

  ·
  ·
  ·
/************************************************************************/ /* Test to see if collection contains entry for key in keya */ /************************************************************************/ keylength = sizeof(keya); if ((elem_ptr = TO2_atKey(&keyb, env_ptr, keya, &keylength)) == 0) { printf("TO2_atKey failed!\n"); process_error(env_ptr); return; } else { dataField = elem_ptr->data; printf("TO2_atKey is successful. Key found!\n"); }
  ·
  ·
  ·
free(elem_ptr);

Related Information