-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
79 lines (73 loc) · 1.81 KB
/
main.go
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
)
func main() {
//Retrieve file name parameters from command line
var (
master string
tx string
)
flag.StringVar(&master, "master", "", "The Master file")
flag.StringVar(&tx, "tx", "", "The Transaction File to be processed")
flag.Parse()
seen := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
if !(seen["master"] && seen["tx"]) {
flag.PrintDefaults()
os.Exit(2)
}
chMaster := openFileChannel(master)
chTx := openFileChannel(tx)
//Main loop
mLine, isMasterActive := <-chMaster
txLine, isTransactionActive := <-chTx
for isMasterActive || isTransactionActive {
var action, value string
var nextMaster, nextTx bool
if !isTransactionActive {
action, value, nextMaster, nextTx = "del", mLine, true, false
} else if !isMasterActive {
action, value, nextMaster, nextTx = "new", txLine, false, true
} else {
if txLine == mLine {
action, value, nextMaster, nextTx = "upt", txLine, true, true
} else if txLine > mLine {
action, value, nextMaster, nextTx = "del", mLine, true, false
} else if txLine < mLine {
action, value, nextMaster, nextTx = "new", txLine, false, true
}
}
if nextMaster {
mLine, isMasterActive = <-chMaster
}
if nextTx {
txLine, isTransactionActive = <-chTx
}
fmt.Println(value + "," + action)
}
}
func openFileChannel(file string) <-chan string {
ch := make(chan string)
go func(ch chan string) {
defer close(ch)
f, err := os.Open(file)
if err != nil {
log.Printf("Could not open file: %v. %v", file, err)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
ch <- scanner.Text()
}
if err := scanner.Err(); err != nil {
log.Printf("Error while scanning file: %v. %v", file, err)
}
}(ch)
return ch
}