Skip to content

Commit f18b616

Browse files
committed
还有RedisObject的toSnapshot
1 parent ea844e3 commit f18b616

13 files changed

+98
-17
lines changed

src/main/java/ginyu/core/Snapshot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public interface Snapshot {
44

5-
byte[] toSnapshot();
5+
String toSnapshot();
66
}

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package ginyu.db;
22

33
import ginyu.core.ClientTimeoutWrapper;
4+
import ginyu.core.Snapshot;
45
import ginyu.event.BlockEvent;
56
import ginyu.object.Dict;
67
import ginyu.object.RedisObject;
78
import lombok.Getter;
8-
import lombok.NoArgsConstructor;
99
import lombok.Setter;
1010

1111
import java.util.Set;
@@ -18,7 +18,7 @@
1818
* @description:
1919
*/
2020
@SuppressWarnings("all")
21-
public class Database {
21+
public class Database implements Snapshot {
2222

2323
@Getter
2424
@Setter
@@ -30,7 +30,7 @@ public class Database {
3030

3131
@Getter
3232
@Setter
33-
private ConcurrentSkipListMap<String, Long> expired;
33+
private Dict<String, Long> expired;
3434

3535
private Dict<String, ConcurrentSkipListSet<BlockEvent>> blockingDict;
3636

@@ -45,15 +45,15 @@ public Database() {
4545
public Database(Integer id) {
4646
this.id = id;
4747
this.dict = new Dict<>();
48-
this.expired = new ConcurrentSkipListMap<>();
48+
this.expired = new Dict<>();
4949
this.blockingDict = new Dict<>();
5050
this.timeoutSets = new ConcurrentSkipListSet<>();
5151
}
5252

5353
public Database(Database database) {
5454
this.id = database.id;
5555
this.dict = new Dict<>(database.getDict());
56-
this.expired = new ConcurrentSkipListMap<>(database.getExpired());
56+
this.expired = new Dict<>(database.getExpired());
5757
this.blockingDict = new Dict<>();
5858
this.timeoutSets = new ConcurrentSkipListSet<>();
5959
}
@@ -156,4 +156,13 @@ public void deleteIfNeeded(String key) {
156156
public void removeBlockKey(String blockKey) {
157157
this.blockingDict.remove(blockKey);
158158
}
159+
160+
@Override
161+
public String toSnapshot() {
162+
StringBuilder sb = new StringBuilder();
163+
sb.append(this.id);
164+
sb.append(this.dict.toSnapshot());
165+
sb.append(this.expired.toSnapshot());
166+
return sb.toString();
167+
}
159168
}

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ginyu.db;
22

3+
import ginyu.core.Snapshot;
34
import lombok.Getter;
4-
import lombok.NoArgsConstructor;
55

66
import java.util.ArrayList;
77
import java.util.List;
@@ -11,7 +11,7 @@
1111
* @date: 2020/10/13 10:03 下午
1212
* @description:
1313
*/
14-
public class Db {
14+
public class Db implements Snapshot {
1515

1616
@Getter
1717
private List<Database> databases;
@@ -39,11 +39,19 @@ public Database getDatabase(Integer id) {
3939
}
4040

4141
public void swapDb(Integer index1, Integer index2) {
42-
synchronized (this.databases) {
43-
Database database1 = databases.get(index1);
44-
Database database2 = databases.get(index2);
45-
databases.set(index1, database2);
46-
databases.set(index2, database1);
42+
Database database1 = databases.get(index1);
43+
Database database2 = databases.get(index2);
44+
databases.set(index1, database2);
45+
databases.set(index2, database1);
46+
}
47+
48+
@Override
49+
public String toSnapshot() {
50+
StringBuilder sb = new StringBuilder();
51+
sb.append(this.databases.size());
52+
for (Database database : this.databases) {
53+
sb.append(database.toSnapshot());
4754
}
55+
return sb.toString();
4856
}
4957
}

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
package ginyu.object;
22

3+
import ginyu.core.Snapshot;
4+
35
import java.util.concurrent.ConcurrentHashMap;
46

57
/**
68
* @author: junjiexun
79
* @date: 2020/10/13 10:10 下午
810
* @description: TODO redis底层双数组
911
*/
10-
public class Dict<K, V> extends ConcurrentHashMap<K, V> {
12+
public class Dict<K, V> extends ConcurrentHashMap<K, V> implements Snapshot {
1113

1214
public Dict() {
1315
}
1416

1517
public Dict(Dict<K, V> dict) {
1618
super(dict);
1719
}
20+
21+
@Override
22+
public String toSnapshot() {
23+
StringBuilder sb = new StringBuilder();
24+
sb.append(this.size());
25+
for (Entry<K, V> kvEntry : this.entrySet()) {
26+
// TODO 如果没有实现Snapshot的话 用toString肯定有问题的, key的长度也要存下来
27+
if (kvEntry.getKey() instanceof Snapshot) {
28+
sb.append(((Snapshot) kvEntry.getKey()).toSnapshot());
29+
} else {
30+
sb.append(kvEntry.getKey().toString());
31+
}
32+
if (kvEntry.getValue() instanceof Snapshot) {
33+
sb.append(((Snapshot) kvEntry.getValue()).toSnapshot());
34+
} else {
35+
sb.append(kvEntry.getValue().toString());
36+
}
37+
}
38+
return sb.toString();
39+
}
1840
}

src/main/java/ginyu/object/HashObject.java

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ public HashObject() {
1616
public boolean isEmptyValue() {
1717
return super.isEmptyValue() || this.getOriginal().isEmpty();
1818
}
19+
20+
@Override
21+
public String toSnapshot() {
22+
return null;
23+
}
1924
}

src/main/java/ginyu/object/ListObject.java

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ public ListObject() {
1616
public boolean isEmptyValue() {
1717
return super.isEmptyValue() || this.getOriginal().isEmpty();
1818
}
19+
20+
@Override
21+
public String toSnapshot() {
22+
return null;
23+
}
1924
}

src/main/java/ginyu/object/RedisObject.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ginyu.object;
22

3+
import ginyu.core.Snapshot;
34
import lombok.Data;
45

56
/**
@@ -9,7 +10,7 @@
910
*/
1011

1112
@Data
12-
public abstract class RedisObject<T> {
13+
public abstract class RedisObject<T> implements Snapshot {
1314

1415
private ObjectType type;
1516

src/main/java/ginyu/object/SetObject.java

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public boolean isEmptyValue() {
1919
return super.isEmptyValue() || this.getOriginal().isEmpty();
2020
}
2121

22+
@Override
23+
public String toSnapshot() {
24+
return null;
25+
}
26+
2227
public static class None {
2328
private None() {
2429
}

src/main/java/ginyu/object/StringObject.java

+5
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public StringObject(String value) {
1919
this.setType(ObjectType.STRING);
2020
this.setOriginal(new GinyuString(value));
2121
}
22+
23+
@Override
24+
public String toSnapshot() {
25+
return null;
26+
}
2227
}

src/main/java/ginyu/object/ZSetObject.java

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public ZSetObject() {
1111
this.setType(ObjectType.ZSET);
1212
this.setOriginal(new ZSet());
1313
}
14+
15+
@Override
16+
public String toSnapshot() {
17+
return null;
18+
}
1419
}

src/main/java/ginyu/persist/ServerForSaver.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ginyu.persist;
22

3+
import ginyu.core.Snapshot;
34
import ginyu.db.Db;
45
import lombok.Data;
56

@@ -9,7 +10,12 @@
910
* @description:
1011
*/
1112
@Data
12-
public class ServerForSaver {
13+
public class ServerForSaver implements Snapshot {
1314

1415
private Db db;
16+
17+
@Override
18+
public String toSnapshot() {
19+
return this.db.toSnapshot();
20+
}
1521
}

src/main/java/ginyu/persist/SnapshotSaver.java

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public synchronized void save() {
4949
final Server server = Server.INSTANCE;
5050
ServerForSaver serverForSaver = new ServerForSaver();
5151
serverForSaver.setDb(new Db(server.getDb()));
52+
String snapshotPath = Server.INSTANCE.getGinyuConfig().getSnapshotPath();
53+
if (snapshotPath == null || snapshotPath.isEmpty()) {
54+
snapshotPath = DEFAULT_SNAPSHOT_PATH;
55+
}
56+
try {
57+
FileUtils.writeByteArrayToFile(new File(snapshotPath), serverForSaver.toSnapshot().getBytes());
58+
} catch (IOException e) {
59+
Consoles.error(e.getMessage());
60+
}
5261
return null;
5362
});
5463
}

src/main/java/ginyu/task/CleanExpiredTask.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ginyu.core.Server;
44
import ginyu.db.Database;
55
import ginyu.db.Db;
6+
import ginyu.object.Dict;
67

78
import java.util.*;
89
import java.util.concurrent.ConcurrentSkipListMap;
@@ -23,7 +24,7 @@ public void run() {
2324
Db db = Server.INSTANCE.getDb();
2425
long now = System.currentTimeMillis();
2526
for (Database database : db.getDatabases()) {
26-
ConcurrentSkipListMap<String, Long> expired = database.getExpired();
27+
Dict<String, Long> expired = database.getExpired();
2728
List<Map.Entry<String, Long>> entryList = new ArrayList<>(expired.entrySet());
2829
entryList.sort(Comparator.comparingLong(Map.Entry::getValue));
2930
for (Map.Entry<String, Long> entry : entryList) {

0 commit comments

Comments
 (0)