28
28
/// Provide function for manipulate files.
30
internal sealed class Files
30
internal sealed class Files : IDisposable
33
/// Object for database operations.
37
/// Object for show messages to user.
41
/// If true, all disposable fields are disposed.
46
/// Standard class constructor.
48
/// <param name="quiet">
49
/// A <see cref="System.Boolean"/> if true, don't show messages to user.
35
51
public Files (bool quiet)
37
53
this.db = new Database();
47
63
public void Backup(string filename)
67
throw new ObjectDisposedException(GetType().Name);
49
69
this.util.Message("Creating database backup ... ", false);
50
StreamWriter writer = new StreamWriter(filename);
51
StringBuilder urls2 = new StringBuilder();
52
this.util.ProgressStart(0, "URL's was saved");
70
using (StreamWriter writer = new StreamWriter(filename))
55
urls2.Append(this.db.SelectURLs(this.util.Curamount));
56
if (urls2.Length == 0)
72
StringBuilder urls2 = new StringBuilder();
73
this.util.ProgressStart(0, "URL's was saved");
76
urls2.Append(this.db.SelectURLs(this.util.Curamount));
77
if (urls2.Length == 0)
81
urls2.Append(Environment.NewLine);
84
urls2.Remove(0, urls2.Length);
85
this.util.Curamount += 100000;
86
GC.GetTotalMemory(true);
60
urls2.Append(Environment.NewLine);
63
urls2.Remove(0, urls2.Length);
64
this.util.Curamount += 100000;
65
GC.GetTotalMemory(true);
88
this.util.ProgressStop();
67
this.util.ProgressStop();
70
91
this.util.Message(" done.", true);
71
92
this.util.Message("Compressing backup file ... ", false);
72
93
using (FileStream file = File.OpenRead(filename))
157
182
System.Collections.Generic.List<string> records = new System.Collections.Generic.List<string>();
158
183
System.Collections.Generic.Dictionary<string, string[]> urls = new System.Collections.Generic.Dictionary<string, string[]>();
159
ParseURLs parseurl = new ParseURLs();
161
185
string record, hash1, hash2, record2 = String.Empty;
162
186
using (StreamReader reader = new StreamReader(filename))
164
while (!reader.EndOfStream)
188
using (ParseURLs parseurl = new ParseURLs())
166
for (int j = 0; j < 25000; j++)
168
record2 = reader.ReadLine();
173
records.Add(record2);
175
//Remove bad URL's and create hash for each
176
foreach (string record3 in records)
178
record = ParseURLs.ParseURL(record3);
180
if (record.Length == 0)
184
hash1 = parseurl.GetHash(record);
185
if (!record.StartsWith("www."))
187
hash2 = parseurl.GetHash("www." + record);
190
while (!reader.EndOfStream)
192
for (int j = 0; j < 25000; j++)
194
record2 = reader.ReadLine();
199
records.Add(record2);
201
//Remove bad URL's and create hash for each
202
foreach (string record3 in records)
204
record = ParseURLs.ParseURL(record3);
206
if (record.Length == 0)
210
hash1 = parseurl.GetHash(record);
211
if (!record.StartsWith("www."))
213
hash2 = parseurl.GetHash("www." + record);
217
hash2 = parseurl.GetHash(record.Substring(4));
219
if (!urls.ContainsKey(record))
221
urls.Add(record, new string[] {hash1, hash2});
223
this.util.Curamount += record3.Length;
227
amount += this.db.InsertURLs(urls, false);
191
hash2 = parseurl.GetHash(record.Substring(4));
193
if (!urls.ContainsKey(record))
195
urls.Add(record, new string[] {hash1, hash2});
197
this.util.Curamount += record3.Length;
201
amount += this.db.InsertURLs(urls, false);
205
amount += this.db.InsertURLs(urls, true);
209
records.TrimExcess();
231
amount += this.db.InsertURLs(urls, true);
235
records.TrimExcess();
213
240
this.util.ProgressStop();
214
241
this.util.Message(String.Empty, true);
215
242
this.util.Message(amount.ToString() + " URL's was inserted from " + i.ToString() + " URL's.", true);
246
/// Function dispose unmanaged resources.
248
public void Dispose()
250
if (this.util != null)
254
this.disposed = true;