Skip to content

Commit 26ca73d

Browse files
authored
Merge pull request #161 from TileDB-Inc/victorgiannakouris/ch1877/add-support-for-non-empty-domain-var-by-name
Add support for non empty domain var by name [ch1877]
2 parents a9a9e47 + 78541dc commit 26ca73d

File tree

6 files changed

+288
-61
lines changed

6 files changed

+288
-61
lines changed

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

Lines changed: 124 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,46 +456,37 @@ 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 {
461+
Domain domain = schema.getDomain();
462+
long numDims = domain.getNDim();
463+
for (long dimIdx = 0; dimIdx < numDims; ++dimIdx) {
464+
Dimension dimension = domain.getDimension(dimIdx);
465+
Pair p = getNonEmptyDomainFromIndex(dimIdx);
466+
ret.put(dimension.getName(), p);
480467
}
468+
} catch (TileDBError error) {
469+
throw error;
481470
}
482471
return ret;
483472
}
484473

485474
/**
486-
* Given a dimension's index, return the bounding coordinates for that dimension.
475+
* Given a dimension's index, return the bounding coordinates for that dimension. The method
476+
* checks if the dimension is var-sized or not, and it works for both cases.
487477
*
488478
* @param index THe dimension's index
489479
* @return A Pair that contains the dimension's bounds
490480
* @exception TileDBError A TileDB exception
491481
*/
492482
public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
493483
checkIsOpen();
494-
Pair p;
495484
try (Domain domain = schema.getDomain();
496485
NativeArray domainArray =
497486
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
498487

488+
if (domain.getDimension(index).isVar()) return getNonEmptyDomainVarFromIndex(index);
489+
499490
SWIGTYPE_p_int emptyp = tiledb.new_intp();
500491
try {
501492
ctx.handleError(
@@ -514,7 +505,8 @@ public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
514505
}
515506

516507
/**
517-
* Given a dimension's name, return the bounding coordinates for that dimension.
508+
* Given a dimension's name, return the bounding coordinates for that dimension. The method checks
509+
* if the dimension is var-sized or not, and it works for both cases.
518510
*
519511
* @param name THe dimension's name
520512
* @return A Pair that contains the dimension's bounds
@@ -525,6 +517,9 @@ public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
525517
try (Domain domain = schema.getDomain();
526518
NativeArray domainArray =
527519
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
520+
521+
if (domain.getDimension(name).isVar()) return this.getNonEmptyDomainVarFromName(name);
522+
528523
SWIGTYPE_p_int emptyp = tiledb.new_intp();
529524
try {
530525
ctx.handleError(
@@ -540,6 +535,111 @@ public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
540535
}
541536
}
542537

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

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)