RedisX v0.9
A simple, light-weight Redis database client
Loading...
Searching...
No Matches
resp.c File Reference

Macros

#define _DEFAULT_SOURCE
 strcasecmp() feature macro starting glibc 2.20 (2014-09-08)
 

Functions

int redisxAppendRESP (RESP *resp, RESP *part)
 
int redisxCheckDestroyRESP (RESP *resp, enum resp_type expectedType, int expectedSize)
 
int redisxCheckRESP (const RESP *resp, enum resp_type expectedType, int expectedSize)
 
RESPredisxCopyOfRESP (const RESP *resp)
 
void redisxDestroyRESP (RESP *resp)
 
RedisMapredisxGetKeywordEntry (const RESP *map, const char *key)
 
RedisMapredisxGetMapEntry (const RESP *map, const RESP *key)
 
boolean redisxHasComponents (const RESP *r)
 
boolean redisxIsArrayType (const RESP *r)
 
boolean redisxIsEqualRESP (const RESP *a, const RESP *b)
 
boolean redisxIsMapType (const RESP *r)
 
boolean redisxIsScalarType (const RESP *r)
 
boolean redisxIsStringType (const RESP *r)
 
void redisxPrintDelimited (const RESP *resp, const char *delim, const char *groupPrefix)
 
int redisxPrintJSON (const char *name, const RESP *resp)
 
int redisxPrintRESP (const RESP *resp)
 
char * redisxRESP2JSON (const char *name, const RESP *resp)
 
XFieldredisxRESP2XField (const char *name, const RESP *resp)
 
int redisxSplitText (RESP *resp, char **text)
 

Detailed Description

Date
Created on Dec 6, 2024
Author
Attila Kovacs

A set of utilities for handling RESP responses from a Redis / Valkey server.

Function Documentation

◆ redisxAppendRESP()

int redisxAppendRESP ( RESP resp,
RESP part 
)

Appends a part to an existing RESP of the same type, before discarding the part.

Parameters
[in,out]respThe RESP to which the part is appended
partThe part, which is destroyed after the content is appended to the first RESP argument.
Returns
X_SUCCESS (0) if successful, or else X_NULL if the first argument is NULL, or REDIS_UNEXPECTED_RESP if the types do not match, or X_FAILURE if there was an allocation error.

References RESP::n, REDIS_UNEXPECTED_RESP, redisxDestroyRESP(), redisxIsArrayType(), redisxIsMapType(), redisxIsScalarType(), RESP3_NULL, RESP::type, RESP::value, x_error(), X_FAILURE, X_NULL, and X_SUCCESS.

◆ redisxCheckDestroyRESP()

int redisxCheckDestroyRESP ( RESP resp,
enum resp_type  expectedType,
int  expectedSize 
)

Like redisxCheckRESP(), but it also destroys the RESP in case of an error.

Parameters
respPointer to the RESP structure from Redis.
expectedTypeThe RESP type expected (e.g. RESP_ARRAY) or 0 if not checking type.
expectedSizeThe expected size of the RESP (array or bytes) or <=0 to skip checking
Returns
The return value of redisxCheckRESP().
See also
redisxCheckRESP()

References redisxCheckRESP(), and redisxDestroyRESP().

◆ redisxCheckRESP()

int redisxCheckRESP ( const RESP resp,
enum resp_type  expectedType,
int  expectedSize 
)

Checks a Redis RESP for NULL values or unexpected values.

Parameters
respPointer to the RESP structure from Redis.
expectedTypeThe RESP type expected (e.g. RESP_ARRAY) or 0 if not checking type.
expectedSizeThe expected size of the RESP (array or bytes) or <=0 to skip checking
Returns
X_SUCCESS (0) if the RESP passes the tests, or X_NULL if the RESP is NULL (garbled response). REDIS_NULL if Redis returned (nil), REDIS_UNEXPECTED_TYPE if got a reply of a different type than expected REDIS_UNEXPECTED_ARRAY_SIZE if got a reply of different size than expected.

or the error returned in resp->n.

References RESP::n, REDIS_NULL, REDIS_UNEXPECTED_RESP, RESP3_BOOLEAN, RESP3_NULL, RESP_INT, RESP::type, RESP::value, x_error(), X_FAILURE, X_NULL, and X_SUCCESS.

◆ redisxCopyOfRESP()

RESP * redisxCopyOfRESP ( const RESP resp)

Creates an independent deep copy of the RESP, which shares no references with the original.

Parameters
respThe original RESP data structure (it may be NULL).
Returns
A copy of the original, with no shared references.

References RedisMap::key, RESP::n, redisxCopyOfRESP(), RESP3_ATTRIBUTE, RESP3_BIG_NUMBER, RESP3_BLOB_ERROR, RESP3_DOUBLE, RESP3_MAP, RESP3_PUSH, RESP3_SET, RESP3_VERBATIM_STRING, RESP_ARRAY, RESP_BULK_STRING, RESP_ERROR, RESP_SIMPLE_STRING, RESP::type, RESP::value, and RedisMap::value.

◆ redisxDestroyRESP()

void redisxDestroyRESP ( RESP resp)

Frees up the resources used by a RESP structure that was dynamically allocated. The call will segfault if the same RESP is destroyed twice or if the argument is a static allocation.

Parameters
respPointer to the RESP structure to be destroyed, which may be NULL (no action taken).

References RedisMap::key, RESP::n, redisxDestroyRESP(), RESP3_ATTRIBUTE, RESP3_MAP, RESP3_PUSH, RESP3_SET, RESP_ARRAY, RESP::type, RESP::value, and RedisMap::value.

◆ redisxGetKeywordEntry()

RedisMap * redisxGetKeywordEntry ( const RESP map,
const char *  key 
)

Retrieves a entry, by its string keyword, from a map-type RESP data structure.

Parameters
mapThe map-type REST data structure containing a dictionary
keyThe string keyword to match
Returns
The matching map entry or NULL if the map contains no such entry.
See also
RESP3_MAP
RESP3_ATTRIBUTE
redisxGetMapEntry()

References RedisMap::key, RESP::n, redisxIsMapType(), redisxIsStringType(), and RESP::value.

◆ redisxGetMapEntry()

RedisMap * redisxGetMapEntry ( const RESP map,
const RESP key 
)

Retrieves a keyed entry from a map-type RESP data structure.

Parameters
mapThe map-type REST data structure containing a dictionary
keyThe RESP key to match
Returns
The matching map entry or NULL if the map contains no such entry.
See also
RESP3_MAP
RESP3_ATTRIBUTE
redisxGetKeywordEntry()

References RedisMap::key, RESP::n, redisxIsMapType(), RESP::type, and RESP::value.

◆ redisxHasComponents()

boolean redisxHasComponents ( const RESP r)

Checks if a RESP has subcomponents, such as arrays or maps (dictionaries).

Parameters
rPointer to a RESP data structure
Returns
TRUE (1) if the data has sub-components, or else FALSE (0).
See also
redisxIsArrayType()
redisxIsMapType()
RESP3_MAP
RESP3_ATTRIBUTE

References FALSE, RESP::n, redisxIsArrayType(), and redisxIsMapType().

◆ redisxIsArrayType()

boolean redisxIsArrayType ( const RESP r)

Checks if a RESP holds an array of RESP pointers, and whose value can be cast to (RESP **) to use.

Parameters
rPointer to a RESP data structure
Returns
TRUE (1) if the data holds an array of RESP * pointers, or else FALSE (0).
See also
redisxIsScalarType()
redisxIsStringType()
redisxIsMapType()
RESP_ARRAY
RESP3_SET
RESP3_PUSH

References FALSE, RESP3_PUSH, RESP3_SET, RESP_ARRAY, TRUE, and RESP::type.

◆ redisxIsEqualRESP()

boolean redisxIsEqualRESP ( const RESP a,
const RESP b 
)

Checks if two RESP are equal, that is they hold the same type of data, have the same 'n' value, and the values match byte-for-byte, or are both NULL.

Parameters
aPonter to a RESP data structure.
bPointer to another RESP data structure.
Returns
TRUE (1) if the two RESP structures match, or else FALSE (0).

References FALSE, RESP::n, TRUE, RESP::type, and RESP::value.

◆ redisxIsMapType()

boolean redisxIsMapType ( const RESP r)

Checks if a RESP holds a dictionary, and whose value can be cast to (RedisMap *) to use.

Parameters
rPointer to a RESP data structure
Returns
TRUE (1) if the data holds a dictionary (a RedisMap array), or else FALSE (0).
See also
redisxIsScalarType()
redisxIsStringType()
redisxIsMapType()
RESP3_MAP
RESP3_ATTRIBUTE

References FALSE, RESP3_ATTRIBUTE, RESP3_MAP, TRUE, and RESP::type.

◆ redisxIsScalarType()

boolean redisxIsScalarType ( const RESP r)

Checks if a RESP holds a scalar type value, such as an integer, a boolean or a double-precision value, or a null value.

Parameters
rPointer to a RESP data structure
Returns
TRUE (1) if the data holds a scalar-type value, or else FALSE (0).
See also
redisxIsStringType()
redisxIsArrayType()
redisxIsMapType()
RESP_INT
RESP3_BOOLEAN
RESP3_DOUBLE
RESP3_NULL

References FALSE, RESP3_BOOLEAN, RESP3_DOUBLE, RESP3_NULL, RESP_INT, TRUE, and RESP::type.

◆ redisxIsStringType()

boolean redisxIsStringType ( const RESP r)

Checks if a RESP holds a string type value, whose value can be cast to (char *) to use.

Parameters
rPointer to a RESP data structure
Returns
TRUE (1) if the data holds a string type value, or else FALSE (0).
See also
redisxIsScalarType()
redisxIsArrayType()
redisxIsMapType()
RESP_SIMPLE_STRING
RESP_ERROR
RESP_BULK_STRING
RESP3_BLOB_ERROR
RESP3_VERBATIM_STRING

References FALSE, RESP3_BIG_NUMBER, RESP3_BLOB_ERROR, RESP3_VERBATIM_STRING, RESP_BULK_STRING, RESP_ERROR, RESP_SIMPLE_STRING, TRUE, and RESP::type.

◆ redisxPrintDelimited()

void redisxPrintDelimited ( const RESP resp,
const char *  delim,
const char *  groupPrefix 
)

Prints a RESP in raw form using delimiters only.

Parameters
respPointer to a RESP (it may be NULL)
delimDelimiter between elements
groupPrefixPrefix in front of arrays and maps
See also
redisxPrintRESP()
redisxPrintJSON()

References RESP::n, redisxPrintDelimited(), RESP3_ATTRIBUTE, RESP3_BIG_NUMBER, RESP3_BLOB_ERROR, RESP3_DOUBLE, RESP3_MAP, RESP3_NULL, RESP3_PUSH, RESP3_SET, RESP3_VERBATIM_STRING, RESP_ARRAY, RESP_BULK_STRING, RESP_ERROR, RESP_INT, RESP_SIMPLE_STRING, RESP::type, and RESP::value.

◆ redisxPrintJSON()

int redisxPrintJSON ( const char *  name,
const RESP resp 
)

Prints a RESP as a JSON fragmen to the standard output with the specified name

Parameters
nameThe name/ID to assign to the RESP
respThe RESP data to print
Returns
0
See also
redisxPrintRESP()
redisxPrintDelimited()
redisxRESP2JSON()

References redisxRESP2JSON(), and X_SUCCESS.

◆ redisxPrintRESP()

int redisxPrintRESP ( const RESP resp)

Prints a RESP to the standard output, in a format that is similar to the one used by the standard redis-cli tool.

Parameters
respPointer to a RESP data structure. (It may be NULL).
Returns
X_SUCCESS (0) if successful or else X_FAILURE if there was an error.
See also
redisxPrintJSON()
redisxPrintDelimited()

References X_FAILURE, and X_SUCCESS.

◆ redisxRESP2JSON()

char * redisxRESP2JSON ( const char *  name,
const RESP resp 
)

Converts a RESP to the xchange representation as an appropriate XField.

Parameters
nameThe name to assign to the field
respThe RESP data to convert
Returns
An XField with the data from the RESP, or NULL if there was an error (errno will be set to indicate the type of error).
See also
redisxRESP2XField()
redisxPrintJSON()

References redisxRESP2XField(), and xjsonFieldToString().

◆ redisxRESP2XField()

XField * redisxRESP2XField ( const char *  name,
const RESP resp 
)

Converts a RESP to the xchange representation as an appropriate XField.

  • RESP3_NULL values are converted to NULL.
  • Scalar values are converted to an XField with the equivalent type.
  • Homogenerous arrays are converted to a field with a 1D array of corresponding xchange type.
  • Heterogeneous arrays are converted to a field with a 1D array of X_FIELD type (containing an array of fields).
  • Maps with string keywords are converted to an X_STRUCT.
  • Maps with non-string keywords are added under a sub-structure named '.non-string-keys' as indexed structures with separate 'key' and 'value' fields.
  • The original RESP type (single character) is preserved as a 0-terminated string in XField.subtype field.
Parameters
nameThe name to assign to the field
respThe RESP data to convert
Returns
An XField with the data from the RESP, or NULL if there was an error (errno will be set to indicate the type of error).
See also
redisxRESP2JSON()

References RESP::n, RESP3_ATTRIBUTE, RESP3_BIG_NUMBER, RESP3_BLOB_ERROR, RESP3_BOOLEAN, RESP3_DOUBLE, RESP3_MAP, RESP3_NULL, RESP3_PUSH, RESP3_SET, RESP3_VERBATIM_STRING, RESP_ARRAY, RESP_BULK_STRING, RESP_ERROR, RESP_INT, RESP_SIMPLE_STRING, RESP::type, RESP::value, X_UNKNOWN, xCreateBooleanField(), xCreateDoubleField(), xCreateIntField(), xCreateScalarField(), xCreateStringField(), and xStringCopyOf().

◆ redisxSplitText()

int redisxSplitText ( RESP resp,
char **  text 
)

Splits the string value of a RESP into two components, by terminating the first component with a null byte and optionally returning the remaining part and length in the output parameters. Only RESP_ERROR RESP_BLOB_ERROR and RESP_VERBATIM_STRING types can be split this way. All others will return REDIS_UNEXPECTED_RESP.

Parameters
respThe input RESP.
[out]text(optional) pointer in which to return the start of the remnant text component.
Returns
n the length of the remnant text (<=0), or else X_NULL if the input RESP was NULL, or REDIS_UNEXPEXCTED_RESP if the input RESP does not contain a two-component string value.
See also
RESP_ERROR
RESP3_BLOB_ERROR
RESP3_VERBATIM_STRING

References RESP::n, REDIS_UNEXPECTED_RESP, RESP3_BLOB_ERROR, RESP3_VERBATIM_STRING, RESP_ERROR, RESP::type, RESP::value, x_error(), X_NULL, and X_PARSE_ERROR.