-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_test.go
245 lines (196 loc) · 5.49 KB
/
collection_test.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package inventory
import (
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"regexp"
"testing"
"time"
)
type meta struct {
id string
name string
}
type fooItem struct {
meta
fooValue string
}
type barItem struct {
meta
barValue string
foos []*fooItem
valuePattern string
}
func TestCollection(t *testing.T) {
db := NewDB()
foos := []*fooItem{{
meta: meta{"1", "foo1"},
fooValue: "I'm foo",
}, {
meta: meta{"2", "foo2"},
fooValue: "I'm foo #2",
}}
bars := []*barItem{{
meta: meta{"1", "bar1"},
barValue: "I'm bar",
foos: foos,
valuePattern: `\w+`,
}}
barCol := NewCollection[*barItem](db, "bar",
Extractor(func(load func(in ...*barItem)) { load(bars...) }),
PrimaryKey("id", func(item *barItem, keyVal func(string)) { keyVal(item.id) }),
AdditionalKey("name", func(item *barItem, keyVal func(string)) { keyVal(item.name) }),
)
fooCol := Infer(barCol, "foo-by-bar", func(src *barItem, f func(kv string, items ...*fooItem)) {
f(src.id, src.foos...)
})
getFooById := fooCol.PrimaryKey("id", func(item *fooItem, keyVal func(string)) { keyVal(item.id) })
getBarByID := barCol.PrimaryKey("id", func(item *barItem, keyVal func(string)) { keyVal(item.id) })
barByName := barCol.GetBy("name")
barsByFooId := barCol.MapBy("foo", func(item *barItem, keyVal func(string)) {
for _, f := range item.foos {
keyVal(f.id)
}
})
barCol.Invalidate()
foo, ok := getFooById("1")
assert.True(t, ok)
assert.Equal(t, "I'm foo", foo.fooValue)
bar, ok := getBarByID("1")
assert.True(t, ok)
assert.Equal(t, "I'm bar", bar.barValue)
bar, ok = barByName("bar1")
assert.True(t, ok)
assert.Equal(t, "I'm bar", bar.barValue)
barList, err := barsByFooId("1")
if !assert.NoError(t, err) {
return
}
barCol.Invalidate()
assert.Len(t, barList, 1)
fooList, err := fooCol.Query("1")
if !assert.NoError(t, err) {
return
}
assert.Len(t, fooList, 2)
foos[0].fooValue = "this foo has changed"
bars[0].barValue = "this bar has changed"
barCol.Invalidate()
foo2, ok := getFooById("2")
assert.True(t, ok)
assert.Equal(t, "I'm foo #2", foo2.fooValue)
bar, ok = getBarByID("1")
assert.True(t, ok)
assert.Equal(t, "this bar has changed", bar.barValue)
foo, ok = getFooById("1")
assert.True(t, ok)
assert.Equal(t, "this foo has changed", foo.fooValue)
der := Derive(barCol, "regex-pattern", func(bar *barItem) (out *regexp.Regexp, err error) {
return regexp.Compile(bar.valuePattern)
})
re, err := der(bars[0])
assert.NoError(t, err)
assert.True(t, re.MatchString("foo"))
bars[0].valuePattern = `\d+`
re, err = der(bars[0])
assert.NoError(t, err)
assert.True(t, re.MatchString("foo"))
barCol.Invalidate()
re, err = der(bars[0])
assert.NoError(t, err)
assert.False(t, re.MatchString("foo"))
}
/*
benchmark result as first committed the solution on a MacBook Pro 2020 model
goos: darwin
goarch: amd64
pkg: github.com/avivklas/inventory
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
Benchmark_collection
Benchmark_collection/get
Benchmark_collection/get-8 3820045 296.1 ns/op
Benchmark_collection/query_one-to-one_relation
Benchmark_collection/query_one-to-one_relation-8 1074028 1100 ns/op
Benchmark_collection/query_one-to-many_relation
Benchmark_collection/query_one-to-many_relation-8 797988 1504 ns/op
*/
func Benchmark_collection(b *testing.B) {
db := NewDB()
b.SetParallelism(1)
var foos []*fooItem
for i := 0; i < 1000; i++ {
foos = append(foos, &fooItem{
meta: meta{fmt.Sprintf("%d", i), fmt.Sprintf("foo %d", i)},
})
}
var bars []*barItem
for i := 0; i < 1000/4; i++ {
var foos []*fooItem
for j := 0; j < 4; j++ {
foos = append(foos, &fooItem{meta: meta{fmt.Sprintf("%d", i*4+j), fmt.Sprintf("bar %d", i*4+j)}})
}
bars = append(bars, &barItem{
meta: meta{fmt.Sprintf("%d", i), fmt.Sprintf("bar %d", i)},
foos: foos,
})
}
barCol := NewCollection[*barItem](db, "bar",
Extractor(func(load func(in ...*barItem)) { load(bars...) }),
PrimaryKey("id", func(item *barItem, keyVal func(string)) { keyVal(item.id) }),
)
fooCol := Infer(barCol, "foo-by-bar", func(src *barItem, f func(kv string, items ...*fooItem)) {
f(src.id, src.foos...)
})
barCol.Invalidate()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
b.Run("get", func(b *testing.B) {
getFoo := fooCol.PrimaryKey("id", func(item *fooItem, keyVal func(string)) { keyVal(item.id) })
barCol.Invalidate()
var ok bool
b.ReportAllocs()
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("%d", rnd.Int63n(1000))
_, ok = getFoo(key)
if !assert.True(b, ok, key) {
return
}
}
})
b.Run("query one-to-one relation", func(b *testing.B) {
barsByFooId := barCol.MapBy("foo", func(item *barItem, keyVal func(string)) {
for _, foo := range item.foos {
keyVal(foo.id)
}
})
barCol.Invalidate()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
barList, err := barsByFooId(fmt.Sprintf("%d", rnd.Int63n(1000)))
if !assert.NoError(b, err) {
b.Fatal(err)
}
if !assert.Len(b, barList, 1) {
return
}
}
})
b.Run("query one-to-many relation", func(b *testing.B) {
barCol.Invalidate()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
fooList, err := fooCol.Query(fmt.Sprintf("%d", rnd.Int63n(1000/4)))
if !assert.NoError(b, err) {
b.Fatal(err)
}
assert.Len(b, fooList, 4)
}
})
b.Run("invalidate", func(b *testing.B) {
barCol.Invalidate()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
barCol.Invalidate()
}
})
}