Skip to content

Commit

Permalink
version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
0125nia committed Aug 21, 2023
1 parent 0d8ec50 commit 2ba6781
Show file tree
Hide file tree
Showing 24 changed files with 210 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*.class

# Log file
*.log
#*.log

# BlueJ files
*.ctxt
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/nia/DataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ public class DataBase {
public static void main(String[] args) {
//创建Reactor
Reactor reactor = Reactor.getInstance();
//注册钩子函数
Runtime.getRuntime().addShutdownHook(new Thread(reactor::stop));
//开启Reactor
reactor.start();

// 在关闭钩子中停止服务器
Runtime.getRuntime().addShutdownHook(new Thread(reactor::stop));

}
}
14 changes: 0 additions & 14 deletions src/main/java/com/nia/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,7 @@ public void start() {
}
}

public void close() {
try {
if (socketChannel != null && socketChannel.isOpen()) {
socketChannel.close();
}

if (selector != null && selector.isOpen()) {
selector.close();
}

System.out.println("连接已关闭");
} catch (IOException e) {
e.printStackTrace();
}
}


}
8 changes: 6 additions & 2 deletions src/main/java/com/nia/command/AbstractArrayListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.nia.dao.loader.DataCacheProcessor;
import com.nia.pojo.arraylist.MArrayList;

public interface AbstractArrayListCommand extends Command{
public interface AbstractArrayListCommand extends Command {

default MArrayList<String> getArrayList(String key){
default MArrayList<String> getArrayList(String key) {
MArrayList<String> list = DataCacheProcessor.get(key);
if (list == null) {
DataCacheProcessor.<MArrayList<String>>put(key, new MArrayList<>());
}
return DataCacheProcessor.get(key);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/nia/command/AbstractHashMapCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public interface AbstractHashMapCommand extends Command {
* @return 对应的映射数据
*/
default MHashMap<String, String> getHashMap(String key) {
MHashMap<String, String> map = DataCacheProcessor.get(key);
if (map == null) {
DataCacheProcessor.<MHashMap<String, String>>put(key, new MHashMap<>());
}
return DataCacheProcessor.get(key);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/nia/command/AbstractLinkedListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public interface AbstractLinkedListCommand extends Command {
* @return 返回key对应的链表
*/
default MLinkedList<String> getLinkedList(String key) {
MLinkedList<String> list = DataCacheProcessor.get(key);
if (list == null){
DataCacheProcessor.<MLinkedList<String>>put(key,new MLinkedList<>());
}
return DataCacheProcessor.get(key);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/nia/command/AbstractPushCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public String handleCommand(String[] cmd, String cmdStr) {
pushToList(linkedList, value);
//修改数据到持久化策略类中
PersistenceContext.appendToStrategy(cmdStr, PersistentDataIdentifier.LINKEDLIST_DATA);
System.out.println("111");
//返回信息
return ResponseMsg.SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nia/command/DDLCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public String handleCommand(String[] cmd, String cmdStr) {
try {
ddl = DataCacheProcessor.ddl(key);
} catch (NullPointerException e) {
return ResponseMsg.NULL_DATA;
return null;
}
return String.valueOf(ddl/1000);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/nia/command/DelCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nia.command;

import com.nia.dao.loader.DataCacheProcessor;
import com.nia.dao.persistent.PersistenceContext;
import com.nia.pojo.PersistentDataIdentifier;
import com.nia.pojo.hashmap.MHashMap;
Expand All @@ -14,8 +15,8 @@ public class DelCommand implements AbstractStringCommand {
@Override
public String handleCommand(String[] cmd, String cmdStr) {
String key = cmd[1];
MHashMap<String, String> stringData = new MHashMap<>();
String remove = stringData.remove(key);
String remove = DataCacheProcessor.remove(key);
System.out.println(remove);
//修改数据到持久化策略类中
PersistenceContext.appendToStrategy(cmdStr, PersistentDataIdentifier.STRING_DATA);
//返回成功数据
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nia/dao/loader/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void addToKeyList(String key) {
//移除key
public <V> V remove(String key) {
CacheObject<?> remove = cacheMap.remove(key);
return (V) remove;
return (V) remove.getValue();
}

//判断是否含有该key
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/com/nia/dao/loader/DataCacheProcessor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.nia.dao.loader;

import com.nia.dao.persistent.PersistenceContext;
import com.nia.reactor.Reactor;

/**
* 数据缓存处理器
*/
public class DataCacheProcessor {

private static Cache cache = Cache.getInstance();//获取缓存池实例
private static final String FLUSH_TYPE = ConfigLoader.getString("flush");

//私有化构造器
private DataCacheProcessor() {
Expand All @@ -31,14 +33,17 @@ public static <V> void put(String key, V value) {
*
* @param key 操作的键
* @param <V> 缓存数据的泛型
* @return 返回缓存数据
* @return 返回数据
* @throws NullPointerException 抛出异常交给具体命令类处理
*/
public static <V> V get(String key) throws NullPointerException {
if (cache.containKey(key)) {
return cache.get(key);
if (!cache.containKey(key)){
boolean b = PersistenceContext.loadData(key);
if (!b){
return null;
}
}
return PersistenceContext.loadData(key);
return cache.get(key);
}


Expand All @@ -56,13 +61,26 @@ public static void expire(String key, long delay) throws NullPointerException {

//清空缓存
public static void flush() {
cache.clear();
if (FLUSH_TYPE.equals("async")){
asyncFlush();
}else {
cache.clear();
Reactor.LOGGER.info("sync flush cache");
}
}
private static void asyncFlush(){
Runnable runnable = cache::clear;//创建Runnable对象
//线程池分发线程处理
Reactor.threadDistributor.distribute(runnable);
Reactor.LOGGER.info("async flush cache");
}


//移除缓存池中key对应的数据
public static <V> V remove(String key) {
return cache.remove(key);
V remove = cache.remove(key);
PersistenceContext.bgSaveData();//异步存储数据
return remove;
}


Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/nia/dao/persistent/BinaryStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

Expand All @@ -29,6 +31,8 @@ public void save() {
for (MMap.MEntry<String, byte[]> entry : binaryDataMap) {
String key = entry.getKey();
byte[] value = entry.getValue();
byte[] bytes1 = key.getBytes(StandardCharsets.UTF_8);

}

//写入文件
Expand All @@ -48,8 +52,19 @@ public void save() {


@Override
public <V> void load(String key) {

public <V> boolean load(String key) {
//判断文件是否存在
if (!Files.exists(path)) {
//若不存在则新建并直接返回
try {
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

return false;

}

Expand Down Expand Up @@ -78,9 +93,8 @@ private byte[] convertToBinary(Object value) {
return binaryData;

} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}


Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/nia/dao/persistent/LogAppendingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LogAppendingStrategy implements PersistenceStrategy {
/**
* 将新的指令存放到集合中
*
* @param cmd 追加的指令
* @param cmd 追加的指令
* @param sign 数据类型标识
*/
@Override
Expand All @@ -49,7 +49,6 @@ public void appendQueue(String cmd, byte sign) {
} else {
list.add(cmd);
}

}


Expand Down Expand Up @@ -91,17 +90,21 @@ public void save() {
} catch (IOException e) {
e.printStackTrace();
}
Reactor.LOGGER.info("save data");//将save操作写入日志
}


@Override
public <V> void load(String key) {
public boolean load(String key) {
//存放日志文件内容的字符串
//判断文件是否存在
if (!Files.exists(path)) {
//若不存在则直接抛出空指针异常
throw new NullPointerException();
//若不存在则新建并直接返回
try {
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
//获取FileChannel
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.CREATE)) {
Expand All @@ -110,14 +113,20 @@ public <V> void load(String key) {
//判断读取的字符串是否只含有空格或为空
if (!commands.trim().isEmpty()) {
String[] keyCommands = getKeyCommands(commands, key);
if (keyCommands.length == 0){
return false;
}
//使用Stream流对数据中的数据进行处理
String[] commandArray = processCommands(keyCommands);
//写入缓存
executeCmd(commandArray);
return true;
}

} catch (IOException e) {
e.printStackTrace();
}
return false;
}

private String[] getKeyCommands(String commands, String key) {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/nia/dao/persistent/PersistenceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class PersistenceContext {
//定义静态策略类常量
private static final PersistenceStrategy PERSISTENCESTRATEGY;

public static PersistenceStrategy getPERSISTENCESTRATEGY() {
return PERSISTENCESTRATEGY;
}

//静态代码块,判断持久化类型
static {
Expand All @@ -29,21 +32,26 @@ public class PersistenceContext {

public static void saveData() {
PERSISTENCESTRATEGY.save();
Reactor.LOGGER.info("save data");//将save操作写入日志
}

//读取数据写入缓存
public static<V> V loadData(String key) {
public static boolean loadData(String key) {
Reactor.LOGGER.info("load data");//加载数据
// return PERSISTENCESTRATEGY.load();
return null;
return PERSISTENCESTRATEGY.load(key);
}

public static void appendToStrategy(String cmd, byte sign) {
PERSISTENCESTRATEGY.appendQueue(cmd, sign);
}

/**
* 异步持久化数据
*/
public static void bgSaveData() {
new Thread(PERSISTENCESTRATEGY::save).start();
Runnable runnable = PERSISTENCESTRATEGY::save;//创建Runnable对象
//线程池分发线程处理
Reactor.threadDistributor.distribute(runnable);
Reactor.LOGGER.info("bgsave data");//将bgsave操作写入日志
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface PersistenceStrategy {

void save();

<V> void load(String key);
<V> boolean load(String key);

void appendQueue(String cmd, byte sign);
}
Loading

0 comments on commit 2ba6781

Please sign in to comment.