Skip to content

Commit d2acf76

Browse files
committed
Add metadata overloads + cover them with tests
1 parent dc80fec commit d2acf76

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,24 @@ public HashMap<String, Pair<Long, Long>> maxBufferElements(NativeArray subarray)
615615
return ret;
616616
}
617617

618+
/**
619+
* Get a metadata key-value HashMap from an open array. The array must be opened in READ mode,
620+
* otherwise the function will error out.
621+
*
622+
* @return HashMap<String, NativeArray> which contains all the metadata
623+
* @throws TileDBError A TileDB exception
624+
*/
625+
public HashMap<String, NativeArray> getMetadataMap() throws TileDBError {
626+
HashMap<String, NativeArray> ret = new HashMap<String, NativeArray>();
627+
628+
for (long i = 0; i < getMetadataNum().longValue(); i++) {
629+
Pair<String, NativeArray> p = getMetadataFromIndex(i);
630+
ret.put(p.getFirst(), p.getSecond());
631+
}
632+
633+
return ret;
634+
}
635+
618636
/**
619637
* Get a metadata key-value item from an open array. The array must be opened in READ mode,
620638
* otherwise the function will error out.
@@ -767,6 +785,19 @@ public Boolean hasMetadataKey(String key) throws TileDBError {
767785
return result;
768786
}
769787

788+
/**
789+
* Puts a metadata key-value item to an open array. The array must be opened in WRITE mode,
790+
* otherwise the function will error out.
791+
*
792+
* @param key a key to assign to the input value
793+
* @param buffer the metadata to put into the Array metadata
794+
* @param javaType javaType of the metadata
795+
* @throws TileDBError A TileDB exception
796+
*/
797+
public void putMetadata(String key, Object buffer, Class javaType) throws TileDBError {
798+
putMetadata(key, new NativeArray(ctx, buffer, javaType));
799+
}
800+
770801
/**
771802
* Puts a metadata key-value item to an open array. The array must be opened in WRITE mode,
772803
* otherwise the function will error out.

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.nio.charset.StandardCharsets;
1010
import java.sql.Timestamp;
1111
import java.util.Arrays;
12+
import java.util.HashMap;
13+
import java.util.Map;
1214
import org.junit.*;
1315
import org.junit.rules.TemporaryFolder;
1416

@@ -393,6 +395,26 @@ public void testArrayMetadata() throws Exception {
393395
Assert.assertArrayEquals(getArray(expectedArrays[i]), getArray(a.toJavaArray()));
394396
}
395397

398+
// getMetadataMap test
399+
HashMap<String, Object> expectedArraysMap =
400+
new HashMap<String, Object>() {
401+
{
402+
put(byteKey, metadataByte.toJavaArray());
403+
put(doubleKey, metadataDouble.toJavaArray());
404+
put(floatKey, metadataFloat.toJavaArray());
405+
put(intKey, metadataInt.toJavaArray());
406+
put(shortKey, metadataShort.toJavaArray());
407+
}
408+
};
409+
410+
HashMap<String, NativeArray> actualMetadataMap = arrayn.getMetadataMap();
411+
412+
for (Map.Entry<String, NativeArray> entry : actualMetadataMap.entrySet()) {
413+
String key = entry.getKey();
414+
Object value = entry.getValue().toJavaArray();
415+
Assert.assertArrayEquals(getArray(expectedArraysMap.get(key)), getArray(value));
416+
}
417+
396418
arrayn.close();
397419

398420
// open a new write session
@@ -415,4 +437,34 @@ public void testArrayMetadata() throws Exception {
415437

416438
arraydn.close();
417439
}
440+
441+
@Test
442+
public void testArrayPutMetadataOverload() throws Exception {
443+
Array.create(arrayURI, schemaCreate());
444+
445+
long[] array_a = new long[] {1, 2, 3, 6};
446+
insertArbitraryValues(new NativeArray(ctx, array_a, Long.class));
447+
448+
Array arrayw = new Array(ctx, arrayURI, TILEDB_WRITE);
449+
450+
String floatKey = "md-float";
451+
float[] metadataFloat =
452+
new float[] {
453+
0.1f, 0.2f, 1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f,
454+
4.1f, 4.2f, 5.1f, 5.2f, 6.1f, 6.2f, 7.1f, 7.2f,
455+
8.1f, 8.2f, 9.1f, 9.2f, 10.1f, 10.2f, 11.1f, 11.2f,
456+
12.1f, 12.2f, 13.1f, 13.2f, 14.1f, 14.2f, 15.1f, 15.2f
457+
};
458+
459+
arrayw.putMetadata(floatKey, metadataFloat, Float.class);
460+
arrayw.close();
461+
462+
Array array = new Array(ctx, arrayURI, TILEDB_READ);
463+
464+
float[] metadataFloatActual = (float[]) array.getMetadata(floatKey).toJavaArray();
465+
466+
Assert.assertArrayEquals(metadataFloat, metadataFloatActual, 1e-10f);
467+
468+
array.close();
469+
}
418470
}

0 commit comments

Comments
 (0)