-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileStatistics.cs
268 lines (243 loc) · 11.6 KB
/
FileStatistics.cs
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
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.IO;
namespace mso_test
{
internal class FileStatistics
{
private Dictionary<string, HashSet<string>> failOpenOriginalFiles = new Dictionary<string, HashSet<string>>();
private Dictionary<string, HashSet<string>> passOpenOriginalFiles = new Dictionary<string, HashSet<string>>();
private HashSet<string> hasNewFailOpenOriginalFiles = new HashSet<string>();
private HashSet<string> hasNewPassOpenOriginalFiles = new HashSet<string>();
public Dictionary<string, int> currFailOpenOriginalFilesNo = new Dictionary<string, int>();
public Dictionary<string, int> currPassOpenOriginalFilesNo = new Dictionary<string, int>();
const string failOpenOrigNameSuffix = "-failOpenOriginalFiles.txt";
const string failOpenOrigNameTmpSuffix = "-failOpenOriginalFiles-tmp.txt";
const string passOpenOrigNameSuffix = "-passOpenOriginalFiles.txt";
const string passOpenOrigNameTmpSuffix = "-passOpenOriginalFiles-tmp.txt";
// Temporary file streams that are filled when adding new items, and deleted in the end
private Dictionary<string, StreamWriter> failOpenOriginalFilesTemp = new Dictionary<string, StreamWriter>();
private Dictionary<string, StreamWriter> passOpenOriginalFilesTemp = new Dictionary<string, StreamWriter>();
private Dictionary<string, long> timeToOpenOriginalFiles = new Dictionary<string, long>();
// Conversion might be done by multiple parallel tasks, let's lock the methods that manipulate the collections
private Dictionary<string, HashSet<string>> failConvertFiles = new Dictionary<string, HashSet<string>>();
private Dictionary<string, long> timeToConvert = new Dictionary<string, long>();
private object failLock = new object();
private object timeLock = new object();
private Dictionary<string, HashSet<string>> failOpenConvertedFiles = new Dictionary<string, HashSet<string>>();
private Dictionary<string, long> timeToOpenConvertedFiles = new Dictionary<string, long>();
public void initOpenOriginalFileLists(DirectoryInfo baseDir, string typeName)
{
failOpenOriginalFiles[typeName] = ReadFileToSet(Path.Combine(baseDir.FullName, typeName + failOpenOrigNameSuffix));
passOpenOriginalFiles[typeName] = ReadFileToSet(Path.Combine(baseDir.FullName, typeName + passOpenOrigNameSuffix));
// Set up temporary files based on existing list of passing/failing files
File.Delete(Path.Combine(baseDir.FullName, typeName + failOpenOrigNameTmpSuffix));
File.Delete(Path.Combine(baseDir.FullName, typeName + passOpenOrigNameTmpSuffix));
if (failOpenOriginalFiles[typeName].Count != 0)
WriteSetToFile(baseDir, typeName + failOpenOrigNameTmpSuffix, failOpenOriginalFiles[typeName]);
if (passOpenOriginalFiles[typeName].Count != 0)
WriteSetToFile(baseDir, typeName + passOpenOrigNameTmpSuffix, passOpenOriginalFiles[typeName]);
failOpenOriginalFilesTemp[typeName] = new StreamWriter(Path.Combine(baseDir.FullName, typeName + failOpenOrigNameTmpSuffix), true);
passOpenOriginalFilesTemp[typeName] = new StreamWriter(Path.Combine(baseDir.FullName, typeName + passOpenOrigNameTmpSuffix), true);
}
public void saveOpenOriginalFileLists(DirectoryInfo baseDir, string typeName)
{
if (hasNewFailOpenOriginalFiles.Contains(typeName))
WriteSetToFile(baseDir, typeName + failOpenOrigNameSuffix, failOpenOriginalFiles[typeName]);
if (hasNewPassOpenOriginalFiles.Contains(typeName))
WriteSetToFile(baseDir, typeName + passOpenOrigNameSuffix, passOpenOriginalFiles[typeName]);
failOpenOriginalFilesTemp[typeName].Close();
File.Delete(Path.Combine(baseDir.FullName, typeName + failOpenOrigNameTmpSuffix));
passOpenOriginalFilesTemp[typeName].Close();
File.Delete(Path.Combine(baseDir.FullName, typeName + passOpenOrigNameTmpSuffix));
}
public void saveFailedFileLists(DirectoryInfo baseDir, string typeName)
{
if (failConvertFiles.ContainsKey(typeName))
WriteSetToFile(baseDir, typeName + "-failConvertFiles.txt", failConvertFiles[typeName]);
if (failOpenConvertedFiles.ContainsKey(typeName))
WriteSetToFile(baseDir, typeName + "-failOpenConvertedFiles.txt", failOpenConvertedFiles[typeName]);
}
public int getCurrFailOpenOriginalFilesNo(string typeName)
{
if (!currFailOpenOriginalFilesNo.ContainsKey(typeName))
return 0;
return currFailOpenOriginalFilesNo[typeName];
}
public int getCurrPassOpenOriginalFilesNo(string typeName)
{
if (!currPassOpenOriginalFilesNo.ContainsKey(typeName))
return 0;
return currPassOpenOriginalFilesNo[typeName];
}
public void incrCurrFailOpenOriginalFilesNo(string typeName)
{
if (!currFailOpenOriginalFilesNo.ContainsKey(typeName))
currFailOpenOriginalFilesNo.Add(typeName, 1);
else
currFailOpenOriginalFilesNo[typeName]++;
}
public void incrCurrPassOpenOriginalFilesNo(string typeName)
{
if (!currPassOpenOriginalFilesNo.ContainsKey(typeName))
currPassOpenOriginalFilesNo.Add(typeName, 1);
else
currPassOpenOriginalFilesNo[typeName]++;
}
public void addToFailOpenOriginalFiles(string typeName, string fileName)
{
if (!failOpenOriginalFiles.ContainsKey(typeName))
failOpenOriginalFiles.Add(typeName, new HashSet<string>());
bool newItem = failOpenOriginalFiles[typeName].Add(fileName);
if (newItem)
{
hasNewFailOpenOriginalFiles.Add(typeName);
if (failOpenOriginalFilesTemp.ContainsKey(typeName))
{
failOpenOriginalFilesTemp[typeName].WriteLine(fileName);
if (failOpenOriginalFiles[typeName].Count % 20 == 0)
failOpenOriginalFilesTemp[typeName].Flush();
}
}
}
public void addToPassOpenOriginalFiles(string typeName, string fileName)
{
if (!passOpenOriginalFiles.ContainsKey(typeName))
passOpenOriginalFiles.Add(typeName, new HashSet<string>());
bool newItem = passOpenOriginalFiles[typeName].Add(fileName);
if (newItem)
{
hasNewPassOpenOriginalFiles.Add(typeName);
if (passOpenOriginalFilesTemp.ContainsKey(typeName))
{
passOpenOriginalFilesTemp[typeName].WriteLine(fileName);
if (passOpenOriginalFiles[typeName].Count % 1000 == 0)
passOpenOriginalFilesTemp[typeName].Flush();
}
}
}
public void addToFailConvertFiles(string typeName, string fileName)
{
lock (failLock)
{
if (!failConvertFiles.ContainsKey(typeName))
failConvertFiles.Add(typeName, new HashSet<string> { fileName });
else
failConvertFiles[typeName].Add(fileName);
}
}
public void addToFailOpenConvertedFiles(string typeName, string fileName)
{
if (!failOpenConvertedFiles.ContainsKey(typeName))
failOpenConvertedFiles.Add(typeName, new HashSet<string> { fileName });
else
failOpenConvertedFiles[typeName].Add(fileName);
}
public bool isFailOpenOriginalFile(string typeName, string fileName)
{
if (!failOpenOriginalFiles.ContainsKey(typeName))
return false;
return failOpenOriginalFiles[typeName].Contains(fileName);
}
public bool isPassOpenOriginalFile(string typeName, string fileName)
{
if (!passOpenOriginalFiles.ContainsKey(typeName))
return false;
return passOpenOriginalFiles[typeName].Contains(fileName);
}
public int getPassOpenOriginalFilesNo(string typeName)
{
if (!passOpenOriginalFiles.ContainsKey(typeName))
return 0;
return passOpenOriginalFiles[typeName].Count;
}
public int getFailConvertFilesNo(string typeName)
{
if (!failConvertFiles.ContainsKey(typeName))
return 0;
return failConvertFiles[typeName].Count;
}
public int getFailOpenConvertedFilesNo(string typeName)
{
if (!failOpenConvertedFiles.ContainsKey(typeName))
return 0;
return failOpenConvertedFiles[typeName].Count;
}
public void addTimeToOpenOriginalFiles(string typeName, long timeSpanInMs)
{
if (!timeToOpenOriginalFiles.ContainsKey(typeName))
timeToOpenOriginalFiles[typeName] = timeSpanInMs;
else
timeToOpenOriginalFiles[typeName] += timeSpanInMs;
}
public void addTimeToConvert(string typeName, long timeSpanInMs)
{
lock(timeLock)
if (!timeToConvert.ContainsKey(typeName))
timeToConvert[typeName] = timeSpanInMs;
else
timeToConvert[typeName] += timeSpanInMs;
}
public void addTimeToOpenConvertedFiles(string typeName, long timeSpanInMs)
{
if (!timeToOpenConvertedFiles.ContainsKey(typeName))
timeToOpenConvertedFiles[typeName] = timeSpanInMs;
else
timeToOpenConvertedFiles[typeName] += timeSpanInMs;
}
public long getTimeToOpenOriginalFiles(string typeName)
{
if (!timeToOpenOriginalFiles.ContainsKey(typeName))
return 0;
return timeToOpenOriginalFiles[typeName];
}
public long getTimeToConvert(string typeName)
{
if (!timeToConvert.ContainsKey(typeName))
return 0;
return timeToConvert[typeName];
}
public long getTimeToOpenConvertedFiles(string typeName)
{
if (!timeToOpenConvertedFiles.ContainsKey(@typeName))
return 0;
return timeToOpenConvertedFiles[typeName];
}
public static HashSet<string> ReadFileToSet(string fileName)
{
HashSet<string> set = new HashSet<string>();
try
{
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
set.Add(line.Trim());
}
}
}
catch (FileNotFoundException)
{
}
return set;
}
public static void WriteSetToFile(DirectoryInfo baseDir, string fileName, HashSet<string> hashed)
{
try
{
SortedSet<string> sorted = new SortedSet<string>(hashed);
using (StreamWriter writer = new StreamWriter(Path.Combine(baseDir.FullName, fileName)))
{
foreach (string entry in sorted)
{
writer.WriteLine(entry);
}
}
}
catch (Exception)
{
}
}
}
}