forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyspace.go
79 lines (65 loc) · 2.25 KB
/
keyspace.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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package consultopo
import (
"fmt"
"path"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// CreateKeyspace implements topo.Server.
func (s *Server) CreateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace) error {
data, err := proto.Marshal(value)
if err != nil {
return err
}
keyspacePath := path.Join(keyspacesPath, keyspace, topo.KeyspaceFile)
_, err = s.Create(ctx, topo.GlobalCell, keyspacePath, data)
return err
}
// UpdateKeyspace implements topo.Server.
func (s *Server) UpdateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace, existingVersion int64) (int64, error) {
data, err := proto.Marshal(value)
if err != nil {
return -1, err
}
keyspacePath := path.Join(keyspacesPath, keyspace, topo.KeyspaceFile)
version, err := s.Update(ctx, topo.GlobalCell, keyspacePath, data, VersionFromInt(existingVersion))
if err != nil {
return -1, err
}
return int64(version.(ConsulVersion)), nil
}
// GetKeyspace implements topo.Server.
func (s *Server) GetKeyspace(ctx context.Context, keyspace string) (*topodatapb.Keyspace, int64, error) {
keyspacePath := path.Join(keyspacesPath, keyspace, topo.KeyspaceFile)
data, version, err := s.Get(ctx, topo.GlobalCell, keyspacePath)
if err != nil {
return nil, 0, err
}
k := &topodatapb.Keyspace{}
if err = proto.Unmarshal(data, k); err != nil {
return nil, 0, fmt.Errorf("bad keyspace data %v", err)
}
return k, int64(version.(ConsulVersion)), nil
}
// GetKeyspaces implements topo.Server.
func (s *Server) GetKeyspaces(ctx context.Context) ([]string, error) {
children, err := s.ListDir(ctx, topo.GlobalCell, keyspacesPath)
switch err {
case nil:
return children, nil
case topo.ErrNoNode:
return nil, nil
default:
return nil, err
}
}
// DeleteKeyspace implements topo.Server.
func (s *Server) DeleteKeyspace(ctx context.Context, keyspace string) error {
keyspacePath := path.Join(keyspacesPath, keyspace, topo.KeyspaceFile)
return s.Delete(ctx, topo.GlobalCell, keyspacePath, nil)
}