Skip to content

Commit dce18b4

Browse files
authored
Merge pull request #162 from TileDB-Inc/victorgiannakouris/remove_get_coords
Replace getCoordinates with setBuffer
2 parents 8037d3f + dd1aa61 commit dce18b4

File tree

6 files changed

+199
-41
lines changed

6 files changed

+199
-41
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ public HashMap<String, Pair> nonEmptyDomain() throws TileDBError {
482482
public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
483483
checkIsOpen();
484484
try (Domain domain = schema.getDomain();
485-
NativeArray domainArray =
486-
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
485+
NativeArray domainArray = new NativeArray(ctx, 2, domain.getDimension(index).getType())) {
487486

488487
if (domain.getDimension(index).isVar()) return getNonEmptyDomainVarFromIndex(index);
489488

@@ -515,8 +514,7 @@ public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError {
515514
public Pair getNonEmptyDomainFromName(String name) throws TileDBError {
516515
checkIsOpen();
517516
try (Domain domain = schema.getDomain();
518-
NativeArray domainArray =
519-
new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) {
517+
NativeArray domainArray = new NativeArray(ctx, 2, domain.getDimension(name).getType())) {
520518

521519
if (domain.getDimension(name).isVar()) return this.getNonEmptyDomainVarFromName(name);
522520

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public synchronized Query addRange(int dimIdx, Object start, Object end) throws
174174
Datatype dimType;
175175
try (ArraySchema schema = array.getSchema();
176176
Domain domain = schema.getDomain()) {
177-
dimType = domain.getType();
177+
dimType = domain.getDimension(dimIdx).getType();
178178
}
179179

180180
// We use java type check here because we can not tell the difference between unsigned and
@@ -215,7 +215,7 @@ public synchronized Query addRangeVar(int dimIdx, String start, String end) thro
215215
Datatype dimType;
216216
try (ArraySchema schema = array.getSchema();
217217
Domain domain = schema.getDomain()) {
218-
dimType = domain.getType();
218+
dimType = domain.getDimension(dimIdx).getType();
219219
}
220220

221221
Types.javaTypeCheck(start.getClass(), dimType.javaClass());
@@ -276,7 +276,7 @@ public synchronized Pair<String, String> getRangeVar(int dimIdx, BigInteger rang
276276
Datatype dimType;
277277
try (ArraySchema schema = array.getSchema();
278278
Domain domain = schema.getDomain()) {
279-
dimType = domain.getType();
279+
dimType = domain.getDimension(dimIdx).getType();
280280
}
281281

282282
Pair<Long, Long> size = this.getRangeVarSize(dimIdx, rangeIdx);
@@ -325,7 +325,7 @@ public Pair<Object, Object> getRange(int dimIdx, long rangeIdx) throws TileDBErr
325325
Datatype dimType;
326326
try (ArraySchema schema = array.getSchema();
327327
Domain domain = schema.getDomain()) {
328-
dimType = domain.getType();
328+
dimType = domain.getDimension(dimIdx).getType();
329329
}
330330

331331
SWIGTYPE_p_p_void startArrpp = tiledb.new_voidpArray(1);
@@ -368,7 +368,7 @@ public synchronized Query setBuffer(String attr, NativeArray buffer) throws Tile
368368
try (ArraySchema schema = array.getSchema()) {
369369
try (Domain domain = schema.getDomain()) {
370370
if (attr.equals(tiledb.tiledb_coords())) {
371-
Types.typeCheck(domain.getType(), buffer.getNativeType());
371+
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
372372
} else if (domain.hasDimension(attr)) {
373373
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
374374
} else {
@@ -429,7 +429,7 @@ public synchronized Query setBuffer(String attr, NativeArray buffer, long buffer
429429
try (ArraySchema schema = array.getSchema()) {
430430
try (Domain domain = schema.getDomain()) {
431431
if (attr.equals(tiledb.tiledb_coords())) {
432-
Types.typeCheck(domain.getType(), buffer.getNativeType());
432+
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
433433
} else if (domain.hasDimension(attr)) {
434434
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
435435
} else {
@@ -492,7 +492,7 @@ public synchronized Query setBuffer(String attr, NativeArray offsets, NativeArra
492492
try (ArraySchema schema = array.getSchema()) {
493493
try (Domain domain = schema.getDomain()) {
494494
if (attr.equals(tiledb.tiledb_coords())) {
495-
Types.typeCheck(domain.getType(), buffer.getNativeType());
495+
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
496496
} else if (domain.hasDimension(attr)) {
497497
Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType());
498498
} else {
@@ -748,6 +748,7 @@ public Query setCoordinates(NativeArray buffer) throws TileDBError {
748748
* @return The query result coordinate buffer.
749749
* @exception TileDBError A TileDB exception
750750
*/
751+
@Deprecated
751752
public Object getCoordinates() throws TileDBError {
752753
return getBuffer(tiledb.tiledb_coords());
753754
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ public void test() throws Exception {
4242

4343
public void arrayCreate() throws Exception {
4444
// Create getDimensions
45-
Dimension<Integer> rows =
46-
new Dimension<Integer>(ctx, "rows", Integer.class, new Pair<Integer, Integer>(1, 4), 2);
47-
Dimension<Integer> cols =
48-
new Dimension<Integer>(ctx, "cols", Integer.class, new Pair<Integer, Integer>(1, 4), 2);
45+
Dimension<Integer> rows = new Dimension(ctx, "rows", Integer.class, new Pair(1, 4), 2);
46+
Dimension<Integer> cols = new Dimension(ctx, "cols", Integer.class, new Pair(1, 4), 2);
4947

5048
// Create and set getDomain
5149
Domain domain = new Domain(ctx);
@@ -65,18 +63,20 @@ public void arrayCreate() throws Exception {
6563
}
6664

6765
public void arrayWrite() throws Exception {
66+
NativeArray d1_data = new NativeArray(ctx, new int[] {1, 2, 2, 4}, Integer.class);
67+
NativeArray d2_data = new NativeArray(ctx, new int[] {1, 1, 2, 3}, Integer.class);
68+
6869
// Prepare cell buffers
6970
NativeArray data = new NativeArray(ctx, new int[] {1, 2, 3, 4}, Integer.class);
7071

71-
NativeArray coords_buff =
72-
new NativeArray(ctx, new int[] {1, 1, 2, 1, 2, 2, 4, 3}, Integer.class);
73-
7472
// Create query
7573
Array array = new Array(ctx, arrayURI, TILEDB_WRITE);
7674
Query query = new Query(array);
7775
query.setLayout(TILEDB_GLOBAL_ORDER);
76+
query.setBuffer("rows", d1_data);
77+
query.setBuffer("cols", d2_data);
7878
query.setBuffer("a", data);
79-
query.setCoordinates(coords_buff);
79+
8080
// Submit query
8181
query.submitAsync();
8282

@@ -102,9 +102,13 @@ private void arrayRead() throws Exception {
102102
Query query = new Query(array, TILEDB_READ);
103103
query.setLayout(TILEDB_ROW_MAJOR);
104104
query.setBuffer(
105-
"a", new NativeArray(ctx, max_sizes.get("a").getSecond().intValue(), Integer.class));
106-
query.setCoordinates(
105+
"rows",
107106
new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Integer.class));
107+
query.setBuffer(
108+
"cols",
109+
new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Integer.class));
110+
query.setBuffer(
111+
"a", new NativeArray(ctx, max_sizes.get("a").getSecond().intValue(), Integer.class));
108112

109113
// Submit query with callback
110114
query.submitAsync(new ReadCallback());
@@ -119,14 +123,16 @@ private void arrayRead() throws Exception {
119123
// Print cell values (assumes all getAttributes are read)
120124
HashMap<String, Pair<Long, Long>> result_el = query.resultBufferElements();
121125

126+
int[] rows = (int[]) query.getBuffer("rows");
127+
int[] cols = (int[]) query.getBuffer("cols");
122128
int[] data = (int[]) query.getBuffer("a");
123-
int[] coords = (int[]) query.getCoordinates();
124129

125130
query.close();
126131
array.close();
127132

128133
Assert.assertTrue(callbackCalled);
129-
Assert.assertArrayEquals(coords, new int[] {1, 1, 2, 1, 2, 2, 4, 3});
134+
Assert.assertArrayEquals(rows, new int[] {1, 2, 2, 4});
135+
Assert.assertArrayEquals(cols, new int[] {1, 1, 2, 3});
130136
Assert.assertArrayEquals(data, new int[] {1, 2, 3, 4});
131137
}
132138

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public void arrayWrite2() throws Exception {
112112

113113
public void arrayWrite3() throws Exception {
114114
// Prepare cell buffers
115+
NativeArray rows = new NativeArray(ctx, new int[] {1, 3}, Integer.class);
116+
NativeArray cols = new NativeArray(ctx, new int[] {1, 4}, Integer.class);
115117
NativeArray data = new NativeArray(ctx, new int[] {201, 202}, Integer.class);
116118

117119
NativeArray coords = new NativeArray(ctx, new int[] {1, 1, 3, 4}, Integer.class);
@@ -120,8 +122,9 @@ public void arrayWrite3() throws Exception {
120122
Array array = new Array(ctx, arrayURI, TILEDB_WRITE);
121123
Query query = new Query(array);
122124
query.setLayout(TILEDB_UNORDERED);
125+
query.setBuffer("rows", rows);
126+
query.setBuffer("cols", cols);
123127
query.setBuffer("a", data);
124-
query.setCoordinates(coords);
125128
// Submit query
126129
query.submit();
127130
query.close();
@@ -140,24 +143,23 @@ private void arrayRead() throws Exception {
140143
query.setLayout(TILEDB_ROW_MAJOR);
141144
query.setSubarray(subarray);
142145
query.setBuffer("a", new NativeArray(ctx, 16, Integer.class));
143-
query.setCoordinates(new NativeArray(ctx, 32, Integer.class));
146+
query.setBuffer("rows", new NativeArray(ctx, 16, Integer.class));
147+
query.setBuffer("cols", new NativeArray(ctx, 16, Integer.class));
144148

145149
// Submit query
146150
query.submit();
147151
// Print cell values (assumes all getAttributes are read)
148152
HashMap<String, Pair<Long, Long>> result_el = query.resultBufferElements();
149153

154+
int[] rows = (int[]) query.getBuffer("rows");
155+
int[] cols = (int[]) query.getBuffer("cols");
150156
int[] data = (int[]) query.getBuffer("a");
151-
int[] coords = (int[]) query.getCoordinates();
152157
query.close();
153158
array.close();
154159

155-
Assert.assertArrayEquals(
156-
coords,
157-
new int[] {
158-
1, 1, 1, 2, 1, 3, 1, 4, 2, 1, 2, 2, 2, 3, 2, 4, 3, 1, 3, 2, 3, 3, 3, 4, 4, 1, 4, 2, 4, 3,
159-
4, 4
160-
});
160+
Assert.assertArrayEquals(rows, new int[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4});
161+
Assert.assertArrayEquals(cols, new int[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4});
162+
161163
Assert.assertArrayEquals(
162164
data,
163165
new int[] {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020 TileDB, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package io.tiledb.java.api;
26+
27+
import static io.tiledb.java.api.ArrayType.TILEDB_SPARSE;
28+
import static io.tiledb.java.api.Layout.TILEDB_GLOBAL_ORDER;
29+
import static io.tiledb.java.api.Layout.TILEDB_ROW_MAJOR;
30+
import static io.tiledb.java.api.QueryType.TILEDB_READ;
31+
import static io.tiledb.java.api.QueryType.TILEDB_WRITE;
32+
33+
import java.nio.file.Files;
34+
import java.nio.file.Paths;
35+
import org.junit.After;
36+
import org.junit.Assert;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
40+
@SuppressWarnings("ALL")
41+
public class HeterogeneousSparseTest {
42+
43+
private Context ctx;
44+
private String arrayURI = "my_sparse_array";
45+
46+
@Before
47+
public void setup() throws Exception {
48+
ctx = new Context();
49+
if (Files.exists(Paths.get(arrayURI))) {
50+
TileDBObject.remove(ctx, arrayURI);
51+
}
52+
}
53+
54+
@After
55+
public void teardown() throws Exception {
56+
if (Files.exists(Paths.get(arrayURI))) {
57+
TileDBObject.remove(ctx, arrayURI);
58+
}
59+
}
60+
61+
@Test
62+
public void testQuickstartSparse() throws Exception {
63+
arrayCreate();
64+
arrayWrite();
65+
arrayRead();
66+
}
67+
68+
public void arrayCreate() throws Exception {
69+
Dimension d1 = new Dimension(ctx, "d1", Datatype.TILEDB_STRING_ASCII, null, null);
70+
Dimension d2 = new Dimension(ctx, "d2", Datatype.TILEDB_INT32, new Pair(0, 100), 2);
71+
72+
// Create and set getDomain
73+
Domain domain = new Domain(ctx);
74+
domain.addDimension(d1).addDimension(d2);
75+
76+
// Add two attributes "a1" and "a2", so each (i,j) cell can store
77+
// a character on "a1" and a vector of two floats on "a2".
78+
Attribute a1 = new Attribute(ctx, "a1", Integer.class);
79+
80+
ArraySchema schema = new ArraySchema(ctx, TILEDB_SPARSE);
81+
schema.setTileOrder(TILEDB_ROW_MAJOR);
82+
schema.setCellOrder(TILEDB_ROW_MAJOR);
83+
schema.setDomain(domain);
84+
schema.addAttribute(a1);
85+
86+
Array.create(arrayURI, schema);
87+
}
88+
89+
public void arrayWrite() throws Exception {
90+
NativeArray d1_data = new NativeArray(ctx, "aabbccddee", Datatype.TILEDB_STRING_ASCII);
91+
NativeArray d1_off = new NativeArray(ctx, new long[] {0, 2, 4, 6, 8}, Datatype.TILEDB_UINT64);
92+
NativeArray d2_data = new NativeArray(ctx, new int[] {1, 4, 9, 10, 12}, Datatype.TILEDB_INT32);
93+
94+
// Prepare cell buffers
95+
NativeArray a1 = new NativeArray(ctx, new int[] {1, 2, 3, 4, 5}, Integer.class);
96+
97+
// Create query
98+
Array array = new Array(ctx, arrayURI, TILEDB_WRITE);
99+
Query query = new Query(array);
100+
query.setLayout(TILEDB_GLOBAL_ORDER);
101+
102+
query.setBuffer("d1", d1_off, d1_data);
103+
query.setBuffer("d2", d2_data);
104+
query.setBuffer("a1", a1);
105+
106+
// Submit query
107+
query.submit();
108+
109+
query.finalizeQuery();
110+
query.close();
111+
array.close();
112+
}
113+
114+
private void arrayRead() throws Exception {
115+
NativeArray d_data = new NativeArray(ctx, 20, Datatype.TILEDB_STRING_ASCII);
116+
NativeArray d_off = new NativeArray(ctx, 20, Datatype.TILEDB_UINT64);
117+
118+
NativeArray d2_data = new NativeArray(ctx, 20, Datatype.TILEDB_INT32);
119+
120+
Query q = new Query(new Array(ctx, arrayURI), TILEDB_READ);
121+
q.setLayout(TILEDB_GLOBAL_ORDER);
122+
123+
q.setBuffer("d1", d_off, d_data);
124+
q.setBuffer("d2", d2_data);
125+
126+
q.addRangeVar(0, "a", "z");
127+
128+
q.submit();
129+
130+
byte[] data = (byte[]) q.getBuffer("d1");
131+
long[] offsets = q.getVarBuffer("d1");
132+
int[] d2 = (int[]) q.getBuffer("d2");
133+
134+
String[] results = new String[offsets.length];
135+
136+
results = Util.bytesToStrings(offsets, data);
137+
138+
Assert.assertArrayEquals(results, new String[] {"aa", "bb", "cc", "dd", "ee"});
139+
Assert.assertArrayEquals(d2, new int[] {1, 4, 9, 10, 12});
140+
}
141+
}

0 commit comments

Comments
 (0)