Skip to content

Examples

Query example

#define KXVER 3

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "sapi.h"

#ifdef _WIN32
#define strdup _strdup
#endif


char *logCategory = "example";
void logMessage(int level, const char *category, const char *message);
void createDict(KxsHandle *handle);
void createFlip(KxsHandle *handle);

//
// This program provides a basic example for executing commands using the SDK.
//
int main()
{
    SAPIConfig* config = calloc(1, sizeof(SAPIConfig));

    // Set up the connection properties
    //

    config->instanceName = "your-instance-name";
    config->listenPorts = "8000-8020";

    char* discoverySvcHosts[] = {
        "https://your-discovery-node.com:2379"
    };

    config->discoverySvcHosts = discoverySvcHosts;  // Specify the discovery server(s) node urls
    config->discoverySvcHostsCount = 1;             // Amount of discovery server(s)

    config->advertiseHosts = malloc(sizeof(char*)); // Specify the IP to connect back to SAPI
    *config->advertiseHosts = "10.10.0.3";
    config->advertiseHostsCount = 1;

    SAPIRetryPolicy *rp = malloc(sizeof(SAPIRetryPolicy));
    rp->maxAttempts = 10;                           // Attempts 
    rp->retryInterval = 1000;                       // Retry interval
    rp->retryScaling = 1.5f;                        // Scales up the retry interval after each attempt
    rp->timeout = 20000;                            // Timeout in milliseconds

    config->retryPolicy = rp;                      // Assign the retry policy to the connection options

    sapiRegisterLogHandler(logMessage);             // Register log handler

    KxsResponse resp;


    KxsHandle kxsHandle = sapiConnect(config, &resp);   // Attempt the connection process

    if (resp.rc != 0)
    {
        printf("Unexpected RC response, src=%s rc=%d ac=%d", __func__, resp.rc, resp.ac);
        kxsHandle = NULL;

        return 1;
    }

    for (int i = 0; i < 10; i++)
    {
        createDict(kxsHandle); // Creates a c.Dict that will be sent to the server and displays the result
        createFlip(kxsHandle); // Creates a c.Flip that will be sent to the server and displays the result
    }

    sapiDisconnect(kxsHandle);

    return 0;
}

void createDict(KxsHandle *handle)
{
    K keys = ktn(KS, 0);
    K vals = ktn(0, 0);

    keys = js(&keys, ss("query"));
    vals = jk(&vals, kp("(`a`b`c)!(1 2 3)"));

    K request = xD(keys, vals);

    KxsResponse response;

    bool result = sapiExecute(handle, ".kxs.execute", request, NULL, true, NULL, &response);

    if (result && response.rc == 0)
    {
        K headers = kK(response.payload)[0];
        K values = kK(response.payload)[1];

        for (int i = 0; i < headers->n; i++)
        {
            char buf[100];
#ifdef _WIN32
            sprintf_s(buf, 100, "name=%s value=%lld", kS(headers)[i], kJ(values)[i]);
#else
            sprintf(buf, "name=%s value=%lld", kS(headers)[i], kJ(values)[i]);
#endif
            logMessage(1, logCategory, buf);
        }
    }
}

void createFlip(KxsHandle *handle)
{
    K keys = ktn(KS, 0);
    K vals = ktn(0, 0);

    keys = js(&keys, ss("query"));
    vals = jk(&vals, kp("10#enlist (`a`b`c)!(1 2 3)"));

    K request = xD(keys, vals);

    KxsResponse response;

    bool result = sapiExecute(handle, ".kxs.execute", request, NULL, true, NULL, &response);

    if (result && response.rc == 0)
    {
        K headers = kK(response.payload->k)[0];
        K values = kK(response.payload->k)[1];

        for (int i = 0; i < headers->n; i++)
        {
            K entries = kK(values)[i];
            for (int j = 0; j < entries->n; j++)
            {
                char buf[100];
#ifdef _WIN32
                sprintf_s(buf, 100, "name=%s value=%lld", kS(headers)[i], kJ(entries)[j]);
#else
                sprintf(buf, "name=%s value=%lld", kS(headers)[i], kJ(entries)[j]);
#endif
                logMessage(1, logCategory, buf);
            }
        }
    }   
}

void logMessage(int level, const char* category, const char* message)
{
    printf("%d [%s] %s\n", level, category, message);
}