Skip to content

Commit

Permalink
INTERNAL: Change decode logic in asyncBopGetByPosition.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Aug 11, 2023
1 parent 3b3b3be commit 2609684
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 89 deletions.
90 changes: 30 additions & 60 deletions src/main/java/net/spy/memcached/ArcusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -3083,13 +3082,15 @@ private <T> CollectionFuture<Map<Integer, Element<T>>> asyncBopGetByPosition(
}

final CountDownLatch latch = new CountDownLatch(1);
final CollectionFuture<Map<Integer, Element<T>>> rv =
new CollectionFuture<Map<Integer, Element<T>>>(latch, operationTimeout);
final CollectionGetFuture<Map<Integer, Element<T>>> rv =
new CollectionGetFuture<Map<Integer, Element<T>>>(latch, operationTimeout);

Operation op = opFact.bopGetByPosition(k, get, new BTreeGetByPositionOperation.Callback() {

private final TreeMap<Integer, Element<T>> map = new TreeMap<Integer, Element<T>>(
(reverse) ? Collections.reverseOrder() : null);
private final TreeMap<Integer, Element<T>> result =
new TreeMap<Integer, Element<T>>((reverse) ? Collections.reverseOrder() : null);
private final HashMap<Integer, Entry<BKeyObject, CachedData>> cachedDataMap
= new HashMap<Integer, Entry<BKeyObject, CachedData>>();

public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
Expand All @@ -3100,7 +3101,7 @@ public void receivedStatus(OperationStatus status) {
cstatus = new CollectionOperationStatus(status);
}
if (cstatus.isSuccess()) {
rv.set(map, cstatus);
rv.set(result, cstatus);
return;
}
switch (cstatus.getResponse()) {
Expand All @@ -3109,7 +3110,7 @@ public void receivedStatus(OperationStatus status) {
getLogger().debug("Key(%s) not found : %s", k, cstatus);
break;
case NOT_FOUND_ELEMENT:
rv.set(map, cstatus);
rv.set(result, cstatus);
getLogger().debug("Element(%s) not found : %s", k, cstatus);
break;
case UNREADABLE:
Expand All @@ -3125,17 +3126,24 @@ public void receivedStatus(OperationStatus status) {
}
}

@Override
public void complete() {
latch.countDown();
}

public void gotData(String key, int flags, int pos, BKeyObject bkeyObject, byte[] eflag,
byte[] data) {
assert key.equals(k) : "Wrong key returned";
Element<T> element = makeBTreeElement(key, flags, bkeyObject, eflag, data, tc);
@Override
public void gotData(int pos, int flags, BKeyObject bkeyObject, byte[] eflag, byte[] data) {
CachedData cachedData = new CachedData(flags, data, eflag, tc.getMaxSize());
cachedDataMap.put(pos, new AbstractMap.SimpleEntry<BKeyObject, CachedData>(bkeyObject, cachedData));
}

if (element != null) {
map.put(pos, element);
@Override
public void addResult() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
for (Entry<Integer, Entry<BKeyObject, CachedData>> entry : cachedDataMap.entrySet()) {
Entry<BKeyObject, CachedData> cachedDataEntry = entry.getValue();
result.put(entry.getKey(), makeBTreeElement(cachedDataEntry.getKey(), cachedDataEntry.getValue(), tc));
}
}
}
});
Expand Down Expand Up @@ -3290,9 +3298,8 @@ private <T> CollectionFuture<Map<Integer, Element<T>>> asyncBopFindPositionWithG

private final TreeMap<Integer, Element<T>> result
= new TreeMap<Integer, Element<T>>();
private final LinkedHashMap<BKeyObject, CachedData> cachedDataMap
= new LinkedHashMap<BKeyObject, CachedData>();
private int startPosition;
private final HashMap<Integer, Entry<BKeyObject, CachedData>> cachedDataMap
= new HashMap<Integer, Entry<BKeyObject, CachedData>>();

public void receivedStatus(OperationStatus status) {
CollectionOperationStatus cstatus;
Expand Down Expand Up @@ -3338,21 +3345,17 @@ public void complete() {
}

@Override
public void getStartPosition(int start) {
startPosition = start;
}

public void gotData(int flags, BKeyObject bkeyObject, byte[] eflag, byte[] data) {
public void gotData(int pos, int flags, BKeyObject bkeyObject, byte[] eflag, byte[] data) {
CachedData cachedData = new CachedData(flags, data, eflag, tc.getMaxSize());
cachedDataMap.put(bkeyObject, cachedData);
cachedDataMap.put(pos, new AbstractMap.SimpleEntry<BKeyObject, CachedData>(bkeyObject, cachedData));
}

@Override
public void addResult() {
if (result.isEmpty() && !cachedDataMap.isEmpty()) {
for (Map.Entry<BKeyObject, CachedData> entry : cachedDataMap.entrySet()) {
result.put(startPosition, makeBTreeElement(entry.getKey(), entry.getValue(), tc));
startPosition++;
for (Map.Entry<Integer, Map.Entry<BKeyObject, CachedData>> entry : cachedDataMap.entrySet()) {
Entry<BKeyObject, CachedData> cachedDataEntry = entry.getValue();
result.put(entry.getKey(), makeBTreeElement(cachedDataEntry.getKey(), cachedDataEntry.getValue(), tc));
}
}
}
Expand Down Expand Up @@ -3488,9 +3491,8 @@ public void complete() {
latch.countDown();
}

public void gotData(String key, int flags, BKeyObject bkeyObject,
byte[] eflag, byte[] data) {
assert key.equals(k) : "Wrong key returned";
@Override
public void gotData(int flags, BKeyObject bkeyObject, byte[] eflag, byte[] data) {
cachedData = new CachedData(flags, data, eflag, tc.getMaxSize());
}

Expand All @@ -3507,37 +3509,6 @@ public void addResult() {
return rv;
}

/**
* Utility method to create a b+tree element from individual parameters.
*
* @param key b+tree item's key
* @param flags item flags, used when creating the item
* @param bkey element key
* @param eflag element flags
* @param data element data
* @param tc transcoder to serialize and unserialize value
* @return element object containing all the parameters and transcoded value
*/
private <T> Element<T> makeBTreeElement(String key, int flags,
BKeyObject bkey, byte[] eflag, byte[] data,
Transcoder<T> tc) {
Element<T> element = null;
T value = tc.decode(new CachedData(flags, data, tc.getMaxSize()));

switch (bkey.getType()) {
case LONG:
element = new Element<T>(bkey.getLongBKey(), value, eflag);
break;
case BYTEARRAY:
element = new Element<T>(bkey.getByteArrayBKeyRaw(), value, eflag);
break;
default:
getLogger().error("Unexpected bkey type : (key:" + key + ", bkey:"
+ bkey.toString() + ")");
}

return element;
}
private <T> Element<T> makeBTreeElement(BKeyObject bkey, CachedData cachedData, Transcoder<T> tc) {
Element<T> element = null;
T value = tc.decode(cachedData);
Expand All @@ -3553,7 +3524,6 @@ private <T> Element<T> makeBTreeElement(BKeyObject bkey, CachedData cachedData,
getLogger().error("Unexpected bkey type : (bkey:"
+ bkey.toString() + ")");
}

return element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ public BTreeStoreAndGetFuture(CountDownLatch l, AtomicReference<T> oref, long op
}

public Element<E> getElement() {
Boolean result;
try {
result = (Boolean) super.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
if (result) {
CollectionGetOpCallback callback = (CollectionGetOpCallback) op.getCallback();
callback.addResult();
}
CollectionGetOpCallback callback = (CollectionGetOpCallback) op.getCallback();
callback.addResult();
return element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public interface BTreeFindPositionWithGetOperation extends KeyedOperation {
BTreeFindPositionWithGet getGet();

interface Callback extends CollectionGetOpCallback {
void gotData(int flags, BKeyObject bkey, byte[] eflag, byte[] data);
void getStartPosition(int startPosition);
void gotData(int pos, int flags, BKeyObject bkey, byte[] eflag, byte[] data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public interface BTreeGetByPositionOperation extends KeyedOperation {

BTreeGetByPosition getGet();

interface Callback extends OperationCallback {
void gotData(String key, int flags, int pos, BKeyObject bkey, byte[] eflag, byte[] data);
interface Callback extends CollectionGetOpCallback {
void gotData(int pos, int flags, BKeyObject bkey, byte[] eflag, byte[] data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface BTreeInsertAndGetOperation extends KeyedOperation {
BTreeInsertAndGet<?> getGet();

interface Callback extends CollectionGetOpCallback {
void gotData(String key, int flags, BKeyObject bkeyObject, byte[] elementFlag, byte[] data);
void gotData(int flags, BKeyObject bkeyObject, byte[] elementFlag, byte[] data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class BTreeFindPositionWithGetOperationImpl extends OperationImpl impleme
protected int count = 0;
protected int index = 0;
protected int pos = 0;
protected int posDiff = 0;
protected byte[] data = null;
protected int readOffset = 0;
protected byte lookingFor = '\0';
Expand Down Expand Up @@ -112,9 +113,8 @@ public void handleLine(String line) {

assert count > 0;
// position counter
BTreeFindPositionWithGetOperation.Callback cb =
(BTreeFindPositionWithGetOperation.Callback) getCallback();
cb.getStartPosition(position - index);
pos = position - index;
posDiff = 1;

// start to read actual data
setReadType(OperationReadType.DATA);
Expand Down Expand Up @@ -201,8 +201,10 @@ public void handleRead(ByteBuffer bb) {
// put an element data.
BTreeFindPositionWithGetOperation.Callback cb =
(BTreeFindPositionWithGetOperation.Callback) getCallback();
cb.gotData(flags, get.getBkey(), get.getEflag(), data);
cb.gotData(pos, flags, get.getBkey(), get.getEflag(), data);

//next position.
pos += posDiff;
lookingFor = '\r';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void handleRead(ByteBuffer bb) {
// put an element data.
BTreeGetByPositionOperation.Callback cb =
(BTreeGetByPositionOperation.Callback) getCallback();
cb.gotData(key, flags, pos, get.getBkey(), get.getEflag(), data);
cb.gotData(pos, flags, get.getBkey(), get.getEflag(), data);

// next position.
pos += posDiff;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void handleRead(ByteBuffer bb) {
if (lookingFor == '\0' && readOffset == data.length) {
// put an element data.
BTreeInsertAndGetOperation.Callback cb = (BTreeInsertAndGetOperation.Callback) getCallback();
cb.gotData(key, flags, get.getBkeyObject(), get.getElementFlag(), data);
cb.gotData(flags, get.getBkeyObject(), get.getElementFlag(), data);

lookingFor = '\r';
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/manual/net/spy/memcached/MultibyteKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,11 @@ public void BTreeGetByPositionOperationImplTest() {
new BTreeGetByPosition(BTreeOrder.ASC, 0),
new BTreeGetByPositionOperation.Callback() {
@Override
public void gotData(String key, int flags, int pos, BKeyObject bkey,
byte[] eflag, byte[] data) {
public void addResult() {
}

@Override
public void gotData(int pos, int flags, BKeyObject bkey, byte[] eflag, byte[] data) {
}

@Override
Expand Down Expand Up @@ -667,8 +670,7 @@ public void BTreeInsertAndGetOperationImplTest() {
new Random().nextInt(), new CollectionAttributes()),
testData, new BTreeInsertAndGetOperation.Callback() {
@Override
public void gotData(String key, int flags, BKeyObject bkeyObject,
byte[] elementFlag, byte[] data) {
public void gotData(int flags, BKeyObject bkeyObject, byte[] elementFlag, byte[] data) {
}

@Override
Expand All @@ -682,8 +684,7 @@ public void complete() {
@Override
public void addResult() {
}

}).initialize();
}).initialize();
} catch (java.nio.BufferOverflowException e) {
Assert.fail();
}
Expand Down

0 comments on commit 2609684

Please sign in to comment.