![]() |
smax-clib v1.0
A C/C++ client library for SMA-X
|
A collection of commonly used functions for the SMA-X library. More...
Macros | |
| #define | _GNU_SOURCE |
| for strcasecmp() | |
| #define | _POSIX_C_SOURCE 199309 |
| for clock_gettime() | |
Functions | |
| int | smax2xField (XField *f) |
| int | smax2xStruct (XStructure *s) |
| XField * | smaxCreateField (const char *name, XType type, int ndim, const int *sizes, const void *value) |
| XMeta * | smaxCreateMeta () |
| int | smaxDeletePattern (const char *pattern) |
| int | smaxError (const char *func, int errorCode) |
| const char * | smaxErrorDescription (int code) |
| int | smaxGetMetaCount (const XMeta *m) |
| char * | smaxGetScriptSHA1 (const char *scriptName, int *status) |
| int | smaxGetServerTime (struct timespec *t) |
| double | smaxGetTime (const char *timestamp) |
| int | smaxParseTime (const char *timestamp, time_t *secs, long *nanosecs) |
| void | smaxResetMeta (XMeta *m) |
| int | smaxScriptError (const char *name, int status) |
| int | smaxScriptErrorAsync (const char *name, int status) |
| void | smaxSetOrigin (XMeta *m, const char *origin) |
| void | smaxSocketErrorHandler (Redis *redis, enum redisx_channel channel, const char *op) |
| int | smaxStringToValues (const char *str, void *value, XType type, int eCount, int *pos) |
| char * | smaxStringType (XType type) |
| int | smaxTimestamp (char *buf) |
| __inline__ int | smaxTimeToString (const struct timespec *time, char *buf) |
| */ | |
| XType | smaxTypeForString (const char *type) |
| int | smaxUnpackStrings (const char *data, int len, int count, char **dst) |
| char * | smaxValuesToString (const void *value, XType type, int eCount, char *trybuf, int trylength) |
| int | x2smaxField (XField *f) |
| int | x2smaxStruct (XStructure *s) |
A collection of commonly used functions for the SMA-X library.
| int smax2xField | ( | XField * | f | ) |
Converts SMA-X field with serialized string value storage to a standard xchange field with a native value storage.
| f | Pointer to field to convert |
References FALSE, XField::isSerialized, smax2xStruct(), smaxStringToValues(), XField::type, XField::value, x_error(), X_NULL, X_RAW, X_SIZE_INVALID, X_STRUCT, X_SUCCESS, x_trace(), X_TYPE_INVALID, xElementSizeOf(), and xGetFieldCount().
| int smax2xStruct | ( | XStructure * | s | ) |
Converts an SMA-X structure with serialized string value storage to a standard xchange structure with a native value storage.
| s | Pointer to structure to convert |
References XStructure::firstField, XField::next, smax2xField(), x_error(), X_STRUCT_INVALID, and X_SUCCESS.
| XField * smaxCreateField | ( | const char * | name, |
| XType | type, | ||
| int | ndim, | ||
| const int * | sizes, | ||
| const void * | value | ||
| ) |
Creates a generic field of a given name and type and dimensions using the specified native values. It is like xCreateField() except that the field is created in serialized form for SMA-X.
| name | Field name |
| type | Storage type, e.g. X_INT. |
| ndim | Number of dimensionas (1:20). If ndim < 1, it will be reinterpreted as ndim=1, size[0]=1; |
| sizes | Array of sizes along each dimensions, with at least ndim elements, or NULL with ndim<1. |
| value | Pointer to the native data location in memory. Unless it is of type X_STRUCT, the data stored in the field is a copy (for type X_RAW) or serialized string (otherwise). |
References XField::isSerialized, smaxValuesToString(), TRUE, XField::value, X_RAW, X_STRING, X_STRUCT, x_trace_null(), xCreateField(), xGetElementCount(), and xStringElementSizeOf().
| XMeta * smaxCreateMeta | ( | ) |
Creates a new SMA-X metadata object with defaults. Effectively the same as calling calloc() followed by xResetMeta().
References smaxResetMeta().
| int smaxDeletePattern | ( | const char * | pattern | ) |
Deletes variables and metadata from SMA-X.
| pattern | Glob variable name pattern |
References smaxError(), smaxGetRedis(), x_error(), X_NO_INIT, X_NULL, and X_SEP.
| int smaxError | ( | const char * | func, |
| int | errorCode | ||
| ) |
Prints a descriptive error message to stderr, and returns the error code.
| func | String that describes the function or location where the error occurred. |
| errorCode | Error code that describes the failure. |
References redisxError(), smaxErrorDescription(), X_NO_SERVICE, and xDebug.
| const char * smaxErrorDescription | ( | int | code | ) |
Returns a string description for one of the RM error codes.
References redisxErrorDescription().
| int smaxGetMetaCount | ( | const XMeta * | m | ) |
Returns the number of elements stored from a metadata.
| m | pointer to metadata that defines the dimension and shape of elements. |
References XMeta::storeDim, XMeta::storeSizes, and xGetElementCount().
| char * smaxGetScriptSHA1 | ( | const char * | scriptName, |
| int * | status | ||
| ) |
Gets the SHA1 script ID for the currently loaded script with the specified name.
| scriptName | Case-sensitive name of the script, e.g. "GetStruct". |
| status | Pointer int which to return status, which is X_SUCCESS if the SHA1 id was successfully obtained, or else an appropriate error code. |
References redisxCheckDestroyRESP(), redisxDestroyRESP(), redisxRequest(), RESP_BULK_STRING, SMAX_SCRIPTS, smaxError(), smaxGetRedis(), RESP::value, x_error(), X_NAME_INVALID, X_NO_INIT, and x_trace_null().
| int smaxGetServerTime | ( | struct timespec * | t | ) |
Returns the current time on the Redis server instance.
| t | Pointer to a timespec structure in which to return the server time. |
References redisxGetTime(), smaxError(), smaxGetRedis(), X_NO_INIT, and X_SUCCESS.
| double smaxGetTime | ( | const char * | timestamp | ) |
Returns the a sub-second precision UNIX time value for the given SMA-X timestamp
| timestamp | The string timestamp returned by SMA-X |
References NAN, smaxParseTime(), x_error(), X_NULL, and x_trace().
| int smaxParseTime | ( | const char * | timestamp, |
| time_t * | secs, | ||
| long * | nanosecs | ||
| ) |
Parses a timestamp into broken-down UNIX time.
| [in] | timestamp | Timestamp string as returned in redis queries; |
| [out] | secs | Pointer to the returned UNIX time (seconds). |
| [out] | nanosecs | Pointer to the retuned sub-second remainder as nanoseconds, or NULL if nor requested. |
secs argument is NULL. X_PARSE_ERROR if the seconds could not be parsed. 1 if there was an error parsing the nanosec part. X_NULL if the secs arhument is NULL References x_error(), X_NULL, X_PARSE_ERROR, and X_SUCCESS.
| void smaxResetMeta | ( | XMeta * | m | ) |
Set metadata to their default values. After resetting the supplied metadata will have exactly the same content as if it were initialized with the X_META_INIT macro.
| m | Pointer to the metadata that is to be cleared. |
References X_META_INIT.
| int smaxScriptError | ( | const char * | name, |
| int | status | ||
| ) |
SMA-X error handler for when the LUA scripts do not execute. It prints a message to stderr, then depending on whether SMA-X is in resilient mode, it will try to reconnect to SMA-X in the background, or else exits the program with X_NO_SERVICE. You must not call this function with a locked config mutex (via smaxConfigLock()). Instead use the async version of this function after smaxConfigLock().
| name | The name of the calling function or name of script (whichever is more informative). |
| status | An approprioate error code from xchange.h to indicate the type of error. |
References smaxScriptErrorAsync().
| int smaxScriptErrorAsync | ( | const char * | name, |
| int | status | ||
| ) |
Same as smaxScriptError(), but can be used after smaxConfigLock().
| name | The name of the calling function or name of script (whichever is more informative). |
| status | An approprioate error code from xchange.h to indicate the type of error. |
References smaxErrorDescription(), smaxIsConnected(), smaxIsResilient(), TRUE, X_FAILURE, X_NO_SERVICE, and X_NULL.
| void smaxSetOrigin | ( | XMeta * | m, |
| const char * | origin | ||
| ) |
Sets the 'origin' field of an SMA-X metadata to the specified value, truncating as necessary to fit into the allotted fixed storage.
| origin | The origination information, usually as hostname:progname |
| m | Pointer to metadata to set. |
References XMeta::origin, and SMAX_ORIGIN_LENGTH.
| void smaxSocketErrorHandler | ( | Redis * | redis, |
| enum redisx_channel | channel, | ||
| const char * | op | ||
| ) |
The SMA-X error handler for Redis transmit (send or receive) errors. It prints a message to stderr, then depending on whether SMA-X is in resilient mode, it will try to reconnect to SMA-X in the background, or else exits the program with X_NO_SERVICE.
| redis | The Redis instance in which the error occurred. In case of SMA-X this will always be the Redis instance used by SMA-X. |
| channel | The Redis channel index on which the error occured, such as REDIS_INTERAVTIVE_CHANNEL |
| op | The operation during which the error occurred, e.g. 'send' or 'read'. |
References smaxGetRedis(), smaxIsResilient(), TRUE, X_FAILURE, and X_NO_SERVICE.
| int smaxStringToValues | ( | const char * | str, |
| void * | value, | ||
| XType | type, | ||
| int | eCount, | ||
| int * | pos | ||
| ) |
Deserializes a string to binary values.
| [in] | str | Serialized ASCII representation of the data (as stored by Redis). |
| [out] | value | Pointer to the buffer that will hold the binary values. The caller is responsible for ensuring the buffer is sufficiently sized for holding the data for the given variable. |
| [in] | type | Share type, e.g. X_INT. The types X_RAW, X_STRUCT are not supported by this function. |
| [in] | eCount | Number of elements to retrieve. Ignored for X_STRUCT. |
| [out] | pos | Parse position, i.e. the number of characters parsed from the input string... |
X_NULL If the value or str argument is NULL.
X_TYPE_INVALID If the type is not supported.
X_SIZE_INVALID If size is invalid (e.g. X_RAW, X_STRUCT)
X_PARSE_ERROR If the tokens could not be parsed in the format expected
References smaxUnpackStrings(), X_BOOLEAN, X_BYTE, X_DOUBLE, x_error(), X_FLOAT, X_INT, X_LLONG, X_LONG, X_NULL, X_RAW, X_SHORT, X_SIZE_INVALID, X_STRING, X_STRUCT, x_trace(), X_TYPE_INVALID, xElementSizeOf(), xIsCharSequence(), xParseBoolean(), xParseDouble(), xParseFloat(), and xZero().
| char * smaxStringType | ( | XType | type | ) |
Returns the string type for a given XType argument as a constant expression. For examples X_LONG -> "int64".
| type | SMA-X type, e.g. X_FLOAT |
References X_BOOLEAN, X_BYTE, X_DOUBLE, x_error(), X_FLOAT, X_INT16, X_INT32, X_INT64, X_RAW, X_STRING, X_STRUCT, and X_UNKNOWN.
| int smaxTimestamp | ( | char * | buf | ) |
Prints the current time into the supplied buffer with subsecond precision.
| [out] | buf | Pointer to string buffer, must be at least X_TIMESTAMP_LENGTH in size. |
buf argument is NULL. References smaxTimeToString().
| __inline__ int smaxTimeToString | ( | const struct timespec * | time, |
| char * | buf | ||
| ) |
*/
Prints the given UNIX time into the supplied buffer with subsecond precision.
| [in] | time | Pointer to time value. |
| [out] | buf | Pointer to string buffer, must be at least X_TIMESTAMP_LENGTH in size. |
buf argument is NULL. | XType smaxTypeForString | ( | const char * | type | ) |
Returns the XType for a given case-sensitive type string. For example "float" -> X_FLOAT. The value "raw" will return X_RAW.
| type | String type, e.g. "struct". |
References X_BOOLEAN, X_DOUBLE, x_error(), X_FLOAT, X_INT, X_RAW, X_STRING, X_STRUCT, and X_UNKNOWN.
| int smaxUnpackStrings | ( | const char * | data, |
| int | len, | ||
| int | count, | ||
| char ** | dst | ||
| ) |
Returns an array of dynamically allocated strings from a packed buffer of consecutive 0-terminated or '\r'-separated string elements.
| [in] | data | Pointer to the packed string data buffer. |
| [in] | len | length of packed string (excl. termination). |
| [in] | count | Number of string elements expected. If fewer than that are found in the packed data, then the returned array of pointers will be padded with NULL. |
| [out] | dst | An array of string pointers (of size 'count') which will point to dynamically allocated string (char*) elements. The array is assumed to be uninitialized, and elements will be allocated as necessary. |
References x_error(), X_INCOMPLETE, X_NULL, and X_SUCCESS.
| char * smaxValuesToString | ( | const void * | value, |
| XType | type, | ||
| int | eCount, | ||
| char * | trybuf, | ||
| int | trylength | ||
| ) |
Serializes binary values into a string representation (for Redis).
| [in] | value | Pointer to an array of values, or NULL to produce all zeroes. If type is X_STRING value should be a pointer to a char** (array of string pointers), as opposed to X_CHAR(n), which expects a contiguous char* buffer with [n * eCount] length (Note, a char[eCount][n] is equivalent to such a char* buffer). |
| [in] | type | Share type, e.g. X_DOUBLE. All type except X_STRUCT are supported. |
| [in] | eCount | Number of elements (ignored for X_RAW). |
| [in,out] | trybuf | (optional) An optional pointer to a buffer that will be used if sufficient (can be NULL). |
| [in] | trylength | (optional) Size of the optional buffer. |
References X_BOOLEAN, X_BYTE, X_DOUBLE, x_error(), X_FLOAT, X_INT, X_LLONG, X_LONG, X_RAW, X_SHORT, X_STRING, X_STRUCT, x_trace_null(), X_UNKNOWN, xElementSizeOf(), xIsCharSequence(), xPrintDouble(), xPrintFloat(), and xStringElementSizeOf().
| int x2smaxField | ( | XField * | f | ) |
Converts a standard xchange field (with a native value storage) to an SMA-X field with serialized string value storage.
| [in,out] | f | Pointer to field to convert |
References XStructure::firstField, XField::isSerialized, XField::name, XField::next, smaxValuesToString(), TRUE, XField::type, XField::value, x2smaxField(), x2smaxStruct(), x_error(), X_FIELD, X_NULL, X_RAW, X_STRUCT, X_SUCCESS, x_trace(), xClearField(), xCreateStruct(), xGetFieldCount(), and xStringCopyOf().
| int x2smaxStruct | ( | XStructure * | s | ) |
Converts a standard xchange structure (with a native value storage) to an SMA-X structure with serialized string value storage.
| s | Pointer to structure to convert |
References XStructure::firstField, XField::next, x2smaxField(), x_error(), X_STRUCT_INVALID, and X_SUCCESS.