22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .lang .reflect .Array ;
56import java .util .ArrayList ;
67import java .util .List ;
78import java .util .Map ;
@@ -248,7 +249,6 @@ public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expec
248249 return result ;
249250 }
250251
251-
252252 /**
253253 * Save each object in the database. This method will perform a REPLACE on the existing record so any existing
254254 * data will be overwritten by the data in the passed object. This is a convenience method for
@@ -339,6 +339,13 @@ public <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Object us
339339 return read (readPolicy , clazz , key , entry , resolveDependencies );
340340 }
341341
342+ /**
343+ * Read a record from the repository and map it to an instance of the passed class.
344+ * @param clazz - The type of be returned.
345+ * @param userKey - The key of the record. The namespace and set will be derived from the values specified on the passed class.
346+ * @return The returned mapped record.
347+ * @throws AerospikeException an AerospikeException will be thrown in case of an error.
348+ */
342349 public <T > T read (@ NotNull Class <T > clazz , @ NotNull Object userKey ) throws AerospikeException {
343350 return this .read (clazz , userKey , true );
344351 }
@@ -364,7 +371,7 @@ private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key,
364371 } else {
365372 try {
366373 ThreadLocalKeySaver .save (key );
367- T result = ( T ) convertToObject (clazz , record , entry , resolveDepenencies );
374+ T result = convertToObject (clazz , record , entry , resolveDepenencies );
368375 return result ;
369376 } catch (ReflectiveOperationException e ) {
370377 throw new AerospikeException (e );
@@ -375,7 +382,68 @@ private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key,
375382 }
376383 }
377384
378-
385+ /**
386+ * Read a batch of records from the repository and map them to an instance of the passed class.
387+ * @param clazz - The type of be returned.
388+ * @param userKeys - The keys of the record. The namespace and set will be derived from the values specified on the passed class.
389+ * @return The returned mapped records.
390+ * @throws AerospikeException an AerospikeException will be thrown in case of an error.
391+ */
392+ public <T > T [] read (@ NotNull Class <T > clazz , @ NotNull Object ... userKeys ) throws AerospikeException {
393+ return this .read (null , clazz , userKeys );
394+ }
395+
396+ /**
397+ * Read a batch of records from the repository and map them to an instance of the passed class.
398+ * @param batchPolicy A given batch policy.
399+ * @param clazz - The type of be returned.
400+ * @param userKeys - The keys of the record. The namespace and set will be derived from the values specified on the passed class.
401+ * @return The returned mapped records.
402+ * @throws AerospikeException an AerospikeException will be thrown in case of an error.
403+ */
404+ public <T > T [] read (BatchPolicy batchPolicy , @ NotNull Class <T > clazz , @ NotNull Object ... userKeys ) throws AerospikeException {
405+ ClassCacheEntry <T > entry = getEntryAndValidateNamespace (clazz );
406+ String set = entry .getSetName ();
407+ Key [] keys = new Key [userKeys .length ];
408+ for (int i = 0 ; i < userKeys .length ; i ++) {
409+ if (userKeys [i ] == null ) {
410+ throw new AerospikeException ("Cannot pass null to object " + i + " in multi-read call" );
411+ }
412+ else {
413+ keys [i ] = new Key (entry .getNamespace (), set , Value .get (entry .translateKeyToAerospikeKey (userKeys [i ])));
414+ }
415+ }
416+
417+ return this .readBatch (batchPolicy , clazz , keys , entry );
418+ }
419+
420+ private <T > T [] readBatch (BatchPolicy batchPolicy , @ NotNull Class <T > clazz , @ NotNull Key [] keys , @ NotNull ClassCacheEntry <T > entry ) {
421+ if (batchPolicy == null ) {
422+ batchPolicy = entry .getBatchPolicy ();
423+ }
424+ Record [] records = mClient .get (batchPolicy , keys );
425+ T [] results = (T [])Array .newInstance (clazz , records .length );
426+ for (int i = 0 ; i < records .length ; i ++) {
427+ if (records [i ] == null ) {
428+ results [i ] = null ;
429+ }
430+ else {
431+ try {
432+ ThreadLocalKeySaver .save (keys [i ]);
433+ T result = convertToObject (clazz , records [i ], entry , false );
434+ results [i ] = result ;
435+ } catch (ReflectiveOperationException e ) {
436+ throw new AerospikeException (e );
437+ }
438+ finally {
439+ ThreadLocalKeySaver .clear ();
440+ }
441+ }
442+ }
443+ resolveDependencies (entry );
444+ return results ;
445+ }
446+
379447 public <T > boolean delete (@ NotNull Class <T > clazz , @ NotNull Object userKey ) throws AerospikeException {
380448 return this .delete (null , clazz , userKey );
381449 }
0 commit comments