forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
835 lines (695 loc) · 24.9 KB
/
handler.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
* 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 grpc
import (
"context"
"fmt"
"net/http"
"runtime/debug"
"strings"
"time"
index2 "github.com/pydio/cells/common/sql/index"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/object"
"github.com/pydio/cells/common/proto/tree"
servicecontext "github.com/pydio/cells/common/service/context"
cindex "github.com/pydio/cells/common/sql/index"
"github.com/pydio/cells/common/utils/mtree"
"github.com/pydio/cells/data/source/index"
"github.com/pydio/cells/data/source/index/sessions"
)
// TreeServer definition.
type TreeServer struct {
DataSourceName string
client client.Client
sessionStore sessions.DAO
}
/* =============================================================================
* Server public Methods
* ============================================================================ */
func init() {}
func getDAO(ctx context.Context, session string) index.DAO {
dao := cindex.NewFolderSizeCacheDAO(
cindex.NewHiddenFileDuplicateRemoverDAO(servicecontext.GetDAO(ctx).(index.DAO)),
)
if session != "" {
if dao := index.GetDAOCache(session); dao != nil {
return dao.(index.DAO)
}
return index.NewDAOCache(session, dao).(index.DAO)
}
return dao
}
// NewTreeServer factory.
func NewTreeServer(dsn string) *TreeServer {
return &TreeServer{
DataSourceName: dsn,
client: client.NewClient(),
sessionStore: sessions.NewSessionMemoryStore(),
}
}
// updateMeta simplifies the dao.SetNodeMeta call
func (s *TreeServer) updateMeta(dao index.DAO, node *mtree.TreeNode, reqNode *tree.Node) (previousEtag string, contentChange bool, err error) {
if node.IsLeaf() {
previousEtag = node.Etag
if previousEtag != common.NODE_FLAG_ETAG_TEMPORARY {
contentChange = true
}
}
// Replace meta
node.Size = reqNode.Size
node.Etag = reqNode.Etag
node.Type = reqNode.Type
node.MTime = reqNode.MTime
node.Mode = reqNode.Mode
err = dao.SetNodeMeta(node)
return
}
// CreateNode implementation for the TreeServer.
func (s *TreeServer) CreateNode(ctx context.Context, req *tree.CreateNodeRequest, resp *tree.CreateNodeResponse) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic recovered in CreateNode: %s. Node path was %s", r, req.Node.Path)
fmt.Printf("%s\n", debug.Stack())
}
}()
dao := getDAO(ctx, req.GetIndexationSession())
name := servicecontext.GetServiceName(ctx)
var node *mtree.TreeNode
var previousEtag string
eventType := tree.NodeChangeEvent_CREATE
inSession := req.IndexationSession != ""
reqUUID := req.GetNode().GetUuid()
updateIfExists := req.GetUpdateIfExists() || !req.GetNode().IsLeaf()
log.Logger(ctx).Debug("CreateNode", req.GetNode().Zap())
// Updating node based on UUID
if reqUUID != "" {
if node, err = dao.GetNodeByUUID(reqUUID); err != nil {
return errors.Forbidden(name, "Could not retrieve by uuid: %s", err.Error())
} else if node != nil && updateIfExists {
if etag, content, err := s.updateMeta(dao, node, req.GetNode()); err != nil {
return errors.Forbidden(name, "Could not replace previous node: %s", err.Error())
} else {
previousEtag = etag
if content && previousEtag != common.NODE_FLAG_ETAG_TEMPORARY {
eventType = tree.NodeChangeEvent_UPDATE_CONTENT
}
node.Path = req.GetNode().GetPath()
node.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if err := s.UpdateParentsAndNotify(ctx, dao, req.GetNode().GetSize(), eventType, nil, node, req.IndexationSession); err != nil {
return errors.InternalServerError(common.SERVICE_DATA_INDEX_, "Error while updating parents: %s", err.Error())
}
resp.Success = true
resp.Node = node.Node
return nil
}
} else if node != nil {
return errors.New(name, fmt.Sprintf("A node with same UUID already exists. Pass updateIfExists parameter if you are sure to override. %v", err), http.StatusConflict)
}
}
reqPath := safePath(req.GetNode().GetPath())
var path mtree.MPath
var created []*mtree.TreeNode
var exists bool
if updateIfExists {
// First search node by path to avoid false created value
if existing, _, err := dao.Path(reqPath, false, req.GetNode()); err == nil && existing != nil {
path = existing
exists = true
}
}
if !exists {
path, created, err = dao.Path(reqPath, true, req.GetNode())
if err != nil {
return errors.InternalServerError(name, "Error while inserting node: %s", err.Error())
}
}
// Checking if we have a node with the same path
if len(created) == 0 {
if updateIfExists {
node, _ = dao.GetNode(path)
if etag, content, err := s.updateMeta(dao, node, req.GetNode()); err != nil {
return errors.Forbidden(name, "Could not replace previous node: %s", err.Error())
} else {
previousEtag = etag
if content && previousEtag != common.NODE_FLAG_ETAG_TEMPORARY {
eventType = tree.NodeChangeEvent_UPDATE_CONTENT
}
}
} else {
return errors.New(name, "Node path already in use", http.StatusConflict)
}
} else if len(created) > 1 && !updateIfExists && !inSession {
// Special case : when not in indexation mode, if node creation
// has triggered creation of parents, send notifications for parents as well
for _, parent := range created[:len(created)-1] {
parent.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
client.Publish(ctx, client.NewPublication(common.TOPIC_INDEX_CHANGES, &tree.NodeChangeEvent{
Type: tree.NodeChangeEvent_CREATE,
Target: parent.Node,
}))
}
}
if node == nil {
node, err = dao.GetNode(path)
if err != nil || node == nil {
return fmt.Errorf("could not retrieve node %s", reqPath)
}
}
// Updating Commits - This is never used, avoid overhead of an insert
/*
newEtag := req.GetNode().GetEtag()
if node.IsLeaf() && newEtag != common.NODE_FLAG_ETAG_TEMPORARY && (previousEtag == "" || newEtag != previousEtag) {
if err := dao.PushCommit(node); err != nil {
log.Logger(ctx).Error("Error while pushing commit for node", node.Zap(), zap.Error(err))
}
}
*/
node.Path = reqPath
node.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if err := s.UpdateParentsAndNotify(ctx, dao, req.GetNode().GetSize(), eventType, nil, node, req.IndexationSession); err != nil {
return errors.InternalServerError(common.SERVICE_DATA_INDEX_, "Error while updating parents: %s", err.Error())
}
resp.Success = true
resp.Node = node.Node
return nil
}
// ReadNode implementation for the TreeServer.
func (s *TreeServer) ReadNode(ctx context.Context, req *tree.ReadNodeRequest, resp *tree.ReadNodeResponse) error {
defer track(ctx, "ReadNode", time.Now(), req, resp)
var session = ""
md, has := metadata.FromContext(ctx)
if has {
if s, ok := md["x-indexation-session"]; ok {
session = s
}
}
dao := getDAO(ctx, session)
name := servicecontext.GetServiceName(ctx)
var node *mtree.TreeNode
var err error
if req.GetNode().GetPath() == "" && req.GetNode().GetUuid() != "" {
node, err = dao.GetNodeByUUID(req.GetNode().GetUuid())
if err != nil || node == nil {
return errors.NotFound(name, "Could not find node by UUID with %s ", req.GetNode().GetUuid())
}
// In the case we've retrieve the node by uuid, we need to retrieve the path
var path []string
for pnode := range dao.GetNodes(node.MPath.Parents()...) {
path = append(path, pnode.Name())
}
path = append(path, node.Name())
node.Path = safePath(strings.Join(path, "/"))
} else {
reqPath := safePath(req.GetNode().GetPath())
path, _, err := dao.Path(reqPath, false)
if err != nil {
return errors.InternalServerError(name, "Error while retrieving path [%s], cause: %s", reqPath, err.Error())
}
if path == nil {
//return errors.New("Could not retrieve file path")
// Do not return error, or send a file not exists?
return errors.NotFound(name, "Could not retrieve node %s", reqPath)
}
node, err = dao.GetNode(path)
if err != nil {
if len(path) == 1 && path[0] == 1 {
// This is the root node, let's create it
node = index.NewNode(&tree.Node{
Uuid: "ROOT",
Type: tree.NodeType_COLLECTION,
}, path, []string{""})
if err = dao.AddNode(node); err != nil {
return err
}
} else {
return errors.NotFound(name, "Could not retrieve node %s", reqPath)
}
}
node.Path = reqPath
}
resp.Success = true
node.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if req.WithExtendedStats && !node.IsLeaf() {
folderCount, fileCount := dao.GetNodeChildrenCounts(node.MPath)
node.SetMeta("ChildrenCount", folderCount+fileCount)
node.SetMeta("ChildrenFolders", folderCount)
node.SetMeta("ChildrenFiles", fileCount)
}
if req.WithCommits && node.IsLeaf() {
if commits, err := dao.ListCommits(node); err == nil {
node.Commits = commits
} else {
log.Logger(ctx).Error("error while listing node commits", node.Zap(), zap.Error(err))
}
}
resp.Node = node.Node
return nil
}
// ListNodes implementation for the TreeServer.
func (s *TreeServer) ListNodes(ctx context.Context, req *tree.ListNodesRequest, resp tree.NodeProvider_ListNodesStream) error {
defer track(ctx, "ListNodes", time.Now(), req, resp)
var session = ""
dao := getDAO(ctx, session)
name := servicecontext.GetServiceName(ctx)
defer resp.Close()
if req.Ancestors && req.Recursive {
return errors.InternalServerError(name, "Please use either Recursive (children) or Ancestors (parents) flag, but not both.")
}
var c chan *mtree.TreeNode
// Special case for "Ancestors", node can have either Path or Uuid
// There is no need to compute additional stats here (folderSize)
if req.Ancestors {
var node *mtree.TreeNode
var err error
if req.GetNode().GetPath() == "" && req.GetNode().GetUuid() != "" {
node, err = dao.GetNodeByUUID(req.GetNode().GetUuid())
if err != nil {
return errors.NotFound(name, "could not find node by UUID with %s, cause: %s", req.GetNode().GetUuid(), err.Error())
}
} else {
reqPath := safePath(req.GetNode().GetPath())
path, _, err := dao.Path(reqPath, false)
if err != nil {
return errors.InternalServerError(name, "cannot retrieve path for %s, cause: %s", reqPath, err.Error())
}
if path == nil {
return errors.NotFound(name, "Could not retrieve node %s", reqPath)
}
node, err = dao.GetNode(path)
if err != nil {
return errors.InternalServerError(name, "cannot get node at %s, cause: %s", reqPath, err.Error())
}
}
// Get Ancestors tree and rebuild pathes for each
var path []string
nodes := []*mtree.TreeNode{}
for pnode := range dao.GetNodes(node.MPath.Parents()...) {
path = append(path, pnode.Name())
pnode.Path = safePath(strings.Join(path, "/"))
nodes = append(nodes, pnode)
}
// Now Reverse Slice
last := len(nodes) - 1
for i := 0; i < len(nodes)/2; i++ {
nodes[i], nodes[last-i] = nodes[last-i], nodes[i]
}
for _, n := range nodes {
resp.Send(&tree.ListNodesResponse{Node: n.Node})
}
} else {
reqNode := req.GetNode()
reqPath := safePath(reqNode.GetPath())
path, _, err := dao.Path(reqPath, false)
if err != nil {
return errors.InternalServerError(name, "cannot resolve path %s, cause: %s", reqPath, err.Error())
}
if path == nil {
return errors.NotFound(name, "Could not retrieve node %s", reqPath)
}
if req.WithCommits {
rootNode, _ := dao.GetNode(path)
if err := dao.ResyncDirtyEtags(rootNode); err != nil {
log.Logger(ctx).Error("could not re-sync dirty etags", rootNode.Zap(), zap.Error(err))
}
}
if req.Recursive {
c = dao.GetNodeTree(path)
} else {
c = dao.GetNodeChildren(path)
}
// Additional filters
metaFilter := tree.NewMetaFilter(reqNode)
hasFilter := metaFilter.Parse()
limitDepth := metaFilter.LimitDepth()
log.Logger(ctx).Debug("Listing nodes on DS with Filter", zap.Int32("req.FilterType", int32(req.FilterType)), zap.Bool("true", req.FilterType == tree.NodeType_COLLECTION))
names := strings.Split(reqPath, "/")
for node := range c {
if req.FilterType == tree.NodeType_COLLECTION && node.Type == tree.NodeType_LEAF {
continue
}
if req.Recursive && node.Path == reqPath {
continue
}
if node.Level > cap(names) {
newNames := make([]string, len(names), node.Level)
copy(newNames, names)
names = newNames
}
names = names[0:node.Level]
names[node.Level-1] = node.Name()
node.Path = safePath(strings.Join(names, "/"))
node.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if hasFilter && !metaFilter.Match(node.Name(), node.Node) {
continue
}
if req.FilterType == tree.NodeType_LEAF && node.Type == tree.NodeType_COLLECTION {
continue
}
if limitDepth > 0 && node.Level != limitDepth {
continue
}
if req.WithCommits && node.IsLeaf() {
if commits, e := dao.ListCommits(node); e == nil {
node.Commits = commits
} else {
log.Logger(ctx).Error("could not list node commits", node.Zap(), zap.Error(err))
}
}
resp.Send(&tree.ListNodesResponse{Node: node.Node})
}
}
return nil
}
// UpdateNode implementation for the TreeServer.
func (s *TreeServer) UpdateNode(ctx context.Context, req *tree.UpdateNodeRequest, resp *tree.UpdateNodeResponse) (err error) {
defer track(ctx, "UpdateNode", time.Now(), req, resp)
log.Logger(ctx).Debug("Entering UpdateNode")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic recovered in UpdateNode: %s. Params From:%s, To:%s", r, req.From.Path, req.To.Path)
}
log.Logger(ctx).Debug("Finished UpdateNode")
}()
// dao := servicecontext.GetDAO(ctx).(index.DAO)
dao := getDAO(ctx, req.GetIndexationSession())
name := servicecontext.GetServiceName(ctx)
reqFromPath := safePath(req.GetFrom().GetPath())
reqToPath := safePath(req.GetTo().GetPath())
var pathFrom, pathTo mtree.MPath
var nodeFrom, nodeTo *mtree.TreeNode
if pathFrom, _, err = dao.Path(reqFromPath, false); err != nil {
return errors.InternalServerError(name, "cannot resolve pathFrom %s, cause: %s", reqFromPath, err.Error())
}
if pathFrom == nil {
return errors.NotFound(name, "Could not retrieve node %s", req.From.Path)
}
if nodeFrom, err = dao.GetNode(pathFrom); err != nil {
return errors.NotFound(name, "Could not retrieve node %s", req.From.Path)
}
if cache, o := dao.(index2.CacheDAO); o {
pathTo, nodeTo, err = cache.PathCreateNoAdd(reqToPath)
} else {
if pathTo, _, err = dao.Path(reqToPath, true); err != nil {
return errors.InternalServerError(name, "cannot resolve pathTo %s, cause: %s", reqToPath, err.Error())
}
if nodeTo, err = dao.GetNode(pathTo); err != nil {
return errors.NotFound(name, "Could not retrieve node %s", req.From.Path)
}
// Legacy : to Avoid Duplicate error when using a CacheDAO - Flush now and after delete.
dao.Flush(false)
// First of all, we delete the existing node
if nodeTo != nil {
if err = dao.DelNode(nodeTo); err != nil {
return errors.InternalServerError(name, "Could not delete former to node at %s", req.To.Path)
}
}
dao.Flush(false)
}
nodeFrom.Path = reqFromPath
nodeTo.Path = reqToPath
nodeFrom.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
nodeTo.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if err = dao.MoveNodeTree(nodeFrom, nodeTo); err != nil {
return err
}
newNode, err := dao.GetNode(pathTo)
if err == nil && newNode != nil {
newNode.Path = reqToPath
newNode.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
if err := s.UpdateParentsAndNotify(ctx, dao, nodeFrom.GetSize(), tree.NodeChangeEvent_UPDATE_PATH, nodeFrom, newNode, req.IndexationSession); err != nil {
return errors.InternalServerError(common.SERVICE_DATA_INDEX_, "error while updating parents: %s", err.Error())
}
}
resp.Success = true
return nil
}
// DeleteNode implementation for the TreeServer.
func (s *TreeServer) DeleteNode(ctx context.Context, req *tree.DeleteNodeRequest, resp *tree.DeleteNodeResponse) (err error) {
log.Logger(ctx).Debug("DeleteNode", zap.Any("request", req))
defer track(ctx, "DeleteNode", time.Now(), req, resp)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic recovered in DeleteNode: %s. Node path was %s", r, req.Node.Path)
}
}()
dao := getDAO(ctx, req.GetIndexationSession())
name := servicecontext.GetServiceName(ctx)
reqPath := safePath(req.GetNode().GetPath())
path, _, pE := dao.Path(reqPath, false)
if pE != nil {
return errors.NotFound(name, "Could not compute path %s (%s)", reqPath, pE.Error())
}
node, err := dao.GetNode(path)
if err != nil {
return errors.NotFound(name, "Could not retrieve node %s", reqPath)
}
node.Path = reqPath
node.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
var childrenEvents []*tree.NodeChangeEvent
if node.Type == tree.NodeType_COLLECTION {
c := dao.GetNodeTree(path)
names := strings.Split(reqPath, "/")
for child := range c {
if child.Name() == common.PYDIO_SYNC_HIDDEN_FILE_META {
continue
}
if child.Level > cap(names) {
newNames := make([]string, len(names), child.Level)
copy(newNames, names)
names = newNames
}
names = names[0:child.Level]
names[child.Level-1] = child.Name()
child.Path = safePath(strings.Join(names, "/"))
child.SetMeta(common.META_NAMESPACE_DATASOURCE_NAME, s.DataSourceName)
childrenEvents = append(childrenEvents, &tree.NodeChangeEvent{
Type: tree.NodeChangeEvent_DELETE,
Source: child.Node,
Silent: true,
})
}
}
if err := dao.DelNode(node); err != nil {
return errors.InternalServerError(name, "Could not delete node at %s, cause: %s", reqPath, err.Error())
}
if err := dao.DeleteCommits(node); err != nil {
return errors.InternalServerError(name, "Could not delete node commits for %s, cause: %s", reqPath, err.Error())
}
if err := s.UpdateParentsAndNotify(ctx, dao, node.Size, tree.NodeChangeEvent_DELETE, node, nil, req.IndexationSession); err != nil {
return errors.InternalServerError(common.SERVICE_DATA_INDEX_, "Error while updating parents: %s", err.Error())
}
if len(childrenEvents) > 0 {
var batcher sessions.SessionBatcher
if req.IndexationSession != "" {
sess, batch, err := s.sessionStore.ReadSession(req.IndexationSession)
if err == nil && sess != nil {
batcher = batch
}
}
for _, ev := range childrenEvents {
if batcher != nil {
batcher.Notify(common.TOPIC_INDEX_CHANGES, ev)
} else {
client.Publish(ctx, client.NewPublication(common.TOPIC_INDEX_CHANGES, ev))
}
}
}
resp.Success = true
return nil
}
// OpenSession opens an indexer session.
func (s *TreeServer) OpenSession(ctx context.Context, req *tree.OpenSessionRequest, resp *tree.OpenSessionResponse) error {
log.Logger(ctx).Info("Opening Indexation Session " + req.GetSession().GetUuid())
s.sessionStore.PutSession(req.GetSession())
resp.Session = req.GetSession()
return nil
}
// FlushSession allows to flsuh what's in the dao cache for the current session to ensure we are up to date moving on to the next phase of the indexation.
func (s *TreeServer) FlushSession(ctx context.Context, req *tree.FlushSessionRequest, resp *tree.FlushSessionResponse) error {
session, _, _ := s.sessionStore.ReadSession(req.GetSession().GetUuid())
if session != nil {
log.Logger(ctx).Info("Flushing Indexation Session " + req.GetSession().GetUuid())
dao := getDAO(ctx, session.GetUuid())
err := dao.Flush(false)
if err != nil {
log.Logger(ctx).Error("Error while flushing indexation Session "+req.GetSession().GetUuid(), zap.Error(err))
return err
}
}
resp.Session = req.GetSession()
return nil
}
// CloseSession closes an indexer session.
func (s *TreeServer) CloseSession(ctx context.Context, req *tree.CloseSessionRequest, resp *tree.CloseSessionResponse) error {
session, batcher, _ := s.sessionStore.ReadSession(req.GetSession().GetUuid())
if session != nil {
log.Logger(ctx).Info("Closing Indexation Session " + req.GetSession().GetUuid())
dao := getDAO(ctx, session.GetUuid())
err := dao.Flush(true)
batcher.Flush(ctx, dao)
s.sessionStore.DeleteSession(req.GetSession())
if err != nil {
log.Logger(ctx).Error("Error while closing (flush) indexation Session "+req.GetSession().GetUuid(), zap.Error(err))
return err
}
}
resp.Session = req.GetSession()
return nil
}
// CleanResourcesBeforeDelete ensure all resources are cleant before deleting.
func (s *TreeServer) CleanResourcesBeforeDelete(ctx context.Context, request *object.CleanResourcesRequest, response *object.CleanResourcesResponse) error {
dao := servicecontext.GetDAO(ctx).(index.DAO)
err, msg := dao.CleanResourcesOnDeletion()
if err != nil {
response.Success = false
} else {
response.Success = true
response.Message = msg
}
return err
}
// UpdateParentsAndNotify update the parents nodes and notify the tree of the event that occurred.
func (s *TreeServer) UpdateParentsAndNotify(ctx context.Context, dao index.DAO, deltaSize int64, eventType tree.NodeChangeEvent_EventType, sourceNode *mtree.TreeNode, targetNode *mtree.TreeNode, sessionUuid string) error {
var batcher sessions.SessionBatcher
var session *tree.IndexationSession
if sessionUuid != "" {
sess, batch, err := s.sessionStore.ReadSession(sessionUuid)
if err == nil && sess != nil {
session = sess
batcher = batch
}
}
// Init Event from source/target parameters
var event *tree.NodeChangeEvent
mpathes := make(map[*mtree.MPath]int64)
if sourceNode == nil {
// CREATE
mpathes[&targetNode.MPath] = deltaSize
event = &tree.NodeChangeEvent{
Type: eventType,
Target: targetNode.Node,
}
} else if targetNode == nil {
// DELETE
mpathes[&sourceNode.MPath] = -deltaSize
event = &tree.NodeChangeEvent{
Type: eventType,
Source: sourceNode.Node,
}
} else {
// UPDATE
mpathFrom := sourceNode.MPath
mpathTo := targetNode.MPath
if mpathFrom.Parent().String() == mpathTo.Parent().String() {
mpathes[&mpathFrom] = 0
} else {
mpathes[&mpathFrom] = -sourceNode.Size
mpathes[&mpathTo] = sourceNode.Size
}
event = &tree.NodeChangeEvent{
Type: eventType,
Source: sourceNode.Node,
Target: targetNode.Node,
}
}
// Enrich Event Metadata field
if md, ok := metadata.FromContext(ctx); ok {
if event.Metadata == nil {
event.Metadata = make(map[string]string, len(md))
}
for k, v := range md {
for _, header := range common.XSpecialPydioHeaders {
// May return special headers in lowercase, we don't want that.
if k == strings.ToLower(header) {
k = header
break
}
}
event.Metadata[k] = v
}
}
// Publish either to batcher or to broker directly
if batcher != nil && session != nil {
event.Silent = session.Silent
batcher.Notify(common.TOPIC_INDEX_CHANGES, event)
} else {
client.Publish(ctx, client.NewPublication(common.TOPIC_INDEX_CHANGES, event))
}
return nil
}
func (s *TreeServer) batcherUpdateParents(batcher sessions.SessionBatcher, delta int64, mPath mtree.MPath) {
mp := mPath.Parent()
for len(mp) > 0 {
batcher.UpdateMPath(mp, delta)
mp = mp.Parent()
}
}
// Batch update nodes on parents.
func (s *TreeServer) daoUpdateParents(dao index.DAO, delta int64, mPath mtree.MPath) error {
b := dao.SetNodes("-1", delta)
mp := mPath.Parent()
for len(mp) > 0 {
parent := mtree.NewTreeNode()
parent.SetMPath(mp...)
b.Send(parent)
mp = mp.Parent()
}
return b.Close()
}
// CreateNodeStream implementation for the TreeServer.
func (s *TreeServer) CreateNodeStream(ctx context.Context, stream tree.NodeReceiverStream_CreateNodeStreamStream) error {
var (
err error
req *tree.CreateNodeRequest
)
for {
req, err = stream.Recv()
if err != nil {
break
}
rsp := &tree.CreateNodeResponse{}
err = s.CreateNode(ctx, req, rsp)
if err != nil {
break
}
err = stream.Send(rsp)
if err != nil {
break
}
}
stream.Close()
return err
}
func track(ctx context.Context, fn string, start time.Time, req interface{}, resp interface{}) {
log.Logger(ctx).Debug(fn, zap.Duration("time", time.Since(start)), zap.Any("req", req), zap.Any("resp", resp))
}
func safePath(str string) string {
return fmt.Sprintf("/%s", strings.TrimLeft(str, "/"))
}
func dirWithInternalSeparator(filePath string) string {
segments := strings.Split(filePath, "/")
return strings.Join(segments[:len(segments)-1], "/")
}