@@ -8,9 +8,10 @@ import (
8
8
"github.com/reugn/go-streams"
9
9
)
10
10
11
- // SlidingWindow flow
12
- // Generates windows of a specified fixed size
13
- // Slides by a slide interval with records overlap
11
+ // SlidingWindow assigns elements to windows of fixed length configured by the window size parameter.
12
+ // An additional window slide parameter controls how frequently a sliding window is started.
13
+ // Hence, sliding windows can be overlapping if the slide is smaller than the window size.
14
+ // In this case elements are assigned to multiple windows.
14
15
type SlidingWindow struct {
15
16
sync.Mutex
16
17
size time.Duration
@@ -22,26 +23,31 @@ type SlidingWindow struct {
22
23
timestampExtractor func (interface {}) int64
23
24
}
24
25
25
- // NewSlidingWindow returns a new Processing time sliding window
26
- // size - The size of the generated windows
27
- // slide - The slide interval of the generated windows
26
+ // Verify SlidingWindow satisfies the Flow interface.
27
+ var _ streams.Flow = (* SlidingWindow )(nil )
28
+
29
+ // NewSlidingWindow returns a new processing time based SlidingWindow.
30
+ // Processing time refers to the system time of the machine that is executing the respective operation.
31
+ // size is the size of the generated windows.
32
+ // slide is the sliding interval of the generated windows.
28
33
func NewSlidingWindow (size time.Duration , slide time.Duration ) * SlidingWindow {
29
34
return NewSlidingWindowWithTsExtractor (size , slide , nil )
30
35
}
31
36
32
- // NewSlidingWindowWithTsExtractor returns a new Event time sliding window
33
- // Gives correct results on out-of-order events, late events, or on replays of data
34
- // size - The size of the generated windows
35
- // slide - The slide interval of the generated windows
36
- // timestampExtractor - The record timestamp (in nanoseconds) extractor
37
+ // NewSlidingWindowWithTsExtractor returns a new event time based SlidingWindow.
38
+ // Event time is the time that each individual event occurred on its producing device.
39
+ // Gives correct results on out-of-order events, late events, or on replays of data.
40
+ // size is the size of the generated windows.
41
+ // slide is the sliding interval of the generated windows.
42
+ // timestampExtractor is the record timestamp (in nanoseconds) extractor.
37
43
func NewSlidingWindowWithTsExtractor (size time.Duration , slide time.Duration ,
38
44
timestampExtractor func (interface {}) int64 ) * SlidingWindow {
39
45
window := & SlidingWindow {
40
46
size : size ,
41
47
slide : slide ,
42
48
queue : & PriorityQueue {},
43
49
in : make (chan interface {}),
44
- out : make (chan interface {}), //windows channel
50
+ out : make (chan interface {}), // windows channel
45
51
done : make (chan struct {}),
46
52
timestampExtractor : timestampExtractor ,
47
53
}
@@ -50,13 +56,13 @@ func NewSlidingWindowWithTsExtractor(size time.Duration, slide time.Duration,
50
56
return window
51
57
}
52
58
53
- // Via streams a data through the given flow
59
+ // Via streams data through the given flow
54
60
func (sw * SlidingWindow ) Via (flow streams.Flow ) streams.Flow {
55
61
go sw .transmit (flow )
56
62
return flow
57
63
}
58
64
59
- // To streams a data to the given sink
65
+ // To streams data to the given sink
60
66
func (sw * SlidingWindow ) To (sink streams.Sink ) {
61
67
sw .transmit (sink )
62
68
}
@@ -71,15 +77,15 @@ func (sw *SlidingWindow) In() chan<- interface{} {
71
77
return sw .in
72
78
}
73
79
74
- // retransmit an emitted window to the next Inlet
80
+ // submit emitted windows to the next Inlet
75
81
func (sw * SlidingWindow ) transmit (inlet streams.Inlet ) {
76
82
for elem := range sw .Out () {
77
83
inlet .In () <- elem
78
84
}
79
85
close (inlet .In ())
80
86
}
81
87
82
- // extract a timestamp from the record if timestampExtractor is set
88
+ // extract timestamp from the record if the timestampExtractor is set
83
89
// return the system clock time otherwise
84
90
func (sw * SlidingWindow ) timestamp (elem interface {}) int64 {
85
91
if sw .timestampExtractor == nil {
@@ -99,7 +105,7 @@ func (sw *SlidingWindow) receive() {
99
105
close (sw .out )
100
106
}
101
107
102
- // triggered by the slide interval
108
+ // emit is triggered by the sliding interval
103
109
func (sw * SlidingWindow ) emit () {
104
110
for {
105
111
select {
@@ -130,7 +136,7 @@ func (sw *SlidingWindow) emit() {
130
136
}
131
137
sw .Unlock ()
132
138
133
- // send to the out chan
139
+ // send window slice to the out chan
134
140
if len (windowSlice ) > 0 {
135
141
sw .out <- windowSlice
136
142
}
0 commit comments