-
Notifications
You must be signed in to change notification settings - Fork 172
/
ds-selector.go
98 lines (87 loc) · 3.2 KB
/
ds-selector.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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package jobs
import (
"context"
"github.com/golang/protobuf/ptypes"
"github.com/micro/go-micro/client"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/object"
service "github.com/pydio/cells/common/service/proto"
)
func (m *DataSourceSelector) Filter(ctx context.Context, input ActionMessage) (ActionMessage, *ActionMessage, bool) {
var passed, excluded []*object.DataSource
for _, ds := range input.DataSources {
if m.All || m.evaluate(ctx, m.Query, input, ds) {
passed = append(passed, ds)
} else {
excluded = append(passed, ds)
}
}
input.DataSources = passed
var x *ActionMessage
if len(excluded) > 0 {
filteredOutput := input
filteredOutput.DataSources = excluded
x = &filteredOutput
}
return input, x, len(passed) > 0
}
func (m *DataSourceSelector) Select(cl client.Client, ctx context.Context, input ActionMessage, objects chan interface{}, done chan bool) error {
defer func() {
done <- true
}()
for _, ds := range m.loadDSS() {
if m.All || m.evaluate(ctx, m.Query, input, ds) {
objects <- ds
}
}
return nil
}
func (m *DataSourceSelector) MultipleSelection() bool {
return m.Collect
}
func (m *DataSourceSelector) loadDSS() (sources []*object.DataSource) {
for _, ds := range config.ListSourcesFromConfig() {
sources = append(sources, ds)
}
return
}
func (m *DataSourceSelector) evaluate(ctx context.Context, query *service.Query, input ActionMessage, dsObject *object.DataSource) bool {
var bb []bool
for _, q := range query.SubQueries {
msg := &object.DataSourceSingleQuery{}
subQ := &service.Query{}
if e := ptypes.UnmarshalAny(q, msg); e == nil {
// Evaluate fields
msg.Name = EvaluateFieldStr(ctx, input, msg.Name)
msg.ObjectServiceName = EvaluateFieldStr(ctx, input, msg.ObjectServiceName)
msg.StorageConfigurationName = EvaluateFieldStr(ctx, input, msg.StorageConfigurationName)
msg.StorageConfigurationValue = EvaluateFieldStr(ctx, input, msg.StorageConfigurationValue)
msg.PeerAddress = EvaluateFieldStr(ctx, input, msg.PeerAddress)
msg.EncryptionKey = EvaluateFieldStr(ctx, input, msg.EncryptionKey)
msg.VersioningPolicyName = EvaluateFieldStr(ctx, input, msg.VersioningPolicyName)
bb = append(bb, msg.Matches(dsObject))
} else if e := ptypes.UnmarshalAny(q, subQ); e == nil {
bb = append(bb, m.evaluate(ctx, subQ, input, dsObject))
}
}
return service.ReduceQueryBooleans(bb, query.Operation)
}