Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public BTreeBulkInsert(MemcachedNode node, List<String> keyList, String bkey,
CreateAttributes attributes) {
super(node, keyList, cachedData, attributes);
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(
CollectionType.btree, attributes.getOverflowAction()
);
CollectionType.btree.checkOverflowAction(attributes.getOverflowAction());
}
this.bkey = bkey;
this.eflag = eflag;
Expand All @@ -98,7 +96,7 @@ public ByteBuffer getAsciiCommand() {
int kSize = this.keyList.size();
byte[] value = cachedData.getData();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cachedData.getFlags()) : "";
attributes.toCreateClause(cachedData.getFlags()) : "";
for (int i = this.nextOpIndex; i < kSize; i++) {
String key = keyList.get(i);
setArguments(bb, COMMAND, key, bkey, (eflag != null) ? eflag : "", value.length,
Expand Down Expand Up @@ -129,9 +127,7 @@ public MapBulkInsert(MemcachedNode node, List<String> keyList, String mkey,
CachedData cachedData, CreateAttributes attributes) {
super(node, keyList, cachedData, attributes);
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(
CollectionType.map, attributes.getOverflowAction()
);
CollectionType.map.checkOverflowAction(attributes.getOverflowAction());
}
this.mkey = mkey;
}
Expand All @@ -154,7 +150,7 @@ public ByteBuffer getAsciiCommand() {
int kSize = this.keyList.size();
byte[] value = cachedData.getData();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cachedData.getFlags()) : "";
attributes.toCreateClause(cachedData.getFlags()) : "";
for (int i = this.nextOpIndex; i < kSize; i++) {
String key = keyList.get(i);
setArguments(bb, COMMAND, key, mkey, value.length,
Expand Down Expand Up @@ -184,9 +180,7 @@ public SetBulkInsert(MemcachedNode node, List<String> keyList,
CachedData cachedData, CreateAttributes attributes) {
super(node, keyList, cachedData, attributes);
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(
CollectionType.set, attributes.getOverflowAction()
);
CollectionType.set.checkOverflowAction(attributes.getOverflowAction());
}
}

Expand All @@ -207,7 +201,7 @@ public ByteBuffer getAsciiCommand() {
int kSize = this.keyList.size();
byte[] value = cachedData.getData();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cachedData.getFlags()) : "";
attributes.toCreateClause(cachedData.getFlags()) : "";
for (int i = this.nextOpIndex; i < kSize; i++) {
String key = keyList.get(i);
setArguments(bb, COMMAND, key, value.length,
Expand Down Expand Up @@ -237,9 +231,7 @@ public ListBulkInsert(MemcachedNode node, List<String> keyList, int index,
CachedData cachedData, CreateAttributes attributes) {
super(node, keyList, cachedData, attributes);
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(
CollectionType.list, attributes.getOverflowAction()
);
CollectionType.list.checkOverflowAction(attributes.getOverflowAction());
}
this.index = index;
}
Expand All @@ -262,7 +254,7 @@ public ByteBuffer getAsciiCommand() {
int kSize = keyList.size();
byte[] value = cachedData.getData();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cachedData.getFlags()) : "";
attributes.toCreateClause(cachedData.getFlags()) : "";
for (int i = this.nextOpIndex; i < kSize; i++) {
String key = this.keyList.get(i);
setArguments(bb, COMMAND, key, index, value.length,
Expand Down
41 changes: 3 additions & 38 deletions src/main/java/net/spy/memcached/collection/CollectionCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class CollectionCreate {

protected CollectionCreate(CollectionType type, int flags,
CreateAttributes attributes, boolean noreply) {
checkOverflowAction(type, attributes.getOverflowAction());
type.checkOverflowAction(attributes.getOverflowAction());
this.flags = flags;
this.attributes = attributes;
this.noreply = noreply;
Expand All @@ -37,51 +37,16 @@ public String stringify() {
}

StringBuilder b = new StringBuilder();

b.append(flags);
b.append(' ').append(attributes.getExpireTime());
b.append(' ').append(attributes.getMaxCount());

if (null != attributes.getOverflowAction()) {
b.append(' ').append(attributes.getOverflowAction());
}

if (!attributes.getReadable()) {
b.append(' ').append("unreadable");
}

b.append(' ').append(attributes.stringify());
if (noreply) {
b.append((b.length() <= 0) ? "" : " ").append("noreply");
b.append(' ').append("noreply");
}

str = b.toString();
return str;
}

public static String makeCreateClause(CreateAttributes attributes, int flags) {
if (attributes == null) {
return null;
}
StringBuilder b = new StringBuilder();
b.append("create ").append(flags)
.append(" ").append(attributes.getExpireTime())
.append(" ").append(attributes.getMaxCount());
if (attributes.getOverflowAction() != null) {
b.append(" ").append(attributes.getOverflowAction());
}
if (!attributes.getReadable()) {
b.append(" ").append("unreadable");
}
return b.toString();
}

public static void checkOverflowAction(CollectionType type, CollectionOverflowAction action) {
if (action != null && !type.isAvailableOverflowAction(action)) {
throw new IllegalArgumentException(
action + " is unavailable overflow action in " + type + ".");
}
}

public String toString() {
return (str != null) ? str : stringify();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected CollectionInsert() {
protected CollectionInsert(CollectionType type, T value, byte[] elementFlag,
RequestMode requestMode, CreateAttributes attributes) {
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(type, attributes.getOverflowAction());
type.checkOverflowAction(attributes.getOverflowAction());
}
if (elementFlag != null) {
if (elementFlag.length < 1 || elementFlag.length > ElementFlagFilter.MAX_EFLAG_LENGTH) {
Expand All @@ -56,7 +56,7 @@ public String stringify() {
StringBuilder b = new StringBuilder();

if (attributes != null) {
b.append(CollectionCreate.makeCreateClause(attributes, flags));
b.append(attributes.toCreateClause(flags));
}

// an optional request mode like noreply, pipe and getrim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public ListPipedInsert(String key, int index, Collection<T> list,
CreateAttributes attributes, Transcoder<T> tc) {
super(key, attributes, tc, list.size());
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.list,
attributes.getOverflowAction());
CollectionType.list.checkOverflowAction(attributes.getOverflowAction());
}
this.index = index;
this.list = list;
Expand Down Expand Up @@ -88,15 +87,15 @@ public ByteBuffer getAsciiCommand() {
// create ascii operation string
int eSize = encodedList.size();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cd.getFlags()) : "";
attributes.toCreateClause(cd.getFlags()) : "";
for (i = 0; i < eSize; i++) {
byte[] each = encodedList.get(i);
int eIndex = index;
if (index >= 0) {
eIndex += (i + nextOpIndex);
}
setArguments(bb, COMMAND, key, eIndex, each.length,
createClause, (i < eSize - 1) ? PIPE : "");
createClause, (i < eSize - 1) ? PIPE : "");
bb.put(each);
bb.put(CRLF);
}
Expand All @@ -120,8 +119,7 @@ public SetPipedInsert(String key, Collection<T> set,
CreateAttributes attributes, Transcoder<T> tc) {
super(key, attributes, tc, set.size());
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.set,
attributes.getOverflowAction());
CollectionType.set.checkOverflowAction(attributes.getOverflowAction());
}
this.set = set;
}
Expand Down Expand Up @@ -152,7 +150,7 @@ public ByteBuffer getAsciiCommand() {
// create ascii operation string
int eSize = encodedList.size();
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cd.getFlags()) : "";
attributes.toCreateClause(cd.getFlags()) : "";
for (i = 0; i < eSize; i++) {
byte[] each = encodedList.get(i);
setArguments(bb, COMMAND, key, each.length,
Expand Down Expand Up @@ -180,8 +178,7 @@ public BTreePipedInsert(String key, Map<Long, T> map,
CreateAttributes attributes, Transcoder<T> tc) {
super(key, attributes, tc, map.size());
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.btree,
attributes.getOverflowAction());
CollectionType.btree.checkOverflowAction(attributes.getOverflowAction());
}
this.map = map;
}
Expand Down Expand Up @@ -215,7 +212,7 @@ public ByteBuffer getAsciiCommand() {

// create ascii operation string
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cd.getFlags()) : "";
attributes.toCreateClause(cd.getFlags()) : "";
for (i = 0; i < eSize; i++) {
Long bkey = bkeyList.get(i);
byte[] value = encodedList.get(i);
Expand Down Expand Up @@ -244,8 +241,7 @@ public ByteArraysBTreePipedInsert(String key, List<Element<T>> elements,
CreateAttributes attributes, Transcoder<T> tc) {
super(key, attributes, tc, elements.size());
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.btree,
attributes.getOverflowAction());
CollectionType.btree.checkOverflowAction(attributes.getOverflowAction());
}
this.elements = elements;
}
Expand Down Expand Up @@ -279,13 +275,13 @@ public ByteBuffer getAsciiCommand() {

// create ascii operation string
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cd.getFlags()) : "";
attributes.toCreateClause(cd.getFlags()) : "";
for (i = 0; i < eSize; i++) {
Element<T> element = elements.get(i + nextOpIndex);
byte[] value = encodedList.get(i);
setArguments(bb, COMMAND, key,
element.getStringBkey(), element.getStringEFlag(), value.length,
createClause, (i < eSize - 1) ? PIPE : "");
element.getStringBkey(), element.getStringEFlag(), value.length,
createClause, (i < eSize - 1) ? PIPE : "");
bb.put(value);
bb.put(CRLF);
}
Expand All @@ -309,8 +305,7 @@ public MapPipedInsert(String key, Map<String, T> map,
CreateAttributes attributes, Transcoder<T> tc) {
super(key, attributes, tc, map.size());
if (attributes != null) { /* item creation option */
CollectionCreate.checkOverflowAction(CollectionType.map,
attributes.getOverflowAction());
CollectionType.map.checkOverflowAction(attributes.getOverflowAction());
}
this.map = map;
}
Expand Down Expand Up @@ -344,12 +339,12 @@ public ByteBuffer getAsciiCommand() {

// create ascii operation string
String createClause = attributes != null ?
CollectionCreate.makeCreateClause(attributes, cd.getFlags()) : "";
attributes.toCreateClause(cd.getFlags()) : "";
for (i = 0; i < eSize; i++) {
String mkey = mkeyList.get(i);
byte[] value = encodedList.get(i);
setArguments(bb, COMMAND, key, mkey, value.length,
createClause, (i < eSize - 1) ? PIPE : "");
createClause, (i < eSize - 1) ? PIPE : "");
bb.put(value);
bb.put(CRLF);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public static CollectionType find(String value) {
return null;
}

public boolean isAvailableOverflowAction(CollectionOverflowAction overflowAction) {
return availableOverflowAction.contains(overflowAction);
public void checkOverflowAction(CollectionOverflowAction overflowAction) {
if (overflowAction != null && !availableOverflowAction.contains(overflowAction)) {
throw new IllegalArgumentException(
overflowAction + " is unavailable overflow action in " + this + ".");
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/net/spy/memcached/collection/CreateAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class CreateAttributes {
private final long maxCount;
private final CollectionOverflowAction overflowAction;
private final boolean readable;
private String clause;

private CreateAttributes(Builder builder) {
this.expireTime = builder.expireTime;
Expand Down Expand Up @@ -72,6 +73,36 @@ public static CreateAttributes of(CollectionAttributes attr) {
.build();
}

/**
* Serializes the create-command attribute clause into a string.
*
* @return {@code <exp> <maxcount> [overflow action] [unreadable]} (no flags / create / noreply)
*/
public String stringify() {
if (clause == null) {
StringBuilder sb = new StringBuilder();
sb.append(expireTime).append(' ').append(maxCount);
if (overflowAction != null) {
sb.append(' ').append(overflowAction);
}
if (!readable) {
sb.append(' ').append("unreadable");
}
clause = sb.toString();
}
return clause;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stringify() ๋ฉ”์„œ๋“œ์— ์‹ค์ œ stringํ™”ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‘๋Š” ๊ฒƒ์ด ์ž์—ฐ์Šค๋Ÿฝ์ง€ ์•Š๋Š” ์ง€ ?

  if (clause == null) {
    StringBuilder sb = new StringBuilder();
    sb.append(expireTime).append(' ').append(maxCount);
    if (overflowAction != null) {
      sb.append(' ').append(overflowAction);
    }
    if (!readable) {
      sb.append(' ').append("unreadable");
    }
    clause = sb.toString();
  }
  return clause;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜๋ฏธ์ƒ stringify() ์—์„œ ์ง„ํ–‰ํ•˜๋Š”๊ฒŒ ๋” ์ ํ•ฉํ•ด๋ณด์ž…๋‹ˆ๋‹ค.
์ˆ˜์ •ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

}

/**
* Builds the {@code create} clause embedded in an insert-with-create command.
*
* @param flags the item flags
* @return {@code create <flags> } followed by {@link #stringify()}
*/
public String toCreateClause(int flags) {
return "create " + flags + " " + stringify();
}

public long getExpireTime() {
return this.expireTime;
}
Expand Down
Loading