Sub Process_Globals
Type IpAndCount (ip As String, count As Int)
Private serializedMapFile As String = "ips.dat"
End Sub
Sub AppStart (Args() As String)
Dim start As Long = DateTime.Now
Dim ips As Map
ips.Initialize
Log("File size: " & NumberFormat(File.Size("", Args(0)) / (1024 * 1024), 0, 0) & " MB")
Dim raf As RandomAccessFile
If File.Exists(File.DirTemp, serializedMapFile) = False Then
Log("Building map file...")
Dim tr As TextReader
tr.Initialize(File.OpenInput("", Args(0)))
Dim lineCounter As Int
Dim line As String = tr.ReadLine
Do While line <> Null
lineCounter = lineCounter + 1
Dim m As Matcher = Regex.Matcher("^(\d+.\d+.\d+.\d)", line)
If m.Find Then
Dim count As Int
If ips.ContainsKey(m.Match) Then
count = ips.Get(m.Match)
Else
count = 0
End If
ips.Put(m.Match, count + 1)
Else
Log(line) 'not expected to happen
End If
If lineCounter Mod 1000000 = 0 Then
Log("Line: " & NumberFormat(lineCounter,0,0) & ", Map size: " & ips.Size & _
", time: " & (DateTime.Now - start) & " ms")
End If
line = tr.ReadLine
Loop
tr.Close
'save the map for future use
raf.Initialize(File.DirTemp, serializedMapFile, False)
raf.WriteObject(ips, True, 0)
Else
'read the map from the file
Log("Loading map file...")
raf.Initialize(File.DirTemp, serializedMapFile, False)
ips = raf.ReadObject(0)
End If
raf.Close
Dim list1 As List
list1.Initialize
For i = 0 To ips.Size - 1
Dim ic As IpAndCount
ic.Initialize
ic.ip = ips.GetKeyAt(i)
ic.count = ips.GetValueAt(i)
list1.Add(ic)
Next
list1.SortType("count", False)
ic = list1.Get(0) 'get the top item
Log(ic.ip & ": " & ic.count)
Log("Total time: " & (DateTime.Now - start) & " ms, number of lines: " & _
NumberFormat(lineCounter, 0, 0))
End Sub