Skip to content

Commit d99068d

Browse files
committed
Add support for non empty domain var by name [ch1877]
1 parent a9a9e47 commit d99068d

File tree

6 files changed

+287
-61
lines changed

6 files changed

+287
-61
lines changed

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

Lines changed: 123 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,46 +456,36 @@ public static void create(
456456
*/
457457
public HashMap<String, Pair> nonEmptyDomain() throws TileDBError {
458458
checkIsOpen();
459-
HashMap<String, Pair> ret = new HashMap<String, Pair>();
460-
try (Domain domain = schema.getDomain();
461-
NativeArray domainArray =
462-
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
463-
SWIGTYPE_p_int emptyp = tiledb.new_intp();
464-
try {
465-
ctx.handleError(
466-
tiledb.tiledb_array_get_non_empty_domain(
467-
ctx.getCtxp(), arrayp, domainArray.toVoidPointer(), emptyp));
468-
if (tiledb.intp_value(emptyp) == 1) {
469-
return ret;
470-
}
471-
} finally {
472-
tiledb.delete_intp(emptyp);
473-
}
474-
for (int i = 0; i < domain.getRank(); i++) {
475-
try (Dimension d = domain.getDimension(i)) {
476-
ret.put(
477-
d.getName(),
478-
new Pair(domainArray.getItem((2 * i) + 0), domainArray.getItem((2 * i) + 1)));
479-
}
459+
HashMap<String, Pair> ret = new HashMap<>();
460+
try (Domain domain = schema.getDomain()) {
461+
long numDims = domain.getNDim();
462+
for (long dimIdx = 0; dimIdx < numDims; ++dimIdx) {
463+
Dimension dimension = domain.getDimension(dimIdx);
464+
Pair p = getNonEmptyDomainFromIndex(dimIdx);
465+
ret.put(dimension.getName(), p);
480466
}
467+
} catch (TileDBError error) {
468+
throw error;
481469
}
482470
return ret;
483471
}
484472

485473
/**
486-
* Given a dimension's index, return the bounding coordinates for that dimension.
474+
* Given a dimension's index, return the bounding coordinates for that dimension. The method
475+
* checks if the dimension is var-sized or not, and it works for both cases.
487476
*
488477
* @param index THe dimension's index
489478
* @return A Pair that contains the dimension's bounds
490479
* @exception TileDBError A TileDB exception
491480
*/
492481
public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
493482
checkIsOpen();
494-
Pair p;
495483
try (Domain domain = schema.getDomain();
496484
NativeArray domainArray =
497485
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
498486

487+
if (domain.getDimension(index).isVar()) return getNonEmptyDomainVarFromIndex(index);
488+
499489
SWIGTYPE_p_int emptyp = tiledb.new_intp();
500490
try {
501491
ctx.handleError(
@@ -514,7 +504,8 @@ public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
514504
}
515505

516506
/**
517-
* Given a dimension's name, return the bounding coordinates for that dimension.
507+
* Given a dimension's name, return the bounding coordinates for that dimension. The method checks
508+
* if the dimension is var-sized or not, and it works for both cases.
518509
*
519510
* @param name THe dimension's name
520511
* @return A Pair that contains the dimension's bounds
@@ -525,6 +516,9 @@ public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
525516
try (Domain domain = schema.getDomain();
526517
NativeArray domainArray =
527518
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
519+
520+
if (domain.getDimension(name).isVar()) return this.getNonEmptyDomainVarFromName(name);
521+
528522
SWIGTYPE_p_int emptyp = tiledb.new_intp();
529523
try {
530524
ctx.handleError(
@@ -540,6 +534,111 @@ public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
540534
}
541535
}
542536

537+
/**
538+
* Retrieves the non-empty domain range sizes from an array for a given dimension index. This is
539+
* the union of the non-empty domains of the array fragments on the given dimension. Applicable
540+
* only to var-sized dimensions.
541+
*
542+
* @param index The dimension index
543+
* @return The non-empty domain range sizes
544+
* @throws TileDBError A TileDB exception
545+
*/
546+
public Pair<BigInteger, BigInteger> getNonEmptyDomainVarSizeFromIndex(long index)
547+
throws TileDBError {
548+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
549+
SWIGTYPE_p_unsigned_long_long startSize = tiledb.new_ullp();
550+
SWIGTYPE_p_unsigned_long_long endSize = tiledb.new_ullp();
551+
552+
ctx.handleError(
553+
tiledb.tiledb_array_get_non_empty_domain_var_size_from_index(
554+
ctx.getCtxp(), arrayp, index, startSize, endSize, emptyp));
555+
556+
return new Pair(tiledb.ullp_value(startSize), tiledb.ullp_value(endSize));
557+
}
558+
559+
/**
560+
* Retrieves the non-empty domain range sizes from an array for a given dimension name. This is
561+
* the union of the non-empty domains of the array fragments on the given dimension. Applicable
562+
* only to var-sized dimensions.
563+
*
564+
* @param name The dimension name
565+
* @return The non-empty domain range sizes
566+
* @throws TileDBError A TileDB exception
567+
*/
568+
public Pair<BigInteger, BigInteger> getNonEmptyDomainVarSizeFromName(String name)
569+
throws TileDBError {
570+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
571+
SWIGTYPE_p_unsigned_long_long startSize = tiledb.new_ullp();
572+
SWIGTYPE_p_unsigned_long_long endSize = tiledb.new_ullp();
573+
574+
ctx.handleError(
575+
tiledb.tiledb_array_get_non_empty_domain_var_size_from_name(
576+
ctx.getCtxp(), arrayp, name, startSize, endSize, emptyp));
577+
578+
return new Pair(tiledb.ullp_value(startSize), tiledb.ullp_value(endSize));
579+
}
580+
581+
/**
582+
* Retrieves the non-empty domain from an array for a given dimension index. This is the union of
583+
* the non-empty domains of the array fragments on the given dimension. Applicable only to
584+
* var-sized dimensions.
585+
*
586+
* @param index The dimension index
587+
* @return The non-empty domain
588+
* @throws TileDBError A TileDB exception
589+
*/
590+
public Pair<String, String> getNonEmptyDomainVarFromIndex(long index) throws TileDBError {
591+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
592+
593+
Dimension dim = this.schema.getDomain().getDimension(index);
594+
Pair<BigInteger, BigInteger> size = this.getNonEmptyDomainVarSizeFromIndex(index);
595+
596+
Datatype dimType = dim.getType();
597+
int startSize = size.getFirst().intValue();
598+
int endSize = size.getSecond().intValue();
599+
600+
NativeArray start = new NativeArray(ctx, startSize, dimType);
601+
NativeArray end = new NativeArray(ctx, endSize, dimType);
602+
603+
ctx.handleError(
604+
tiledb.tiledb_array_get_non_empty_domain_var_from_index(
605+
ctx.getCtxp(), arrayp, index, start.toVoidPointer(), end.toVoidPointer(), emptyp));
606+
607+
return new Pair(
608+
new String((byte[]) start.toJavaArray()), new String((byte[]) end.toJavaArray()));
609+
}
610+
611+
/**
612+
* Retrieves the non-empty domain from an array for a given dimension name. This is the union of
613+
* the non-empty domains of the array fragments on the given dimension. Applicable only to
614+
* var-sized dimensions.
615+
*
616+
* @param name The dimension name
617+
* @return The non-empty domain
618+
* @throws TileDBError A TileDB exception
619+
*/
620+
public Pair<String, String> getNonEmptyDomainVarFromName(String name) throws TileDBError {
621+
SWIGTYPE_p_int emptyp = tiledb.new_intp();
622+
623+
Dimension dim = this.schema.getDomain().getDimension(name);
624+
625+
Pair<BigInteger, BigInteger> size = this.getNonEmptyDomainVarSizeFromName(name);
626+
627+
Datatype dimType = dim.getType();
628+
int startSize = size.getFirst().intValue();
629+
int endSize = size.getSecond().intValue();
630+
631+
NativeArray start = new NativeArray(ctx, startSize, dimType);
632+
NativeArray end = new NativeArray(ctx, endSize, dimType);
633+
634+
ctx.handleError(
635+
tiledb.tiledb_array_get_non_empty_domain_var_from_name(
636+
ctx.getCtxp(), arrayp, name, start.toVoidPointer(), end.toVoidPointer(), emptyp));
637+
638+
return new Pair(
639+
new String((byte[]) start.toJavaArray()), new String((byte[]) end.toJavaArray()));
640+
}
641+
543642
/**
544643
* Compute an upper bound on the buffer elements needed to read a subarray.
545644
*

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public Dimension<T> setFilterList(FilterList filters) throws TileDBError {
260260
* Sets the number of values per cell for the dimension.
261261
*
262262
* @param cellValNum The number of values per cell
263-
* @throws TileDBError
263+
* @throws TileDBError TileDBError A TileDB error
264264
*/
265265
public void setCellValNum(long cellValNum) throws TileDBError {
266266
try {
@@ -275,7 +275,7 @@ public void setCellValNum(long cellValNum) throws TileDBError {
275275
* Retrieves the number of values per cell for the dimension.
276276
*
277277
* @return The number of values per cell
278-
* @throws TileDBError
278+
* @throws TileDBError TileDBError A TileDB error
279279
*/
280280
public long getCellValNum() throws TileDBError {
281281
SWIGTYPE_p_unsigned_int uint = tiledb.new_uintp();
@@ -288,6 +288,16 @@ public long getCellValNum() throws TileDBError {
288288
}
289289
}
290290

291+
/**
292+
* Checks whether the dimension is var-sized
293+
*
294+
* @return True if the dimension is var-sized (e.g. String) and False otherwise
295+
* @throws TileDBError A TileDB error
296+
*/
297+
public boolean isVar() throws TileDBError {
298+
return this.getCellValNum() == Constants.TILEDB_VAR_NUM;
299+
}
300+
291301
/**
292302
* @return A string representation of the extent.
293303
* @throws TileDBError A TileDB exception
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.tiledb.java.api;
2+
3+
import java.util.Arrays;
4+
5+
/** Contains helper-functions */
6+
public class Util {
7+
/**
8+
* Converts an input array of bytes to a list of Strings, according to the offsets
9+
*
10+
* @param offsets The offsets array
11+
* @param data THe data array
12+
* @return The list of Strings
13+
*/
14+
public static String[] bytesToStrings(long[] offsets, byte[] data) {
15+
String[] results = new String[offsets.length];
16+
int start = 0, end;
17+
18+
// Convert bytes to string array
19+
for (int i = 0; i < offsets.length; ++i) {
20+
if (i < offsets.length - 1) {
21+
end = (int) offsets[i + 1];
22+
results[i] = new String(Arrays.copyOfRange(data, start, end));
23+
start = end;
24+
} else {
25+
end = data.length;
26+
results[i] = new String(Arrays.copyOfRange(data, start, end));
27+
}
28+
}
29+
30+
return results;
31+
}
32+
}

0 commit comments

Comments
 (0)