/
index_model.go
143 lines (121 loc) · 3 KB
/
index_model.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
package mongox
import (
"reflect"
"strings"
"github.com/reearth/reearthx/util"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const prefix = "re_"
type Index struct {
Name string
Key bson.D
Unique bool
ExpireAfterSeconds *int32
Filter bson.M `bson:"partialFilterExpression"`
}
func IndexFromKey(key string, unique bool) Index {
return Index{
Name: prefix + key,
Key: toKeyBSON(key),
Unique: unique,
}
}
func IndexFromKeys(keys []string, unique bool) []Index {
return lo.Map(keys, func(k string, _ int) Index {
return IndexFromKey(k, unique)
})
}
func TTLIndexFromKey(key string, expireAfterSeconds int32) Index {
return Index{
Name: prefix + key,
Key: toKeyBSON(key),
ExpireAfterSeconds: &expireAfterSeconds,
}
}
func toKeyBSON(key string) bson.D {
return lo.Map(
strings.Split(key, ","),
func(k string, _ int) bson.E {
k = strings.TrimSpace(k)
v := 1
if strings.HasPrefix(k, "!") {
v = -1
}
k = strings.TrimPrefix(k, "!")
return bson.E{
Key: strings.TrimSpace(k),
Value: v,
}
},
)
}
func (i Index) Normalize() Index {
b, err := bson.Marshal(i)
if err != nil {
return i
}
i2 := Index{}
if err := bson.Unmarshal(b, &i2); err != nil {
return i
}
return i2
}
func (i Index) Model() mongo.IndexModel {
o := options.Index().SetName(i.Name)
if i.Unique {
o.SetUnique(i.Unique)
}
if i.Filter != nil {
o.SetPartialFilterExpression(i.Filter)
}
if i.ExpireAfterSeconds != nil {
o.SetExpireAfterSeconds(*i.ExpireAfterSeconds)
}
return mongo.IndexModel{
Keys: i.Key,
Options: o,
}
}
func (i Index) Equal(j Index) bool {
e := reflect.DeepEqual(i, j)
return e
}
type IndexList []Index
func (l IndexList) Names() []string {
return lo.Map(l, func(i Index, _ int) string { return i.Name })
}
func (l IndexList) NamesWithoutPrefix() []string {
return lo.Map(l, func(i Index, _ int) string { return strings.TrimPrefix(i.Name, prefix) })
}
func (l IndexList) Models() []mongo.IndexModel {
return lo.Map(l, func(i Index, _ int) mongo.IndexModel { return i.Model() })
}
func (l IndexList) Normalize() []Index {
return lo.Map(l, func(i Index, _ int) Index { return i.Normalize() })
}
func (l IndexList) AddNamePrefix() IndexList {
return lo.Map(l, func(i Index, _ int) Index {
if i.Name != "" && !strings.HasPrefix(i.Name, prefix) {
i.Name = prefix + i.Name
}
return i
})
}
func (l IndexList) RemoveDefaultIndex() IndexList {
return lo.Filter(l, func(i Index, _ int) bool {
return i.Name != "_id_"
})
}
type IndexResult util.DiffResult[Index]
func (i IndexResult) AddedNames() []string {
return IndexList(i.Added).NamesWithoutPrefix()
}
func (i IndexResult) UpdatedNames() []string {
return IndexList(util.DiffResult[Index](i).UpdatedNext()).NamesWithoutPrefix()
}
func (i IndexResult) DeletedNames() []string {
return IndexList(i.Deleted).NamesWithoutPrefix()
}