-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapliteral.go
131 lines (108 loc) · 3.8 KB
/
mapliteral.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
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
// mapLiteral is a relation with underlying data stored in a map.
// It is intended to be used for general purpose source data that can only be
// queried as a whole, or that has been preprocessed. It can also be used as
// an adapter to interface with other sources of data. Basically anything
// that can produce a map with key as structs (but empty struct values!!!).
//
// One nice thing is that Maps will never have duplicate keys, which makes them
// a natural way to represent relations.
package rel
import (
"reflect"
)
// mapLiteral is an implementation of Relation using a map
type mapLiteral struct {
// the map of tuples in the relation, with tuples in the key
rbody reflect.Value // should always hold a map
// set of candidate keys
cKeys CandKeys
// the type of the tuples contained within the relation
zero interface{}
// first error encountered during construction or during evaluation
err error
}
// TupleChan sends each tuple in the relation to a channel
func (r1 *mapLiteral) TupleChan(t interface{}) chan<- struct{} {
cancel := make(chan struct{})
// reflect on the channel
chv := reflect.ValueOf(t)
err := EnsureChan(chv.Type(), r1.zero)
if err != nil {
r1.err = err
return cancel
}
if r1.err != nil {
chv.Close()
return cancel
}
go func(res reflect.Value) {
// output channel
resSel := reflect.SelectCase{Dir: reflect.SelectSend, Chan: res}
canSel := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(cancel)}
for _, tup := range r1.rbody.MapKeys() {
resSel.Send = tup
chosen, _, _ := reflect.Select([]reflect.SelectCase{canSel, resSel})
if chosen == 0 {
// cancel has been closed, so close the results
return
}
}
chv.Close()
}(chv)
return cancel
}
// Zero returns the zero value of the relation (a blank tuple)
func (r1 *mapLiteral) Zero() interface{} {
return r1.zero
}
// CKeys is the set of candidate keys in the relation
func (r1 *mapLiteral) CKeys() CandKeys {
return r1.cKeys
}
// GoString returns a text representation of the Relation
func (r1 *mapLiteral) GoString() string {
return goStringTabTable(r1)
}
// String returns a text representation of the Relation
func (r1 *mapLiteral) String() string {
return "Relation(" + HeadingString(r1) + ")"
}
// Project creates a new relation with less than or equal degree
// t2 has to be a new type which is a subdomain of r.
func (r1 *mapLiteral) Project(z2 interface{}) Relation {
return NewProject(r1, z2)
}
// Restrict creates a new relation with less than or equal cardinality
// p has to be a func(tup T) bool where tup is a subdomain of the input r.
func (r1 *mapLiteral) Restrict(p Predicate) Relation {
return NewRestrict(r1, p)
}
// Rename creates a new relation with new column names
// z2 has to be a struct with the same number of fields as the input relation
func (r1 *mapLiteral) Rename(z2 interface{}) Relation {
return NewRename(r1, z2)
}
// Union creates a new relation by unioning the bodies of both inputs
func (r1 *mapLiteral) Union(r2 Relation) Relation {
return NewUnion(r1, r2)
}
// Diff creates a new relation by set minusing the two inputs
func (r1 *mapLiteral) Diff(r2 Relation) Relation {
return NewDiff(r1, r2)
}
// Join creates a new relation by performing a natural join on the inputs
func (r1 *mapLiteral) Join(r2 Relation, zero interface{}) Relation {
return NewJoin(r1, r2, zero)
}
// GroupBy creates a new relation by grouping and applying a user defined func
func (r1 *mapLiteral) GroupBy(t2, gfcn interface{}) Relation {
return NewGroupBy(r1, t2, gfcn)
}
// Map creates a new relation by applying a function to tuples in the source
func (r1 *mapLiteral) Map(mfcn interface{}, ckeystr [][]string) Relation {
return NewMap(r1, mfcn, ckeystr)
}
// Err returns an error encountered during construction or computation
func (r1 *mapLiteral) Err() error {
return r1.err
}