#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <hyperdex/admin.hpp>
#include <hyperdex/client.hpp>
#include <hyperdex/client.h>
#include <e/endian.h>
using namespace std;
int
main(int argc, const char *argv[])
{
struct hyperdex_client *client = NULL;
struct hyperdex_client_attribute attr;
struct hyperdex_client_attribute_check check;
const struct hyperdex_client_attribute *attrs = NULL;
size_t attrs_sz = 0;
size_t check_len = 0;
uint64_t count;
int64_t op_id;
enum hyperdex_client_returncode op_status;
int64_t loop_id;
enum hyperdex_client_returncode loop_status;
size_t i;
const char *ns = "phonebook";
client = hyperdex_client_create("127.0.0.1", 1982);
check.attr = "first";
check.value = "Jack";
check.value_sz = strlen(check.value);
check.datatype = HYPERDATATYPE_STRING;
check.predicate = HYPERPREDICATE_EQUALS;
op_id = hyperdex_client_search(client, ns, &check, 1, &op_status, &attrs, &attrs_sz);
if (op_id < 0)
{
cerr << "search failed: " << hyperdex_client_error_message(client) << endl;
hyperdex_client_destroy(client);
return EXIT_FAILURE;
}
while (true)
{
loop_id = hyperdex_client_loop(client, -1, &loop_status);
if (loop_id < 0)
{
cerr << hyperdex_client_error_message(client) << endl;
break;
}
assert(op_id == loop_id);
if (loop_status == HYPERDEX_CLIENT_SUCCESS)
{
printf("get done(%d)\n", attrs_sz);
for (i = 0; i < attrs_sz; ++i)
{
switch (attrs[i].datatype)
{
case HYPERDATATYPE_STRING:
printf("got attribute \"%s\" = \"%.*s\"\n", attrs[i].attr, attrs[i].value_sz, attrs[i].value);
break;
case HYPERDATATYPE_INT64:
{
uint64_t num = 0;
e::unpack64le(attrs[i].value, &num);
printf("got attribute \"%s\" = \"%lu\"\n", attrs[i].attr, num);
}
break;
case HYPERDATATYPE_FLOAT:
{
double num = 0;
e::unpackdoublele(attrs[i].value, &num);
printf("got attribute \"%s\" = \"%f\"\n", attrs[i].attr, num);
}
break;
default:
printf("got error attribute\n");
}
}
}
}
hyperdex_client_destroy_attrs(attrs, attrs_sz);
hyperdex_client_destroy(client);
return EXIT_SUCCESS;
--- a/client/util.cc
+++ b/client/util.cc
@@ -88,9 +88,20 @@ hyperdex :: value_to_attributes(const configuration& config,
std::vector<hyperdex_client_attribute> ha;
ha.reserve(sc->attrs_sz);
- char* ret = static_cast<char*>(malloc(sz));
+ char *ret = NULL;
+ if (attrs)
+ {
+ if (*attrs == NULL)
+ {
+ ret = static_cast<char*>(malloc(sz));
+ }
+ else
+ {
+ ret = static_cast<char*>(realloc((void *)*attrs, sz));
+ }
+ }
- if (!ret)
+ if (ret == NULL)
[cnangel@localhost hyperdex-1.8.2]$python
Python 2.7.10 (default, Sep 8 2015, 17:20:17)
[GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hyperdex.client
>>> c = hyperdex.client.Client('127.0.0.1', 1982)
>>> [x for x in c.search('phonebook', {'first': 'Jack'})]
[{'username': 'jsmith2', 'phone': 6075551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': 'jsmith2018', 'phone': 6085551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': 'jsmith3188', 'phone': 6075551024L, 'last': 'Smith', 'first': 'Jack'}]
>>> [x for x in c.sorted_search('phonebook', {'first': 'Jack'})]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hyperdex/client.pyx", line 2408, in hyperdex.client.Client.sorted_search (bindings/python/hyperdex/client.c:65509)
TypeError: sorted_search() takes exactly 5 positional arguments (2 given)
>>> [x for x in c.sorted_search('phonebook', {'first': 'Jack'}, 'first', 2, 'max')]
[{'username': 'jsmith2018', 'phone': 6085551024L, 'last': 'Smith', 'first': 'Jack'}, {'username': '\x00\x08\x00\x00\x00\x00\x00\x00\x00@', 'phone': 35789034766692096L, 'last': '\x00\x00\x05\x00\x00', 'first': '\x00\x00f\x1f'}]
...
the search function, the example code like this: