11package io .tiledb .java .api ;
22
3+ import static io .tiledb .java .api .Layout .TILEDB_ROW_MAJOR ;
4+ import static io .tiledb .java .api .QueryType .TILEDB_READ ;
5+ import static io .tiledb .java .api .QueryType .TILEDB_WRITE ;
6+
7+ import java .math .BigInteger ;
38import java .nio .charset .StandardCharsets ;
9+ import java .sql .Timestamp ;
10+ import java .util .Arrays ;
411import org .junit .*;
512import org .junit .rules .TemporaryFolder ;
613
714public class ArrayTest {
815
916 private Context ctx ;
1017 private String arrayURI ;
18+ private String attributeName ;
1119 private byte [] key ;
1220
1321 @ Rule public TemporaryFolder temp = new TemporaryFolder ();
@@ -16,6 +24,7 @@ public class ArrayTest {
1624 public void setup () throws Exception {
1725 ctx = new Context ();
1826 arrayURI = temp .getRoot ().toString ();
27+ attributeName = "a1" ;
1928 String keyString = "0123456789abcdeF0123456789abcdeF" ;
2029 key = keyString .getBytes (StandardCharsets .US_ASCII );
2130 }
@@ -31,7 +40,7 @@ public ArraySchema schemaCreate() throws Exception {
3140 Domain domain = new Domain (ctx );
3241 domain .addDimension (d1 );
3342
34- Attribute a1 = new Attribute (ctx , "a1" , Integer .class );
43+ Attribute a1 = new Attribute (ctx , attributeName , Long .class );
3544 ArraySchema schema = new ArraySchema (ctx , ArrayType .TILEDB_DENSE );
3645 schema .setTileOrder (Layout .TILEDB_ROW_MAJOR );
3746 schema .setCellOrder (Layout .TILEDB_ROW_MAJOR );
@@ -41,6 +50,59 @@ public ArraySchema schemaCreate() throws Exception {
4150 return schema ;
4251 }
4352
53+ public void insertArbitraryValuesMeth (Array array , NativeArray a_data ) throws TileDBError {
54+ // Create query
55+ try (Query query = new Query (array , TILEDB_WRITE )) {
56+ query .setLayout (TILEDB_ROW_MAJOR ).setBuffer (attributeName , a_data );
57+ query .submit ();
58+ }
59+ array .close ();
60+ }
61+
62+ public void insertArbitraryValues (NativeArray a_data ) throws TileDBError {
63+ Array array = new Array (ctx , arrayURI , TILEDB_WRITE );
64+ insertArbitraryValuesMeth (array , a_data );
65+ array .close ();
66+ }
67+
68+ public void insertArbitraryValuesEncrypted (NativeArray a_data ) throws TileDBError {
69+ Array array = new Array (ctx , arrayURI , TILEDB_WRITE , EncryptionType .TILEDB_AES_256_GCM , key );
70+ insertArbitraryValuesMeth (array , a_data );
71+ array .close ();
72+ }
73+
74+ public long [] readArray (Array array ) throws TileDBError {
75+ NativeArray sub_array = new NativeArray (ctx , new long [] {1 , 4 , 1 , 2 }, Long .class );
76+ // Create query
77+ Query query = new Query (array , TILEDB_READ );
78+ query .setLayout (TILEDB_ROW_MAJOR );
79+ query .setSubarray (sub_array );
80+ query .setBuffer (attributeName , new NativeArray (ctx , 10 , Long .class ));
81+
82+ // Submit query
83+ query .submit ();
84+
85+ long [] a_buff = (long []) query .getBuffer (attributeName );
86+
87+ query .close ();
88+ array .close ();
89+
90+ return a_buff ;
91+ }
92+
93+ public long [] readArray () throws TileDBError {
94+ return readArray (new Array (ctx , arrayURI ));
95+ }
96+
97+ public long [] readArrayAt (BigInteger timestamp ) throws TileDBError {
98+ return readArray (new Array (ctx , arrayURI , timestamp ));
99+ }
100+
101+ public long [] readArrayAtEncrypted (BigInteger timestamp ) throws TileDBError {
102+ return readArray (
103+ new Array (ctx , arrayURI , TILEDB_READ , EncryptionType .TILEDB_AES_256_GCM , key , timestamp ));
104+ }
105+
44106 @ Test
45107 public void testArrayExists () throws Exception {
46108 // Test that we can create an array
@@ -97,4 +159,54 @@ public void testLoadingEncryptedArrayWrongKeyLenErrors() throws Exception {
97159 EncryptionType .TILEDB_AES_256_GCM ,
98160 keyString .getBytes (StandardCharsets .US_ASCII ));
99161 }
162+
163+ @ Test
164+ public void testArrayOpenAt () throws Exception {
165+ Array .create (arrayURI , schemaCreate ());
166+
167+ long [] array_a = new long [] {1 , 2 , 3 , 6 };
168+ insertArbitraryValues (new NativeArray (ctx , array_a , Long .class ));
169+ long ts_a = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
170+
171+ Thread .sleep (1000 );
172+
173+ long [] array_b = new long [] {1 , 1 , 1 , 1 };
174+ insertArbitraryValues (new NativeArray (ctx , array_b , Long .class ));
175+ long ts_b = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
176+
177+ Thread .sleep (1000 );
178+
179+ long [] array_c = new long [] {0 , 0 , 0 , 0 };
180+ insertArbitraryValues (new NativeArray (ctx , array_c , Long .class ));
181+ long ts_c = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
182+
183+ assert Arrays .equals (readArrayAt (BigInteger .valueOf (ts_a )), array_a );
184+ assert Arrays .equals (readArrayAt (BigInteger .valueOf (ts_b )), array_b );
185+ assert Arrays .equals (readArrayAt (BigInteger .valueOf (ts_c )), array_c );
186+ }
187+
188+ @ Test
189+ public void testArrayOpenAtEncrypted () throws Exception {
190+ Array .create (arrayURI , schemaCreate (), EncryptionType .TILEDB_AES_256_GCM , key );
191+
192+ long [] array_a = new long [] {1 , 2 , 3 , 6 };
193+ insertArbitraryValuesEncrypted (new NativeArray (ctx , array_a , Long .class ));
194+ long ts_a = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
195+
196+ Thread .sleep (1000 );
197+
198+ long [] array_b = new long [] {1 , 1 , 1 , 1 };
199+ insertArbitraryValuesEncrypted (new NativeArray (ctx , array_b , Long .class ));
200+ long ts_b = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
201+
202+ Thread .sleep (1000 );
203+
204+ long [] array_c = new long [] {0 , 0 , 0 , 0 };
205+ insertArbitraryValuesEncrypted (new NativeArray (ctx , array_c , Long .class ));
206+ long ts_c = new Timestamp (System .currentTimeMillis ()).toInstant ().toEpochMilli ();
207+
208+ assert Arrays .equals (readArrayAtEncrypted (BigInteger .valueOf (ts_a )), array_a );
209+ assert Arrays .equals (readArrayAtEncrypted (BigInteger .valueOf (ts_b )), array_b );
210+ assert Arrays .equals (readArrayAtEncrypted (BigInteger .valueOf (ts_c )), array_c );
211+ }
100212}
0 commit comments