Last active
January 22, 2020 18:17
-
-
Save mpenick/e6804b4466595cb6ef8bc4a0cfeaf0bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdbool.h> | |
#include <stdio.h> /* FIXME: Remove header */ | |
#include <string.h> /* FIXME: Remove header */ | |
#define CASS_INET_V6_LENGTH 16 | |
#define CASS_VALUE_TYPE_MAPPING(XX) \ | |
XX(CASS_VALUE_TYPE_CUSTOM, 0x0000, "", "") \ | |
XX(CASS_VALUE_TYPE_ASCII, 0x0001, "ascii", "org.apache.cassandra.db.marshal.AsciiType") \ | |
XX(CASS_VALUE_TYPE_BIGINT, 0x0002, "bigint", "org.apache.cassandra.db.marshal.LongType") \ | |
XX(CASS_VALUE_TYPE_BLOB, 0x0003, "blob", "org.apache.cassandra.db.marshal.BytesType") \ | |
XX(CASS_VALUE_TYPE_BOOLEAN, 0x0004, "boolean", "org.apache.cassandra.db.marshal.BooleanType") \ | |
XX(CASS_VALUE_TYPE_COUNTER, 0x0005, "counter", "org.apache.cassandra.db.marshal.CounterColumnType") \ | |
XX(CASS_VALUE_TYPE_DECIMAL, 0x0006, "decimal", "org.apache.cassandra.db.marshal.DecimalType") \ | |
XX(CASS_VALUE_TYPE_DOUBLE, 0x0007, "double", "org.apache.cassandra.db.marshal.DoubleType") \ | |
XX(CASS_VALUE_TYPE_FLOAT, 0x0008, "float", "org.apache.cassandra.db.marshal.FloatType") \ | |
XX(CASS_VALUE_TYPE_INT, 0x0009, "int", "org.apache.cassandra.db.marshal.Int32Type") \ | |
XX(CASS_VALUE_TYPE_TEXT, 0x000A, "text", "org.apache.cassandra.db.marshal.UTF8Type") \ | |
XX(CASS_VALUE_TYPE_TIMESTAMP, 0x000B, "timestamp", "org.apache.cassandra.db.marshal.TimestampType") \ | |
XX(CASS_VALUE_TYPE_UUID, 0x000C, "uuid", "org.apache.cassandra.db.marshal.UUIDType") \ | |
XX(CASS_VALUE_TYPE_VARCHAR, 0x000D, "varchar", "") \ | |
XX(CASS_VALUE_TYPE_VARINT, 0x000E, "varint", "org.apache.cassandra.db.marshal.IntegerType") \ | |
XX(CASS_VALUE_TYPE_TIMEUUID, 0x000F, "timeuuid", "org.apache.cassandra.db.marshal.TimeUUIDType") \ | |
XX(CASS_VALUE_TYPE_INET, 0x0010, "inet", "org.apache.cassandra.db.marshal.InetAddressType") \ | |
XX(CASS_VALUE_TYPE_DATE, 0x0011, "date", "org.apache.cassandra.db.marshal.SimpleDateType") \ | |
XX(CASS_VALUE_TYPE_TIME, 0x0012, "time", "org.apache.cassandra.db.marshal.TimeType") \ | |
XX(CASS_VALUE_TYPE_SMALL_INT, 0x0013, "smallint", "org.apache.cassandra.db.marshal.ShortType") \ | |
XX(CASS_VALUE_TYPE_TINY_INT, 0x0014, "tinyint", "org.apache.cassandra.db.marshal.ByteType") \ | |
XX(CASS_VALUE_TYPE_DURATION, 0x0015, "duration", "org.apache.cassandra.db.marshal.DurationType") \ | |
XX(CASS_VALUE_TYPE_LIST, 0x0020, "list", "org.apache.cassandra.db.marshal.ListType") \ | |
XX(CASS_VALUE_TYPE_MAP, 0x0021, "map", "org.apache.cassandra.db.marshal.MapType") \ | |
XX(CASS_VALUE_TYPE_SET, 0x0022, "set", "org.apache.cassandra.db.marshal.SetType") \ | |
XX(CASS_VALUE_TYPE_UDT, 0x0030, "", "") \ | |
XX(CASS_VALUE_TYPE_TUPLE, 0x0031, "tuple", "org.apache.cassandra.db.marshal.TupleType") | |
typedef enum CassValueType_ { | |
CASS_VALUE_TYPE_UNKNOWN = 0xFFFF, | |
#define XX_VALUE_TYPE(name, type, cql, klass) name = type, | |
CASS_VALUE_TYPE_MAPPING(XX_VALUE_TYPE) | |
#undef XX_VALUE_TYPE | |
/* @cond IGNORE */ | |
CASS_VALUE_TYPE_LAST_ENTRY | |
/* @endcond */ | |
} CassValueType; | |
typedef struct CassDataType_ CassDataType; | |
typedef struct CassCollection_ CassCollection; | |
typedef struct CassUuid_ { | |
/** | |
* Represents the time and version part of a UUID. The most significant | |
* 4 bits represent the version and the bottom 60 bits representing the | |
* time part. For version 1 the time part represents the number of | |
* 100 nanosecond periods since 00:00:00 UTC, January 1, 1970 (the Epoch). | |
* For version 4 the time part is randomly generated. | |
*/ | |
uint64_t time_and_version; | |
/** | |
* Represents the clock sequence and the node part of a UUID. The most | |
* significant 16 bits represent the clock sequence (except for the most | |
* significant bit which is always set) and the bottom 48 bits represent | |
* the node part. For version 1 (time-based) the clock sequence part is randomly | |
* generated and the node part can be explicitly set, otherwise, it's generated | |
* from node unique information. For version 4 both the clock sequence and the node | |
* parts are randomly generated. | |
*/ | |
uint64_t clock_seq_and_node; | |
} CassUuid; | |
typedef struct CassInet_ { | |
/** | |
* Big-endian, binary representation of a IPv4 or IPv6 address | |
*/ | |
uint8_t address[CASS_INET_V6_LENGTH]; | |
/** | |
* Number of address bytes. 4 bytes for IPv4 and 16 bytes for IPv6. | |
*/ | |
uint8_t address_length; | |
} CassInet; | |
typedef struct CassValue_ { | |
CassValueType type; | |
union { | |
bool bln; | |
int8_t i8; | |
int16_t i16; | |
int32_t i32; | |
uint32_t u32; | |
int64_t i64; | |
float f32; | |
double f64; | |
struct { | |
int32_t months; | |
int32_t days; | |
int64_t nanos; | |
} duration; | |
struct { | |
const char* str; | |
int32_t len; | |
} string; | |
struct { | |
const char* data; | |
int32_t len; | |
} bytes; | |
struct { | |
const char* varint; | |
int32_t varint_len; | |
int32_t scale; | |
} decimal; | |
struct { | |
const char* data; | |
int32_t len; | |
} varint; | |
CassUuid uuid; | |
CassInet inet; | |
CassCollection* collection; | |
} value; | |
} CassValue; | |
typedef enum { | |
CASS_ITERATOR_ROW, | |
CASS_ITERATOR_COLUMN, | |
CASS_ITERATOR_COLLECTION | |
} CassIteratorType; | |
typedef struct CassIterator_ { | |
CassIteratorType type; | |
void* position; | |
CassDataType* data_type; | |
CassValue value; | |
} CassIterator; | |
typedef enum { | |
CASS_OK = 0 | |
} CassError; | |
typedef struct CassSession_ CassSession; | |
typedef struct CassSessionOptions_ CassSessionOptions; | |
typedef void (*CassConnectCallback)(CassSession* session, void* user_data); | |
typedef struct CassResult_ CassResult; | |
typedef struct CassRow_ CassRow; | |
typedef struct CassQueryOptions_ CassQueryOptions; | |
typedef void (*CassQueryCallback)(CassResult* result, void* user_data); | |
typedef struct CassPrepared_ CassPrepared; | |
typedef struct CassPrepareOptions_ CassPrepareOptions; | |
typedef void (*CassPrepareCallback)(CassPrepared* prepared, void* user_data); | |
/* Value */ | |
CassValue* cass_value_init_int32(CassValue* value, int8_t i32) { | |
return value; | |
} | |
CassValue* cass_value_init_string(CassValue* value, const char* str, int32_t str_len) { | |
return value; | |
} | |
void cass_value_copy(const CassValue* src, CassValue* dst); | |
void cass_value_free(CassValue* value); | |
int32_t cass_value_int32(const CassValue* value) { | |
return 0; | |
} | |
int32_t cass_value_string_length(const CassValue* value) { | |
return 0; | |
} | |
int32_t cass_value_string(const CassValue* value, char* str, int32_t str_len) { | |
return 0; | |
} | |
/* Named Value */ | |
typedef struct CassNamedValue_ { | |
const char* name; | |
int32_t name_len; | |
CassValue value; | |
} CassNamedValue; | |
#define cass_named_value_init_int32(n, fv) do { \ | |
CassNamedValue* n_ = (n); \ | |
(n_)->name = NULL; \ | |
(n_)->name_len = 0; \ | |
cass_value_init_int32(&(n_)->value, fv); \ | |
} while(0) | |
#define cass_named_value_init_int32_name(n, fn, fn_len, fv) do { \ | |
CassNamedValue* n_ = (n); \ | |
(n_)->name = (fn); \ | |
(n_)->name_len = (fn_len); \ | |
cass_value_init_int32(&(n_)->value, fv); \ | |
} while(0) | |
#define cass_named_value_init_string(n, fv, fv_len) do { \ | |
CassNamedValue* n_ = (n); \ | |
(n_)->name = NULL; \ | |
(n_)->name_len = 0; \ | |
cass_value_init_string(&(n_)->value, fv, fv_len); \ | |
} while(0) | |
#define cass_named_value_init_string_name(n, fn, fn_len, fv, fv_len) do { \ | |
CassNamedValue* n_ = (n); \ | |
(n_)->name = (fn); \ | |
(n_)->name_len = (fn_len); \ | |
cass_value_init_string(&(n_)->value, fv, fv_len); \ | |
} while(0) | |
/* Collection */ | |
// 4 cases | |
// List: Arbitrary value type | |
// Map: Arbitrary key and values types | |
// UDT: String key and arbitrary value types | |
// Tuple: Arbitrary value types | |
CassCollection* cass_collection_new(CassValueType type); | |
CassCollection* cass_collection_new_udt(const CassDataType* udt_type); | |
void cass_collection_free(CassCollection* collection); | |
CassError cass_collection_append_values(CassCollection* collection, | |
CassValue* value, int32_t values_count) { | |
return true; | |
} | |
CassValueType cass_collection_value_type(const CassCollection* collection); | |
const CassDataType* cass_collection_type(const CassCollection* collection); | |
/* Iterator */ | |
CassError cass_iterator_init_result(CassIterator* iter, const CassResult* res) { | |
return CASS_OK; | |
} | |
CassError cass_iterator_init_row(CassIterator* iter, const CassRow* row) { | |
return CASS_OK; | |
} | |
CassError cass_iterator_init_collection(CassIterator* iter, const CassCollection* coll) { | |
return CASS_OK; | |
} | |
bool cass_iterator_next(CassIterator* iter) { | |
return false; | |
} | |
const CassRow* cass_iterator_row(const CassIterator* iter) { | |
return NULL; | |
} | |
const CassValue* cass_iterator_value(const CassIterator* iter) { | |
return NULL; | |
} | |
CassIteratorType cass_iterator_type(const CassIterator* iter) { | |
return CASS_ITERATOR_ROW; | |
} | |
CassValueType cass_iterator_value_type(CassIterator* iter) { | |
return CASS_VALUE_TYPE_INT; | |
} | |
const CassDataType* cass_iterator_data_type(const CassIterator* iter) { | |
return NULL; | |
} | |
/* Query Options */ | |
CassQueryOptions* cass_query_options_new(); | |
void cass_query_options_free(CassQueryOptions* options); | |
CassError cass_query_options_set_callback(CassQueryOptions* options, | |
CassQueryCallback cb, void* user_data); | |
/* Prepared Options */ | |
CassPrepareOptions* cass_prepare_options_new(); | |
void cass_prepare_options_free(CassPrepareOptions* options); | |
CassError cass_prepare_options_set_callback(CassPrepareOptions* options, | |
CassPrepareCallback cb, void* user_data); | |
/* Session Options */ | |
CassSessionOptions* cass_session_options_new() { | |
return NULL; | |
} | |
void cass_session_options_free(CassSessionOptions* options) { | |
} | |
CassError cass_session_options_set_contact_points(CassSessionOptions* options, | |
const char* contact_points, int32_t contact_points_len) { | |
} | |
CassError cass_session_options_set_callback(CassSessionOptions* options, | |
CassConnectCallback cb, void* user_data); | |
/* Session */ | |
CassSession* cass_session_connect(const CassSessionOptions* options) { | |
return NULL; | |
} | |
void cass_session_close(CassSession* session) { | |
} | |
CassError cass_session_wait(CassSession* session) { | |
} | |
CassError cass_session_wait_timed(CassSession* session, | |
uint64_t timeout_us) { | |
} | |
CassResult* cass_session_query(CassSession* session, | |
const char* query, int32_t query_len, | |
const CassNamedValue* params, int32_t params_count, | |
const CassQueryOptions* options) { | |
return NULL; | |
} | |
CassPrepared* cass_session_prepare(CassSession* session, | |
const char* query, int32_t query_len, | |
const CassPrepareOptions* options) { | |
return NULL; | |
} | |
CassResult* cass_session_execute(CassSession* session, | |
const CassPrepared* prepared, | |
const CassNamedValue* params, int32_t params_count, | |
const CassQueryOptions* options) { | |
return NULL; | |
} | |
/* Result */ | |
void cass_result_free(CassResult* result) { | |
} | |
CassError cass_result_wait(CassResult* result); | |
CassError cass_result_wait_timed(CassResult* result, | |
uint64_t timeout_us); | |
const CassRow* cass_result_first_row(const CassResult* result); | |
/* Prepared */ | |
void cass_prepared_free(CassPrepared* prepared); | |
CassError cass_prepared_wait(CassPrepared* prepared); | |
CassError cass_prepared_wait_timed(CassPrepared* prepared, | |
uint64_t timeout_us); | |
int main() { | |
CassSessionOptions* session_options = cass_session_options_new(); | |
cass_session_options_set_contact_points(session_options, "127.0.0.1:9042", -1); | |
CassSession* session = cass_session_connect(session_options); | |
CassNamedValue params[1]; | |
cass_named_value_init_string(¶ms[0], "127.0.0.2", -1); // Not a correct param, but an example | |
CassResult* result = cass_session_query(session, | |
"SELECT version FROM system.peers WHERE = ?", -1, | |
params, 1, NULL); | |
CassIterator result_iter; | |
CassError rc = cass_iterator_init_result(&result_iter, result); | |
while (rc == CASS_OK && cass_iterator_next(&result_iter)) { | |
CassIterator row_iter; | |
const CassRow* row = cass_iterator_row(&result_iter); | |
cass_iterator_init_row(&row_iter, row); | |
while (cass_iterator_next(&row_iter)) { | |
char str[256]; | |
const CassValue* value = cass_iterator_value(&row_iter); | |
cass_value_string(value, str, sizeof(str)); | |
printf("Version: %s\n", str); | |
} | |
} | |
cass_result_free(result); | |
cass_session_options_free(session_options); | |
cass_session_close(session); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment