Debian Codegraph @

dstream/join.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "fmt"
5 "os"
6 )
7
8 // Join performs a streaming join on several Dstreams that have been
9 // segmented by id variables. If join has type Join, then
10 // join.Data[i] is the current chunk of the i^th stream. All streams
11 // being joined must have been segmented by an id variable whose
12 // values are ascending. The id variable must have type uint64.
13 //
14 // A call to the Next method always advances the first stream
15 // (Data[0]) by one chunk. The other elements of Data are advanced
16 // until their id variable is equal to (if possible) or greater than
17 // the id variable of Data[0]. If equality is achieved, the
18 // corresponding element of join.status is set to true.
19 // join.Status[0] is always false and has no meaning.
20 //
21 // The dstream values to be joined must be segmented so that the id
22 // variable is constant within chunks, and increases in numeric value
23 // with subsequent calls to the Next method.
24 type Join struct {
25
26 // A sequence of segmented Dstreams to advance in unison.
27 Data []Dstream
28
29 // Status[j] means that the id variable for Data value j is
30 // equal to the id variable for Data value 0. Status[0] is
31 // not used.
32 Status []bool
33
34 inames []string
35
36 ipos []int
37
38 id [][]uint64
39 }
40
41 // NewJoin creates a Join of the given Dstreams, using the variable
42 // names in names as ids. The Dstreams in data must be segmented by
43 // the inames variables before calling NewJoin.
44 func NewJoin(data []Dstream, names []string) *Join {
45
46 if len(data) != len(names) {
47 panic("NewJoin: data and names should have same length\n")
48 }
49
50 w := &Join{
51 Data: data,
52 inames: names,
53 }
54
55 for k, da := range data {
56 na := da.Names()
57 f := false
58 for j, na := range na {
59 if na == names[k] {
60 w.ipos = append(w.ipos, j)
61 f = true
62 break
63 }
64 }
65 if !f {
66 msg := fmt.Sprintf("Can't find index variable %s in %dth dataset", names[k], k)
67 os.Stderr.WriteString(msg)
68 os.Exit(1)
69 }
70 }
71
72 w.Status = make([]bool, len(data))
73 w.id = make([][]uint64, len(data))
74
75 return w
76 }
77
78 func (w *Join) needsadvance(j int) bool {
79 if w.id[j] == nil || len(w.id[j]) == 0 {
80 return true
81 }
82
83 if w.id[j][0] < w.id[0][0] {
84 return true
85 }
86
87 return false
88 }
89
90 func (w *Join) clearstatus() {
91 for j := range w.Status {
92 w.Status[j] = false
93 }
94 }
95
96 // Next advances to the next chunk. The first dstream, which is
97 // contained in join.Data[0], always advances to the next sequential
98 // value of its id variable. The other dstreams (join.Data[j] for j >
99 // 0) advance until their id variables are equal to or greater than
100 // the id variable for the current chunk of join.Data[0]. The status
101 // field (join.status) indicates which dstreams in the join are
102 // currently on the same id value as the first dstream (join.Data[0]).
103 func (w *Join) Next() bool {
104
105 // Advance the index stream
106 f := w.Data[0].Next()
107 if !f {
108 return false
109 }
110 w.id[0] = w.Data[0].GetPos(w.ipos[0]).([]uint64)
111
112 // Advance the other streams
113 w.clearstatus()
114 for j := 1; j < len(w.Data); j++ {
115
116 // Keep advancing as long as the stream is behind the index stream.
117 for w.needsadvance(j) {
118 f := w.Data[j].Next()
119 if !f {
120 break
121 }
122 w.id[j] = w.Data[j].GetPos(w.ipos[j]).([]uint64)
123 }
124
125 w.Status[j] = w.id[j][0] == w.id[0][0]
126 }
127
128 return true
129 }