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

A Redis client library that compiles on older and newer platforms alike, such as LynxOS 3.1.0 PowerPCs. It is quite full featured, supporting multiple Redis instances, pipelining, user-specified connect and disconnect hooks, multiple user-specified subscription listeners, and user-specified pipeline processors. More...

Functions

RESPredisxArrayRequest (Redis *redis, char *args[], int lengths[], int n, int *status)
 
int redisxCheckDestroyRESP (RESP *resp, char expectedType, int expectedSize)
 
int redisxCheckRESP (const RESP *resp, char expectedType, int expectedSize)
 
void redisxDebugTraffic (boolean value)
 
void redisxDestroyRESP (RESP *resp)
 
int redisxError (const char *func, int errorCode)
 
const char * redisxErrorDescription (int code)
 
int redisxGetTime (Redis *redis, struct timespec *t)
 
boolean redisxHasPipeline (Redis *redis)
 
boolean redisxIsVerbose ()
 
int redisxPing (Redis *redis, const char *message)
 
RESPredisxRequest (Redis *redis, const char *command, const char *arg1, const char *arg2, const char *arg3, int *status)
 
int redisxSelectDB (Redis *redis, int idx)
 
int redisxSetPassword (Redis *redis, const char *passwd)
 
int redisxSetPipelineConsumer (Redis *redis, void(*f)(RESP *))
 
int redisxSetTransmitErrorHandler (Redis *redis, RedisErrorHandler f)
 
int redisxSetUser (Redis *redis, const char *username)
 
void redisxSetVerbose (boolean value)
 

Detailed Description

A Redis client library that compiles on older and newer platforms alike, such as LynxOS 3.1.0 PowerPCs. It is quite full featured, supporting multiple Redis instances, pipelining, user-specified connect and disconnect hooks, multiple user-specified subscription listeners, and user-specified pipeline processors.

Date
May 4, 2018
Author
Attila Kovacs

Function Documentation

◆ redisxArrayRequest()

RESP * redisxArrayRequest ( Redis redis,
char *  args[],
int  lengths[],
int  n,
int *  status 
)

Returns the result of the most generic type of Redis request with any number of arguments. This is not the highest throughput mode (that would be sending asynchronous pipeline request, and then asynchronously collecting the results such as with redisxSendArrayRequestAsync() / redisxReadReplyAsync(), because it requires separate network roundtrips for each and every request. But, it is simple and perfectly good method when one needs to retrieve only a few (<1000) variables per second...

Parameters
redisPointer to a Redis instance.
argsAn array of strings to send to Redis, corresponding to a single query.
lengthsArray indicating the number of bytes to send from each string argument. Zero values can be used to determine the string length automatically using strlen(), and the length argument itself may be NULL to determine the lengths of all string arguments automatically.
nNumber of string arguments.
statusPointer to the return error status, which is either
                 X_SUCCESS       on success.
                 X_NO_INIT       if the Redis client librarywas not initialized via initRedis.
                 X_NULL          if the argument is NULL or n<1.
                 X_NO_SERVICE    if not connected to Redis.
                 X_FAILURE       If there was a socket level error.
Returns
A freshly allocated RESP array containing the Redis response, or NULL if no valid response could be obtained.
See also
redisxRequest()
redisxSendArrayRequestAsync()
redisxReadReplyAsync()

References Redis::interactive, redisxLockConnected(), redisxReadReplyAsync(), redisxSendArrayRequestAsync(), redisxUnlockClient(), x_error(), X_NULL, X_SUCCESS, x_trace_null(), and xvprintf.

◆ redisxCheckDestroyRESP()

int redisxCheckDestroyRESP ( RESP resp,
char  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,
char  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, RESP_INT, RESP::type, RESP::value, x_error(), X_FAILURE, X_NULL, and X_SUCCESS.

◆ redisxDebugTraffic()

void redisxDebugTraffic ( boolean  value)

Enable or disable verbose reporting of all Redis bound traffic. It may be useful when debugging programs that use the redisx interface. Verbose reporting is DISABLED by default.

Parameters
valueTRUE to enable verbose reporting, or FALSE to disable.
See also
redisxSetVerbose()

References FALSE, and TRUE.

◆ 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 RESP::n, redisxDestroyRESP(), RESP_ARRAY, RESP::type, and RESP::value.

◆ redisxError()

int redisxError ( const char *  func,
int  errorCode 
)

Prints a descriptive error message to stderr, and returns the error code.

Parameters
funcA string that describes the function or location where the error occurred.
errorCodeThe error code that describes the failure.
Returns
the error code.

References MAX_DEBUG_ERROR_COUNT, REDIS_INCOMPLETE_TRANSFER, redisxErrorDescription(), and xDebug.

◆ redisxErrorDescription()

const char * redisxErrorDescription ( int  code)

Returns a string description for one of the RM error codes.

Parameters
codeOne of the error codes defined in 'rm.h' or in 'redisrm.h' (e.g. X_NO_PIPELINE)
Returns
A constant string with the error description.

References REDIS_ERROR, REDIS_INCOMPLETE_TRANSFER, REDIS_INVALID_CHANNEL, REDIS_NULL, REDIS_UNEXPECTED_ARRAY_SIZE, REDIS_UNEXPECTED_RESP, and xErrorDescription().

◆ redisxGetTime()

int redisxGetTime ( Redis redis,
struct timespec *  t 
)

Returns the current time on the Redis server instance.

Parameters
redisPointer to a Redis instance.
[out]tPointer to a timespec structure in which to return the server time.
Returns
X_SUCCESS (0) if successful, or X_NULL if either argument is NULL, or X_PARSE_ERROR if could not parse the response, or another error returned by redisxCheckRESP().

References redisxCheckDestroyRESP(), redisxCheckRESP(), redisxDestroyRESP(), redisxRequest(), RESP_ARRAY, RESP_BULK_STRING, RESP::value, x_error(), X_NULL, X_PARSE_ERROR, X_SUCCESS, and x_trace().

◆ redisxHasPipeline()

boolean redisxHasPipeline ( Redis redis)

Checks if a Redis instance has the pipeline connection enabled.

Parameters
redisPointer to a Redis instance.
Returns
TRUE (1) if the pipeline client is enabled on the Redis intance, or FALSE (0) otherwise.

References FALSE, and Redis::pipeline.

◆ redisxIsVerbose()

boolean redisxIsVerbose ( )

Checks id verbose reporting is enabled.

Returns
TRUE if verbose reporting is enabled, otherwise FALSE.

References xIsVerbose().

◆ redisxPing()

int redisxPing ( Redis redis,
const char *  message 
)

Pings the Redis server (see the Redis PING command), and checks the response.

Parameters
redisPointer to a Redis instance.
messageOptional message , or NULL for PING without an argument.
Returns
X_SUCCESS (0) if successful, or else an error code (<0) from redisx.h / xchange.h.

References REDIS_UNEXPECTED_RESP, redisxCheckRESP(), redisxDestroyRESP(), redisxRequest(), RESP_BULK_STRING, RESP_SIMPLE_STRING, RESP::value, x_error(), X_NULL, and X_SUCCESS.

◆ redisxRequest()

RESP * redisxRequest ( Redis redis,
const char *  command,
const char *  arg1,
const char *  arg2,
const char *  arg3,
int *  status 
)

Returns the result of a Redis command with up to 3 regularly terminated string arguments. This is not the highest throughput mode (that would be sending asynchronous pipeline request, and then asynchronously collecting the results such as with redisxSendRequestAsync() / redisxReadReplyAsync(), because it requires separate network roundtrips for each and every request. But, it is simple and perfectly good method when one needs to retrieve only a few (<1000) variables per second...

To make Redis calls with binary (non-string) data, you can use redisxArrayRequest() instead, where you can set the number of bytes for each argument explicitly.

Parameters
redisPointer to a Redis instance.
commandRedis command, e.g. "HGET"
arg1First terminated string argument or NULL.
arg2Second terminated string argument or NULL.
arg3Third terminated string argument or NULL.
statusPointer to the return error status, which is either X_SUCCESS on success or else the error code set by redisxArrayRequest().
Returns
A freshly allocated RESP array containing the Redis response, or NULL if no valid response could be obtained.
See also
redisxArrayRequest()
redisxSendRequestAsync()
redisxReadReplyAsync()

References redisxArrayRequest(), x_error(), X_NULL, and x_trace_null().

◆ redisxSelectDB()

int redisxSelectDB ( Redis redis,
int  idx 
)

Switches to another database index on the Redis server. Note that you cannot change the database on an active PUB/SUB channel, hence the call will return X_INCOMPLETE if attempted. You should instead switch DB when there are no active subscriptions.

Parameters
redisPointer to a Redis instance.
idxzero-based database index
Returns
X_SUCCESS (0) if successful, or X_NULL if the redis argument is NULL, X_INCOMPLETE if there is an active subscription channel that cannot be switched or one of the channels could not confirm the switch, or else another error code (<0) from redisx.h / xchange.h.
See also
redisxSelectDB()
redisxLockConnected()

References REDIS_INVALID_CHANNEL, REDISX_CHANNELS, REDISX_PIPELINE_CHANNEL, REDISX_SUBSCRIPTION_CHANNEL, redisxAddConnectHook(), redisxGetClient(), redisxIsConnected(), redisxLockConnected(), redisxRemoveConnectHook(), redisxUnlockClient(), x_error(), X_INCOMPLETE, X_NULL, X_SUCCESS, and x_trace().

◆ redisxSetPassword()

int redisxSetPassword ( Redis redis,
const char *  passwd 
)

Sets the password to use for authenticating on the Redis server after connection. See the AUTH Redis command for more explanation. Naturally, you need to call this prior to connecting your Redis instance to have the desired effect.

Parameters
redisPointer to the Redis instance for which to set credentials
passwdthe password to use for authenticating on the server, or NULL to clear a previously configured password.
Returns
X_SUCCESS (0) if successful, X_NULL if the redis argument is NULL, or X_ALREADY_OPEN if called after Redis was already connected.
See also
redisxSetUser()

References redisxIsConnected(), X_ALREADY_OPEN, x_error(), X_NULL, X_SUCCESS, and xStringCopyOf().

◆ redisxSetPipelineConsumer()

int redisxSetPipelineConsumer ( Redis redis,
void(*)(RESP *)  f 
)

Sets the function processing valid pipeline responses.

Parameters
redisPointer to a Redis instance.
fT he function that processes a single argument of type RESP pointer.
Returns
X_SUCCESS (0) if successful, or X_NULL if the Redis instance is NULL.

References x_error(), X_NULL, and X_SUCCESS.

◆ redisxSetTransmitErrorHandler()

int redisxSetTransmitErrorHandler ( Redis redis,
RedisErrorHandler  f 
)

Sets the user-specific error handler to call if a socket level trasmit error occurs. It replaces any prior handlers set earlier.

Parameters
redisThe Redis instance to configure.
fThe error handler function, which is called with the pointer to the redis instance that had the errror, the redis channel index (e.g. REDIS_INTERACTIVE_CHANNEL) and the operation (e.g. 'send' or 'read') that failed. Note, that the call may be made with the affected Redis channel being in a locked state. As such the handler should not directly attempt to change the connection state of the Redis instance. Any calls that require exlusive access to the affected channel should instead be spawn off into a separate thread, which can obtain the necessary lock when it is released.
Returns
X_SUCCESS if the handler was successfully configured, or X_NULL if the Redis instance is NULL.

References x_error(), X_NULL, and X_SUCCESS.

◆ redisxSetUser()

int redisxSetUser ( Redis redis,
const char *  username 
)

Sets the user name to use for authenticating on the Redis server after connection. See the AUTH Redis command for more explanation. Naturally, you need to call this prior to connecting your Redis instance to have the desired effect.

Parameters
redisPointer to the Redis instance for which to set credentials
usernamethe password to use for authenticating on the server, or NULL to clear a previously configured password.
Returns
X_SUCCESS (0) if successful, X_NULL if the redis argument is NULL, or X_ALREADY_OPEN if called after Redis was already connected.
See also
redisxSetPassword()

References redisxIsConnected(), X_ALREADY_OPEN, x_error(), X_NULL, X_SUCCESS, and xStringCopyOf().

◆ redisxSetVerbose()

void redisxSetVerbose ( boolean  value)

Enable or disable verbose reporting of all Redis operations (and possibly some details of them). Reporting is done on the standard output (stdout). It may be useful when debugging programs that use the redisx interface. Verbose reporting is DISABLED by default.

Parameters
valueTRUE to enable verbose reporting, or FALSE to disable.
See also
redisxDebugTraffic()

References xSetVerbose().