forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vschema.go
48 lines (40 loc) · 1.25 KB
/
vschema.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
package etcd2topo
import (
"fmt"
"path"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
)
/*
This file contains the vschema management code for etcdtopo.Server
*/
// SaveVSchema saves the JSON vschema into the topo.
func (s *Server) SaveVSchema(ctx context.Context, keyspace string, vschema *vschemapb.Keyspace) error {
nodePath := path.Join(keyspacesPath, keyspace, topo.VSchemaFile)
data, err := proto.Marshal(vschema)
if err != nil {
return err
}
if len(data) == 0 {
// No vschema, remove it. So we can remove the keyspace.
err = s.Delete(ctx, topo.GlobalCell, nodePath, nil)
} else {
_, err = s.Update(ctx, topo.GlobalCell, nodePath, data, nil)
}
return err
}
// GetVSchema fetches the vschema from the topo.
func (s *Server) GetVSchema(ctx context.Context, keyspace string) (*vschemapb.Keyspace, error) {
nodePath := path.Join(keyspacesPath, keyspace, topo.VSchemaFile)
data, _, err := s.Get(ctx, topo.GlobalCell, nodePath)
if err != nil {
return nil, err
}
vs := &vschemapb.Keyspace{}
if err := proto.Unmarshal(data, vs); err != nil {
return nil, fmt.Errorf("bad vschema data (%v): %q", err, data)
}
return vs, nil
}