Skip to content

Commit f1a7bf9

Browse files
committed
saver 声明完成
1 parent c94e57d commit f1a7bf9

10 files changed

+144
-1
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
<artifactId>commons-lang3</artifactId>
5252
<version>3.11</version>
5353
</dependency>
54+
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
55+
<dependency>
56+
<groupId>com.alibaba</groupId>
57+
<artifactId>fastjson</artifactId>
58+
<version>1.2.74</version>
59+
</dependency>
60+
5461
</dependencies>
5562

5663

src/main/java/ginyu/common/Consoles.java

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public static void debug(String format, Object... arguments) {
108108
debug(format(format, arguments));
109109
}
110110

111+
public static void info(Object msg) {
112+
info(String.valueOf(msg));
113+
}
111114
public static void info(String msg) {
112115
if (canLog(Level.INFO)) {
113116
if (enableColor) {

src/main/java/ginyu/core/Server.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import ginyu.db.Db;
66
import ginyu.io.Communicator;
77
import ginyu.io.NettyCommunicator;
8+
import ginyu.persist.Saver;
9+
import ginyu.persist.SnapshotSaver;
810
import ginyu.task.CleanExpiredTask;
911
import ginyu.task.WakeupTimeoutClientTask;
1012
import ginyu.utils.ConfigUtils;
1113
import lombok.Getter;
14+
import lombok.ToString;
1215
import org.apache.commons.io.FileUtils;
1316

1417
import java.io.File;
@@ -24,6 +27,7 @@
2427
* @description:
2528
*/
2629
@SuppressWarnings("all")
30+
@ToString
2731
public class Server {
2832

2933
public static final Server INSTANCE = new Server();
@@ -36,6 +40,8 @@ public class Server {
3640
@Getter
3741
private Db db;
3842

43+
private Saver saver;
44+
3945
private CleanExpiredTask cleanExpiredTask;
4046

4147
private WakeupTimeoutClientTask wakeupTimeoutClientTask;
@@ -49,6 +55,7 @@ public void init(String[] args) {
4955
this.db = new Db(this.ginyuConfig.getDbSize());
5056
this.cleanExpiredTask = new CleanExpiredTask();
5157
this.wakeupTimeoutClientTask = new WakeupTimeoutClientTask();
58+
this.saver = new SnapshotSaver();
5259
}
5360

5461
private void printEnv() {
@@ -80,7 +87,6 @@ public void start() throws Exception {
8087
Consoles.info("start listening port {}", ginyuConfig.getPort());
8188
this.communicator.start(ginyuConfig);
8289
this.startTask();
83-
Consoles.info("server started");
8490
}
8591

8692
public void addClient(Client client) {
@@ -95,4 +101,8 @@ private void startTask() {
95101
this.cleanExpiredTask.start();
96102
this.wakeupTimeoutClientTask.start();
97103
}
104+
105+
public void save() {
106+
this.saver.save();
107+
}
98108
}

src/main/java/ginyu/db/Database.java

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Database {
2121
@Getter
2222
private final Integer id;
2323

24+
@Getter
2425
private final Dict<String, RedisObject> dict;
2526

2627
@Getter
@@ -39,6 +40,14 @@ public Database(Integer id) {
3940
this.timeoutSets = new ConcurrentSkipListSet<>();
4041
}
4142

43+
public Database(Database database) {
44+
this.id = database.id;
45+
this.dict = new Dict<>(database.getDict());
46+
this.expired = new ConcurrentSkipListMap<>(database.getExpired());
47+
this.blockingDict = new Dict<>();
48+
this.timeoutSets = new ConcurrentSkipListSet<>();
49+
}
50+
4251
public RedisObject get(String key) {
4352
return this.dict.get(key);
4453
}

src/main/java/ginyu/db/Db.java

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public Db(Integer dbSize) {
2222
}
2323
}
2424

25+
public Db(Db db) {
26+
List<Database> databases = db.getDatabases();
27+
this.databases = new ArrayList<>(databases.size());
28+
for (Database database : databases) {
29+
this.databases.add(new Database(database));
30+
}
31+
}
32+
2533
public Database getDatabase(Integer id) {
2634
return this.databases.get(id);
2735
}

src/main/java/ginyu/object/Dict.java

+7
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@
88
* @description: TODO redis底层双数组
99
*/
1010
public class Dict<K, V> extends ConcurrentHashMap<K, V> {
11+
12+
public Dict() {
13+
}
14+
15+
public Dict(Dict<K, V> dict) {
16+
super(dict);
17+
}
1118
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ginyu.persist;
2+
3+
import ginyu.core.Server;
4+
5+
public interface Saver {
6+
7+
void save();
8+
9+
default void load() {
10+
load(null);
11+
}
12+
13+
void load(String filePath);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ginyu.persist;
2+
3+
import ginyu.db.Db;
4+
import lombok.Data;
5+
6+
/**
7+
* @author: junjiexun
8+
* @date: 2020/10/27 9:48 下午
9+
* @description:
10+
*/
11+
@Data
12+
public class ServerForSaver {
13+
14+
private Db db;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ginyu.persist;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import ginyu.common.Consoles;
5+
import ginyu.core.Server;
6+
import ginyu.db.Db;
7+
import ginyu.event.Events;
8+
9+
import java.util.function.Supplier;
10+
11+
/**
12+
* @author: junjiexun
13+
* @date: 2020/10/27 9:47 下午
14+
* @description:
15+
*/
16+
public class SnapshotSaver implements Saver {
17+
18+
@Override
19+
public void save() {
20+
Events.post((Supplier<Void>) () -> {
21+
final Server server = Server.INSTANCE;
22+
ServerForSaver serverForSaver = new ServerForSaver();
23+
serverForSaver.setDb(new Db(server.getDb()));
24+
String s = JSONObject.toJSONString(serverForSaver, true);
25+
Consoles.info(s);
26+
return null;
27+
});
28+
}
29+
30+
@Override
31+
public void load(String filePath) {
32+
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ginyu.persist;
2+
3+
import com.google.common.eventbus.Subscribe;
4+
import ginyu.common.Consoles;
5+
import ginyu.core.Client;
6+
import ginyu.core.ClientTimeoutWrapper;
7+
import ginyu.db.Database;
8+
import ginyu.event.BlockEvent;
9+
import ginyu.event.list.BlockByBPopEvent;
10+
import ginyu.event.list.PushEvent;
11+
import ginyu.object.ListObject;
12+
import ginyu.object.ObjectType;
13+
import ginyu.protocol.Arrays;
14+
import ginyu.protocol.Resp2;
15+
import ginyu.protocol.Validates;
16+
import ginyu.utils.BlockUtils;
17+
18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
import java.util.function.Supplier;
22+
23+
/**
24+
* @author: junjiexun
25+
* @date: 2020/10/20 1:41 下午
26+
* @description:
27+
*/
28+
@SuppressWarnings("all")
29+
public class SnapshotSaverListener {
30+
31+
@Subscribe
32+
public void saveSnapshot(Supplier<Void> supplier) {
33+
supplier.get();
34+
Consoles.info("save done");
35+
}
36+
}

0 commit comments

Comments
 (0)