From 9df364f195febf251037e957e2429bf08207d41b Mon Sep 17 00:00:00 2001 From: gsvic Date: Wed, 27 Jan 2021 22:45:33 +0200 Subject: [PATCH] fill value API additions --- .../java/io/tiledb/java/api/Attribute.java | 78 +++++++++++++++++++ .../io/tiledb/java/api/AttributeTest.java | 54 +++---------- 2 files changed, 90 insertions(+), 42 deletions(-) diff --git a/src/main/java/io/tiledb/java/api/Attribute.java b/src/main/java/io/tiledb/java/api/Attribute.java index b2924bdc..7a5117d2 100644 --- a/src/main/java/io/tiledb/java/api/Attribute.java +++ b/src/main/java/io/tiledb/java/api/Attribute.java @@ -261,6 +261,18 @@ public FilterList getFilterList() throws TileDBError { return filterlist; } + /** + * Sets the default fill value for the input attribute. This value will be used for the input + * attribute whenever querying (1) an empty cell in a dense array, or (2) a non-empty cell (in + * either dense or sparse array) when values on the input attribute are missing (e.g., if the user + * writes a subset of the attributes in a write operation). + * + *

Applicable to var-sized attributes. + * + * @param value The fill value + * @param size The fill value size + * @throws TileDBError + */ public void setFillValue(NativeArray value, BigInteger size) throws TileDBError { try { ctx.handleError( @@ -271,6 +283,44 @@ public void setFillValue(NativeArray value, BigInteger size) throws TileDBError } } + /** + * Sets the default fill value for the input attribute. This value will be used for the input + * attribute whenever querying (1) an empty cell in a dense array, or (2) a non-empty cell (in + * either dense or sparse array) when values on the input attribute are missing (e.g., if the user + * writes a subset of the attributes in a write operation). + * + *

Applicable to var-sized attributes. + * + * @param value The fill value + * @throws TileDBError + */ + public void setFillValue(Object value) throws TileDBError { + NativeArray array = new NativeArray(ctx, this.type.getNativeSize(), this.type.javaClass()); + array.setItem(0, value); + + try { + ctx.handleError( + tiledb.tiledb_attribute_set_fill_value( + ctx.getCtxp(), + attributep, + array.toVoidPointer(), + BigInteger.valueOf(this.type.getNativeSize()))); + } catch (TileDBError err) { + throw err; + } + } + + /** + * Gets the default fill value for the input attribute. This value will be used for the input + * attribute whenever querying (1) an empty cell in a dense array, or (2) a non-empty cell (in + * either dense or sparse array) when values on the input attribute are missing (e.g., if the user + * writes a subset of the attributes in a write operation). + * + *

Applicable to both fixed-sized and var-sized attributes. + * + * @return A pair with the fill value and its size + * @throws TileDBError + */ public Pair getFillValue() throws TileDBError { try (NativeArray value = new NativeArray(ctx, this.type.getNativeSize(), this.type)) { @@ -317,6 +367,34 @@ public void setFillValueNullable(NativeArray value, BigInteger size, boolean val } } + /** + * Sets the default fill value for the input, nullable attribute. This value will be used for the + * input attribute whenever querying (1) an empty cell in a dense array, or (2) a non-empty cell + * (in either dense or sparse array) when values on the input attribute are missing (e.g., if the + * user writes a subset of the attributes in a write operation). + * + * @param value The fill value to set. + * @param valid The validity fill value, zero for a null value and non-zero for a valid attribute. + * @throws TileDBError + */ + public void setFillValueNullable(Object value, boolean valid) throws TileDBError { + + NativeArray valueArray = new NativeArray(ctx, this.type.getNativeSize(), this.type.javaClass()); + valueArray.setItem(0, value); + + try { + ctx.handleError( + tiledb.tiledb_attribute_set_fill_value_nullable( + ctx.getCtxp(), + attributep, + valueArray.toVoidPointer(), + BigInteger.valueOf(this.type.getNativeSize()), + valid ? (short) 1 : (short) 0)); + } catch (TileDBError err) { + throw err; + } + } + /** * Gets the default fill value for the input, nullable attribute. This value will be used for the * input attribute whenever querying (1) an empty cell in a dense array, or (2) a non-empty cell diff --git a/src/test/java/io/tiledb/java/api/AttributeTest.java b/src/test/java/io/tiledb/java/api/AttributeTest.java index 48aacce5..3db19bc5 100644 --- a/src/test/java/io/tiledb/java/api/AttributeTest.java +++ b/src/test/java/io/tiledb/java/api/AttributeTest.java @@ -66,46 +66,31 @@ public void testAttributeSetFillValue() throws Exception { try (Context ctx = new Context(); Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT32)) { - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_INT32); - - arr.setItem(0, 5); - - a.setFillValue(arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize())); + a.setFillValue(5); Assert.assertEquals(5, a.getFillValue().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), - a.getFillValue().getSecond()); + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond()); } try (Context ctx = new Context(); Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_INT64)) { - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_INT64); - - arr.setItem(0, 5L); - - a.setFillValue(arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize())); + a.setFillValue(5L); Assert.assertEquals(5L, a.getFillValue().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), - a.getFillValue().getSecond()); + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond()); } try (Context ctx = new Context(); Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_CHAR)) { - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_CHAR); - - arr.setItem(0, "c"); - - a.setFillValue(arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize())); + a.setFillValue((byte) 'c'); Assert.assertEquals((byte) 'c', a.getFillValue().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), - a.getFillValue().getSecond()); + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond()); } } @@ -116,16 +101,11 @@ public void testAttributeSetFillValueNullable() throws Exception { a.setNullable(true); - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_INT32); - - arr.setItem(0, 5); - - a.setFillValueNullable( - arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), true); + a.setFillValueNullable(5, true); Assert.assertEquals(5, a.getFillValueNullable().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValueNullable().getSecond().getFirst()); Assert.assertEquals(true, a.getFillValueNullable().getSecond().getSecond()); @@ -136,16 +116,11 @@ public void testAttributeSetFillValueNullable() throws Exception { a.setNullable(true); - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_INT64); - - arr.setItem(0, 5L); - - a.setFillValueNullable( - arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), false); + a.setFillValueNullable(5L, false); Assert.assertEquals(5L, a.getFillValueNullable().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValueNullable().getSecond().getFirst()); Assert.assertEquals(false, a.getFillValueNullable().getSecond().getSecond()); @@ -156,16 +131,11 @@ public void testAttributeSetFillValueNullable() throws Exception { a.setNullable(true); - NativeArray arr = new NativeArray(ctx, 1, Datatype.TILEDB_CHAR); - - arr.setItem(0, "c"); - - a.setFillValueNullable( - arr, BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), false); + a.setFillValueNullable((byte) 'c', false); Assert.assertEquals((byte) 'c', a.getFillValueNullable().getFirst()); Assert.assertEquals( - BigInteger.valueOf(arr.getNativeTypeSize() * arr.getSize()), + BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValueNullable().getSecond().getFirst()); Assert.assertEquals(false, a.getFillValueNullable().getSecond().getSecond());