forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-meta-endpoint.go
91 lines (80 loc) · 3.26 KB
/
index-meta-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
package index
import (
"context"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
"github.com/pydio/cells/common/sync/model"
)
// ClientWithMeta is a wrapper for index client that implements the MetadataReceiver interface
type ClientWithMeta struct {
Client
metaClient tree.NodeReceiverClient
}
// NewClientWithMeta creates a new client supporting metadata load
func NewClientWithMeta(dsName string, reader tree.NodeProviderClient, writer tree.NodeReceiverClient, sessionClient tree.SessionIndexerClient) *ClientWithMeta {
m := &ClientWithMeta{}
c := NewClient(dsName, reader, writer, sessionClient)
m.Client = *c
m.metaClient = tree.NewNodeReceiverClient(registry.GetClient(common.ServiceMeta))
return m
}
// Walk wraps the initial Walk function to load metadata on the fly
func (m *ClientWithMeta) Walk(walknFc model.WalkNodesFunc, root string, recursive bool) (err error) {
metaClient := tree.NewNodeProviderStreamerClient(registry.GetClient(common.ServiceMeta))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
metaStreamer, e := metaClient.ReadNodeStream(ctx)
if e != nil {
return e
}
defer metaStreamer.Close()
walkWrapped := func(path string, node *tree.Node, err error) {
if err == nil {
metaStreamer.Send(&tree.ReadNodeRequest{Node: node})
if resp, e := metaStreamer.Recv(); e == nil && resp.Node != nil && resp.Node.MetaStore != nil {
if node.MetaStore == nil {
node.MetaStore = make(map[string]string, len(resp.Node.MetaStore))
}
for k, v := range resp.Node.MetaStore {
node.MetaStore[k] = v
}
}
}
walknFc(path, node, err)
}
return m.Client.Walk(walkWrapped, root, recursive)
}
// CreateMetadata calls metaClient.CreateNode
func (m *ClientWithMeta) CreateMetadata(ctx context.Context, node *tree.Node, namespace string, jsonValue string) error {
log.Logger(ctx).Info("Create Meta : ", node.ZapUuid(), zap.String("namespace", namespace), zap.String("value", jsonValue))
if node.MetaStore == nil {
node.MetaStore = make(map[string]string, 1)
}
node.MetaStore[namespace] = jsonValue
_, e := m.metaClient.CreateNode(ctx, &tree.CreateNodeRequest{Node: node})
return e
}
// UpdateMetadata calls metaClient.UpdateNode
func (m *ClientWithMeta) UpdateMetadata(ctx context.Context, node *tree.Node, namespace string, jsonValue string) error {
log.Logger(ctx).Info("Update Meta : ", node.ZapUuid(), zap.String("namespace", namespace), zap.String("value", jsonValue))
if node.MetaStore == nil {
node.MetaStore = make(map[string]string, 1)
}
node.MetaStore[namespace] = jsonValue
_, e := m.metaClient.UpdateNode(ctx, &tree.UpdateNodeRequest{To: node})
return e
}
// DeleteMetadata calls metaClient.UpdateNode with the given namespace and an empty value
// (not DeleteNode, as it would remove all meta at once)
func (m *ClientWithMeta) DeleteMetadata(ctx context.Context, node *tree.Node, namespace string) error {
log.Logger(ctx).Info("Delete Meta : ", node.ZapUuid(), zap.String("namespace", namespace))
if node.MetaStore == nil {
node.MetaStore = make(map[string]string, 1)
}
node.MetaStore[namespace] = ""
_, e := m.metaClient.UpdateNode(ctx, &tree.UpdateNodeRequest{To: node})
return e
}