-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMilestone2.java
261 lines (231 loc) · 7.55 KB
/
Milestone2.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.io.*;
import java.util.*;
import java.security.*;
public class Milestone2 {
public static final long sizeBound = (1<<20);
public static final int smallM = 4;
public static final long smallMax = 1024;
public static final long smallMask = 0xF;
public static final int largeM = 1024;
public static final long largeMax = (1<<20);
public static final long largeMask = 0xFFF;
public static final long d = 257;
public static final long q = 0xFFFFFFFFL; // mod q (1<<32)
public static final long Q = 0x100000000L; // mod q (1<<32)
public static long[] modQ = new long[256];
public static long totalChunk = 0;
public static long uniqueChunk = 0;
public static long duplicatedChunk = 0;
public static long byteWith = 0;
public static long byteWithout = 0;
public static void init(long d, int m, long q) {
long dm = 1;
while (m > 0) {
if ((m & 1) == 1)
dm = (dm * d) & q;
m = m / 2;
d = (d * d) & q;
}
for (int i = 0; i < 256; i++)
modQ[i] = (i * dm) & q;
}
public static String bytesToHex(byte[] b) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j < b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
public static Integer one = new Integer(1);
public static void manageChunk(byte[] chunk, int len, StringBuffer metaBuf) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(Arrays.copyOfRange(chunk, 0, len));
byte[] sha1out = md.digest();
String sha1hex = bytesToHex(sha1out);
// System.out.println("SHA1HEX = "+sha1hex);
metaBuf.append(sha1hex);
metaBuf.append('\n');
Integer val = chunkIndex.get(sha1hex);
if (val == null) {
//Unique chunk!
//TODO Upload to Swift in Milestone 3
chunkIndex.put(sha1hex, one);
chunkSizeIndex.put(sha1hex, new Integer(len));
uniqueChunk++;
byteWith += len;
} else {
//Duplicated chunk! Do not upload
chunkIndex.put(sha1hex, new Integer(val.intValue()+1));
duplicatedChunk++;
}
totalChunk ++;
byteWithout += len;
} catch (Exception e) {
e.printStackTrace();
}
}
public static void chunkFile(File file, int m, long d, long q, long maxSize, long mask) {
init(d, m, q);
try {
FileInputStream fis = new FileInputStream(file);
StringBuffer metaBuf = new StringBuffer();
//TODO for Milestone3, write metaBuf to Swift instead of fos
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+".meta");
int n, head = 0, tail = 0, bval, chunkLen = 0;
long rfp = 0, offset = 0, lastPos = 0;
int[] byteQ = new int[m];
byte[] buf = new byte[1<<20];
byte[] chunk = new byte[(1<<20)+5];
// System.out.println(offset);
if ((n = fis.read(buf, 0, m)) > 0) {
offset += n;
for (int i = 0; i < n; ++i) {
bval = ((int) buf[i]) & 0xFF;
byteQ[tail] = bval;
tail = (tail + 1) % m;
rfp = ((rfp * d ) & q) + bval;
chunk[chunkLen++] = buf[i];
}
while ((n = fis.read(buf, 0, 1<<20)) > 0) {
for (int i = 0; i < n; ++i) {
if ((offset - lastPos >= m) && ((rfp & mask) == 0)) {
manageChunk(chunk, chunkLen, metaBuf);
chunkLen = 0;
// System.out.println(offset);
lastPos = offset;
} else if (offset - lastPos == maxSize) {
manageChunk(chunk, chunkLen, metaBuf);
chunkLen = 0;
// System.out.println(offset);
lastPos = offset;
}
bval = ((int) buf[i]) & 0xFF;
chunk[chunkLen++] = buf[i];
rfp = (((rfp * d) & q) + Q - modQ[byteQ[head]] + bval) & q;
head = (head + 1) % m;
byteQ[tail] = bval;
tail = (tail + 1) % m;
offset++;
}
}
if (chunkLen > 0) {
manageChunk(chunk, chunkLen, metaBuf);
}
}
fos.write(metaBuf.toString().getBytes());
fos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void fileAdd(String filepath) {
try {
File inputFile = new File(filepath);
if (inputFile.exists() && inputFile.isFile() && inputFile.canRead()) {
System.out.print("Start processing......");
if (inputFile.length() < sizeBound) {
chunkFile(inputFile, smallM, d, q, smallMax, smallMask);
} else {
chunkFile(inputFile, largeM, d, q, largeMax, largeMask);
}
System.out.println("Done!");
System.out.println("- Total chunks = " + totalChunk);
System.out.println("- No. of unique chunks = " + uniqueChunk);
System.out.println("- No. of duplicated chunks = " + duplicatedChunk);
System.out.println("- No. of Bytes with deduplication = " + byteWith);
System.out.println("- No. of Bytes without deduplication = " + byteWithout);
} else {
System.out.println("Filepath is invalid or file cannot be read");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void fileDel(String filepath) {
try {
//TODO: in milestone3, get the inputstream from swift instead of local file
File inputFile = new File(filepath+".meta");
if (inputFile.exists() && inputFile.isFile() && inputFile.canRead()) {
System.out.print("Start processing......");
FileInputStream fis = new FileInputStream(inputFile);
Scanner sc = new Scanner(fis);
String nextSHA1;
Integer val;
while (sc.hasNext()) {
nextSHA1 = sc.next();
val = chunkIndex.get(nextSHA1);
if (val == null) {
System.out.println("Error: No such chunk!");
} else
if (val.intValue() == 1) {
//TODO delete this chunk in Milestone 3
chunkIndex.remove(nextSHA1);
Integer sval = chunkSizeIndex.remove(nextSHA1);
uniqueChunk--;
byteWith -= sval.intValue();
byteWithout -= sval.intValue();
} else {
chunkIndex.put(nextSHA1, new Integer(val.intValue()-1));
Integer sval = chunkSizeIndex.get(nextSHA1);
duplicatedChunk --;
byteWithout -= sval.intValue();
}
totalChunk --;
}
sc.close();
fis.close();
if (!inputFile.delete()) {
System.out.println("Error: Cannot delete file: "+inputFile.getAbsolutePath());
}
System.out.println("Done!");
System.out.println("- Total chunks = " + totalChunk);
System.out.println("- No. of unique chunks = " + uniqueChunk);
System.out.println("- No. of duplicated chunks = " + duplicatedChunk);
System.out.println("- No. of Bytes with deduplication = " + byteWith);
System.out.println("- No. of Bytes without deduplication = " + byteWithout);
} else {
System.out.println("Filepath is invalid or file has not been uploaded");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static HashMap<String, Integer> chunkIndex = null;
public static HashMap<String, Integer> chunkSizeIndex = null;
public static void main(String[] args) {
boolean exit = false;
String input, cmd, filepath;
Scanner scan = new Scanner(System.in);
chunkIndex = new HashMap<String, Integer>();
chunkSizeIndex = new HashMap<String, Integer>();
while (!exit) {
System.out.print("[cmd (add/del/exit)?] ");
input = (scan.nextLine()).trim();
if (input.length() < 4) {
System.out.println("Invalid command!");
continue;
}
cmd = input.substring(0, 4);
if (cmd.equals("add ")) {
filepath = input.substring(4);
fileAdd(filepath);
} else
if (cmd.equals("del ")) {
filepath = input.substring(4);
fileDel(filepath);
} else
if (cmd.equals("exit")) {
exit = true;
} else {
System.out.println("Invalid command!");
}
}
System.out.println("Bye Bye!");
scan.close();
}
}