28
28
29
29
import io .tiledb .libtiledb .*;
30
30
import java .math .BigInteger ;
31
- import java .nio .ByteBuffer ;
32
- import java .nio .ByteOrder ;
31
+ import java .nio .*;
32
+ import java .nio .charset .Charset ;
33
+ import java .nio .charset .StandardCharsets ;
33
34
import java .util .*;
34
35
35
36
/**
@@ -541,19 +542,7 @@ public synchronized Query setBuffer(String attr, long bufferElements) throws Til
541
542
throw new TileDBError ("Number of buffer elements must be >= 1" );
542
543
}
543
544
544
- Datatype dt ;
545
-
546
- try (ArraySchema schema = array .getSchema ()) {
547
- try (Domain domain = schema .getDomain ()) {
548
- if (domain .hasDimension (attr )) {
549
- dt = domain .getDimension (attr ).getType ();
550
- } else {
551
- try (Attribute attribute = schema .getAttribute (attr )) {
552
- dt = attribute .getType ();
553
- }
554
- }
555
- }
556
- }
545
+ Datatype dt = Util .getFieldDatatype (array , attr );
557
546
558
547
int size = Util .castLongToInt (bufferElements * dt .getNativeSize ());
559
548
@@ -1057,33 +1046,33 @@ public Query resetBufferSizes() {
1057
1046
/**
1058
1047
* Return a Java primitive array object as a copy of the attribute buffer
1059
1048
*
1060
- * @param attr attribute name
1049
+ * @param bufferName attribute name
1061
1050
* @return A Java array
1062
1051
* @exception TileDBError A TileDB exception
1063
1052
*/
1064
- public Object getBuffer (String attr ) throws TileDBError {
1065
- if (buffers_ .containsKey (attr )) {
1066
- NativeArray buffer = buffers_ .get (attr ).getSecond ();
1053
+ public Object getBuffer (String bufferName ) throws TileDBError {
1054
+ if (buffers_ .containsKey (bufferName )) {
1055
+ NativeArray buffer = buffers_ .get (bufferName ).getSecond ();
1067
1056
Integer nelements =
1068
1057
(buffer_sizes_
1069
- .get (attr )
1058
+ .get (bufferName )
1070
1059
.getSecond ()
1071
1060
.getitem (0 )
1072
1061
.divide (BigInteger .valueOf (buffer .getNativeTypeSize ())))
1073
1062
.intValue ();
1074
1063
return buffer .toJavaArray (nelements );
1075
- } else if (buffers_ .containsKey (attr )) {
1076
- NativeArray buffer = buffers_ .get (attr ).getSecond ();
1064
+ } else if (buffers_ .containsKey (bufferName )) {
1065
+ NativeArray buffer = buffers_ .get (bufferName ).getSecond ();
1077
1066
Integer nelements =
1078
1067
(buffer_sizes_
1079
- .get (attr )
1068
+ .get (bufferName )
1080
1069
.getSecond ()
1081
1070
.getitem (0 )
1082
1071
.divide (BigInteger .valueOf (buffer .getNativeTypeSize ())))
1083
1072
.intValue ();
1084
1073
return buffer .toJavaArray (nelements );
1085
1074
} else {
1086
- throw new TileDBError ("Query attribute buffer does not exist: " + attr );
1075
+ throw new TileDBError ("Query attribute buffer does not exist: " + bufferName );
1087
1076
}
1088
1077
}
1089
1078
@@ -1102,25 +1091,209 @@ public Pair<ByteBuffer, ByteBuffer> getByteBuffer(String attr) throws TileDBErro
1102
1091
/**
1103
1092
* Return an array containing offsets for a variable attribute buffer
1104
1093
*
1105
- * @param attr attribute name
1094
+ * @param bufferName attribute name
1106
1095
* @return A Java long[] array
1107
1096
* @throws TileDBError A TileDB exception
1108
1097
*/
1109
- public long [] getVarBuffer (String attr ) throws TileDBError {
1110
- if (!buffers_ .containsKey (attr )) {
1111
- throw new TileDBError ("Query variable attribute buffer does not exist: " + attr );
1098
+ public long [] getVarBuffer (String bufferName ) throws TileDBError {
1099
+ if (!buffers_ .containsKey (bufferName )) {
1100
+ throw new TileDBError ("Query variable attribute buffer does not exist: " + bufferName );
1112
1101
}
1113
- NativeArray buffer = buffers_ .get (attr ).getFirst ();
1102
+ NativeArray buffer = buffers_ .get (bufferName ).getFirst ();
1114
1103
Integer nelements =
1115
1104
(buffer_sizes_
1116
- .get (attr )
1105
+ .get (bufferName )
1117
1106
.getFirst ()
1118
1107
.getitem (0 )
1119
1108
.divide (BigInteger .valueOf (buffer .getNativeTypeSize ())))
1120
1109
.intValue ();
1121
1110
return (long []) buffer .toJavaArray (nelements );
1122
1111
}
1123
1112
1113
+ /**
1114
+ * Retrieves an IntBuffer of an attribute attr of type Integer
1115
+ *
1116
+ * @param bufferName The attribute name
1117
+ * @return The IntBuffer
1118
+ * @throws TileDBError A TileDB exception
1119
+ */
1120
+ public Pair <LongBuffer , IntBuffer > getIntBuffer (String bufferName ) throws TileDBError {
1121
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1122
+
1123
+ if (dt .javaClass () != Integer .class )
1124
+ throw new TileDBError (
1125
+ "IntBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1126
+
1127
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1128
+ if (byteBuffers_ .containsKey (bufferName )) {
1129
+ LongBuffer offsets = null ;
1130
+ if (buffer .getFirst () != null ) offsets = buffer .getFirst ().asLongBuffer ();
1131
+ return new Pair (offsets , buffer .getSecond ().asIntBuffer ());
1132
+ } else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1133
+ }
1134
+
1135
+ /**
1136
+ * Retrieves a LongBuffer of an attribute bufferName of type Long
1137
+ *
1138
+ * @param bufferName The attribute name
1139
+ * @return The IntBuffer
1140
+ * @throws TileDBError A TileDB exception
1141
+ */
1142
+ public Pair <LongBuffer , LongBuffer > getLongBuffer (String bufferName ) throws TileDBError {
1143
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1144
+
1145
+ if (dt .javaClass () != Long .class )
1146
+ throw new TileDBError (
1147
+ "LongBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1148
+
1149
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1150
+ if (byteBuffers_ .containsKey (bufferName ))
1151
+ return new Pair (buffer .getFirst ().asLongBuffer (), buffer .getSecond ().asLongBuffer ());
1152
+ else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1153
+ }
1154
+
1155
+ /**
1156
+ * Retrieves the CharBuffer of an attribute bufferName of type Char
1157
+ *
1158
+ * @param bufferName The attribute name
1159
+ * @return The CharBuffer
1160
+ * @throws TileDBError A TileDB exception
1161
+ */
1162
+ public Pair <LongBuffer , ShortBuffer > getShortBuffer (String bufferName ) throws TileDBError {
1163
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1164
+
1165
+ if (dt .javaClass () != Short .class )
1166
+ throw new TileDBError (
1167
+ "ShortBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1168
+
1169
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1170
+ if (byteBuffers_ .containsKey (bufferName )) {
1171
+ LongBuffer offsets = null ;
1172
+ if (buffer .getFirst () != null ) offsets = buffer .getFirst ().asLongBuffer ();
1173
+ return new Pair (offsets , buffer .getSecond ().asShortBuffer ());
1174
+ } else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1175
+ }
1176
+
1177
+ /**
1178
+ * Retrieves the CharBuffer of an attribute bufferName of type Char
1179
+ *
1180
+ * @param bufferName The attribute name
1181
+ * @return The CharBuffer
1182
+ * @throws TileDBError A TileDB exception
1183
+ */
1184
+ public Pair <LongBuffer , CharBuffer > getCharBuffer (String bufferName ) throws TileDBError {
1185
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1186
+
1187
+ if (dt .javaClass () != Byte .class )
1188
+ throw new TileDBError (
1189
+ "CharBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1190
+
1191
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1192
+ if (byteBuffers_ .containsKey (bufferName )) {
1193
+ LongBuffer offsets = null ;
1194
+ if (buffer .getFirst () != null ) offsets = buffer .getFirst ().asLongBuffer ();
1195
+
1196
+ // Set the US_ASCII charset and decode, so each character is treated as a single byte instead
1197
+ // of two.
1198
+ Charset charset = StandardCharsets .US_ASCII ;
1199
+ CharBuffer charBuffer = charset .decode (buffer .getSecond ());
1200
+ return new Pair (offsets , charBuffer );
1201
+ } else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1202
+ }
1203
+
1204
+ /**
1205
+ * Retrieves the an FloatBuffer of an attribute bufferName of type Float
1206
+ *
1207
+ * @param bufferName The attribute name
1208
+ * @return The FloatBuffer
1209
+ * @throws TileDBError A TileDB exception
1210
+ */
1211
+ public Pair <LongBuffer , FloatBuffer > getFloatBuffer (String bufferName ) throws TileDBError {
1212
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1213
+
1214
+ if (dt .javaClass () != Float .class )
1215
+ throw new TileDBError (
1216
+ "FloatBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1217
+
1218
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1219
+ if (byteBuffers_ .containsKey (bufferName )) {
1220
+ LongBuffer offsets = null ;
1221
+ if (buffer .getFirst () != null ) offsets = buffer .getFirst ().asLongBuffer ();
1222
+ return new Pair (offsets , buffer .getSecond ().asFloatBuffer ());
1223
+ } else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1224
+ }
1225
+
1226
+ /**
1227
+ * Retrieves the an DoubleBuffer of an attribute bufferName of type Double
1228
+ *
1229
+ * @param bufferName The attribute name
1230
+ * @return The DoubleBuffer
1231
+ * @throws TileDBError A TileDB exception
1232
+ */
1233
+ public Pair <LongBuffer , DoubleBuffer > getDoubleBuffer (String bufferName ) throws TileDBError {
1234
+ Datatype dt = Util .getFieldDatatype (array , bufferName );
1235
+
1236
+ if (dt .javaClass () != Double .class )
1237
+ throw new TileDBError (
1238
+ "DoubleBuffer requested, but attribute " + bufferName + " has type " + dt .name ());
1239
+
1240
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1241
+ if (byteBuffers_ .containsKey (bufferName )) {
1242
+ LongBuffer offsets = null ;
1243
+ if (buffer .getFirst () != null ) offsets = buffer .getFirst ().asLongBuffer ();
1244
+ return new Pair (offsets , buffer .getSecond ().asDoubleBuffer ());
1245
+ } else throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1246
+ }
1247
+
1248
+ /**
1249
+ * Drains a ByteBuffer and returns its contents as a byte[] array
1250
+ *
1251
+ * @param bufferName The attribute name
1252
+ * @return The byte[] array
1253
+ * @throws TileDBError A TileDB exception
1254
+ */
1255
+ public byte [] getByteArray (String bufferName ) throws TileDBError {
1256
+ ByteBuffer buffer = this .byteBuffers_ .get (bufferName ).getSecond ();
1257
+ if (byteBuffers_ .containsKey (bufferName )) {
1258
+
1259
+ byte [] bytes = new byte [buffer .limit ()];
1260
+ int idx = 0 ;
1261
+ while (buffer .hasRemaining ()) bytes [idx ++] = buffer .get ();
1262
+
1263
+ // Reset buffer position after draining, so it can be reused.
1264
+ buffer .flip ();
1265
+
1266
+ return bytes ;
1267
+ }
1268
+
1269
+ throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1270
+ }
1271
+
1272
+ /**
1273
+ * Drains a variable-sized buffer and returns its offsets as a byte[] Array
1274
+ *
1275
+ * @param bufferName The attribute name
1276
+ * @return The byte[] array
1277
+ * @throws TileDBError A TileDB exception
1278
+ */
1279
+ public long [] getOffsetArray (String bufferName ) throws TileDBError {
1280
+ Pair <ByteBuffer , ByteBuffer > buffer = this .byteBuffers_ .get (bufferName );
1281
+ if (byteBuffers_ .containsKey (bufferName )) {
1282
+ LongBuffer offsets = null ;
1283
+ if (buffer .getFirst () != null ) {
1284
+ offsets = buffer .getFirst ().asLongBuffer ();
1285
+
1286
+ long [] offsetArr = new long [offsets .limit ()];
1287
+ int idx = 0 ;
1288
+ while (offsets .hasRemaining ()) offsetArr [idx ++] = offsets .get ();
1289
+
1290
+ return offsetArr ;
1291
+ }
1292
+ }
1293
+
1294
+ throw new TileDBError ("ByteBuffer does not exist for attribute: " + bufferName );
1295
+ }
1296
+
1124
1297
/**
1125
1298
* Returns the result size estimate for each attribute/dimension
1126
1299
*
0 commit comments