-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-endpoint.go
216 lines (178 loc) · 5.79 KB
/
index-endpoint.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
/*
* Copyright (c) 2018. 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 sync
import (
"context"
"errors"
"strings"
"go.uber.org/zap"
"github.com/pborman/uuid"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
commonsync "github.com/pydio/cells/data/source/sync/lib/common"
)
type IndexEndpoint struct {
readerClient tree.NodeProviderClient
writerClient tree.NodeReceiverClient
sessionClient tree.SessionIndexerClient
internalSession *tree.IndexationSession
streamer *IndexStreamer
}
func (i *IndexEndpoint) GetEndpointInfo() commonsync.EndpointInfo {
return commonsync.EndpointInfo{
RequiresFoldersRescan: false,
RequiresNormalization: false,
}
}
func (i *IndexEndpoint) Walk(walknFc commonsync.WalkNodesFunc, pathes ...string) (err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
responseClient, e := i.readerClient.ListNodes(ctx, &tree.ListNodesRequest{
Node: &tree.Node{
Path: "",
},
Recursive: true,
})
if e != nil {
return e
}
defer responseClient.Close()
for {
response, rErr := responseClient.Recv()
if rErr != nil {
walknFc("", nil, rErr)
}
if response == nil {
break
}
response.Node.Path = strings.TrimLeft(response.Node.Path, "/")
walknFc(response.Node.Path, response.Node, nil)
}
return nil
}
func (i *IndexEndpoint) Watch(recursivePath string) (*commonsync.WatchObject, error) {
return nil, errors.New("Watch Not Implemented")
}
func (i *IndexEndpoint) LoadNode(ctx context.Context, path string, leaf ...bool) (node *tree.Node, err error) {
log.Logger(ctx).Debug("LoadNode", zap.String("path", path))
resp, e := i.readerClient.ReadNode(ctx, &tree.ReadNodeRequest{
Node: &tree.Node{
Path: path,
},
})
if e != nil {
return nil, e
}
return resp.Node, nil
}
// LoadNodeByUuid makes this endpoint an UuidProvider
func (i *IndexEndpoint) LoadNodeByUuid(ctx context.Context, uuid string) (node *tree.Node, err error) {
if resp, e := i.readerClient.ReadNode(ctx, &tree.ReadNodeRequest{
Node: &tree.Node{
Uuid: uuid,
},
}); e != nil {
return nil, e
} else {
log.Logger(ctx).Debug("Loading Node By Uuid has response:", resp.Node.Zap())
return resp.Node, nil
}
}
func (i *IndexEndpoint) CreateNode(ctx context.Context, node *tree.Node, updateIfExists bool) (err error) {
session := i.indexationSession()
_, err = i.writerClient.CreateNode(ctx, &tree.CreateNodeRequest{
Node: node,
UpdateIfExists: updateIfExists,
IndexationSession: session,
})
if session == "" {
log.Logger(ctx).Info("CreateNode", zap.Any("node", node), zap.Error(err))
}
return err
}
func (i *IndexEndpoint) UpdateNode(ctx context.Context, node *tree.Node) error {
return i.CreateNode(ctx, node, true)
}
func (i *IndexEndpoint) DeleteNode(ctx context.Context, path string) (err error) {
session := i.indexationSession()
if session == "" {
log.Logger(ctx).Info("DeleteNode", zap.String("path", path))
}
_, err = i.writerClient.DeleteNode(ctx, &tree.DeleteNodeRequest{
Node: &tree.Node{
Path: path,
},
IndexationSession: session,
})
return err
}
func (i *IndexEndpoint) MoveNode(ctx context.Context, oldPath string, newPath string) (err error) {
log.Logger(ctx).Info("MoveNode", zap.String("oldPath", oldPath), zap.String("newPath", newPath))
_, err = i.writerClient.UpdateNode(ctx, &tree.UpdateNodeRequest{
From: &tree.Node{
Path: oldPath,
},
To: &tree.Node{
Path: newPath,
},
IndexationSession: i.indexationSession(),
})
log.Logger(ctx).Info("MoveNode Finished", zap.String("oldPath", oldPath), zap.String("newPath", newPath))
return err
}
func (i *IndexEndpoint) StartSession(ctx context.Context, rootNode *tree.Node) (*tree.IndexationSession, error) {
id := uuid.NewUUID().String()
sess := &tree.IndexationSession{
Uuid: id,
Description: "Indexation",
RootNode: rootNode,
}
resp, err := i.sessionClient.OpenSession(ctx, &tree.OpenSessionRequest{Session: sess})
if err != nil {
return nil, err
} else {
i.internalSession = resp.Session
return resp.Session, nil
}
}
func (i *IndexEndpoint) FlushSession(ctx context.Context, sessionUuid string) error {
_, err := i.sessionClient.FlushSession(ctx, &tree.FlushSessionRequest{Session: &tree.IndexationSession{Uuid: sessionUuid}})
return err
}
func (i *IndexEndpoint) FinishSession(ctx context.Context, sessionUuid string) error {
_, err := i.sessionClient.CloseSession(ctx, &tree.CloseSessionRequest{Session: &tree.IndexationSession{Uuid: sessionUuid}})
i.internalSession = nil
return err
}
func (i *IndexEndpoint) indexationSession() string {
sessionUuid := ""
if i.internalSession != nil {
sessionUuid = i.internalSession.Uuid
}
return sessionUuid
}
func NewIndexEndpoint(dsName string, reader tree.NodeProviderClient, writer tree.NodeReceiverClient, sessionClient tree.SessionIndexerClient) *IndexEndpoint {
return &IndexEndpoint{
readerClient: reader,
writerClient: writer,
sessionClient: sessionClient,
//streamer: NewIndexStreamer(common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_DATA_INDEX_ + dsName),
}
}