Skip to content

Instantly share code, notes, and snippets.

@ltsampros
Created November 25, 2014 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltsampros/1db4059ab6fe01855ed7 to your computer and use it in GitHub Desktop.
Save ltsampros/1db4059ab6fe01855ed7 to your computer and use it in GitHub Desktop.
Modified query/simple/src/main/example.c
/*******************************************************************************
* Copyright 2008-2013 by Aerospike.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
//==========================================================
// Includes
//
#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"
//==========================================================
// Constants
//
const char TEST_INDEX_NAME[] = "timestamp";
//==========================================================
// Forward Declarations
//
bool query_cb(const as_val* p_val, void* udata);
bool insert_records(aerospike* p_as);
//==========================================================
// SIMPLE QUERY Example
//
int
main(int argc, char* argv[])
{
// Parse command line arguments.
if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {
exit(-1);
}
// Connect to the aerospike database cluster.
aerospike as;
example_connect_to_aerospike(&as);
as_error err;
// Create an as_query object.
as_query query;
as_query_init(&query, "test", "demo2");
// Generate an as_query.where condition. Note that as_query_destroy() takes
// care of destroying all the query's member objects if necessary. However
// using as_query_where_inita() does avoid internal heap usage.
as_query_select_inita(&query, 2);
as_query_select(&query, "stamp");
as_query_select(&query, "originalId");
as_query_where_inita(&query, 1);
as_query_where(&query, "stamp", integer_range(1,9999999999));
// Execute the query. This call blocks - callbacks are made in the scope of
// this call.
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);
exit(-1);
}
LOG("query executed");
as_query_destroy(&query);
LOG("simple query example successfully completed");
return 0;
}
//==========================================================
// Query Callback
//
bool
query_cb(const as_val* p_val, void* udata)
{
if (! p_val) {
LOG("query callback returned null - query is complete");
return true;
}
// The query didn't use a UDF, so the as_val object should be an as_record.
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;
}
//==========================================================
// Helpers
//
bool
insert_records(aerospike* p_as)
{
// Create an as_record object with one (integer value) bin. By using
// as_record_inita(), we won't need to destroy the record if we only set
// bins using as_record_set_int64().
as_record rec;
as_record_inita(&rec, 1);
// Re-using rec, write records into the database such that each record's key
// and (test-bin) value is based on the loop index.
for (uint32_t i = 0; i < g_n_keys; i++) {
as_error err;
// No need to destroy a stack as_key object, if we only use
// as_key_init_int64().
as_key key;
as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);
// In general it's ok to reset a bin value - all as_record_set_... calls
// destroy any previous value.
as_record_set_int64(&rec, "test-bin", (int64_t)i);
// Write a record to the database.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment