|
5 | 5 | import static io.tiledb.java.api.QueryType.TILEDB_READ;
|
6 | 6 | import static io.tiledb.java.api.QueryType.TILEDB_WRITE;
|
7 | 7 |
|
8 |
| -import java.nio.file.Files; |
9 |
| -import java.nio.file.Paths; |
10 | 8 | import java.util.HashMap;
|
11 |
| -import org.junit.After; |
12 | 9 | import org.junit.Assert;
|
13 | 10 | import org.junit.Before;
|
| 11 | +import org.junit.Rule; |
14 | 12 | import org.junit.Test;
|
| 13 | +import org.junit.rules.TemporaryFolder; |
15 | 14 |
|
16 | 15 | public class MultiRangeQueryTest {
|
| 16 | + |
| 17 | + @Rule public TemporaryFolder temp = new TemporaryFolder(); |
| 18 | + |
17 | 19 | private Context ctx;
|
18 |
| - private String arrayURI = "multi_range_query"; |
| 20 | + private String arrayURI; |
19 | 21 |
|
20 | 22 | @Before
|
21 | 23 | public void setup() throws Exception {
|
22 | 24 | ctx = new Context();
|
23 |
| - if (Files.exists(Paths.get(arrayURI))) { |
24 |
| - TileDBObject.remove(ctx, arrayURI); |
25 |
| - } |
| 25 | + arrayURI = temp.getRoot().toPath().resolve("multi_range_query").toString(); |
26 | 26 | arrayCreate();
|
27 | 27 | arrayWrite();
|
28 | 28 | }
|
29 | 29 |
|
30 |
| - @After |
31 |
| - public void teardown() throws Exception { |
32 |
| - if (Files.exists(Paths.get(arrayURI))) { |
33 |
| - TileDBObject.remove(ctx, arrayURI); |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 | 30 | @Test
|
38 | 31 | public void test() throws Exception {
|
39 | 32 | arrayRead();
|
@@ -82,65 +75,63 @@ public void arrayWrite() throws Exception {
|
82 | 75 | Float.class);
|
83 | 76 |
|
84 | 77 | // Create query
|
85 |
| - Array array = new Array(ctx, arrayURI, TILEDB_WRITE); |
86 |
| - Query query = new Query(array); |
87 |
| - query.setLayout(TILEDB_ROW_MAJOR); |
88 |
| - query.setBuffer("a1", a1); |
89 |
| - query.setBuffer("a2", a2); |
90 |
| - // Submit query |
91 |
| - query.submit(); |
92 |
| - query.close(); |
93 |
| - array.close(); |
| 78 | + try (Array array = new Array(ctx, arrayURI, TILEDB_WRITE); |
| 79 | + Query query = new Query(array)) { |
| 80 | + query.setLayout(TILEDB_ROW_MAJOR); |
| 81 | + query.setBuffer("a1", a1); |
| 82 | + query.setBuffer("a2", a2); |
| 83 | + // Submit query |
| 84 | + query.submit(); |
| 85 | + } |
94 | 86 | }
|
95 | 87 |
|
96 | 88 | private void arrayRead() throws Exception {
|
97 |
| - Array array = new Array(ctx, arrayURI, TILEDB_READ); |
98 |
| - |
99 |
| - // Create query |
100 |
| - Query query = new Query(array, TILEDB_READ); |
101 |
| - |
102 |
| - // Slice only rows 1, 2 and cols 2, 3, 4 |
103 |
| - query.addRange(0, (int) 1, (int) 2); |
104 |
| - query.addRange(1, (int) 2, (int) 4); |
105 |
| - query.setLayout(TILEDB_ROW_MAJOR); |
106 |
| - |
107 |
| - Assert.assertEquals(1, query.getRangeNum(0)); |
108 |
| - Assert.assertEquals(1, query.getRangeNum(1)); |
109 |
| - Assert.assertEquals(1, query.getRange(0, 0).getFirst()); |
110 |
| - Assert.assertEquals(2, query.getRange(0, 0).getSecond()); |
111 |
| - Assert.assertEquals(2, query.getRange(1, 0).getFirst()); |
112 |
| - Assert.assertEquals(4, query.getRange(1, 0).getSecond()); |
113 |
| - |
114 |
| - // Prepare the vector that will hold the result |
115 |
| - // (of size 6 elements for "a1" and 12 elements for "a2" since |
116 |
| - // it stores two floats per cell) |
117 |
| - query.setBuffer("a1", new NativeArray(ctx, 6, Character.class)); |
118 |
| - query.setBuffer("a2", new NativeArray(ctx, 12, Float.class)); |
119 |
| - |
120 |
| - // Submit query |
121 |
| - query.submit(); |
122 |
| - |
123 |
| - HashMap<String, Pair<Long, Long>> result_el = query.resultBufferElements(); |
124 |
| - |
125 |
| - byte[] a1 = (byte[]) query.getBuffer("a1"); |
126 |
| - float[] a2 = (float[]) query.getBuffer("a2"); |
127 |
| - |
128 |
| - query.close(); |
129 |
| - array.close(); |
130 |
| - |
131 |
| - Assert.assertArrayEquals(a1, new byte[] {'b', 'c', 'd', 'f', 'g', 'h'}); |
132 |
| - |
133 |
| - float[] expected_a2 = |
134 |
| - new float[] {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 5.1f, 5.2f, 6.1f, 6.2f, 7.1f, 7.2f}; |
135 |
| - for (int i = 0; i < a2.length; i++) { |
136 |
| - Assert.assertEquals(a2[i], expected_a2[i], 0.01f); |
| 89 | + // Create array and query |
| 90 | + try (Array array = new Array(ctx, arrayURI, TILEDB_READ); |
| 91 | + Query query = new Query(array, TILEDB_READ)) { |
| 92 | + |
| 93 | + // Slice only rows 1, 2 and cols 2, 3, 4 |
| 94 | + query.addRange(0, (int) 1, (int) 2); |
| 95 | + query.addRange(1, (int) 2, (int) 4); |
| 96 | + query.setLayout(TILEDB_ROW_MAJOR); |
| 97 | + |
| 98 | + Assert.assertEquals(1, query.getRangeNum(0)); |
| 99 | + Assert.assertEquals(1, query.getRangeNum(1)); |
| 100 | + Assert.assertEquals(1, query.getRange(0, 0).getFirst()); |
| 101 | + Assert.assertEquals(2, query.getRange(0, 0).getSecond()); |
| 102 | + Assert.assertEquals(2, query.getRange(1, 0).getFirst()); |
| 103 | + Assert.assertEquals(4, query.getRange(1, 0).getSecond()); |
| 104 | + |
| 105 | + // Prepare the vector that will hold the result |
| 106 | + // (of size 6 elements for "a1" and 12 elements for "a2" since |
| 107 | + // it stores two floats per cell) |
| 108 | + query.setBuffer("a1", new NativeArray(ctx, 6, Character.class)); |
| 109 | + query.setBuffer("a2", new NativeArray(ctx, 12, Float.class)); |
| 110 | + |
| 111 | + // Submit query |
| 112 | + query.submit(); |
| 113 | + |
| 114 | + HashMap<String, Pair<Long, Long>> result_el = query.resultBufferElements(); |
| 115 | + Assert.assertNotNull(result_el); |
| 116 | + |
| 117 | + byte[] a1 = (byte[]) query.getBuffer("a1"); |
| 118 | + float[] a2 = (float[]) query.getBuffer("a2"); |
| 119 | + |
| 120 | + Assert.assertArrayEquals(a1, new byte[] {'b', 'c', 'd', 'f', 'g', 'h'}); |
| 121 | + |
| 122 | + float[] expected_a2 = |
| 123 | + new float[] {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 5.1f, 5.2f, 6.1f, 6.2f, 7.1f, 7.2f}; |
| 124 | + for (int i = 0; i < a2.length; i++) { |
| 125 | + Assert.assertEquals(a2[i], expected_a2[i], 0.01f); |
| 126 | + } |
137 | 127 | }
|
138 | 128 | }
|
139 | 129 |
|
140 | 130 | @Test(expected = TileDBError.class)
|
141 | 131 | public void wrongDatatype() throws Exception {
|
142 |
| - Array array = new Array(ctx, arrayURI, TILEDB_READ); |
143 |
| - Query query = new Query(array, TILEDB_READ); |
144 |
| - query.addRange(0, (long) 1, (long) 2); |
| 132 | + try (Array array = new Array(ctx, arrayURI, TILEDB_READ); |
| 133 | + Query query = new Query(array, TILEDB_READ)) { |
| 134 | + query.addRange(0, (long) 1, (long) 2); |
| 135 | + } |
145 | 136 | }
|
146 | 137 | }
|
0 commit comments