aboutsummaryrefslogtreecommitdiffstats
path: root/skate/zipkey/zipkey.go
blob: 3805535bf4b402567dc5dc6ef22339fa731c7e38 (plain)
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
// Package zipkey implements ZipRun, a type that allows to attach a callback to
// a group of elements with a shared key taken from two streams.
package zipkey

import (
	"bufio"
	"io"
)

// Group groups items by key and will contain the complete records (e.g. line)
// for further processing.
type Group struct {
	Key string
	G0  []string
	G1  []string
}

type (
	keyFunc   func(line string) (key string, err error)
	groupFunc func(g *Group) error
)

// ZipRun reads records (separated by sep) from two readers, extracts a key
// from each record with a keyFunc and collects records from the two streams
// into a Group. A callback groupFunc can be registered, which allows to
// customize the processing of the group. Current limitation: both streams need
// to use the same keyFunc.
type ZipRun struct {
	r0, r1 *bufio.Reader
	kf     keyFunc
	gf     groupFunc
	sep    byte
}

// New create a new ready to run ZipRun value.
func New(r0, r1 io.Reader, kf keyFunc, gf groupFunc) *ZipRun {
	return &ZipRun{
		r0:  bufio.NewReader(r0),
		r1:  bufio.NewReader(r1),
		kf:  kf,
		gf:  gf,
		sep: '\n',
	}
}

// Run starts reading from both readers. The process stops, if one reader is
// exhausted or reads from any reader fail.
func (c *ZipRun) Run() error {
	var (
		k0, k1, c0, c1 string // key: k0, k1; current line: c0, c1
		done           bool
		err            error
		lineKey        = func(r *bufio.Reader) (line, key string, err error) {
			if line, err = r.ReadString(c.sep); err != nil {
				return
			}
			key, err = c.kf(line)
			return
		}
	)
	for {
		if done {
			break
		}
		switch {
		case k0 == "" || k0 < k1:
			for k0 == "" || k0 < k1 {
				c0, k0, err = lineKey(c.r0)
				if err == io.EOF {
					return nil
				}
				if err != nil {
					return err
				}
			}
		case k1 == "" || k0 > k1:
			for k1 == "" || k0 > k1 {
				c1, k1, err = lineKey(c.r1)
				if err == io.EOF {
					return nil
				}
				if err != nil {
					return err
				}
			}
		case k0 == k1:
			g := &Group{
				Key: k0,
				G0:  []string{c0},
				G1:  []string{c1},
			}
			for {
				c0, err = c.r0.ReadString(c.sep)
				if err == io.EOF {
					done = true
					break
				}
				if err != nil {
					return err
				}
				k, err := c.kf(c0)
				if err != nil {
					return err
				}
				if k == k0 {
					g.G0 = append(g.G0, c0)
					k0 = k
				} else {
					k0 = k
					break
				}
			}
			for {
				c1, err = c.r1.ReadString(c.sep)
				if err == io.EOF {
					done = true
					break
				}
				if err != nil {
					return err
				}
				k, err := c.kf(c1)
				if err != nil {
					return err
				}
				if k == k1 {
					g.G1 = append(g.G1, c1)
					k1 = k
				} else {
					k1 = k
					break
				}
			}
			if c.gf != nil {
				if err := c.gf(g); err != nil {
					return err
				}
			}
		}
	}
	return nil
}