/
dataset_schema.go
197 lines (170 loc) · 5.2 KB
/
dataset_schema.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
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"golang.org/x/exp/slices"
"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearth/server/pkg/dataset"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
)
var (
datasetSchemaIndexes = []string{"scene"}
datasetSchemaUniqueIndexes = []string{"id"}
)
type DatasetSchema struct {
client *mongox.ClientCollection
f repo.SceneFilter
}
func NewDatasetSchema(client *mongox.Client) *DatasetSchema {
return &DatasetSchema{
client: client.WithCollection("datasetSchema"),
}
}
func (r *DatasetSchema) Init(ctx context.Context) error {
return createIndexes(ctx, r.client, datasetSchemaIndexes, datasetSchemaUniqueIndexes)
}
func (r *DatasetSchema) Filtered(f repo.SceneFilter) repo.DatasetSchema {
return &DatasetSchema{
client: r.client,
f: r.f.Merge(f),
}
}
func (r *DatasetSchema) FindByID(ctx context.Context, id id.DatasetSchemaID) (*dataset.Schema, error) {
return r.findOne(ctx, bson.M{
"id": id.String(),
})
}
func (r *DatasetSchema) FindByIDs(ctx context.Context, ids id.DatasetSchemaIDList) (dataset.SchemaList, error) {
if len(ids) == 0 {
return nil, nil
}
filter := bson.M{
"id": bson.M{
"$in": ids.Strings(),
},
}
res, err := r.find(ctx, filter)
if err != nil {
return nil, err
}
return filterDatasetSchemas(ids, res), nil
}
func (r *DatasetSchema) FindByScene(ctx context.Context, sceneID id.SceneID, pagination *usecasex.Pagination) (dataset.SchemaList, *usecasex.PageInfo, error) {
if !r.f.CanRead(sceneID) {
return nil, usecasex.EmptyPageInfo(), nil
}
return r.paginate(ctx, bson.M{
"scene": sceneID.String(),
}, pagination)
}
func (r *DatasetSchema) FindBySceneAll(ctx context.Context, sceneID id.SceneID) (dataset.SchemaList, error) {
if !r.f.CanRead(sceneID) {
return nil, nil
}
return r.find(ctx, bson.M{
"scene": sceneID.String(),
})
}
func (r *DatasetSchema) FindBySceneAndSource(ctx context.Context, sceneID id.SceneID, source string) (dataset.SchemaList, error) {
if !r.f.CanRead(sceneID) {
return nil, nil
}
return r.find(ctx, bson.M{
"scene": sceneID.String(),
"source": string(source),
})
}
func (r *DatasetSchema) CountByScene(ctx context.Context, id id.SceneID) (int, error) {
if r.f.Readable != nil && !slices.Contains(r.f.Readable, id) {
return 0, nil
}
res, err := r.client.Count(ctx, bson.M{
"scene": id.String(),
})
if err != nil {
return 0, err
}
return int(res), nil
}
func (r *DatasetSchema) Save(ctx context.Context, datasetSchema *dataset.Schema) error {
if !r.f.CanWrite(datasetSchema.Scene()) {
return repo.ErrOperationDenied
}
doc, id := mongodoc.NewDatasetSchema(datasetSchema)
return r.client.SaveOne(ctx, id, doc)
}
func (r *DatasetSchema) SaveAll(ctx context.Context, datasetSchemas dataset.SchemaList) error {
if len(datasetSchemas) == 0 {
return nil
}
docs, ids := mongodoc.NewDatasetSchemas(datasetSchemas, r.f.Writable)
return r.client.SaveAll(ctx, ids, docs)
}
func (r *DatasetSchema) Remove(ctx context.Context, id id.DatasetSchemaID) error {
return r.client.RemoveOne(ctx, r.writeFilter(bson.M{"id": id.String()}))
}
func (r *DatasetSchema) RemoveAll(ctx context.Context, ids id.DatasetSchemaIDList) error {
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, r.writeFilter(bson.M{
"id": bson.M{"$in": ids.Strings()},
}))
}
func (r *DatasetSchema) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
if !r.f.CanWrite(sceneID) {
return nil
}
if _, err := r.client.Client().DeleteMany(ctx, bson.M{
"scene": sceneID.String(),
}); err != nil {
return rerror.ErrInternalByWithContext(ctx, err)
}
return nil
}
func (r *DatasetSchema) find(ctx context.Context, filter any) ([]*dataset.Schema, error) {
c := mongodoc.NewDatasetSchemaConsumer(r.f.Readable)
if err := r.client.Find(ctx, filter, c); err != nil {
return nil, err
}
return c.Result, nil
}
func (r *DatasetSchema) findOne(ctx context.Context, filter any) (*dataset.Schema, error) {
c := mongodoc.NewDatasetSchemaConsumer(r.f.Readable)
if err := r.client.FindOne(ctx, filter, c); err != nil {
return nil, err
}
return c.Result[0], nil
}
func (r *DatasetSchema) paginate(ctx context.Context, filter bson.M, pagination *usecasex.Pagination) ([]*dataset.Schema, *usecasex.PageInfo, error) {
c := mongodoc.NewDatasetSchemaConsumer(r.f.Readable)
pageInfo, err := r.client.Paginate(ctx, filter, nil, pagination, c)
if err != nil {
return nil, nil, rerror.ErrInternalByWithContext(ctx, err)
}
return c.Result, pageInfo, nil
}
func filterDatasetSchemas(ids []id.DatasetSchemaID, rows []*dataset.Schema) []*dataset.Schema {
res := make([]*dataset.Schema, 0, len(ids))
for _, id := range ids {
var r2 *dataset.Schema
for _, r := range rows {
if r.ID() == id {
r2 = r
break
}
}
res = append(res, r2)
}
return res
}
// func (r *DatasetSchema) readFilter(filter any) any {
// return applySceneFilter(filter, r.f.Readable)
// }
func (r *DatasetSchema) writeFilter(filter any) any {
return applySceneFilter(filter, r.f.Writable)
}