Skip to content

Commit ccdf975

Browse files
limit validity Bytebuffers after query submit. In this commit we use the limit method to limit the number of bytes in the validity ByteBuffers in READ mode. This will use less memory
1 parent 8e20074 commit ccdf975

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,35 @@ public QueryStatus submit() throws TileDBError {
133133
// Set the actual number of bytes received to each ByteBuffer
134134
for (String attribute : byteBuffers_.keySet()) {
135135
boolean isVar;
136+
boolean isNullable = false;
137+
Datatype datatype;
136138

137139
try (ArraySchema arraySchema = array.getSchema()) {
138140
if (arraySchema.hasAttribute(attribute)) {
139141
try (Attribute attr = arraySchema.getAttribute(attribute)) {
140142
isVar = attr.isVar();
143+
isNullable = attr.getNullable();
144+
datatype = attr.getType();
141145
}
142146
} else {
143147
try (Dimension dim = arraySchema.getDomain().getDimension(attribute)) {
144148
isVar = dim.isVar();
149+
datatype = dim.getType();
145150
}
146151
}
147152
}
148153

154+
int nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
155+
this.byteBuffers_.get(attribute).getSecond().limit(nbytes);
156+
149157
if (isVar) {
150158
int offset_nbytes = this.buffer_sizes_.get(attribute).getFirst().getitem(0).intValue();
151-
int data_nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
152159
this.byteBuffers_.get(attribute).getFirst().limit(offset_nbytes);
153-
this.byteBuffers_.get(attribute).getSecond().limit(data_nbytes);
154-
} else {
155-
int nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
156-
this.byteBuffers_.get(attribute).getSecond().limit(nbytes);
160+
}
161+
162+
if (isNullable) {
163+
int validity_nbytes = nbytes / datatype.getNativeSize();
164+
this.validityByteMapsByteBuffers_.get(attribute).limit(validity_nbytes);
157165
}
158166
}
159167

@@ -177,27 +185,35 @@ public QueryStatus submitAndFinalize() throws TileDBError {
177185
// Set the actual number of bytes received to each ByteBuffer
178186
for (String attribute : byteBuffers_.keySet()) {
179187
boolean isVar;
188+
boolean isNullable = false;
189+
Datatype datatype;
180190

181191
try (ArraySchema arraySchema = array.getSchema()) {
182192
if (arraySchema.hasAttribute(attribute)) {
183193
try (Attribute attr = arraySchema.getAttribute(attribute)) {
184194
isVar = attr.isVar();
195+
isNullable = attr.getNullable();
196+
datatype = attr.getType();
185197
}
186198
} else {
187199
try (Dimension dim = arraySchema.getDomain().getDimension(attribute)) {
188200
isVar = dim.isVar();
201+
datatype = dim.getType();
189202
}
190203
}
191204
}
192205

206+
int nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
207+
this.byteBuffers_.get(attribute).getSecond().limit(nbytes);
208+
193209
if (isVar) {
194210
int offset_nbytes = this.buffer_sizes_.get(attribute).getFirst().getitem(0).intValue();
195-
int data_nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
196211
this.byteBuffers_.get(attribute).getFirst().limit(offset_nbytes);
197-
this.byteBuffers_.get(attribute).getSecond().limit(data_nbytes);
198-
} else {
199-
int nbytes = this.buffer_sizes_.get(attribute).getSecond().getitem(0).intValue();
200-
this.byteBuffers_.get(attribute).getSecond().limit(nbytes);
212+
}
213+
214+
if (isNullable) {
215+
int validity_nbytes = nbytes / datatype.getNativeSize();
216+
this.validityByteMapsByteBuffers_.get(attribute).limit(validity_nbytes);
201217
}
202218
}
203219

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,15 @@ public void denseArrayNIOReadTest() throws Exception {
12381238
// Submit query
12391239
query.submit();
12401240

1241+
Assert.assertEquals(4, a1byteMap.limit());
1242+
1243+
try {
1244+
a1byteMap.get(5);
1245+
Assert.fail();
1246+
} catch (IndexOutOfBoundsException e) {
1247+
// do nothing. This is expected
1248+
}
1249+
12411250
int[] dim1 = new int[4];
12421251
int[] dim2 = new int[4];
12431252
byte[] a1 = new byte[4];
@@ -1401,6 +1410,24 @@ public void sparseArrayNIOReadTest() throws Exception {
14011410
// Submit query
14021411
query.submit();
14031412

1413+
Assert.assertEquals(5, a1byteMap.limit());
1414+
1415+
try {
1416+
a1byteMap.get(6);
1417+
Assert.fail();
1418+
} catch (IndexOutOfBoundsException e) {
1419+
// do nothing. This is expected
1420+
}
1421+
1422+
Assert.assertEquals(10, a2byteMapBuffer.limit());
1423+
1424+
try {
1425+
a2byteMapBuffer.get(11);
1426+
Assert.fail();
1427+
} catch (IndexOutOfBoundsException e) {
1428+
// do nothing. This is expected
1429+
}
1430+
14041431
Assert.assertEquals(
14051432
5L,
14061433
(long)
@@ -1436,6 +1463,7 @@ public void sparseArrayNIOReadTest() throws Exception {
14361463

14371464
int idx = 0;
14381465
int bytesIdx = 0;
1466+
14391467
while (a1Buffer.hasRemaining()) {
14401468
a1Values[idx] = a1Buffer.getInt();
14411469
a1ByteMapValues[idx] = a1byteMap.get();

0 commit comments

Comments
 (0)