Skip to content

Commit

Permalink
CLEANUP: Remove TranscoderService from asyncGet()
Browse files Browse the repository at this point in the history
  • Loading branch information
uhm0311 authored and jhpark816 committed Dec 20, 2023
1 parent 632769b commit fb61e7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.spy.memcached.internal.GetFuture;
import net.spy.memcached.internal.OperationFuture;
import net.spy.memcached.internal.SingleElementInfiniteIterator;
import net.spy.memcached.internal.result.GetResult;
import net.spy.memcached.ops.CASOperationStatus;
import net.spy.memcached.ops.CancelledOperationStatus;
import net.spy.memcached.ops.ConcatenationType;
Expand Down Expand Up @@ -887,29 +888,28 @@ public OperationFuture<Boolean> replace(String key, int exp, Object o) {
public <T> GetFuture<T> asyncGet(final String key, final Transcoder<T> tc) {

final CountDownLatch latch = new CountDownLatch(1);
final GetFuture<T> rv = new GetFuture<T>(latch, operationTimeout);
final GetFuture<T> future = new GetFuture<T>(latch, operationTimeout);

Operation op = opFact.get(key,
new GetOperation.Callback() {
private Future<T> val = null;
private final GetResult<T> result = new GetResult<T>(tc);

public void receivedStatus(OperationStatus status) {
rv.set(val, status);
future.set(result, status);
}

public void gotData(String k, int flags, byte[] data) {
assert key.equals(k) : "Wrong key returned";
val = tcService.decode(tc,
new CachedData(flags, data, tc.getMaxSize()));
result.setCachedData(new CachedData(flags, data, tc.getMaxSize()));
}

public void complete() {
latch.countDown();
}
});
rv.setOperation(op);
future.setOperation(op);
addOp(key, op);
return rv;
return future;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/net/spy/memcached/internal/GetFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.internal.result.GetResult;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationStatus;

Expand All @@ -18,10 +19,10 @@
*/
public class GetFuture<T> implements Future<T> {

private final OperationFuture<Future<T>> rv;
private final OperationFuture<GetResult<T>> rv;

public GetFuture(CountDownLatch l, long opTimeout) {
this.rv = new OperationFuture<Future<T>>(l, opTimeout);
this.rv = new OperationFuture<GetResult<T>>(l, opTimeout);
}

public GetFuture(GetFuture<T> parent) {
Expand All @@ -33,22 +34,22 @@ public boolean cancel(boolean ign) {
}

public T get() throws InterruptedException, ExecutionException {
Future<T> decodedTask = rv.get();
return decodedTask == null ? null : decodedTask.get();
GetResult<T> result = rv.get();
return result == null ? null : result.getDecodedValue();
}

public T get(long duration, TimeUnit units)
throws InterruptedException, TimeoutException, ExecutionException {
Future<T> decodedTask = rv.get(duration, units);
return decodedTask == null ? null : decodedTask.get();
GetResult<T> result = rv.get(duration, units);
return result == null ? null : result.getDecodedValue();
}

public OperationStatus getStatus() {
return rv.getStatus();
}

public void set(Future<T> decodedTask, OperationStatus status) {
rv.set(decodedTask, status);
public void set(GetResult<T> result, OperationStatus status) {
rv.set(result, status);
}

public void setOperation(Operation to) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/net/spy/memcached/internal/result/GetResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.spy.memcached.internal.result;

import net.spy.memcached.CachedData;
import net.spy.memcached.transcoders.Transcoder;

public final class GetResult<T> {
private final Transcoder<T> transcoder;

private volatile CachedData cachedData = null;
private volatile T decodedValue = null;

public GetResult(Transcoder<T> transcoder) {
this.transcoder = transcoder;
}

public void setCachedData(CachedData cachedData) {
this.cachedData = cachedData;
}

public T getDecodedValue() {
if (cachedData == null) {
return null;
}

if (decodedValue == null) {
decodedValue = transcoder.decode(cachedData);
}

return decodedValue;
}
}

0 comments on commit fb61e7a

Please sign in to comment.