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 1 for collections that are supported for this function.

Format

#include <tpf/c_to2.h>
TO2_BUF_PTR  TO2_atKey (const TO2_PID_PTR  pid_ptr,
                              TO2_ENV_PTR  env_ptr,
                        const void        *key,
                        const int         *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

  • It is the responsibility of the caller to free the returned buffer once the caller has stopped using the buffer.
  • This function does not use z/TPF transaction services on behalf of the caller.

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 <tpf/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              */
int                 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)) == TO2_ERROR)
{
   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

See z/TPF collection support: non-cursor overview for more information about this function and z/TPF C/C++ language support.