Skip to content

Commit

Permalink
INTERNAL: Change asycnGets return future type.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Jan 8, 2024
1 parent 97ed4b0 commit 5d2bda8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/spy/memcached/ArcusClientPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ public GetFuture<Object> asyncGet(String key) {
}

@Override
public <T> OperationFuture<CASValue<T>> asyncGets(String key, Transcoder<T> tc) {
public <T> GetFuture<CASValue<T>> asyncGets(String key, Transcoder<T> tc) {
return this.getClient().asyncGets(key, tc);
}

@Override
public OperationFuture<CASValue<Object>> asyncGets(String key) {
public GetFuture<CASValue<Object>> asyncGets(String key) {
return this.getClient().asyncGets(key);
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import net.spy.memcached.internal.GetFuture;
import net.spy.memcached.internal.OperationFuture;
import net.spy.memcached.internal.SingleElementInfiniteIterator;
import net.spy.memcached.internal.result.CASGetResultImpl;
import net.spy.memcached.internal.result.GetResult;
import net.spy.memcached.internal.result.GetResultImpl;
import net.spy.memcached.ops.CASOperationStatus;
import net.spy.memcached.ops.CancelledOperationStatus;
import net.spy.memcached.ops.ConcatenationType;
Expand Down Expand Up @@ -892,15 +894,15 @@ public <T> GetFuture<T> asyncGet(final String key, final Transcoder<T> tc) {

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

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

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

public void complete() {
Expand Down Expand Up @@ -935,16 +937,15 @@ public GetFuture<Object> asyncGet(final String key) {
* @throws IllegalStateException in the rare circumstance where queue
* is too full to accept any more requests
*/
public <T> OperationFuture<CASValue<T>> asyncGets(final String key,
public <T> GetFuture<CASValue<T>> asyncGets(final String key,
final Transcoder<T> tc) {

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

Operation op = opFact.gets(key,
new GetsOperation.Callback() {
private CASValue<T> val = null;
private GetResult<CASValue<T>> val = null;

public void receivedStatus(OperationStatus status) {
rv.set(val, status);
Expand All @@ -953,8 +954,7 @@ public void receivedStatus(OperationStatus status) {
public void gotData(String k, int flags, long cas, byte[] data) {
assert key.equals(k) : "Wrong key returned";
assert cas > 0 : "CAS was less than zero: " + cas;
val = new CASValue<T>(cas, tc.decode(
new CachedData(flags, data, tc.getMaxSize())));
val = new CASGetResultImpl<T>(cas, new CachedData(flags, data, tc.getMaxSize()), tc);
}

public void complete() {
Expand All @@ -975,7 +975,7 @@ public void complete() {
* @throws IllegalStateException in the rare circumstance where queue
* is too full to accept any more requests
*/
public OperationFuture<CASValue<Object>> asyncGets(final String key) {
public GetFuture<CASValue<Object>> asyncGets(final String key) {
return asyncGets(key, transcoder);
}

Expand All @@ -992,7 +992,7 @@ public OperationFuture<CASValue<Object>> asyncGets(final String key) {
* is too full to accept any more requests
*/
public <T> CASValue<T> gets(String key, Transcoder<T> tc) {
OperationFuture<CASValue<T>> future = asyncGets(key, tc);
GetFuture<CASValue<T>> future = asyncGets(key, tc);
try {
return future.get(operationTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.spy.memcached.internal.result;

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

public class CASGetResultImpl<T> implements GetResult<CASValue<T>> {

private final long cas;
private final CachedData cachedData;
private final Transcoder<T> transcoder;
private volatile CASValue<T> decodedValue = null;

public CASGetResultImpl(long cas, CachedData cachedData, Transcoder<T> transcoder) {
this.cas = cas;
this.cachedData = cachedData;
this.transcoder = transcoder;
}

@Override
public CASValue<T> getDecodedValue() {
if (decodedValue == null) {
decodedValue = new CASValue<T>(cas, transcoder.decode(cachedData));
}

return decodedValue;
}
}
30 changes: 2 additions & 28 deletions src/main/java/net/spy/memcached/internal/result/GetResult.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
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;
}
public interface GetResult<T> {
T getDecodedValue();
}
28 changes: 28 additions & 0 deletions src/main/java/net/spy/memcached/internal/result/GetResultImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.spy.memcached.internal.result;

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

public class GetResultImpl<T> implements GetResult<T> {
private final Transcoder<T> transcoder;
private final CachedData cachedData;
private volatile T decodedValue = null;

public GetResultImpl(CachedData cachedData, Transcoder<T> transcoder) {
this.cachedData = cachedData;
this.transcoder = transcoder;
}

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

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

return decodedValue;
}
}

0 comments on commit 5d2bda8

Please sign in to comment.