-
Notifications
You must be signed in to change notification settings - Fork 0
/
conlib.go
184 lines (155 loc) · 4.38 KB
/
conlib.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
package main
import (
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
type Branch struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Data ArbitraryData `bson:"data" json:"data"`
ReplacedBy []primitive.ObjectID `bson:"replaced_by" json:"replaced_by"`
StartAt time.Time `bson:"start_at" json:"start_at"`
EndAt time.Time `bson:"end_at" json:"end_at"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
}
func NewBranch(StartAt, EndAt time.Time, Data ArbitraryData) (*Branch, error) {
branch := &Branch{
ID: primitive.NewObjectID(),
Data: Data,
StartAt: StartAt.Truncate(time.Second),
EndAt: EndAt.Truncate(time.Second),
CreatedAt: time.Now().UTC(),
}
if branch.Span() <= 0 {
return branch, fmt.Errorf("this branch would span nothing (%s)", branch)
}
return branch, nil
}
func (b *Branch) Span() time.Duration {
return b.EndAt.Sub(b.StartAt)
}
func (b *Branch) String() string {
var replacedBy []primitive.ObjectID
for _, item := range b.ReplacedBy {
replacedBy = append(replacedBy, item)
}
return fmt.Sprintf(
"<Branch %-10s StartAt=%-22s EndAt=%-22s Span=%-14s Data=%s ReplacedBy=%s>",
b.ID,
b.StartAt.Format(time.RFC3339),
b.EndAt.Format(time.RFC3339),
duration(b.Span()),
b.Data,
replacedBy)
}
type Contract struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Meta ArbitraryData `bson:"meta" json:"meta"`
Items []*Branch `bson:"items" json:"items"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
}
func NewContract(StartAt, EndAt time.Time, Data ArbitraryData, Meta ArbitraryData) (*Contract, error) {
initial, err := NewBranch(StartAt, EndAt, Data)
if err != nil {
return nil, err
}
return &Contract{
ID: primitive.NewObjectID(),
Meta: Meta,
Items: []*Branch{initial},
CreatedAt: time.Now().UTC(),
}, nil
}
func (c *Contract) Contains(branch *Branch) bool {
for _, item := range c.Items {
if item.ID == branch.ID {
return true
}
}
return false
}
func (c *Contract) Shift(old, new *Branch, replace bool) {
if !c.Contains(new) {
c.Items = append(c.Items, new)
}
if replace {
old.ReplacedBy = append(old.ReplacedBy, new.ID)
}
}
func (c *Contract) ResolveDataref(b *Branch) ArbitraryData {
if ref, exists := b.Data["_ref"]; exists {
for _, branch := range c.Items {
if ref == branch.ID {
return c.ResolveDataref(branch)
}
}
}
return ArbitraryData{
"_ref": b.ID,
}
}
func (c *Contract) Branch(StartAt, EndAt time.Time, Data ArbitraryData) (*Branch, error) {
var minStart, maxEnd time.Time
var items []*Branch
for _, item := range c.Items {
if len(item.ReplacedBy) == 0 {
items = append(items, item)
if item.EndAt.After(maxEnd) {
maxEnd = item.EndAt
}
}
}
if StartAt.After(maxEnd) || StartAt.Before(minStart) {
return nil, fmt.Errorf(
"given start date (%s) is out of the boundary, the valid boundary is between %s and %s (inclusively)",
StartAt, minStart, maxEnd)
}
if time.Time.IsZero(EndAt) {
EndAt = maxEnd
}
branch, err := NewBranch(StartAt, EndAt, Data)
if err != nil {
return branch, err
}
for _, item := range items {
latestStart := maxDate(item.StartAt, StartAt)
earliestEnd := minDate(item.EndAt, EndAt)
delta := earliestEnd.Sub(latestStart)
overlap := maxDuration(0, delta)
isExtension := (overlap == 0) && (StartAt == item.EndAt)
if !(overlap != 0 || isExtension) {
continue
}
ldelta := maxDuration(0, StartAt.Sub(item.StartAt))
rdelta := maxDuration(0, item.EndAt.Sub(EndAt))
lsplit := (ldelta != 0) && (ldelta != item.Span())
rsplit := rdelta != 0
var dataref ArbitraryData
if rsplit || lsplit {
dataref = c.ResolveDataref(item)
}
if lsplit {
left, _ := NewBranch(item.StartAt, item.StartAt.Add(ldelta), dataref)
c.Shift(item, left, true)
}
c.Shift(item, branch, !isExtension)
if rsplit {
right, _ := NewBranch(EndAt, item.EndAt, dataref)
c.Shift(item, right, true)
}
}
return branch, nil
}
func (c *Contract) Explain() {
var span time.Duration = 0
for _, item := range c.Items {
prefix := "*"
if len(item.ReplacedBy) == 0 {
span += item.Span()
prefix = ";"
}
fmt.Print(fmt.Sprintf("%s %s\n", prefix, item))
}
fmt.Println(fmt.Sprintf("span=%s,count=%d", duration(span), len(c.Items)))
}