Skip to content

Commit ecc6878

Browse files
authored
Merge pull request #151 from TileDB-Inc/victorgiannakouris/ch1876/add-support-for-non-empty-domain-by-name
Add support for non empty domain by name [ch1876]
2 parents 10f93cb + 3f7850b commit ecc6878

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/main/java/io/tiledb/java/api/Array.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,65 @@ public HashMap<String, Pair> nonEmptyDomain() throws TileDBError {
481481
return ret;
482482
}
483483

484+
/**
485+
* Given a dimension's index, return the bounding coordinates for that dimension.
486+
*
487+
* @param index THe dimension's index
488+
* @return A Pair that contains the dimension's bounds
489+
* @exception TileDBError A TileDB exception
490+
*/
491+
public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
492+
checkIsOpen();
493+
Pair p;
494+
try (Domain domain = schema.getDomain();
495+
NativeArray domainArray =
496+
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
497+
498+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
499+
try {
500+
ctx.handleError(
501+
tiledb.tiledb_array_get_non_empty_domain_from_index(
502+
ctx.getCtxp(), arrayp, index, domainArray.toVoidPointer(), emptyp));
503+
if (tiledb.intp_value(emptyp) == 1) {
504+
System.out.println("Returning empty pair");
505+
return Pair.empty();
506+
}
507+
} finally {
508+
tiledb.delete_intp(emptyp);
509+
}
510+
511+
return new Pair(domainArray.getItem(0), domainArray.getItem(1));
512+
}
513+
}
514+
515+
/**
516+
* Given a dimension's name, return the bounding coordinates for that dimension.
517+
*
518+
* @param name THe dimension's name
519+
* @return A Pair that contains the dimension's bounds
520+
* @exception TileDBError A TileDB exception
521+
*/
522+
public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
523+
checkIsOpen();
524+
HashMap<String, Pair> ret = new HashMap<String, Pair>();
525+
try (Domain domain = schema.getDomain();
526+
NativeArray domainArray =
527+
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
528+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
529+
try {
530+
ctx.handleError(
531+
tiledb.tiledb_array_get_non_empty_domain_from_name(
532+
ctx.getCtxp(), arrayp, name, domainArray.toVoidPointer(), emptyp));
533+
if (tiledb.intp_value(emptyp) == 1) {
534+
return Pair.empty();
535+
}
536+
} finally {
537+
tiledb.delete_intp(emptyp);
538+
}
539+
return new Pair(domainArray.getItem(0), domainArray.getItem(1));
540+
}
541+
}
542+
484543
/**
485544
* Compute an upper bound on the buffer elements needed to read a subarray.
486545
*

src/main/java/io/tiledb/java/api/Pair.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class Pair<F, S> implements java.io.Serializable {
2828
private F first;
2929
private S second;
3030

31+
private Pair() {}
32+
3133
public Pair(F first, S second) {
3234
this.first = first;
3335
this.second = second;
@@ -48,4 +50,13 @@ public S getSecond() {
4850
public void setSecond(S second) {
4951
this.second = second;
5052
}
53+
54+
/**
55+
* Returns an empty Pair
56+
*
57+
* @return The Pair
58+
*/
59+
public static Pair empty() {
60+
return new Pair();
61+
}
5162
}

src/test/java/io/tiledb/java/api/ArrayTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,42 @@ public void testArrayOpenAtEncrypted() throws Exception {
209209
assert Arrays.equals(readArrayAtEncrypted(BigInteger.valueOf(ts_b)), array_b);
210210
assert Arrays.equals(readArrayAtEncrypted(BigInteger.valueOf(ts_c)), array_c);
211211
}
212+
213+
@Test
214+
public void testArraygetNonEmptyDomainFromIndex() throws Exception {
215+
Array.create(arrayURI, schemaCreate());
216+
217+
long[] array_a = new long[] {1, 2, 3, 6};
218+
insertArbitraryValues(new NativeArray(ctx, array_a, Long.class));
219+
220+
Array array = new Array(ctx, arrayURI, TILEDB_READ);
221+
222+
Assert.assertEquals(1L, array.getNonEmptyDomainFromIndex(0).getFirst());
223+
Assert.assertEquals(4L, array.getNonEmptyDomainFromIndex(0).getSecond());
224+
225+
try {
226+
array.getNonEmptyDomainFromIndex(1);
227+
Assert.fail();
228+
} catch (TileDBError error) {
229+
}
230+
}
231+
232+
@Test
233+
public void testArraygetNonEmptyDomainFromName() throws Exception {
234+
Array.create(arrayURI, schemaCreate());
235+
236+
long[] array_a = new long[] {1, 2, 3, 6};
237+
insertArbitraryValues(new NativeArray(ctx, array_a, Long.class));
238+
239+
Array array = new Array(ctx, arrayURI, TILEDB_READ);
240+
241+
Assert.assertEquals(1L, array.getNonEmptyDomainFromName("d1").getFirst());
242+
Assert.assertEquals(4L, array.getNonEmptyDomainFromName("d1").getSecond());
243+
244+
try {
245+
array.getNonEmptyDomainFromName("d2");
246+
Assert.fail();
247+
} catch (TileDBError error) {
248+
}
249+
}
212250
}

0 commit comments

Comments
 (0)