@@ -362,15 +362,31 @@ public static class VirtualList<E> {
362362 private final ClassCacheEntry <?> elementEntry ;
363363 private final String binName ;
364364 private final ListMapper listMapper ;
365- private final Key key ;
365+ private Key key ;
366366 private final EmbedType listType ;
367367 private final EmbedType elementType ;
368368 private final Function <Object , Object > instanceMapper ;
369369
370+ public VirtualList (@ NotNull AeroMapper mapper , @ NotNull Class <?> owningClazz , @ NotNull Object key , @ NotNull String binName , @ NotNull Class <E > clazz ) {
371+ this (mapper , null , owningClazz , key , binName , clazz );
372+ }
370373
371374 public VirtualList (@ NotNull AeroMapper mapper , @ NotNull Object object , @ NotNull String binName , @ NotNull Class <E > clazz ) {
372- Class <?> owningClazz = object .getClass ();
375+ this (mapper , object , null , null , binName , clazz );
376+ }
377+
378+ private VirtualList (@ NotNull AeroMapper mapper , Object object , Class <?> owningClazz , Object key , @ NotNull String binName , @ NotNull Class <E > clazz ) {
379+ if (object != null ) {
380+ owningClazz = object .getClass ();
381+ }
373382 this .owningEntry = (ClassCacheEntry <?>) ClassCache .getInstance ().loadClass (owningClazz , mapper );
383+ Object aerospikeKey ;
384+ if (key == null ) {
385+ aerospikeKey = owningEntry .getKey (object );
386+ }
387+ else {
388+ aerospikeKey = owningEntry .translateKeyToAerospikeKey (key );
389+ }
374390 this .elementEntry = (ClassCacheEntry <?>) ClassCache .getInstance ().loadClass (clazz , mapper );
375391 this .mapper = mapper ;
376392 this .binName = binName ;
@@ -383,7 +399,7 @@ public VirtualList(@NotNull AeroMapper mapper, @NotNull Object object, @NotNull
383399 // Use the null set
384400 set = null ;
385401 }
386- key = new Key (owningEntry .getNamespace (), set , Value .get (owningEntry . getKey ( object ) ));
402+ this . key = new Key (owningEntry .getNamespace (), set , Value .get (aerospikeKey ));
387403
388404 AnnotatedType annotatedType = value .getAnnotatedType ();
389405 AerospikeEmbed embed = annotatedType .getAnnotation (AerospikeEmbed .class );
@@ -405,8 +421,18 @@ public VirtualList(@NotNull AeroMapper mapper, @NotNull Object object, @NotNull
405421 throw new AerospikeException (String .format ("Bin %s on class %s is not mapped via a listMapper. This is unexpected" , binName , clazz .getSimpleName ()));
406422 }
407423 this .instanceMapper = src -> listMapper .fromAerospikeInstanceFormat (src );
408- }
424+
425+ }
409426
427+ public VirtualList <E > changeKey (Object newKey ) {
428+ String set = owningEntry .getSetName ();
429+ if ("" .equals (set )) {
430+ // Use the null set
431+ set = null ;
432+ }
433+ this .key = new Key (owningEntry .getNamespace (), set , Value .get (owningEntry .translateKeyToAerospikeKey (key )));
434+ return this ;
435+ }
410436 public VirtualList <E > keptInSync (boolean inSync ) {
411437 return this ;
412438 }
@@ -429,8 +455,7 @@ public MultiOperation<E> append(E item) {
429455 return this ;
430456 }
431457 public MultiOperation <E > removeByKeyRange (Object startKey , Object endKey ) {
432- // TODO: Be able to change the return type based on the asResult() function call
433- this .interactions .add (getRemoveRangeInteractor (startKey , endKey , true ));
458+ this .interactions .add (getRemoveRangeInteractor (startKey , endKey ));
434459 return this ;
435460 }
436461
@@ -453,6 +478,7 @@ else if (this.indexToReturn >= 0) {
453478 }
454479 else {
455480 this .indexToReturn = this .interactions .size () - 1 ;
481+ this .interactions .get (indexToReturn ).setNeedsResult (true );
456482 }
457483 return this ;
458484 }
@@ -526,33 +552,49 @@ public List<E> removeByKeyRange(WritePolicy writePolicy, Object startKey, Object
526552 writePolicy = new WritePolicy (owningEntry .getWritePolicy ());
527553 writePolicy .recordExistsAction = RecordExistsAction .UPDATE ;
528554 }
529- Interactor interactor = getRemoveRangeInteractor (startKey , endKey , returnResults );
555+ Interactor interactor = getRemoveRangeInteractor (startKey , endKey );
556+ interactor .setNeedsResult (returnResults );
530557 Record record = this .mapper .mClient .operate (writePolicy , key , interactor .getOperation ());
531558
532559 return (List <E >)interactor .getResult (record .getList (binName ));
533560 }
534561
535- private Interactor getRemoveRangeInteractor (Object startKey , Object endKey , boolean returnResults ) {
562+ private Interactor getRemoveRangeInteractor (Object startKey , Object endKey ) {
536563 Object aerospikeStartKey = elementEntry .translateKeyToAerospikeKey (startKey );
537564 Object aerospikeEndKey = elementEntry .translateKeyToAerospikeKey (endKey );
538- Interactor interactor ;
539- if (listType == EmbedType .LIST ) {
540- if (returnResults ) {
541- interactor = new Interactor (ListOperation .removeByValueRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), ListReturnType .VALUE ), new ArrayUnpacker (instanceMapper ));
542- }
543- else {
544- interactor = new Interactor (ListOperation .removeByValueRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), ListReturnType .NONE ));
545- }
546- }
547- else {
548- if (returnResults ) {
549- interactor = new Interactor ( MapOperation .removeByKeyRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), MapReturnType .KEY_VALUE ), new ArrayUnpacker (instanceMapper ));
550- }
551- else {
552- interactor = new Interactor ( MapOperation .removeByKeyRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), MapReturnType .NONE ));
553- }
554- }
555- return interactor ;
565+ DeferredOperation deferred = new DeferredOperation () {
566+
567+ @ Override
568+ public ResultsUnpacker [] getUnpackers (OperationParameters operationParams ) {
569+ if (operationParams .needsResult ()) {
570+ return new ResultsUnpacker [] { new ArrayUnpacker (instanceMapper ) };
571+ }
572+ else {
573+ return new ResultsUnpacker [0 ];
574+ }
575+ }
576+
577+ @ Override
578+ public Operation getOperation (OperationParameters operationParams ) {
579+ if (listType == EmbedType .LIST ) {
580+ if (operationParams .needsResult ()) {
581+ return ListOperation .removeByValueRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), ListReturnType .VALUE );
582+ }
583+ else {
584+ return ListOperation .removeByValueRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), ListReturnType .NONE );
585+ }
586+ }
587+ else {
588+ if (operationParams .needsResult ()) {
589+ return MapOperation .removeByKeyRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), MapReturnType .KEY_VALUE );
590+ }
591+ else {
592+ return MapOperation .removeByKeyRange (binName , Value .get (aerospikeStartKey ), Value .get (aerospikeEndKey ), MapReturnType .NONE );
593+ }
594+ }
595+ }
596+ };
597+ return new Interactor (deferred );
556598 }
557599
558600 private Operation getAppendOperation (Object aerospikeObject ) {
0 commit comments