This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange_attribute_tree.go
191 lines (158 loc) · 4.69 KB
/
range_attribute_tree.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package contentpubsub
type Node struct {
subs []*SubData
upperLimit int
lowerLimit int
left *Node
right *Node
}
func NewNode(upperLimit int, lowerLimit int) *Node {
n := &Node{
upperLimit: upperLimit,
lowerLimit: lowerLimit,
}
if n.lowerLimit == n.upperLimit {
return n
}
localCap := n.upperLimit - n.lowerLimit
n.left = NewNode(n.lowerLimit+localCap/2, n.lowerLimit)
n.right = NewNode(n.upperLimit, n.lowerLimit+localCap/2+1)
return n
}
// InsertSub inserts a sub to all the nodes that he should
// be at, by recursively place it throught the tree
func (n *Node) InsertSub(upper int, lower int, sub *SubData) {
localCap := n.upperLimit - n.lowerLimit + 1
if upper >= n.upperLimit && lower <= n.lowerLimit {
n.subs = append(n.subs, sub)
} else {
if lower <= n.lowerLimit+localCap/2-1 {
n.left.InsertSub(upper, lower, sub)
}
if upper >= n.lowerLimit+localCap/2 {
n.right.InsertSub(upper, lower, sub)
}
}
}
// GetSubsOfEvent returns all subs interested in a certain
// specific value by calling recursivelly the function
func (n *Node) GetSubsOfEvent(value int) []*SubData {
localCap := n.upperLimit - n.lowerLimit + 1
if n.left == nil {
return n.subs
} else if value <= n.lowerLimit+localCap/2-1 {
return append(n.subs, n.left.GetSubsOfEvent(value)...)
} else {
return append(n.subs, n.right.GetSubsOfEvent(value)...)
}
}
// DeleteSubsFromNode recursively called to delete a node throught a tree
func (n *Node) DeleteSubFromNode(upper int, lower int, sub *SubData) {
localCap := n.upperLimit - n.lowerLimit + 1
if upper >= n.upperLimit && lower <= n.lowerLimit {
for i := 0; i < len(n.subs); i++ {
if n.subs[i].addr == sub.addr {
if i == 0 && len(n.subs) == 1 {
n.subs = nil
} else if i == 0 {
n.subs = n.subs[1:]
} else if len(n.subs) == i+1 {
n.subs = n.subs[:i-1]
} else {
n.subs = append(n.subs[:i-1], n.subs[i+1:]...)
i--
}
}
}
} else {
if lower <= n.lowerLimit+localCap/2-1 {
n.left.DeleteSubFromNode(upper, lower, sub)
}
if upper >= n.lowerLimit+localCap/2 {
n.right.DeleteSubFromNode(upper, lower, sub)
}
}
}
// RangeAttributeTree is a structure that organizes the subscribers
// by their interest in a specific value of an attribute for
// then faster/efficient subscriber diffusion
type RangeAttributeTree struct {
root *Node
attrname string
upperValue int
lowerValue int
}
func NewRangeAttributeTree(attr *Attribute) *RangeAttributeTree {
size := attr.rangeQuery[1] - attr.rangeQuery[0] + 1
cap := 2
for {
if cap >= size {
break
}
cap = cap * 2
}
rt := &RangeAttributeTree{
attrname: attr.name,
lowerValue: attr.rangeQuery[0],
upperValue: attr.rangeQuery[1],
}
rt.root = NewNode(cap-1, 0)
return rt
}
// AddSubToTreeRoot adds a sub to a tree root
func (rt *RangeAttributeTree) AddSubToTreeRoot(sub *SubData) {
rt.root.subs = append(rt.root.subs, sub)
}
// RemoveSubFromTreeRoot removes a sub from the tree root
func (rt *RangeAttributeTree) RemoveSubFromTreeRoot(sub *SubData) {
for i := 0; i < len(rt.root.subs); i++ {
if rt.root.subs[i].addr == sub.addr {
if i == 0 && len(rt.root.subs) == 1 {
rt.root.subs = nil
} else if i == 0 {
rt.root.subs = rt.root.subs[1:]
} else if len(rt.root.subs) == i+1 {
rt.root.subs = rt.root.subs[:i-1]
} else {
rt.root.subs = append(rt.root.subs[:i-1], rt.root.subs[i+1:]...)
i--
}
}
}
}
// AddSubToTree adds a sub to the tree translating
// the attribute values for tree insertion
func (rt *RangeAttributeTree) AddSubToTree(sub *SubData) {
var upper, lower int
if sub.pred.attributes[rt.attrname].rangeQuery[1] >= rt.upperValue {
upper = rt.upperValue
} else {
upper = sub.pred.attributes[rt.attrname].rangeQuery[1] - rt.lowerValue
}
if sub.pred.attributes[rt.attrname].rangeQuery[0] <= rt.lowerValue {
lower = 0
} else {
lower = sub.pred.attributes[rt.attrname].rangeQuery[0] - rt.lowerValue
}
rt.root.InsertSub(upper, lower, sub)
}
// GetInterestedSubs collects the subs that are
// interested in an attribute specific value
func (rt *RangeAttributeTree) GetInterestedSubs(value int) []*SubData {
return rt.root.GetSubsOfEvent(value - rt.lowerValue)
}
// DeleteSubFromTree removes a subs from the tree
func (rt *RangeAttributeTree) DeleteSubFromTree(sub *SubData) {
var upper, lower int
if sub.pred.attributes[rt.attrname].rangeQuery[1] >= rt.upperValue {
upper = rt.upperValue
} else {
upper = sub.pred.attributes[rt.attrname].rangeQuery[1] - rt.lowerValue
}
if sub.pred.attributes[rt.attrname].rangeQuery[0] <= rt.lowerValue {
lower = 0
} else {
lower = sub.pred.attributes[rt.attrname].rangeQuery[0] - rt.lowerValue
}
rt.root.DeleteSubFromNode(upper, lower, sub)
}