Search code examples
caerospikeg-wandatabasenosql

connection gwan with aerospike db in C


Hello.

First I'm sorry for my ita-english.

I want use gwan with aerospike but when run the servlet...problem. I start with this example.c of aerospike. In file example.c I put gwan.h and this is the output ./gwan:

loading
        hello.cs: to use   .cs scripts, install C#..
       hello.lua: to use  .lua scripts, install Lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linking example.c: undefined symbol: g_namespace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To run G-WAN, you must fix the error(s) or remove this Servlet.

Inside example.c:

#include "gwan.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#include <aerospike/aerospike.h>
#include <aerospike/aerospike_key.h>
#include <aerospike/aerospike_query.h>
#include <aerospike/as_error.h>
#include <aerospike/as_key.h>
#include <aerospike/as_query.h>
#include <aerospike/as_record.h>
#include <aerospike/as_status.h>
#include <aerospike/as_val.h>
#include "example_utils.h"
const char TEST_INDEX_NAME[] = "test-bin-index";
bool query_cb(const as_val* p_val, void* udata);
void cleanup(aerospike* p_as);
bool insert_records(aerospike* p_as);
int
main(int argc, char* argv[])
{
if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {
    exit(-1);
}
aerospike as;
example_connect_to_aerospike(&as);
example_remove_test_records(&as);
example_remove_index(&as, TEST_INDEX_NAME);
if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME))
{   
    cleanup(&as);
    exit(-1);
}
if (! insert_records(&as)) {
    cleanup(&as);
    exit(-1);
}
if (! example_read_test_records(&as)) {
    cleanup(&as);
    exit(-1);
}
as_error err;
as_query query;
as_query_init(&query, g_namespace, g_set);
as_query_where_inita(&query, 1);
as_query_where(&query, "test-bin", as_integer_equals(7));
LOG("executing query: where test-bin = 7");
if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL)
        != AEROSPIKE_OK) {
    LOG("aerospike_query_foreach() returned %d - %s", err.code,
            err.message);
    as_query_destroy(&query);
    cleanup(&as);
    exit(-1);
}
LOG("query executed");
as_query_destroy(&query);
cleanup(&as);
LOG("simple query example successfully completed");
return 0;
}
bool
query_cb(const as_val* p_val, void* udata)
{
if (! p_val) {
    LOG("query callback returned null - query is complete");
    return true;
}
as_record* p_rec = as_record_fromval(p_val);
if (! p_rec) {
    LOG("query callback returned non-as_record object");
    return true;
}
LOG("query callback returned record:");
example_dump_record(p_rec);
return true;
}
void
cleanup(aerospike* p_as)
{
example_remove_test_records(p_as);
example_remove_index(p_as, TEST_INDEX_NAME);
example_cleanup(p_as);
}
bool
insert_records(aerospike* p_as)
{
set
as_record rec;
as_record_inita(&rec, 1);
for (uint32_t i = 0; i < g_n_keys; i++) {
    as_error err;
    as_key key;
    as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);
    as_record_set_int64(&rec, "test-bin", (int64_t)i);
    if (aerospike_key_put(p_as, &err, NULL, &key, &rec) !=     AEROSPIKE_OK) {
        LOG("aerospike_key_put() returned %d - %s", err.code, err.message);
        return false;
    }
}

LOG("insert succeeded");

return true;
}

how can connect aerospike with gwan? Thank you


Solution

  • You need to #pragma link your aerospike library, and make sure all your required header files are in the right place. See G-WAN FAQ or read example code in the G-WAN tarball.

    Also, in G-WAN the return code of the main function will be used as HTTP response code, so avoid return -1;.