-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogReader.kt
70 lines (62 loc) · 1.97 KB
/
LogReader.kt
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
package org.athenian
import java.io.File
fun main() {
fun getLines(file: File): Sequence<String> {
// Non expression body
return sequence {
val data = file.bufferedReader().lineSequence()
for (line in data)
yield(line)
}
}
fun getBytesColumn(data: Sequence<String>) =
sequence {
for (line in data) {
val cols = line.split(" ")
if (cols.size != 19)
continue
val bytes = cols[9]
yield(bytes)
}
}
fun getBytes(data: Sequence<String>) =
sequence {
data.filter { it != "-" }
.forEach { yield(it.toInt()) }
}
fun withMultipleSequences(file: File): Int {
val linesInFile = getLines(file)
val bytesColumn = getBytesColumn(linesInFile)
val bytes = getBytes(bytesColumn)
return bytes.sum()
}
fun withSingleSequence1(file: File): Int {
val lines =
sequence {
val data = file.bufferedReader().lineSequence()
for (line in data) {
yield(line)
}
}
return lines
.map { it.split(" ") }
.filter { it.size == 19 }
.map { it[9] }
.filter { it != "-" }
.map { it.toInt() }
.sum()
}
fun withSingleSequence2(file: File): Int =
file.bufferedReader()
.lineSequence()
.map { it.split(" ") }
.filter { it.size == 19 }
.map { it[9] }
.filter { it != "-" }
.map { it.toInt() }
.sum()
val file = File("data/access.log")
println("Total bytes with multiple sequences: ${withMultipleSequences(file)}")
println("Total bytes with single sequence1: ${withSingleSequence1(file)}")
println("Total bytes with single sequence2: ${withSingleSequence2(file)}")
}