Skip to content

Commit

Permalink
INTERNAL: Change parsing itemHeader logic in CollectionGetOpImpl.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Aug 25, 2023
1 parent 4318361 commit c85af88
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

import net.spy.memcached.util.BTreeUtil;

/**
Expand Down Expand Up @@ -133,6 +135,10 @@ public void decodeItemHeader(String itemHeader) {
this.dataLength = bytes;
}

@Override
public void decodeItemHeader(List<String> itemHeader) {
}

public BKeyObject getBkey() {
return bkey;
}
Expand Down
47 changes: 12 additions & 35 deletions src/main/java/net/spy/memcached/collection/BTreeGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

import net.spy.memcached.util.BTreeUtil;

public class BTreeGet extends CollectionGet {
Expand All @@ -25,9 +27,6 @@ public class BTreeGet extends CollectionGet {
protected int count = -1;
protected ElementFlagFilter elementFlagFilter;

private boolean isFirstParsing = true;
private boolean elementFlagExists = false;

private BTreeGet(String range,
boolean delete, boolean dropIfEmpty,
ElementFlagFilter elementFlagFilter) {
Expand Down Expand Up @@ -120,15 +119,6 @@ public String getCommand() {
return command;
}

public boolean eachRecordParseCompleted() {
if (elementFlagExists) {
// true if item header was parsed completely
return isFirstParsing;
} else {
return true;
}
}

@Override
public byte[] getAddtionalArgs() {
return null;
Expand All @@ -141,30 +131,17 @@ public boolean headerReady(int spaceCount) {
}

@Override
public void decodeItemHeader(String itemHeader) {
String[] splited = itemHeader.split(" ");

if (isFirstParsing) {
// found bkey
if (splited[0].startsWith("0x")) {
this.subkey = splited[0].substring(2);
} else {
this.subkey = splited[0];
}

// found element flag.
if (splited[1].startsWith("0x")) {
this.elementFlagExists = true;
this.elementFlag = BTreeUtil.hexStringToByteArrays(splited[1].substring(2));
// need second parsing because of EFlag field
isFirstParsing = false;
} else {
this.dataLength = Integer.parseInt(splited[1]);
}
public void decodeItemHeader(List<String> args) {
subkey = args.get(0);
if (args.size() == 2) {
dataLength = Integer.parseInt(args.get(1));
} else {
this.dataLength = Integer.parseInt(splited[1]);
// revert true for next B+Tree element parsing
this.isFirstParsing = true;
elementFlag = BTreeUtil.hexStringToByteArrays(args.get(1));
dataLength = Integer.parseInt(args.get(2));
}
}

@Override
public void decodeItemHeader(String itemHeader) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

import net.spy.memcached.util.BTreeUtil;

/**
Expand Down Expand Up @@ -157,4 +159,8 @@ public boolean isReversed() {
return posFrom > posTo;
}

@Override
public void decodeItemHeader(List<String> itemHeader) {

}
}
9 changes: 4 additions & 5 deletions src/main/java/net/spy/memcached/collection/CollectionGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

public abstract class CollectionGet {

protected boolean delete = false;
Expand Down Expand Up @@ -57,16 +59,13 @@ public boolean headerReady(int spaceCount) {
return headerCount == spaceCount;
}

public boolean eachRecordParseCompleted() {
return true;
}

public abstract byte[] getAddtionalArgs();

public abstract String stringify();

public abstract String getCommand();

public abstract void decodeItemHeader(String itemHeader);
public abstract void decodeItemHeader(List<String> itemHeader);

public abstract void decodeItemHeader(String itemHeader);
}
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/collection/ListGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

public class ListGet extends CollectionGet {

private static final String command = "lop get";
Expand Down Expand Up @@ -73,4 +75,8 @@ public String getCommand() {
public void decodeItemHeader(String itemHeader) {
this.dataLength = Integer.parseInt(itemHeader);
}
@Override
public void decodeItemHeader(List<String> args) {
this.dataLength = Integer.parseInt(args.get(0));
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/collection/MapGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,10 @@ public void decodeItemHeader(String itemHeader) {
this.subkey = splited[0];
this.dataLength = Integer.parseInt(splited[1]);
}

@Override
public void decodeItemHeader(List<String> args) {
this.subkey = args.get(0);
this.dataLength = Integer.parseInt(args.get(1));
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/collection/SetGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package net.spy.memcached.collection;

import java.util.List;

public class SetGet extends CollectionGet {

private static final String command = "sop get";
Expand Down Expand Up @@ -67,4 +69,8 @@ public String getCommand() {
public void decodeItemHeader(String itemHeader) {
this.dataLength = Integer.parseInt(itemHeader);
}
@Override
public void decodeItemHeader(List<String> args) {
this.dataLength = Integer.parseInt(args.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.io.ByteArrayOutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.spy.memcached.KeyUtil;
import net.spy.memcached.collection.BTreeGet;
Expand Down Expand Up @@ -79,6 +81,7 @@ public class CollectionGetOperationImpl extends OperationImpl
protected int readOffset = 0;
protected byte lookingFor = '\0';
protected int spaceCount = 0;
protected final List<String> args = new ArrayList<String>();

public CollectionGetOperationImpl(String key, CollectionGet collectionGet,
OperationCallback cb) {
Expand Down Expand Up @@ -142,32 +145,38 @@ public void handleLine(String line) {
return;
}
}

@Override
public final void handleRead(ByteBuffer bb) {
boolean hasEFlag = false;
// Decode a collection data header.
if (lookingFor == '\0' && data == null) {
while (bb.hasRemaining()) {
byte b = bb.get();

// Handle spaces.
if (b == ' ') {
/*
btree: <bkey> [<eflag>] <bytes> <data>\r\n
list: <bytes> <data>\r\n
set: <bytes> <data>\r\n
map: <field> <bytes> <data>\r\n
*/
String arg = byteBuffer.toString();
byteBuffer.reset();
if (!args.isEmpty() && arg.startsWith("0x")) {
hasEFlag = true;
}
args.add(arg);
spaceCount++;
if (collectionGet.headerReady(spaceCount)) {
/*
btree: <bkey> [<eflag>] <bytes> <data>\r\n
list: <bytes> <data>\r\n
set: <bytes> <data>\r\n
map: <field> <bytes> <data>\r\n
*/
collectionGet.decodeItemHeader(byteBuffer.toString());
byteBuffer.reset();

if (collectionGet.headerReady(spaceCount) && collectionGet.eachRecordParseCompleted()) {
data = new byte[collectionGet.getDataLength()];
spaceCount = 0;
break;
// if item header has eFlag, spaceCount should be 3
if (hasEFlag && spaceCount == 2) {
continue;
}
collectionGet.decodeItemHeader(args);
data = new byte[collectionGet.getDataLength()];
spaceCount = 0;
args.clear();
break;
}
}

Expand All @@ -189,8 +198,10 @@ public final void handleRead(ByteBuffer bb) {
data = null;
break;
}

byteBuffer.write(b);
// Doesn't write space to byteBuffer
if (b != ' ') {
byteBuffer.write(b);
}
}
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/manual/net/spy/memcached/MultibyteKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public String getCommand() {
@Override
public void decodeItemHeader(String itemHeader) {
}

@Override
public void decodeItemHeader(List<String> itemHeader) {
}
}, new CollectionGetOperation.Callback() {
@Override
public void gotData(String subkey, int flags, byte[] data, byte[] eflag) {
Expand Down

0 comments on commit c85af88

Please sign in to comment.