-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspects.go
140 lines (129 loc) · 4.31 KB
/
aspects.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
/*
* Copyright 2022 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mongo
import (
"context"
"github.com/SENERGY-Platform/models/go/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const aspectIdFieldName = "Id"
var aspectIdKey string
func init() {
CreateCollections = append(CreateCollections, func(db *Mongo) error {
var err error
aspectIdKey, err = getBsonFieldName(models.Aspect{}, aspectIdFieldName)
if err != nil {
return err
}
collection := db.client.Database(db.config.MongoTable).Collection(db.config.MongoAspectCollection)
err = db.ensureIndex(collection, "aspectidindex", aspectIdKey, true, true)
if err != nil {
return err
}
return nil
})
}
func (this *Mongo) aspectCollection() *mongo.Collection {
return this.client.Database(this.config.MongoTable).Collection(this.config.MongoAspectCollection)
}
func (this *Mongo) GetAspect(ctx context.Context, id string) (aspect models.Aspect, exists bool, err error) {
result := this.aspectCollection().FindOne(ctx, bson.M{aspectIdKey: id})
err = result.Err()
if err == mongo.ErrNoDocuments {
return aspect, false, nil
}
if err != nil {
return
}
err = result.Decode(&aspect)
if err == mongo.ErrNoDocuments {
return aspect, false, nil
}
return aspect, true, err
}
func (this *Mongo) SetAspect(ctx context.Context, aspect models.Aspect) error {
_, err := this.aspectCollection().ReplaceOne(ctx, bson.M{aspectIdKey: aspect.Id}, aspect, options.Replace().SetUpsert(true))
return err
}
func (this *Mongo) RemoveAspect(ctx context.Context, id string) error {
_, err := this.aspectCollection().DeleteOne(ctx, bson.M{aspectIdKey: id})
return err
}
func (this *Mongo) ListAllAspects(ctx context.Context) (result []models.Aspect, err error) {
cursor, err := this.aspectCollection().Find(ctx, bson.D{}, options.Find().SetSort(bson.D{{aspectIdKey, 1}}))
if err != nil {
return nil, err
}
result = []models.Aspect{}
for cursor.Next(context.Background()) {
aspect := models.Aspect{}
err = cursor.Decode(&aspect)
if err != nil {
return nil, err
}
result = append(result, aspect)
}
err = cursor.Err()
return
}
// returns all aspects used in combination with measuring functions (usage may optionally be by its descendants or ancestors)
func (this *Mongo) ListAspectsWithMeasuringFunction(ctx context.Context, ancestors bool, descendants bool) (result []models.Aspect, err error) {
aspectIds, err := this.deviceTypeCriteriaCollection().Distinct(ctx, DeviceTypeCriteriaBson.AspectId, bson.M{
deviceTypeCriteriaIsControllingFunctionKey: false,
DeviceTypeCriteriaBson.AspectId: bson.M{"$exists": true, "$ne": ""},
})
if err != nil {
return nil, err
}
var cursor *mongo.Cursor
if ancestors || descendants {
or := bson.A{
bson.D{{aspectNodeIdKey, bson.M{"$in": aspectIds}}},
}
if ancestors {
or = append(or, bson.D{{aspectNodeAncestorIdsKey, bson.M{"$in": aspectIds}}})
}
if descendants {
or = append(or, bson.D{{aspectNodeDescendentIdsKey, bson.M{"$in": aspectIds}}})
}
rootIds, err := this.aspectNodeCollection().Distinct(ctx, aspectNodeRootIdKey, bson.D{{"$or", or}})
if err != nil {
return nil, err
}
cursor, err = this.aspectCollection().Find(ctx, bson.M{aspectIdKey: bson.M{"$in": rootIds}}, options.Find().SetSort(bson.D{{aspectIdKey, 1}}))
if err != nil {
return nil, err
}
} else {
cursor, err = this.aspectCollection().Find(ctx, bson.M{aspectIdKey: bson.M{"$in": aspectIds}}, options.Find().SetSort(bson.D{{aspectIdKey, 1}}))
if err != nil {
return nil, err
}
}
result = []models.Aspect{}
for cursor.Next(context.Background()) {
aspect := models.Aspect{}
err = cursor.Decode(&aspect)
if err != nil {
return nil, err
}
result = append(result, aspect)
}
err = cursor.Err()
return
}