Skip to content

Commit

Permalink
add new string.intern
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhuynh27 committed Dec 13, 2021
1 parent 91f809d commit d951e33
Show file tree
Hide file tree
Showing 22 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import dev.keva.protocol.resp.reply.StatusReply;
import io.netty.channel.ChannelHandlerContext;

import java.nio.charset.StandardCharsets;

@Component
@CommandImpl("auth")
@ParamLength(1)
Expand All @@ -27,7 +29,7 @@ public Auth(KevaConfig kevaConfig, AuthManager authManager) {

@Execute
public Reply<?> execute(byte[] password, ChannelHandlerContext ctx) {
String passwordString = new String(password);
String passwordString = new String(password, StandardCharsets.UTF_8).intern();
if (kevaConfig.getPassword().equals(passwordString)) {
authManager.authenticate(ctx.channel());
return new StatusReply("OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import dev.keva.protocol.resp.reply.Reply;
import io.netty.channel.ChannelHandlerContext;

import java.nio.charset.StandardCharsets;

@Component
@CommandImpl("client")
@ParamLength(1)
public class Client {
@Execute
public Reply<?> execute(byte[] param, ChannelHandlerContext ctx) {
String paramStr = new String(param);
String paramStr = new String(param, StandardCharsets.UTF_8).intern();
if (paramStr.equalsIgnoreCase("id")) {
return new BulkReply(ctx.channel().id().asShortText());
} else if (paramStr.equalsIgnoreCase("info")) {
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/dev/keva/core/command/impl/hash/HSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;

import java.nio.charset.StandardCharsets;

@Component
@CommandImpl("hset")
@ParamLength(type = ParamLength.Type.AT_LEAST, value = 3)
Expand All @@ -28,7 +30,7 @@ public IntegerReply execute(byte[][] params) {
if (params[i] == null) {
args[i] = null;
} else {
args[i] = new String(params[i]);
args[i] = new String(params[i], StandardCharsets.UTF_8).intern();
}
}
if (args[0] == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Expire(ExpirationManager expirationManager) {
@Execute
public IntegerReply execute(byte[] key, byte[] after) {
try {
val afterInMillis = Long.parseLong(new String(after, StandardCharsets.UTF_8));
val afterInMillis = Long.parseLong(new String(after, StandardCharsets.UTF_8).intern());
expirationManager.expireAfter(key, afterInMillis);
return new IntegerReply(1);
} catch (Exception ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ExpireAt(ExpirationManager expirationManager) {
@Execute
public IntegerReply execute(byte[] key, byte[] at) {
try {
val atInMillis = Long.parseLong(new String(at, StandardCharsets.UTF_8));
val atInMillis = Long.parseLong(new String(at, StandardCharsets.UTF_8).intern());
expirationManager.expireAt(key, atInMillis);
return new IntegerReply(1);
} catch (Exception ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public IntegerReply execute(byte[] key) {
} catch (NumberFormatException ex) {
throw new CommandException("Failed to parse integer from value stored");
}
return new IntegerReply(Long.parseLong(new String(newVal, StandardCharsets.UTF_8)));
return new IntegerReply(Long.parseLong(new String(newVal, StandardCharsets.UTF_8).intern()));
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/dev/keva/core/command/impl/key/Incrby.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public Incrby(KevaDatabase database) {

@Execute
public IntegerReply execute(byte[] key, byte[] incrBy) {
var amount = Long.parseLong(new String(incrBy, StandardCharsets.UTF_8));
var amount = Long.parseLong(new String(incrBy, StandardCharsets.UTF_8).intern());
byte[] newVal;
try {
newVal = database.incrBy(key, amount);
} catch (NumberFormatException ex) {
throw new CommandException("Failed to parse integer from value stored");
}
return new IntegerReply(Long.parseLong(new String(newVal, StandardCharsets.UTF_8)));
return new IntegerReply(Long.parseLong(new String(newVal, StandardCharsets.UTF_8).intern()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.val;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Component
Expand All @@ -31,7 +32,7 @@ public Restore(KevaDatabase database, ExpirationManager expirationManager) {
@Execute
public Reply<?> execute(byte[] key, byte[] ttl, byte[] dump, byte[] replace) {
val old = database.get(key);
boolean isReplace = replace != null && new String(replace).equalsIgnoreCase("REPLACE");
boolean isReplace = replace != null && new String(replace, StandardCharsets.UTF_8).intern().equalsIgnoreCase("REPLACE");
if (old != null && !isReplace) {
return new ErrorReply("ERR Target key name is busy");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import dev.keva.store.KevaDatabase;
import lombok.val;

import java.nio.charset.StandardCharsets;

@Component
@CommandImpl("lindex")
@ParamLength(2)
Expand All @@ -22,7 +24,7 @@ public LIndex(KevaDatabase database) {

@Execute
public BulkReply execute(byte[] key, byte[] index) {
val got = database.lindex(key, Integer.parseInt(new String(index)));
val got = database.lindex(key, Integer.parseInt(new String(index).intern()));
return got == null ? BulkReply.NIL_REPLY : new BulkReply(got);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Reply<?> execute(byte[] key, byte[] count) {
return got == null ? BulkReply.NIL_REPLY : new BulkReply(got);
}

int countInt = Integer.parseInt(new String(count));
int countInt = Integer.parseInt(new String(count).intern());
Reply<?>[] replies = new Reply[countInt];
for (int i = 0; i < countInt; i++) {
val got = database.lpop(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public LRange(KevaDatabase database) {

@Execute
public Reply<?> execute(byte[] key, byte[] start, byte[] stop) {
val got = database.lrange(key, Integer.parseInt(new String(start)), Integer.parseInt(new String(stop)));
val got = database.lrange(key,
Integer.parseInt(new String(start).intern()),
Integer.parseInt(new String(stop).intern()));
if (got == null) {
return MultiBulkReply.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LRem(KevaDatabase database) {

@Execute
public IntegerReply execute(byte[] key, byte[] count, byte[] value) {
int result = database.lrem(key, Integer.parseInt(new String(count)), value);
int result = database.lrem(key, Integer.parseInt(new String(count).intern()), value);
return new IntegerReply(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LSet(KevaDatabase database) {

@Execute
public StatusReply execute(byte[] key, byte[] index, byte[] value) {
database.lset(key, Integer.parseInt(new String(index)), value);
database.lset(key, Integer.parseInt(new String(index).intern()), value);
return StatusReply.OK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Reply<?> execute(byte[] key, byte[] count) {
return got == null ? BulkReply.NIL_REPLY : new BulkReply(got);
}

int countInt = Integer.parseInt(new String(count));
int countInt = Integer.parseInt(new String(count).intern());
Reply<?>[] replies = new Reply[countInt];
for (int i = 0; i < countInt; i++) {
val got = database.rpop(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.val;
import lombok.var;

import java.nio.charset.StandardCharsets;
import java.util.Set;

@Component
Expand All @@ -30,7 +31,7 @@ public Publish(PubSubManager manager) {
@Execute
public IntegerReply execute(byte[] topicBytes, byte[] message) {
var count = 0;
val topic = new String(topicBytes).toLowerCase();
val topic = new String(topicBytes, StandardCharsets.UTF_8).intern().toLowerCase();
Set<Channel> set = manager.getTopics().get(topic);
if (set != null) {
for (Channel channel : set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.val;
import lombok.var;

import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -43,7 +44,7 @@ public void execute(ChannelHandlerContext ctx, byte[]... topicBytes) {

String[] topicsToSubscribe = new String[topicBytes.length];
for (int i = 0; i < topicBytes.length; i++) {
topicsToSubscribe[i] = new String(topicBytes[i]);
topicsToSubscribe[i] = new String(topicBytes[i], StandardCharsets.UTF_8).intern();
}

for (val topic : topicsToSubscribe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.val;
import lombok.var;

import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -59,7 +60,7 @@ public void execute(ChannelHandlerContext ctx, byte[]... topicBytes) {

String[] topicsToUnsubscribe = new String[topicBytes.length];
for (int i = 0; i < topicBytes.length; i++) {
topicsToUnsubscribe[i] = new String(topicBytes[i]);
topicsToUnsubscribe[i] = new String(topicBytes[i], StandardCharsets.UTF_8).intern();
}

for (val topic : topicsToUnsubscribe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Command command) throws I
val commandWrapper = commandMapper.getMethods().get(new BytesKey(name));
Reply<?> reply;
if (commandWrapper == null) {
reply = new ErrorReply("ERR unknown command `" + new String(name) + "`");
reply = new ErrorReply("ERR unknown command `" + new String(name).intern() + "`");
} else {
reply = commandWrapper.execute(ctx, command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static Command newInstance(byte[][] objects, boolean inline) {
Command command = RECYCLER.get();
if (inline) {
byte[] objs = objects[0];
String[] strings = new String(objs, StandardCharsets.UTF_8).trim().split("\\s+");
String[] strings = new String(objs, StandardCharsets.UTF_8).intern().trim().split("\\s+");
objects = new byte[strings.length][];
for (int i = 0; i < strings.length; i++) {
objects[i] = ByteUtil.getBytes(strings[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public byte[] incrBy(byte[] key, long amount) {
return chronicleMap.compute(key, (k, oldVal) -> {
long curVal = 0L;
if (oldVal != null) {
curVal = Long.parseLong(new String(oldVal, StandardCharsets.UTF_8));
curVal = Long.parseLong(new String(oldVal, StandardCharsets.UTF_8).intern());
}
curVal = curVal + amount;
return Long.toString(curVal).getBytes(StandardCharsets.UTF_8);
Expand Down Expand Up @@ -671,7 +671,7 @@ public int strlen(byte[] key) {
if (value == null) {
return 0;
}
return new String(value, StandardCharsets.UTF_8).length();
return new String(value, StandardCharsets.UTF_8).intern().length();
} finally {
lock.unlock();
}
Expand All @@ -681,7 +681,7 @@ public int strlen(byte[] key) {
public int setrange(byte[] key, byte[] offset, byte[] val) {
lock.lock();
try {
var offsetPosition = Integer.parseInt(new String(offset, StandardCharsets.UTF_8));
var offsetPosition = Integer.parseInt(new String(offset, StandardCharsets.UTF_8).intern());
byte[] oldVal = chronicleMap.get(key);
int newValLength = oldVal == null ? offsetPosition + val.length : Math.max(offsetPosition + val.length, oldVal.length);
byte[] newVal = new byte[newValLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ public int strlen(byte[] key) {
if (value == null) {
return 0;
}
return new String(value, StandardCharsets.UTF_8).length();
return new String(value, StandardCharsets.UTF_8).intern().length();
} finally {
lock.unlock();
}
Expand All @@ -639,7 +639,7 @@ public int strlen(byte[] key) {
public int setrange(byte[] key, byte[] offset, byte[] val) {
lock.lock();
try {
var offsetPosition = Integer.parseInt(new String(offset, StandardCharsets.UTF_8));
var offsetPosition = Integer.parseInt(new String(offset, StandardCharsets.UTF_8).intern());
byte[] oldVal = map.get(new BytesKey(key)).getBytes();
int newValLength = oldVal == null ? offsetPosition + val.length : Math.max(offsetPosition + val.length, oldVal.length);
byte[] newVal = new byte[newValLength];
Expand Down
2 changes: 1 addition & 1 deletion util/src/main/java/dev/keva/util/hashbytes/BytesValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public boolean equals(Object o) {

@Override
public String toString() {
return new String(bytes);
return new String(bytes).intern();
}

public byte[] getBytes() {
Expand Down

0 comments on commit d951e33

Please sign in to comment.