Skip to content

Commit

Permalink
增加ao持久化策略
Browse files Browse the repository at this point in the history
  • Loading branch information
kwsc98 committed Mar 14, 2023
1 parent e20fa86 commit b6d2809
Show file tree
Hide file tree
Showing 40 changed files with 331 additions and 447 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package pers.kedis.core;

import lombok.Getter;
import pers.kedis.core.persistence.PersistenService;
import pers.kedis.core.protocol.netty.NettyService;
import pers.kedis.core.registry.RegistryBuilderFactory;
import pers.kedis.core.registry.RegistryService;

/**
* @author kwsc98
Expand All @@ -13,18 +12,15 @@ public class KedisApplicationContext {

private NettyService nettyService;

private KedisService kedisService;

private RegistryService registryService;

public static KedisApplicationContext build() {
return new KedisApplicationContext();
}

public KedisApplicationContext init(KedisProperties kedisProperties) {
this.kedisService = new KedisService();
this.registryService = RegistryBuilderFactory.build().setRegistryClientInfo(kedisProperties.getRegisteredPath()).init(kedisService);
this.nettyService = new NettyService(kedisProperties.getPort(), this.kedisService);
KedisService.init(kedisProperties);
PersistenService.init(kedisProperties);
this.nettyService = new NettyService(kedisProperties.getPort());
return this;
}

Expand Down
16 changes: 16 additions & 0 deletions kedis-core/src/main/java/pers/kedis/core/KedisDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
public class KedisDb {
private final Dict<KedisKey, KedisValue<?>> dictMap = new Dict<>(8);

private final int index;

public int getIndex() {
return index;
}

public KedisDb(int index) {
this.index = index;
}


public int size() {
return dictMap.size();
}


public KedisValue<?> getValue(KedisKey key) {
Map.Entry<KedisKey, KedisValue<?>> entry = getEntry(key);
return entry == null ? null : entry.getValue();
Expand Down
6 changes: 4 additions & 2 deletions kedis-core/src/main/java/pers/kedis/core/KedisProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
@Data
public class KedisProperties {

private String registeredPath;
private String dataResourcesPath;

private int port = 8080;
private int dbCount = 16;

private int port = 6379;

}
35 changes: 17 additions & 18 deletions kedis-core/src/main/java/pers/kedis/core/KedisService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import pers.kedis.core.dto.KedisData;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.KedisConfig;
import pers.kedis.core.exception.KedisException;
import pers.kedis.core.persistence.AofService;
import pers.kedis.core.persistence.PersistenInterface;
import pers.kedis.core.persistence.PersistenService;

import java.util.*;

Expand All @@ -22,21 +24,26 @@ public class KedisService {
@Getter
private static KedisDb[] KEDIS_DB_ARRAY;

public static KedisConfig KEDISCONFIG;
public static KedisProperties KEDISCONFIG;

public static void init(int dbCount) {
KEDIS_DB_ARRAY = new KedisDb[dbCount];
for (int i = 0; i < KEDIS_DB_ARRAY.length; i++) {
KEDIS_DB_ARRAY[i] = new KedisDb();
public static PersistenService persistenService;

public synchronized static void init(KedisProperties kedisProperties) {
KedisService.KEDISCONFIG = kedisProperties;
int dbCount = kedisProperties.getDbCount();
KedisDb[] kedisDbs = new KedisDb[dbCount];
for (int i = 0; i < kedisDbs.length; i++) {
kedisDbs[i] = new KedisDb(i);
}
KEDIS_DB_ARRAY = kedisDbs;
}

public static KedisDb getkedisDb(int i) {
return KEDIS_DB_ARRAY[i];
}


public List<KedisData> handles(ChannelDTO channelDTO) {
public static List<KedisData> handles(ChannelDTO channelDTO) {
List<KedisData> res = new ArrayList<>();
List<KedisData> kedisDataList = channelDTO.getKedisDataList();
for (KedisData kedisData : kedisDataList) {
Expand All @@ -45,7 +52,7 @@ public List<KedisData> handles(ChannelDTO channelDTO) {
return res;
}

private KedisData handle(ChannelDTO channelDTO) {
private static KedisData handle(ChannelDTO channelDTO) {
printRequestCommand(channelDTO);
try {
KedisData kedisData = CommandService.handler(channelDTO);
Expand All @@ -65,7 +72,7 @@ private KedisData handle(ChannelDTO channelDTO) {
}


private void printRequestCommand(ChannelDTO channelDTO) {
private static void printRequestCommand(ChannelDTO channelDTO) {
StringBuilder stringBuilder = new StringBuilder();
KedisData kedisData = channelDTO.getKedisData();
if (DataType.RESP_ARRAY == kedisData.getDataType()) {
Expand All @@ -77,7 +84,7 @@ private void printRequestCommand(ChannelDTO channelDTO) {
log.debug("Recrive Command : {} Channel : {} KedisDb : {}", stringBuilder.toString(), channelDTO.getChannel(), channelDTO.getKedisDb());
}

private void printResponeCommand(KedisData kedisData) {
private static void printResponeCommand(KedisData kedisData) {
StringBuilder stringBuilder = new StringBuilder();
if (DataType.RESP_ARRAY == kedisData.getDataType()) {
List<KedisData> list = KedisUtil.convertList(kedisData.getData());
Expand All @@ -90,12 +97,4 @@ private void printResponeCommand(KedisData kedisData) {
log.debug("Command Response : {} ", stringBuilder.toString().replace(new String(RespConstants.CRLF), "||"));
}


public synchronized static void refresh(KedisConfig kedisConfig) {
if (Objects.isNull(KEDIS_DB_ARRAY)) {
init(kedisConfig.getDbCount());
}
KedisService.KEDISCONFIG = kedisConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import pers.kedis.core.dto.DataType;
import pers.kedis.core.exception.KedisException;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -37,8 +39,16 @@ public static void encode(KedisData data, ByteBuf byteBuf) {
respEncoder.encode(data, byteBuf);
}

public static List<KedisData> decodeAll(ByteBuf msg) {
List<KedisData> list = new ArrayList<>();
KedisData pre = null;
while ((pre = decode(msg)) != null) {
list.add(pre);
}
return list;
}

public static KedisData decode(ByteBuf byteBuf) {
;
if (byteBuf.readableBytes() <= 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author kwsc98
*/
public abstract class CommandAbstract implements Command {
public abstract class AbstractCommand implements Command {

protected KedisData getNullKedisData() {
return new KedisData(DataType.BULK_STRING).setData(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pers.kedis.core.command;

/**
* @author kwsc98
*/
public abstract class AbstractUpdateCommand extends AbstractCommand{

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package pers.kedis.core.command;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import pers.kedis.core.codec.resp.RespUtil;
import pers.kedis.core.command.impl.*;
import pers.kedis.core.common.utils.KedisUtil;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.KedisData;
import pers.kedis.core.exception.KedisException;
import pers.kedis.core.persistence.PersistenService;

import java.util.HashMap;
import java.util.List;
Expand All @@ -16,6 +22,7 @@
/**
* @author kwsc98
*/
@Slf4j
public class CommandService {

private static final Map<String, Command> COMMAND_MAP;
Expand All @@ -35,6 +42,7 @@ public class CommandService {
COMMAND_MAP.put(CommandType.GET.name().toUpperCase(), new GetCommandImpl());
COMMAND_MAP.put(CommandType.EXPIRE.name().toUpperCase(), new ExpireCommandImpl());
COMMAND_MAP.put(CommandType.SELECT.name().toUpperCase(), new SelectCommandImpl());
COMMAND_MAP.put(CommandType.DBSIZE.name().toUpperCase(), new DbSizeImplCommand());


}
Expand All @@ -59,6 +67,26 @@ public static KedisData handler(ChannelDTO channelDTO) {
if (Objects.isNull(command)) {
command = COMMAND_MAP.get(CommandType.PING.name().toUpperCase());
}
return command.handler(channelDTO);
KedisData response = null;
try {
response = command.handler(channelDTO);
} catch (Exception e) {
log.error("CommandService Error");
String info = "Kedis Error";
if (e instanceof KedisException) {
KedisException exception = (KedisException) e;
if (StringUtils.isNotEmpty(exception.getMessage())) {
info = exception.getMessage();
}
}
response = new KedisData(DataType.ERROR).setData(info);
}
if (DataType.ERROR != response.getDataType() && command instanceof AbstractUpdateCommand) {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
RespUtil.encode(channelDTO.getKedisData(), byteBuf);
PersistenService.saveCommand(byteBuf, channelDTO.getKedisDb().getIndex());
log.debug("This Command Is Update");
}
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum CommandType {
GET,
EXPIRE,
SELECT,
DBSIZE


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.command.CommandAbstract;
import pers.kedis.core.common.utils.KedisUtil;
import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.KedisData;

Expand All @@ -10,7 +9,7 @@
/**
* @author kwsc98
*/
public class ClientCommandImpl extends CommandAbstract {
public class ClientCommandImpl extends AbstractCommand {

public final String setname = "SETNAME";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.KedisService;
import pers.kedis.core.command.CommandAbstract;
import pers.kedis.core.common.utils.KedisUtil;
import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.KedisData;
Expand All @@ -14,7 +13,7 @@
/**
* @author kwsc98
*/
public class ConfigCommandImpl extends CommandAbstract {
public class ConfigCommandImpl extends AbstractCommand {

public String get = "get";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.KedisData;

/**
* @author kwsc98
*/
public class DbSizeImplCommand extends AbstractCommand {

@Override
public KedisData handler(ChannelDTO channelDTO) {
return new KedisData(DataType.INTEGER).setData(channelDTO.getKedisDb().size());
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.KedisDb;
import pers.kedis.core.command.Command;
import pers.kedis.core.command.CommandAbstract;
import pers.kedis.core.common.utils.KedisUtil;
import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.command.AbstractUpdateCommand;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.KedisData;
import pers.kedis.core.dto.KedisKey;

Expand All @@ -14,7 +11,7 @@
/**
* @author kwsc98
*/
public class ExpireCommandImpl extends CommandAbstract {
public class ExpireCommandImpl extends AbstractUpdateCommand {

@Override
public KedisData handler(ChannelDTO channelDTO) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.KedisDb;
import pers.kedis.core.command.Command;
import pers.kedis.core.command.CommandAbstract;
import pers.kedis.core.common.utils.KedisUtil;
import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.dto.*;

import java.util.List;
Expand All @@ -13,7 +10,7 @@
/**
* @author kwsc98
*/
public class GetCommandImpl extends CommandAbstract {
public class GetCommandImpl extends AbstractCommand {
@Override
public KedisData handler(ChannelDTO channelDTO) {
List<KedisData> list = getCommandList(channelDTO);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package pers.kedis.core.command.impl;

import pers.kedis.core.codec.resp.RespConstants;
import pers.kedis.core.command.CommandAbstract;
import pers.kedis.core.command.AbstractCommand;
import pers.kedis.core.dto.ChannelDTO;
import pers.kedis.core.dto.DataType;
import pers.kedis.core.dto.KedisData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author kwsc98
*/
public class InfoCommandImpl extends CommandAbstract {
public class InfoCommandImpl extends AbstractCommand {

@Override
public KedisData handler(ChannelDTO channelDTO) {
Expand Down
Loading

0 comments on commit b6d2809

Please sign in to comment.