-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiBit.java
186 lines (152 loc) · 5.65 KB
/
MultiBit.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class MultiBit extends Trie {
int [] stride ;
private Node root;
public MultiBit(String BGPTablePath, String IPTablePath) {
super(BGPTablePath, IPTablePath);
this.stride = new int[super.stride.length];
System.arraycopy(super.stride, 0, this.stride, 0, super.stride.length);
this.root = new Node(0);
}
private class Node {
String[] prefix ;
Node [] pointer ;
HashMap<String, String> data;
int level;
Node(int level){
this.level= level;
this.prefix = new String [(int)Math.pow(2, stride[level])];
this.pointer = new Node [(int)Math.pow(2, stride[level])];
this.data = new HashMap<>();
increaseNode();
increaseMemory(ptrSize*(int)Math.pow(2, stride[level])*2);
}
public boolean addData(String prefix, String nextHopData) {
if (data.containsKey(prefix)) return false;
data.put(prefix, nextHopData);
return true;
}
public int countPrefix() {
int sum = 0;
for (String s: prefix) {
if (s != null) {
sum++;
}
}
for (Node n: pointer) {
if (n != null) {
sum += n.countPrefix();
}
}
return sum;
}
}
public boolean lookupEntry(String ip){
if(ip==null)return false;
int level = 0;
int memoryAccess = 0;
Node curNode = root;
String nextHopData = null;
String[] ipComponents = calculateIPComponent(ip, 32);
if (ipComponents == null) return false;
while(true){
int ipValue;
if(ipComponents[level].indexOf('/')!=-1){
ipValue = Integer.valueOf(ipComponents[level].split("/")[0]);
}else ipValue = Integer.valueOf(ipComponents[level]);
memoryAccess++;//access to this level node
String longestMatch = internalBestMacth(ipValue,curNode);
if(longestMatch!=null) {
memoryAccess++;
//fetch the data
nextHopData = longestMatch;
}
if(curNode.pointer[ipValue]==null)break;
memoryAccess++;//access pointer array
curNode = curNode.pointer[ipValue];
level++;
}
System.out.println("Look up IP address : " + ip + " => Memory Access: " + memoryAccess + " times");
System.out.println("Next Hop Data: " + (nextHopData == null ? "Not Found" : nextHopData));
return true;
}
public String internalBestMacth(int ipValue, Node curNode){
if(curNode.prefix[ipValue]!=null) {
if (curNode.data.containsKey(curNode.prefix[ipValue]))
return curNode.data.get(curNode.prefix[ipValue]);
}
return null;
}
public boolean insertEntry (String entry){
if (entry==null)return false;
String[] fields = entry.split(" +");
int maskBits = calculateMask(fields[0]);
String[] ipComponents = calculateIPComponent(fields[0], maskBits); // turn prefix into array of n*[child index]+[data index]
if (maskBits == -1 || ipComponents == null) return false;
maskLength[maskBits]++; // record mask length distribution
Node cur = root;
int level =0;
while(level<ipComponents.length-1){
int index = Integer.valueOf(ipComponents[level]);
if (cur.pointer[index]==null){
cur.pointer[index]= new Node(level+1);
}
cur = cur.pointer[index];
level++;
}
String prefix = ipComponents[level];
String strs[] = prefix.split("/");
int diff = stride[level]-Integer.valueOf(strs[1]);
int basic = Integer.valueOf(strs[0])<< diff;
int extend = basic;
for(int i= 0; i<((int)Math.pow(2, diff));i++){
extend = basic+i;
if(cur.prefix[extend]!=null){
if(Integer.valueOf(cur.prefix[extend].split("/")[1])<Integer.valueOf(prefix.split("/")[1])){
cur.prefix[extend]=prefix;
}
}
else cur.prefix[extend]=prefix;
}
if (!cur.addData(prefix, String.join(" ", fields))) return false;
recordMemory();
return true;
}
@Override
public int countPrefix() {
return root.countPrefix();
}
public void display(){
Queue<Node> queue = new LinkedList<>();
Queue<Integer> id = new LinkedList<>();
queue.offer(root);
id.offer(0);
int num=0;
while(!queue.isEmpty()){
Node tmp = queue.poll();
int n = id.poll();
System.out.println();
System.out.println("Node:"+n);
System.out.println("Level"+tmp.level);
int level = tmp.level;
for(int i =0;i<(int)Math.pow(2, stride[level]);i++){
System.out.print(i);
System.out.print(":");
if(tmp.prefix[i]==null) System.out.print("NULL");
else System.out.print(tmp.prefix[i]+"*");
if(tmp.pointer[i]!=null) {
queue.offer(tmp.pointer[i]);
num++;
id.offer(num);
System.out.print("|Point to:Node" +num);
}else{
System.out.print("|Point to:Null");
}
System.out.println();
}
}
}
}