Skip to content

Commit 1bf97dc

Browse files
committed
Allowed parameters to operations to defer evaluation until needed
1 parent 84609ab commit 1bf97dc

File tree

5 files changed

+117
-29
lines changed

5 files changed

+117
-29
lines changed

src/main/java/com/aerospike/mapper/tools/AeroMapper.java

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.aerospike.mapper.tools;
2+
3+
import com.aerospike.client.Operation;
4+
5+
public interface DeferredOperation {
6+
public Operation getOperation(OperationParameters operationParams);
7+
public ResultsUnpacker[] getUnpackers(OperationParameters operationParams);
8+
}

src/main/java/com/aerospike/mapper/tools/Interactor.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,40 @@
22

33
import javax.validation.constraints.NotNull;
44

5+
import com.aerospike.client.AerospikeException;
56
import com.aerospike.client.Operation;
67

78
public class Interactor {
8-
private final Operation operation;
9-
private final ResultsUnpacker []resultsUnpackers;
9+
private Operation operation;
10+
private DeferredOperation deferredOperation;
11+
private final OperationParameters deferredParameters;
12+
private ResultsUnpacker []resultsUnpackers;
1013

1114
public Interactor(@NotNull Operation operation, @NotNull ResultsUnpacker ... resultsUnpackers) {
1215
super();
1316
this.operation = operation;
1417
this.resultsUnpackers = resultsUnpackers;
18+
this.deferredParameters = null;
19+
}
20+
public Interactor(@NotNull DeferredOperation deferredOperation) {
21+
super();
22+
this.deferredOperation = deferredOperation;
23+
this.deferredParameters = new OperationParameters();
24+
}
25+
26+
public void setNeedsResult(boolean needsResult) {
27+
if (this.deferredParameters != null) {
28+
this.deferredParameters.setNeedsResult(needsResult);
29+
}
1530
}
1631
public Operation getOperation() {
32+
if (operation == null && deferredOperation != null) {
33+
operation = deferredOperation.getOperation(this.deferredParameters);
34+
resultsUnpackers = deferredOperation.getUnpackers(this.deferredParameters);
35+
}
1736
return operation;
1837
}
38+
1939
public Object getResult(Object rawResult) {
2040
Object result = rawResult;
2141
for (ResultsUnpacker thisUnpacker : resultsUnpackers) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.aerospike.mapper.tools;
2+
3+
public class OperationParameters {
4+
private boolean needsResult;
5+
6+
public OperationParameters() {
7+
}
8+
public OperationParameters(boolean needsResult) {
9+
super();
10+
this.needsResult = needsResult;
11+
}
12+
public boolean needsResult() {
13+
return needsResult;
14+
}
15+
public void setNeedsResult(boolean needsResult) {
16+
this.needsResult = needsResult;
17+
}
18+
}

src/test/java/com/aerospike/mapper/CollectionMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void test() {
7171
AeroMapper mapper = new AeroMapper.Builder(client).build();
7272
mapper.save(collection);
7373

74-
VirtualList<CollectionElement> list = mapper.asBackedList(collection, "elements", CollectionElement.class).keptInSync(true);
74+
VirtualList<CollectionElement> list = mapper.asBackedList(collection, "elements", CollectionElement.class);
7575
// list.append(new CollectionElement(103, "tom", 45678));
7676
// System.out.println("Get by index returned: " + list.get(2));
7777
// System.out.println("Delete by Key Range returned: " + list.removeByKeyRange(100, 102, true));

0 commit comments

Comments
 (0)