Skip to content

Commit

Permalink
INTERNAL: Change decode logic in collection Get apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 authored and jhpark816 committed Mar 28, 2024
1 parent fb4f324 commit 611e612
Show file tree
Hide file tree
Showing 21 changed files with 422 additions and 295 deletions.
315 changes: 93 additions & 222 deletions src/main/java/net/spy/memcached/ArcusClient.java

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions src/main/java/net/spy/memcached/collection/BTreeGetResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package net.spy.memcached.collection;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
Expand All @@ -25,14 +26,24 @@
import net.spy.memcached.transcoders.Transcoder;

public class BTreeGetResult<K, V> {

private final SortedMap<K, BTreeElement<K, V>> elements;
private final CollectionOperationStatus opStatus;
private final SortedMap<K, BTreeElement<K, V>> elements;

public BTreeGetResult(SortedMap<K, BTreeElement<K, V>> elements,
public BTreeGetResult(List<BTreeElement<K, CachedData>> elementList,
boolean reverse, Transcoder<V> transcoder,
CollectionOperationStatus opStatus) {
this.elements = elements;
this.opStatus = opStatus;

if (elementList == null) {
this.elements = null;
return;
}
this.elements = new ByteArrayTreeMap<K, BTreeElement<K, V>>(
reverse ? Collections.reverseOrder() : null);
for (BTreeElement<K, CachedData> elem : elementList) {
elements.put(elem.getBkey(), new BTreeElement<K, V>(elem.getBkey(), elem.getEflag(),
transcoder.decode(elem.getValue())));
}
}

public Map<K, BTreeElement<K, V>> getElements() {
Expand All @@ -42,14 +53,4 @@ public Map<K, BTreeElement<K, V>> getElements() {
public CollectionOperationStatus getCollectionResponse() {
return opStatus;
}

public void addElements(List<BTreeElement<K, CachedData>> cachedData, Transcoder<V> tc) {
if (elements != null && elements.isEmpty()) {
for (BTreeElement<K, CachedData> elem : cachedData) {
BTreeElement<K, V> decodedElem =
new BTreeElement<K, V>(elem.getBkey(), elem.getEflag(), tc.decode(elem.getValue()));
elements.put(decodedElem.getBkey(), decodedElem);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import net.spy.memcached.OperationTimeoutException;
import net.spy.memcached.collection.Element;
import net.spy.memcached.ops.CollectionGetOpCallback;
import net.spy.memcached.internal.result.GetResult;

/**
* Future object that contains an b+tree element object
Expand All @@ -34,7 +34,7 @@
*/
public class BTreeStoreAndGetFuture<T, E> extends CollectionFuture<T> {

private Element<E> element;
private GetResult<Element<E>> element;

public BTreeStoreAndGetFuture(CountDownLatch l, long opTimeout) {
this(l, new AtomicReference<T>(null), opTimeout);
Expand All @@ -54,13 +54,10 @@ public Element<E> getElement() {
} catch (TimeoutException e) {
throw new OperationTimeoutException(e);
}
CollectionGetOpCallback callback = (CollectionGetOpCallback) op.getCallback();
callback.addResult();
return element;
return element == null ? null : element.getDecodedValue();
}

public void setElement(Element<E> element) {
public void setElement(GetResult<Element<E>> element) {
this.element = element;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.OperationTimeoutException;
import net.spy.memcached.ops.CollectionGetOpCallback;
import net.spy.memcached.internal.result.GetResult;
import net.spy.memcached.ops.CollectionOperationStatus;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationState;
Expand All @@ -37,9 +37,10 @@ public class CollectionGetBulkFuture<T> implements Future<T> {
private final Collection<Operation> ops;
private final long timeout;
private final CountDownLatch latch;
private final T result;
private final GetResult<T> result;

public CollectionGetBulkFuture(CountDownLatch latch, Collection<Operation> ops, T result,
public CollectionGetBulkFuture(CountDownLatch latch, Collection<Operation> ops,
GetResult<T> result,
long timeout) {
this.latch = latch;
this.ops = ops;
Expand Down Expand Up @@ -85,12 +86,7 @@ public T get(long duration, TimeUnit units)
throw new ExecutionException(new RuntimeException(op.getCancelCause()));
}
}
for (Operation op : ops) {
CollectionGetOpCallback callback = (CollectionGetOpCallback) op.getCallback();
callback.addResult();
}

return result;
return result == null ? null : result.getDecodedValue();
}

@Override
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/net/spy/memcached/internal/CollectionGetFuture.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package net.spy.memcached.internal;

import net.spy.memcached.ops.CollectionGetOpCallback;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.internal.result.GetResult;
import net.spy.memcached.ops.CollectionOperationStatus;

public class CollectionGetFuture<T> extends CollectionFuture<T> {
private GetResult<T> result;

public CollectionGetFuture(CountDownLatch l, long opTimeout) {
super(l, opTimeout);
Expand All @@ -16,12 +18,12 @@ public CollectionGetFuture(CountDownLatch l, long opTimeout) {
@Override
public T get(long duration, TimeUnit units)
throws InterruptedException, TimeoutException, ExecutionException {
super.get(duration, units); // for waiting latch.
return result == null ? null : result.getDecodedValue();
}

T result = super.get(duration, units);
if (result != null) {
CollectionGetOpCallback callback = (CollectionGetOpCallback) op.getCallback();
callback.addResult();
}
return result;
public void setResult(GetResult<T> result, CollectionOperationStatus status) {
super.set(null, status);
this.result = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.spy.memcached.internal.result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.spy.memcached.CachedData;
import net.spy.memcached.collection.BTreeElement;
import net.spy.memcached.collection.BTreeGetResult;
import net.spy.memcached.ops.CollectionOperationStatus;
import net.spy.memcached.transcoders.Transcoder;

public class BopGetBulkResultImpl<K, V> implements GetResult<Map<String, BTreeGetResult<K, V>>> {
private final Map<String, List<BTreeElement<K, CachedData>>> cachedDataMap;
private final Map<String, CollectionOperationStatus> opStatusMap;
private final boolean reverse;
private final Transcoder<V> transcoder;
private Map<String, BTreeGetResult<K, V>> result
= new HashMap<String, BTreeGetResult<K, V>>();

public BopGetBulkResultImpl(Map<String, List<BTreeElement<K, CachedData>>> cachedDataMap,
Map<String, CollectionOperationStatus> opStatusMap,
boolean reverse, Transcoder<V> transcoder) {
this.cachedDataMap = cachedDataMap;
this.opStatusMap = opStatusMap;
this.reverse = reverse;
this.transcoder = transcoder;
}

@Override
public Map<String, BTreeGetResult<K, V>> getDecodedValue() {
if (result.isEmpty() && !opStatusMap.isEmpty()) {
Map<String, BTreeGetResult<K, V>> temp = new HashMap<String, BTreeGetResult<K, V>>(result);
for (Map.Entry<String, CollectionOperationStatus> entry : opStatusMap.entrySet()) {
String key = entry.getKey();
temp.put(key, new BTreeGetResult<K, V>(cachedDataMap.get(key),
reverse, transcoder, entry.getValue()));
}
result = temp;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.spy.memcached.internal.result;

import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import net.spy.memcached.CachedData;
import net.spy.memcached.collection.BKeyObject;
import net.spy.memcached.collection.Element;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.BTreeUtil;

public class BopGetByPositionResultImpl<T> implements GetResult<Map<Integer, Element<T>>> {
private final Map<Integer, Map.Entry<BKeyObject, CachedData>> cachedDataMap;
private final Transcoder<T> transcoder;
private SortedMap<Integer, Element<T>> result;

public BopGetByPositionResultImpl(Map<Integer, Map.Entry<BKeyObject, CachedData>> cachedDataMap,
boolean reverse,
Transcoder<T> transcoder) {
this.cachedDataMap = cachedDataMap;
this.result = new TreeMap<Integer, Element<T>>((reverse) ? Collections.reverseOrder() : null);
this.transcoder = transcoder;
}

@Override
public Map<Integer, Element<T>> getDecodedValue() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
SortedMap<Integer, Element<T>> temp = new TreeMap<Integer, Element<T>>(result);
for (Map.Entry<Integer, Map.Entry<BKeyObject, CachedData>> entry : cachedDataMap.entrySet()) {
Map.Entry<BKeyObject, CachedData> cachedDataEntry = entry.getValue();
temp.put(entry.getKey(), BTreeUtil.makeBTreeElement(
cachedDataEntry.getKey(), cachedDataEntry.getValue(), transcoder));
}
result = temp;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.spy.memcached.internal.result;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import net.spy.memcached.CachedData;
import net.spy.memcached.collection.BKeyObject;
import net.spy.memcached.collection.ByteArrayBKey;
import net.spy.memcached.collection.ByteArrayTreeMap;
import net.spy.memcached.collection.Element;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.BTreeUtil;

public class BopGetResultImpl<K, V> implements GetResult<Map<K, Element<V>>> {
private final Map<K, CachedData> cachedDataMap;
private final boolean reverse;
private final Transcoder<V> transcoder;
private SortedMap<K, Element<V>> result;

public BopGetResultImpl(Map<K, CachedData> cachedDataMap,
boolean reverse, Transcoder<V> transcoder) {
this.cachedDataMap = cachedDataMap;
this.result = new TreeMap<K, Element<V>>((reverse) ? Collections.reverseOrder() : null);
this.reverse = reverse;
this.transcoder = transcoder;
}

@Override
public Map<K, Element<V>> getDecodedValue() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
Set<Map.Entry<K, CachedData>> entrySet = cachedDataMap.entrySet();
K bKey = entrySet.iterator().next().getKey();

boolean isByteBKey = (bKey instanceof ByteArrayBKey);
boolean isLongBKey = (bKey instanceof Long);

SortedMap<K, Element<V>> temp;

if (isByteBKey) {
temp = new ByteArrayTreeMap<K, Element<V>>(reverse ? Collections.<K>reverseOrder() : null);
} else if (isLongBKey) {
temp = new TreeMap<K, Element<V>>(reverse ? Collections.<K>reverseOrder() : null);
} else {
return result;
}

for (Map.Entry<K, CachedData> entry : cachedDataMap.entrySet()) {
bKey = entry.getKey();
CachedData cachedData = entry.getValue();
if (isByteBKey) {
temp.put(bKey, BTreeUtil.makeBTreeElement(
new BKeyObject((ByteArrayBKey) bKey), cachedData, transcoder));
} else {
temp.put(bKey, BTreeUtil.makeBTreeElement(
new BKeyObject((Long) bKey), cachedData, transcoder));
}
}
result = temp;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.spy.memcached.internal.result;

import net.spy.memcached.CachedData;
import net.spy.memcached.collection.BKeyObject;
import net.spy.memcached.collection.Element;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.BTreeUtil;

public class BopStoreAndGetResultImpl<T> implements GetResult<Element<T>> {
private final BKeyObject bKeyObject;
private final CachedData cachedData;
private final Transcoder<T> transcoder;
private Element<T> result = null;

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

@Override
public Element<T> getDecodedValue() {
if (cachedData != null && result == null) {
result = BTreeUtil.makeBTreeElement(bKeyObject, cachedData, transcoder);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.spy.memcached.internal.result;

import java.util.ArrayList;
import java.util.List;

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

public class LopGetResultImpl<T> implements GetResult<List<T>> {
private final List<CachedData> cachedDataList;
private final Transcoder<T> transcoder;
private List<T> result = new ArrayList<T>();

public LopGetResultImpl(List<CachedData> cachedDataList, Transcoder<T> transcoder) {
this.cachedDataList = cachedDataList;
this.transcoder = transcoder;
}

@Override
public List<T> getDecodedValue() {
if (result.isEmpty() && !cachedDataList.isEmpty()) {
List<T> temp = new ArrayList<T>();
for (CachedData cachedData : cachedDataList) {
temp.add(transcoder.decode(cachedData));
}
result = temp;
}
return result;
}
}
Loading

0 comments on commit 611e612

Please sign in to comment.