forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeersStore.java
More file actions
77 lines (67 loc) · 1.9 KB
/
PeersStore.java
File metadata and controls
77 lines (67 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.tron.core.db;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.tron.common.overlay.discover.node.Node;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
@Component
public class PeersStore extends TronDatabase<Set<Node>> {
@Autowired
public PeersStore(ApplicationContext ctx) {
super("peers");
}
@Override
public LevelDbDataSourceImpl getDbSource() {
return super.getDbSource();
}
@Override
public void reset() {
super.reset();
}
@Override
public void put(byte[] key, Set<Node> nodes) {
StringBuffer sb = new StringBuffer();
nodes.forEach(node -> sb.append(node.getEnodeURL()).append("&").append(node.getReputation())
.append("||"));
dbSource.putData(key, sb.toString().getBytes());
}
@Override
public void delete(byte[] key) {
dbSource.deleteData(key);
}
@Override
public Set<Node> get(byte[] key) {
Set<Node> nodes = new HashSet<>();
byte[] value = dbSource.getData(key);
if (value != null) {
StringTokenizer st = new StringTokenizer(new String(value), "||");
while (st.hasMoreElements()) {
String strN = st.nextToken();
int ps = strN.indexOf("&");
int rept;
Node n;
if (ps > 0) {
n = new Node(strN.substring(0, ps));
try {
rept = Integer.parseInt(strN.substring(ps + 1, strN.length()));
} catch (NumberFormatException e) {
rept = 0;
}
} else {
n = new Node(strN);
rept = 0;
}
n.setReputation(rept);
nodes.add(n);
}
}
return nodes;
}
@Override
public boolean has(byte[] key) {
return dbSource.getData(key) != null;
}
}