Skip to content

Commit

Permalink
delombok var val (#94)
Browse files Browse the repository at this point in the history
* done for server package

* done for config package

* done for command.impl package

* done for command package

* done for all

* run refactor auto by intellij

* done config props

* fix tests
  • Loading branch information
tuhuynh27 committed Dec 23, 2021
1 parent 9a80c8d commit 4fdb23d
Show file tree
Hide file tree
Showing 45 changed files with 129 additions and 171 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/dev/keva/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import com.keva.config.ConfigLoader;
import dev.keva.core.config.KevaConfig;
import dev.keva.core.server.KevaServer;
import dev.keva.core.server.Server;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.slf4j.LoggerFactory;

@Slf4j
public final class Application {
static {
val ctx = (LoggerContext) LoggerFactory.getILoggerFactory();
LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory();
ctx.getLogger("io.netty").setLevel(Level.OFF);
ctx.getLogger("net.openhft").setLevel(Level.OFF);
ctx.getLogger("org.reflections").setLevel(Level.OFF);
Expand All @@ -21,8 +21,8 @@ public final class Application {

public static void main(String[] args) {
try {
val config = ConfigLoader.loadConfig(args, KevaConfig.class);
val server = KevaServer.of(config);
KevaConfig config = ConfigLoader.loadConfig(args, KevaConfig.class);
Server server = KevaServer.of(config);
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdown));
server.run();
} catch (Exception e) {
Expand Down
12 changes: 6 additions & 6 deletions config/src/main/java/com/keva/config/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.keva.config;

import com.keva.config.util.ArgsHolder;
import com.keva.config.util.ArgsParser;
import com.keva.config.util.ConfigLoaderUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -16,10 +16,10 @@ public final class ConfigLoader {

public static <T> T loadConfig(String[] args, Class<T> clazz) throws IOException {
T returnConf = ConfigLoaderUtil.fromProperties(new Properties(), clazz);
val config = ArgsParser.parse(args);
val overrider = ConfigLoaderUtil.fromArgs(config, clazz);
ArgsHolder config = ArgsParser.parse(args);
T overrider = ConfigLoaderUtil.fromArgs(config, clazz);

val configFilePath = config.getArgVal("f");
String configFilePath = config.getArgVal("f");
if (configFilePath != null) {
returnConf = loadConfigFromFile(configFilePath, clazz);
}
Expand All @@ -33,8 +33,8 @@ public static <T> T loadConfigFromFile(String filePath, Class<T> clazz) throws I
if (filePath.isEmpty()) {
filePath = DEFAULT_FILE_PATH;
}
val props = new Properties();
try (val file = new FileInputStream(filePath)) {
Properties props = new Properties();
try (FileInputStream file = new FileInputStream(filePath)) {
props.load(file);
}

Expand Down
8 changes: 3 additions & 5 deletions config/src/main/java/com/keva/config/util/ArgsParser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.keva.config.util;

import lombok.val;

public final class ArgsParser {
public static ArgsHolder parse(String[] args) {
val holder = new ArgsHolder();
ArgsHolder holder = new ArgsHolder();
for (int i = 0; i < args.length; i++) {
val token = args[i];
String token = args[i];
if (token.startsWith("--")) {
val name = token.substring(2);
String name = token.substring(2);
if (i >= args.length - 1) {
holder.addFlag(name);
} else if (args[i + 1].startsWith("--")) {
Expand Down
26 changes: 13 additions & 13 deletions config/src/main/java/com/keva/config/util/ConfigLoaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
import com.keva.config.annotation.ConfigProp;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;

import java.lang.reflect.Field;
import java.util.Properties;

public class ConfigLoaderUtil {
@SneakyThrows
public static <T> T fromProperties(@NonNull Properties props, Class<T> clazz) {
val configHolder = clazz.getDeclaredConstructor().newInstance();
val fields = clazz.getDeclaredFields();
for (val field : fields) {
T configHolder = clazz.getDeclaredConstructor().newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(ConfigProp.class)) {
field.setAccessible(true);
val annotation = field.getAnnotation(ConfigProp.class);
val value = parse(props.getProperty(annotation.name(), annotation.defaultVal()), field.getType());
ConfigProp annotation = field.getAnnotation(ConfigProp.class);
Object value = parse(props.getProperty(annotation.name(), annotation.defaultVal()), field.getType());
field.set(configHolder, value);
}
}
Expand All @@ -28,21 +28,21 @@ public static <T> T fromProperties(@NonNull Properties props, Class<T> clazz) {

@SneakyThrows
public static <T> T fromArgs(@NonNull ArgsHolder args, Class<T> clazz) {
val configHolder = clazz.getDeclaredConstructor().newInstance();
T configHolder = clazz.getDeclaredConstructor().newInstance();

val fields = clazz.getDeclaredFields();
for (val field : fields) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(CliProp.class)) {
field.setAccessible(true);
val cliAnnotate = field.getAnnotation(CliProp.class);
CliProp cliAnnotate = field.getAnnotation(CliProp.class);
String strVal = null;
if (cliAnnotate.type() == CliPropType.VAL) {
strVal = args.getArgVal(cliAnnotate.name());
} else if (cliAnnotate.type() == CliPropType.FLAG) {
strVal = args.getFlag(cliAnnotate.name());
}
if (strVal != null) {
val value = parse(strVal, field.getType());
Object value = parse(strVal, field.getType());
field.set(configHolder, value);
}
}
Expand All @@ -54,9 +54,9 @@ public static <T> T fromArgs(@NonNull ArgsHolder args, Class<T> clazz) {
@SneakyThrows
public static <T> void merge(T obj1, T obj2) {
if (obj1 != null && obj2 != null && !obj1.equals(obj2)) {
for (val field : obj2.getClass().getDeclaredFields()) {
for (Field field : obj2.getClass().getDeclaredFields()) {
field.setAccessible(true);
val overrideVal = field.get(obj2);
Object overrideVal = field.get(obj2);
if (overrideVal != null) {
field.set(obj1, overrideVal);
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/dev/keva/core/aof/AOFManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package dev.keva.core.aof;

import dev.keva.core.command.mapping.CommandMapper;
import dev.keva.core.command.mapping.CommandWrapper;
import dev.keva.core.config.KevaConfig;
import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.Command;
import dev.keva.util.hashbytes.BytesKey;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -36,8 +36,8 @@ public void init() {
if (commands != null) {
log.info("Processing {} commands from AOF file", commands.size());
for (Command command : commands) {
val name = command.getName();
val commandWrapper = commandMapper.getMethods().get(new BytesKey(name));
byte[] name = command.getName();
CommandWrapper commandWrapper = commandMapper.getMethods().get(new BytesKey(name));
if (commandWrapper != null) {
commandWrapper.execute(null, command);
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/dev/keva/core/command/impl/hash/HDel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hdel")
Expand All @@ -25,8 +24,8 @@ public HDel(KevaDatabase database) {
@Execute
public IntegerReply execute(byte[] key, byte[]... fields) {
int deleted = 0;
for (val field : fields) {
val result = database.hdel(key, field);
for (byte[] field : fields) {
boolean result = database.hdel(key, field);
if (result) {
deleted++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hexists")
Expand All @@ -22,7 +21,7 @@ public HExists(KevaDatabase database) {

@Execute
public IntegerReply execute(byte[] key, byte[] field) {
val got = database.hget(key, field);
byte[] got = database.hget(key, field);
return got == null ? new IntegerReply(0) : new IntegerReply(1);
}
}
3 changes: 1 addition & 2 deletions core/src/main/java/dev/keva/core/command/impl/hash/HGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hget")
Expand All @@ -22,7 +21,7 @@ public HGet(KevaDatabase database) {

@Execute
public BulkReply execute(byte[] key, byte[] field) {
val got = database.hget(key, field);
byte[] got = database.hget(key, field);
return got == null ? BulkReply.NIL_REPLY : new BulkReply(got);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hgetall")
Expand All @@ -23,7 +22,7 @@ public HGetAll(KevaDatabase database) {

@Execute
public MultiBulkReply execute(byte[] key) {
val got = database.hgetAll(key);
byte[][] got = database.hgetAll(key);
BulkReply[] replies = new BulkReply[got.length];
for (int i = 0; i < got.length; i++) {
replies[i] = new BulkReply(got[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hkeys")
Expand All @@ -23,7 +22,7 @@ public HKeys(KevaDatabase database) {

@Execute
public MultiBulkReply execute(byte[] key) {
val got = database.hkeys(key);
byte[][] got = database.hkeys(key);
BulkReply[] replies = new BulkReply[got.length];
for (int i = 0; i < got.length; i++) {
replies[i] = new BulkReply(got[i]);
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/dev/keva/core/command/impl/hash/HLen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hlen")
Expand All @@ -22,7 +21,7 @@ public HLen(KevaDatabase database) {

@Execute
public IntegerReply execute(byte[] key) {
val got = database.hgetAll(key);
byte[][] got = database.hgetAll(key);
return got == null ? new IntegerReply(0) : new IntegerReply(got.length / 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hstrlen")
Expand All @@ -22,7 +21,7 @@ public HStrLen(KevaDatabase database) {

@Execute
public IntegerReply execute(byte[] key, byte[] field) {
val got = database.hget(key, field);
byte[] got = database.hget(key, field);
return got == null ? new IntegerReply(0) : new IntegerReply(got.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

@Component
@CommandImpl("hvals")
Expand All @@ -23,7 +22,7 @@ public HVals(KevaDatabase database) {

@Execute
public MultiBulkReply execute(byte[] key) {
val got = database.hvals(key);
byte[][] got = database.hvals(key);
BulkReply[] replies = new BulkReply[got.length];
for (int i = 0; i < got.length; i++) {
replies[i] = new BulkReply(got[i]);
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/dev/keva/core/command/impl/key/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.Reply;
import dev.keva.store.KevaDatabase;
import lombok.val;

import java.util.Base64;

Expand All @@ -25,7 +24,7 @@ public Dump(KevaDatabase database) {

@Execute
public Reply<?> execute(byte[] key) {
val got = database.get(key);
byte[] got = database.get(key);
if (got == null) {
return BulkReply.NIL_REPLY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.store.KevaDatabase;

import java.nio.charset.StandardCharsets;

import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("expireat")
@ParamLength(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import dev.keva.protocol.resp.reply.Reply;
import dev.keva.protocol.resp.reply.StatusReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

import java.math.BigInteger;
import java.util.Base64;
Expand All @@ -27,7 +26,7 @@ public Restore(KevaDatabase database) {

@Execute
public Reply<?> execute(byte[] key, byte[] ttl, byte[] dump, byte[] replace) {
val old = database.get(key);
byte[] old = database.get(key);
boolean isReplace = replace != null && new String(replace).equalsIgnoreCase("REPLACE");
if (old != null && !isReplace) {
return new ErrorReply("ERR Target key name is busy");
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/dev/keva/core/command/impl/key/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.StatusReply;
import dev.keva.store.KevaDatabase;
import lombok.val;
import org.apache.commons.lang3.SerializationException;
import org.apache.commons.lang3.SerializationUtils;

Expand All @@ -28,7 +27,7 @@ public Type(KevaDatabase database) {

@Execute
public StatusReply execute(byte[] key) {
val got = database.get(key);
byte[] got = database.get(key);
if (got == null) {
return new StatusReply("none");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.store.KevaDatabase;
import lombok.val;

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

@Execute
public BulkReply execute(byte[] key, byte[] index) {
val got = database.lindex(key, Integer.parseInt(new String(index)));
byte[] got = database.lindex(key, Integer.parseInt(new String(index)));
return got == null ? BulkReply.NIL_REPLY : new BulkReply(got);
}
}
Loading

0 comments on commit 4fdb23d

Please sign in to comment.