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 Sep 5, 2023
1 parent 4318361 commit 5080c41
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 214 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 All @@ -40,30 +42,27 @@
* - SERVER_ERROR
*/
public class BTreeFindPositionWithGet extends CollectionGet {

public static final int HEADER_EFLAG_POSITION = 1; // 0-based

private static final String command = "bop pwg";

private final BKeyObject bkeyObject;
private final BTreeOrder order;
private final int count;
//private String str;

private BKeyObject bkey;
private byte[] eflag;
private int bytes;

public BTreeFindPositionWithGet(long longBKey, BTreeOrder order, int count) {
this.bkeyObject = new BKeyObject(longBKey);
this.order = order;
this.count = count;
this.eHeadCount = 2;
this.eFlagIndex = 1;
}

public BTreeFindPositionWithGet(byte[] byteArrayBKey, BTreeOrder order, int count) {
this.bkeyObject = new BKeyObject(byteArrayBKey);
this.order = order;
this.count = count;
this.eHeadCount = 2;
this.eFlagIndex = 1;
}

public String stringify() {
Expand Down Expand Up @@ -97,11 +96,6 @@ public int getCount() {
return count;
}

@Override
public boolean headerReady(int spaceCount) {
return spaceCount == 2;
}

@Override
public byte[] getAddtionalArgs() {
return null;
Expand All @@ -112,36 +106,23 @@ public byte[] getAddtionalArgs() {
* <bkey> [<eflag>] <bytes> <data>\r\n
* END\r\n
*/
public void decodeItemHeader(String itemHeader) {
String[] splited = itemHeader.split(" ");

// <bkey>
if (splited[0].startsWith("0x")) {
this.bkey = new BKeyObject(BTreeUtil.hexStringToByteArrays(splited[0].substring(2)));
@Override
public void decodeElemHeader(List<String> tokens) {
subkey = tokens.get(0);
if (subkey.startsWith("0x")) {
bkey = new BKeyObject(BTreeUtil.hexStringToByteArrays(subkey.substring(2)));
} else {
this.bkey = new BKeyObject(Long.parseLong(splited[0]));
bkey = new BKeyObject(Long.parseLong(subkey));
}
if (splited[1].startsWith("0x")) {
// <eflag> <bytes>
this.eflag = BTreeUtil.hexStringToByteArrays(splited[1].substring(2));
this.bytes = Integer.parseInt(splited[2]);
if (tokens.size() == 2) {
dataLength = Integer.parseInt(tokens.get(1));
} else {
// <bytes> only
this.bytes = Integer.parseInt(splited[1]);
elementFlag = BTreeUtil.hexStringToByteArrays(tokens.get(1));
dataLength = Integer.parseInt(tokens.get(2));
}

this.dataLength = bytes;
}

public BKeyObject getBkey() {
return bkey;
}

public byte[] getEflag() {
return eflag;
}

public int getBytes() {
return bytes;
}
}
52 changes: 10 additions & 42 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,17 +27,15 @@ 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) {
this.headerCount = 2;
this.range = range;
this.delete = delete;
this.dropIfEmpty = dropIfEmpty;
this.elementFlagFilter = elementFlagFilter;
this.eHeadCount = 2;
this.eFlagIndex = 1;
}

public BTreeGet(long bkey,
Expand Down Expand Up @@ -120,51 +120,19 @@ 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;
}

@Override
public boolean headerReady(int spaceCount) {
// non-eFlag header has 2 spaces and eFlag header has 3 spaces
return spaceCount == 2 || spaceCount == 3;
}

@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 decodeElemHeader(List<String> tokens) {
subkey = tokens.get(0);
if (tokens.size() == 2) {
dataLength = Integer.parseInt(tokens.get(1));
} else {
this.dataLength = Integer.parseInt(splited[1]);
// revert true for next B+Tree element parsing
this.isFirstParsing = true;
elementFlag = BTreeUtil.hexStringToByteArrays(tokens.get(1));
dataLength = Integer.parseInt(tokens.get(2));
}
}
}
62 changes: 16 additions & 46 deletions src/main/java/net/spy/memcached/collection/BTreeGetByPosition.java
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 All @@ -39,25 +41,24 @@ public class BTreeGetByPosition extends CollectionGet {
private final String range;
private final int posFrom;
private final int posTo;

private BKeyObject bkey;
private byte[] eflag;
private int bytes;

public BTreeGetByPosition(BTreeOrder order, int pos) {
this.headerCount = 2;
this.order = order;
this.range = String.valueOf(pos);
this.posFrom = pos;
this.posTo = pos;
this.eHeadCount = 2;
this.eFlagIndex = 1;
}

public BTreeGetByPosition(BTreeOrder order, int posFrom, int posTo) {
this.headerCount = 2;
this.order = order;
this.range = String.valueOf(posFrom) + ".." + String.valueOf(posTo);
this.posFrom = posFrom;
this.posTo = posTo;
this.eHeadCount = 2;
this.eFlagIndex = 1;
}

public BTreeOrder getOrder() {
Expand Down Expand Up @@ -85,66 +86,36 @@ public String getCommand() {
return command;
}

@Override
public boolean headerReady(int spaceCount) {
return spaceCount == 2;
}

@Override
public byte[] getAddtionalArgs() {
return null;
}

private static final int BKEY = 0;
private static final int EFLAG_OR_BYTES = 1;
private static final int BYTES = 2;

/*
* VALUE <flags> <count>\r\n
* <bkey> [<eflag>] <bytes> <data>\r\n
* END\r\n
*/
public void decodeItemHeader(String itemHeader) {
String[] splited = itemHeader.split(" ");
boolean hasEFlag = false;

// <bkey>
if (splited[BKEY].startsWith("0x")) {
this.bkey = new BKeyObject(BTreeUtil.hexStringToByteArrays(splited[0].substring(2)));
@Override
public void decodeElemHeader(List<String> tokens) {
subkey = tokens.get(0);
if (subkey.startsWith("0x")) {
bkey = new BKeyObject(BTreeUtil.hexStringToByteArrays(subkey.substring(2)));
} else {
this.bkey = new BKeyObject(Long.parseLong(splited[0]));
bkey = new BKeyObject(Long.parseLong(subkey));
}

// <eflag> or <bytes>
if (splited[EFLAG_OR_BYTES].startsWith("0x")) {
// <eflag>
hasEFlag = true;
this.eflag = BTreeUtil
.hexStringToByteArrays(splited[EFLAG_OR_BYTES].substring(2));
if (tokens.size() == 2) {
dataLength = Integer.parseInt(tokens.get(1));
} else {
this.bytes = Integer.parseInt(splited[EFLAG_OR_BYTES]);
elementFlag = BTreeUtil.hexStringToByteArrays(tokens.get(1));
dataLength = Integer.parseInt(tokens.get(2));
}

// <bytes>
if (hasEFlag) {
this.bytes = Integer.parseInt(splited[BYTES]);
}

this.dataLength = bytes;
}

public BKeyObject getBkey() {
return bkey;
}

public byte[] getEflag() {
return eflag;
}

public int getBytes() {
return bytes;
}

public int getPosFrom() {
return posFrom;
}
Expand All @@ -156,5 +127,4 @@ public int getPosTo() {
public boolean isReversed() {
return posFrom > posTo;
}

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

import java.util.List;

public abstract class CollectionGet {

protected boolean delete = false;
protected boolean dropIfEmpty = false;

protected String range;
protected String str;
protected int headerCount;
protected String subkey;
protected int dataLength;

protected byte[] elementFlag;
protected int eHeadCount;
protected int eFlagIndex;

public boolean isDelete() {
return delete;
Expand All @@ -53,12 +56,12 @@ public byte[] getElementFlag() {
return elementFlag;
}

public boolean headerReady(int spaceCount) {
return headerCount == spaceCount;
public int getEFlagIndex() {
return eFlagIndex;
}

public boolean eachRecordParseCompleted() {
return true;
public int getEHeadCount() {
return eHeadCount;
}

public abstract byte[] getAddtionalArgs();
Expand All @@ -67,6 +70,5 @@ public boolean eachRecordParseCompleted() {

public abstract String getCommand();

public abstract void decodeItemHeader(String itemHeader);

public abstract void decodeElemHeader(List<String> itemHeader);
}
10 changes: 7 additions & 3 deletions src/main/java/net/spy/memcached/collection/ListGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
*/
package net.spy.memcached.collection;

import java.util.List;

public class ListGet extends CollectionGet {

private static final String command = "lop get";

private ListGet(String range, boolean delete, boolean dropIfEmpty) {
this.headerCount = 1;
this.range = range;
this.delete = delete;
this.dropIfEmpty = dropIfEmpty;
this.eHeadCount = 1;
this.eFlagIndex = -1;
}

public ListGet(int index, boolean delete, boolean dropIfEmpty) {
Expand Down Expand Up @@ -70,7 +73,8 @@ public String getCommand() {
return command;
}

public void decodeItemHeader(String itemHeader) {
this.dataLength = Integer.parseInt(itemHeader);
@Override
public void decodeElemHeader(List<String> tokens) {
this.dataLength = Integer.parseInt(tokens.get(0));
}
}
Loading

0 comments on commit 5080c41

Please sign in to comment.