Skip to content

Add support for non empty domain by name [ch1876] #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/main/java/io/tiledb/java/api/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,65 @@ public HashMap<String, Pair> nonEmptyDomain() throws TileDBError {
return ret;
}

/**
* Given a dimension's index, return the bounding coordinates for that dimension.
*
* @param index THe dimension's index
* @return A Pair that contains the dimension's bounds
* @exception TileDBError A TileDB exception
*/
public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
checkIsOpen();
Pair p;
try (Domain domain = schema.getDomain();
NativeArray domainArray =
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {

SWIGTYPE_p_int emptyp = tiledb.new_intp();
try {
ctx.handleError(
tiledb.tiledb_array_get_non_empty_domain_from_index(
ctx.getCtxp(), arrayp, index, domainArray.toVoidPointer(), emptyp));
if (tiledb.intp_value(emptyp) == 1) {
System.out.println("Returning empty pair");
return Pair.empty();
}
} finally {
tiledb.delete_intp(emptyp);
}

return new Pair(domainArray.getItem(0), domainArray.getItem(1));
}
}

/**
* Given a dimension's name, return the bounding coordinates for that dimension.
*
* @param name THe dimension's name
* @return A Pair that contains the dimension's bounds
* @exception TileDBError A TileDB exception
*/
public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
checkIsOpen();
HashMap<String, Pair> ret = new HashMap<String, Pair>();
try (Domain domain = schema.getDomain();
NativeArray domainArray =
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
SWIGTYPE_p_int emptyp = tiledb.new_intp();
try {
ctx.handleError(
tiledb.tiledb_array_get_non_empty_domain_from_name(
ctx.getCtxp(), arrayp, name, domainArray.toVoidPointer(), emptyp));
if (tiledb.intp_value(emptyp) == 1) {
return Pair.empty();
}
} finally {
tiledb.delete_intp(emptyp);
}
return new Pair(domainArray.getItem(0), domainArray.getItem(1));
}
}

/**
* Compute an upper bound on the buffer elements needed to read a subarray.
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/tiledb/java/api/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Pair<F, S> implements java.io.Serializable {
private F first;
private S second;

private Pair() {}

public Pair(F first, S second) {
this.first = first;
this.second = second;
Expand All @@ -48,4 +50,13 @@ public S getSecond() {
public void setSecond(S second) {
this.second = second;
}

/**
* Returns an empty Pair
*
* @return The Pair
*/
public static Pair empty() {
return new Pair();
}
}
38 changes: 38 additions & 0 deletions src/test/java/io/tiledb/java/api/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,42 @@ public void testArrayOpenAtEncrypted() throws Exception {
assert Arrays.equals(readArrayAtEncrypted(BigInteger.valueOf(ts_b)), array_b);
assert Arrays.equals(readArrayAtEncrypted(BigInteger.valueOf(ts_c)), array_c);
}

@Test
public void testArraygetNonEmptyDomainFromIndex() throws Exception {
Array.create(arrayURI, schemaCreate());

long[] array_a = new long[] {1, 2, 3, 6};
insertArbitraryValues(new NativeArray(ctx, array_a, Long.class));

Array array = new Array(ctx, arrayURI, TILEDB_READ);

Assert.assertEquals(1L, array.getNonEmptyDomainFromIndex(0).getFirst());
Assert.assertEquals(4L, array.getNonEmptyDomainFromIndex(0).getSecond());

try {
array.getNonEmptyDomainFromIndex(1);
Assert.fail();
} catch (TileDBError error) {
}
}

@Test
public void testArraygetNonEmptyDomainFromName() throws Exception {
Array.create(arrayURI, schemaCreate());

long[] array_a = new long[] {1, 2, 3, 6};
insertArbitraryValues(new NativeArray(ctx, array_a, Long.class));

Array array = new Array(ctx, arrayURI, TILEDB_READ);

Assert.assertEquals(1L, array.getNonEmptyDomainFromName("d1").getFirst());
Assert.assertEquals(4L, array.getNonEmptyDomainFromName("d1").getSecond());

try {
array.getNonEmptyDomainFromName("d2");
Assert.fail();
} catch (TileDBError error) {
}
}
}