Skip to content

Commit e527146

Browse files
vicravelShelnutt2
authored andcommitted
added openArray() implementation with timestamp argument
1 parent 3d0b48a commit e527146

File tree

3 files changed

+209
-2
lines changed

3 files changed

+209
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group 'io.tiledb'
12-
version '0.2.0-SNAPSHOT'
12+
version '0.2.1-SNAPSHOT'
1313

1414
repositories {
1515
jcenter()

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static io.tiledb.java.api.QueryType.*;
2828

2929
import io.tiledb.libtiledb.*;
30+
import java.math.BigInteger;
3031
import java.util.HashMap;
3132

3233
/**
@@ -73,6 +74,25 @@ public Array(Context ctx, String uri) throws TileDBError {
7374
openArray(ctx, uri, TILEDB_READ, EncryptionType.TILEDB_NO_ENCRYPTION, new byte[] {});
7475
}
7576

77+
/**
78+
* Constructs an Array object opening the array for reading at a user-given timestamp
79+
* (time-travelling).
80+
*
81+
* <pre><b>Example:</b>
82+
* {@code
83+
* Context ctx = new Context();
84+
* Array array new Array(ctx, "s3://bucket-name/array-name");
85+
* }</pre>
86+
*
87+
* @param ctx TileDB context
88+
* @param uri The array URI
89+
* @param timestamp The timestamp
90+
* @exception TileDBError A TileDB exception
91+
*/
92+
public Array(Context ctx, String uri, BigInteger timestamp) throws TileDBError {
93+
openArray(ctx, uri, TILEDB_READ, EncryptionType.TILEDB_NO_ENCRYPTION, new byte[] {}, timestamp);
94+
}
95+
7696
/**
7797
* Constructs an Array object, opening the array for the given query type.
7898
*
@@ -119,6 +139,39 @@ public Array(
119139
openArray(ctx, uri, query_type, encryption_type, key);
120140
}
121141

142+
/**
143+
* Constructs an Array object, opening the encrypted array for the given query type.
144+
*
145+
* <pre><b>Example:</b>
146+
* {@code
147+
* Context ctx = new Context();
148+
* String key = "0123456789abcdeF0123456789abcdeF";
149+
* Array array new Array(ctx, "s3://bucket-name/array-name",
150+
* TILEDB_READ,
151+
* TILEDB_AES_256_GCM,
152+
* key.getBytes(StandardCharsets.UTF_8));
153+
* }
154+
* </pre>
155+
*
156+
* @param ctx TileDB context
157+
* @param uri The array URI
158+
* @param query_type Query type to open the array for
159+
* @param encryption_type The encryption type to use
160+
* @param key The encryption key to use
161+
* @param timestamp The timestamp
162+
* @throws TileDBError A TileDB exception
163+
*/
164+
public Array(
165+
Context ctx,
166+
String uri,
167+
QueryType query_type,
168+
EncryptionType encryption_type,
169+
byte[] key,
170+
BigInteger timestamp)
171+
throws TileDBError {
172+
openArray(ctx, uri, query_type, encryption_type, key, timestamp);
173+
}
174+
122175
private synchronized void openArray(
123176
Context ctx, String uri, QueryType query_type, EncryptionType encryption_type, byte[] key)
124177
throws TileDBError {
@@ -155,6 +208,48 @@ private synchronized void openArray(
155208
this.arrayp = _arrayp;
156209
}
157210

211+
private synchronized void openArray(
212+
Context ctx,
213+
String uri,
214+
QueryType query_type,
215+
EncryptionType encryption_type,
216+
byte[] key,
217+
BigInteger timestamp)
218+
throws TileDBError {
219+
SWIGTYPE_p_p_tiledb_array_t _arraypp = tiledb.new_tiledb_array_tpp();
220+
try {
221+
ctx.handleError(tiledb.tiledb_array_alloc(ctx.getCtxp(), uri, _arraypp));
222+
} catch (TileDBError err) {
223+
tiledb.delete_tiledb_array_tpp(_arraypp);
224+
throw err;
225+
}
226+
SWIGTYPE_p_tiledb_array_t _arrayp = tiledb.tiledb_array_tpp_value(_arraypp);
227+
ArraySchema _schema;
228+
try (NativeArray keyArray = new NativeArray(ctx, key, Byte.class)) {
229+
try {
230+
ctx.handleError(
231+
tiledb.tiledb_array_open_at_with_key(
232+
ctx.getCtxp(),
233+
_arrayp,
234+
query_type.toSwigEnum(),
235+
encryption_type.toSwigEnum(),
236+
keyArray.toVoidPointer(),
237+
keyArray.getSize(),
238+
timestamp));
239+
} catch (TileDBError err) {
240+
tiledb.delete_tiledb_array_tpp(_arraypp);
241+
throw err;
242+
}
243+
_schema = new ArraySchema(ctx, uri, encryption_type, key);
244+
}
245+
this.ctx = ctx;
246+
this.uri = uri;
247+
this.query_type = query_type;
248+
this.schema = _schema;
249+
this.arraypp = _arraypp;
250+
this.arrayp = _arrayp;
251+
}
252+
158253
private void checkIsOpen() throws TileDBError {
159254
if (arrayp == null) {
160255
throw new TileDBError("TileDB Array " + uri + " is closed");

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

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package 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;
38
import java.nio.charset.StandardCharsets;
9+
import java.sql.Timestamp;
10+
import java.util.Arrays;
411
import org.junit.*;
512
import org.junit.rules.TemporaryFolder;
613

714
public 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

Comments
 (0)