Skip to content

Commit

Permalink
Almost working document support in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Mast committed Feb 22, 2015
1 parent 073a718 commit 6deae06
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 93 deletions.
7 changes: 7 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ EXTRA_DIST += bindings/java.py
EXTRA_DIST += bindings/java/org/hyperdex/client/ByteString.java
EXTRA_DIST += bindings/java/org_hyperdex_client_Client.definitions.c
EXTRA_DIST += bindings/java/org/hyperdex/client/Client.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Document.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Deferred.java
EXTRA_DIST += bindings/java/org/hyperdex/client/GreaterEqual.java
EXTRA_DIST += bindings/java/org/hyperdex/client/GreaterThan.java
Expand All @@ -634,6 +635,7 @@ EXTRA_DIST += bindings/java/org/hyperdex/client/LengthGreaterEqual.java
EXTRA_DIST += bindings/java/org/hyperdex/client/LengthLessEqual.java
EXTRA_DIST += bindings/java/org/hyperdex/client/LessEqual.java
EXTRA_DIST += bindings/java/org/hyperdex/client/LessThan.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Microtransaction.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Operation.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Predicate.java
EXTRA_DIST += bindings/java/org/hyperdex/client/Range.java
Expand Down Expand Up @@ -881,6 +883,7 @@ java_wrappers =
java_wrappers += test/sh/bindings.java.BasicSearch.sh
java_wrappers += test/sh/bindings.java.Basic.sh
java_wrappers += test/sh/bindings.java.CondPut.sh
java_wrappers += test/sh/bindings.java.DataTypeDocument.sh
java_wrappers += test/sh/bindings.java.DataTypeFloat.sh
java_wrappers += test/sh/bindings.java.DataTypeInt.sh
java_wrappers += test/sh/bindings.java.DataTypeListFloat.sh
Expand All @@ -900,6 +903,7 @@ java_wrappers += test/sh/bindings.java.DataTypeSetInt.sh
java_wrappers += test/sh/bindings.java.DataTypeSetString.sh
java_wrappers += test/sh/bindings.java.DataTypeString.sh
java_wrappers += test/sh/bindings.java.LengthString.sh
java_wrappers += test/sh/bindings.java.Microtransactions.sh
java_wrappers += test/sh/bindings.java.MultiAttribute.sh
java_wrappers += test/sh/bindings.java.RangeSearchInt.sh
java_wrappers += test/sh/bindings.java.RangeSearchString.sh
Expand Down Expand Up @@ -1026,13 +1030,15 @@ EXTRA_DIST += test/python/GroupAtomic.py
EXTRA_DIST += test/python/HyperMongo.py
EXTRA_DIST += test/python/LengthString.py
EXTRA_DIST += test/python/MultiAttribute.py
EXTRA_DIST += test/python/Microtransactions.py
EXTRA_DIST += test/python/RangeSearchInt.py
EXTRA_DIST += test/python/RangeSearchString.py
EXTRA_DIST += test/python/RegexSearch.py
EXTRA_DIST += test/python/testlib.py
EXTRA_DIST += test/java/Basic.java
EXTRA_DIST += test/java/BasicSearch.java
EXTRA_DIST += test/java/CondPut.java
EXTRA_DIST += test/java/DataTypeDocument.java
EXTRA_DIST += test/java/DataTypeFloat.java
EXTRA_DIST += test/java/DataTypeInt.java
EXTRA_DIST += test/java/DataTypeListFloat.java
Expand All @@ -1052,6 +1058,7 @@ EXTRA_DIST += test/java/DataTypeSetInt.java
EXTRA_DIST += test/java/DataTypeSetString.java
EXTRA_DIST += test/java/DataTypeString.java
EXTRA_DIST += test/java/LengthString.java
EXTRA_DIST += test/java/Microtransactions.java
EXTRA_DIST += test/java/MultiAttribute.java
EXTRA_DIST += test/java/RangeSearchInt.java
EXTRA_DIST += test/java/RangeSearchString.java
Expand Down
2 changes: 1 addition & 1 deletion bindings/java.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2014, Cornell University
# Copyright (c) 2013-2015, Cornell University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion bindings/java/org/hyperdex/client/ByteString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (c) 2011-2013, Nick Tolomiczenko
* Copyright (c) 2013, Cornell University
* Copyright (c) 2013-2015, Cornell University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down
40 changes: 38 additions & 2 deletions bindings/java/org_hyperdex_client_Client.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ static jclass _byte_string;
static jmethodID _byte_string_init;
static jmethodID _byte_string_get;

static jclass _document;
static jmethodID _document_init;
static jmethodID _document_to_string;

static jclass _boolean;
static jmethodID _boolean_init;

Expand Down Expand Up @@ -166,6 +170,10 @@ Java_org_hyperdex_client_Client_initialize(JNIEnv* env, jclass client)

/* cache class String */
REF(_string, (*env)->FindClass(env, "java/lang/String"));
/* cache class Document */
REF(_document, (*env)->FindClass(env, "org/hyperdex/client/Document"));
_document_init = (*env)->GetMethodID(env, _document, "<init>", "(Ljava/lang/String;)V");
_document_to_string = (*env)->GetMethodID(env, _document, "toString", "()Ljava/lang/String;");
/* cache class ByteString */
REF(_byte_string, (*env)->FindClass(env, "org/hyperdex/client/ByteString"));
_byte_string_init = (*env)->GetMethodID(env, _byte_string, "<init>", "([B)V");
Expand Down Expand Up @@ -265,6 +273,9 @@ Java_org_hyperdex_client_Client_initialize(JNIEnv* env, jclass client)
_pred_length_greater_equal_x = (*env)->GetFieldID(env, _pred_length_greater_equal, "x", "Ljava/lang/Object;");

CHECK_CACHE(_string);
CHECK_CACHE(_document);
CHECK_CACHE(_document_init);
CHECK_CACHE(_document_to_string);
CHECK_CACHE(_byte_string);
CHECK_CACHE(_byte_string_init);
CHECK_CACHE(_byte_string_get);
Expand Down Expand Up @@ -344,6 +355,7 @@ Java_org_hyperdex_client_Client_initialize(JNIEnv* env, jclass client)
JNIEXPORT HYPERDEX_API void JNICALL
Java_org_hyperdex_client_Client_terminate(JNIEnv* env, jclass client)
{
(*env)->DeleteGlobalRef(env, _document);
(*env)->DeleteGlobalRef(env, _string);
(*env)->DeleteGlobalRef(env, _byte_string);
(*env)->DeleteGlobalRef(env, _boolean);
Expand Down Expand Up @@ -496,6 +508,7 @@ typedef int (*elem_float_fptr)(void*, double, enum hyperdex_ds_returncode*);
return -1; \
}

/* Convert a single elment of a list, map, or set*/
static int
hyperdex_java_client_convert_elem(JNIEnv* env,
jobject obj,
Expand Down Expand Up @@ -784,6 +797,27 @@ hyperdex_java_client_convert_type(JNIEnv* env,
*datatype = HYPERDATATYPE_GENERIC;
return 0;
}
else if ((*env)->IsInstanceOf(env, x, _document) == JNI_TRUE)
{
x = (*env)->CallObjectMethod(env, x, _document_to_string);
tmp_str = (*env)->GetStringUTFChars(env, x, 0);
ERROR_CHECK(-1);
tmp_str_sz = (*env)->GetStringUTFLength(env, x);
ERROR_CHECK(-1);
success = hyperdex_ds_copy_string(arena, tmp_str, tmp_str_sz,
&error, value, value_sz);
(*env)->ReleaseStringUTFChars(env, x, tmp_str);
ERROR_CHECK(-1);
*datatype = HYPERDATATYPE_DOCUMENT;

if (success < 0)
{
hyperdex_java_out_of_memory(env);
return -1;
}

return 0;
}
else if ((*env)->IsInstanceOf(env, x, _string) == JNI_TRUE)
{
tmp_str = (*env)->GetStringUTFChars(env, x, 0);
Expand Down Expand Up @@ -1449,8 +1483,10 @@ hyperdex_java_client_build_attribute(JNIEnv* env,
BUILD_FLOAT(tmp, tmp_d);
return tmp;
case HYPERDATATYPE_DOCUMENT:
hyperdex_java_client_throw_exception(env, HYPERDEX_CLIENT_WRONGTYPE, "Java bindings do not support JSON objects");
return 0;
BUILD_STRING(tmp2, attr->value, attr->value_sz);
// Build document from string
tmp = (*env)->NewObject(env, _document, _document_init, tmp2);
return tmp;
case HYPERDATATYPE_LIST_STRING:
hyperdex_ds_iterator_init(&iter, attr->datatype, attr->value, attr->value_sz);
ret = (*env)->NewObject(env, _array_list, _array_list_init);
Expand Down
50 changes: 34 additions & 16 deletions test/java/DataTypeDocument.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import java.util.*;

import org.hyperdex.client.Client;
import org.hyperdex.client.ByteString;
import org.hyperdex.client.Document;
import org.hyperdex.client.HyperDexClientException;
import org.hyperdex.client.Iterator;
import org.hyperdex.client.LessEqual;
import org.hyperdex.client.GreaterEqual;
import org.hyperdex.client.LessThan;
import org.hyperdex.client.GreaterThan;
import org.hyperdex.client.Range;
import org.hyperdex.client.Regex;
import org.hyperdex.client.LengthEquals;
import org.hyperdex.client.LengthLessEqual;
import org.hyperdex.client.LengthGreaterEqual;

import org.junit.*;
import static org.junit.Assert.* ;

public class DataTypeListString
public class DataTypeDocument
{
private Client c;

@Before
public void setUp() throws Exception {
c = new Client(args[0], Integer.parseInt(args[1]));
public void setUpHyperdexClient() throws Exception {
c = new Client(System.getProperty("hyperdex_host"),
Integer.parseInt(System.getProperty("hyperdex_port")));
}

@After
public void destroyHyperdexClient() {
c = null;
}

@Test
public void insertEmptyValue() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();

Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", new Document("{}"));
assertEquals(expected, get);
}

@Test
public static void insertDocument()
{
public void insertEmptyDocument() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", new Document("{}"));

Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", new Document("{}"));
assertEquals(expected, get);
}
}

124 changes: 64 additions & 60 deletions test/java/DataTypeInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,94 +22,98 @@ public class DataTypeInt
private Client c;

@Before
public void setUp() throws Exception {
public void setUpHyperdexClient() throws Exception {
c = new Client(System.getProperty("hyperdex_host"),
Integer.parseInt(System.getProperty("hyperdex_port")));
}

@After
public void destroyHyperdexClient() {
c = null;
}

@Test
public void testInsertEmpty() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
Object obj = c.put("kv", "k", attrs);
assertNotNull(obj);
Boolean bool = (Boolean)obj;
assertTrue(bool);
Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
assertNotNull(get);
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", 0L);

assertEquals(expected, get);
}

@Test
public void testInsertOne() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", 1L);

Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", 0);
expected.put("v", 1L);

assertEquals(expected, get);
}

@Test
public void testInsertZero() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", 1);
attrs.put("v", 0L);

Boolean res = (Boolean)c.put("kv", "k", attrs);
Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
assertNotNull(get);
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", 0L);

assertEquals(expected, get);
}

@Test
public void testNegativeValue() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", -1L);
Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", 1);
expected.put("v", -1L);

assertEquals(expected, get);
}

@Test
public void testInteger() throws HyperDexClientException {
Map<String, Object> attrs4 = new HashMap<String, Object>();
attrs4.put("v", -1);
Object obj4 = c.put("kv", "k", attrs4);
assert(obj4 != null);
Boolean bool4 = (Boolean)obj4;
assert(bool4 == true);
Map<String, Object> get5 = c.get("kv", "k");
assert(get5 != null);
Map<String, Object> expected5 = new HashMap<String, Object>();
expected5.put("v", -1);
get5.entrySet().containsAll(expected5.entrySet());
expected5.entrySet().containsAll(get5.entrySet());
Map<String, Object> attrs6 = new HashMap<String, Object>();
attrs6.put("v", 0);
Object obj6 = c.put("kv", "k", attrs6);
assert(obj6 != null);
Boolean bool6 = (Boolean)obj6;
assert(bool6 == true);
Map<String, Object> get7 = c.get("kv", "k");
assert(get7 != null);
Map<String, Object> expected7 = new HashMap<String, Object>();
expected7.put("v", 0);
get7.entrySet().containsAll(expected7.entrySet());
expected7.entrySet().containsAll(get7.entrySet());
Map<String, Object> attrs8 = new HashMap<String, Object>();
attrs8.put("v", 9223372036854775807L);
Object obj8 = c.put("kv", "k", attrs8);
assert(obj8 != null);
Boolean bool8 = (Boolean)obj8;
assert(bool8 == true);
Map<String, Object> get9 = c.get("kv", "k");
assert(get9 != null);
Map<String, Object> expected9 = new HashMap<String, Object>();
expected9.put("v", 9223372036854775807L);
get9.entrySet().containsAll(expected9.entrySet());
expected9.entrySet().containsAll(get9.entrySet());
Map<String, Object> attrs10 = new HashMap<String, Object>();
attrs10.put("v", -9223372036854775808L);
Object obj10 = c.put("kv", "k", attrs10);
assert(obj10 != null);
Boolean bool10 = (Boolean)obj10;
assert(bool10 == true);
Map<String, Object> get11 = c.get("kv", "k");
assert(get11 != null);
Map<String, Object> expected11 = new HashMap<String, Object>();
expected11.put("v", -9223372036854775808L);
get11.entrySet().containsAll(expected11.entrySet());
expected11.entrySet().containsAll(get11.entrySet());
public void testInsertHugePositiveValue() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", 9223372036854775807L);
Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", 9223372036854775807L);

assertEquals(expected, get);
}

@Test
public void testInsertHugeNegativeValue() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", -9223372036854775807L);
Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", -9223372036854775807L);

assertEquals(expected, get);
}
}
Loading

0 comments on commit 6deae06

Please sign in to comment.