-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vschema.go
48 lines (42 loc) · 1.24 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 etcdtopo
import (
"encoding/json"
"fmt"
"golang.org/x/net/context"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
"github.com/youtube/vitess/go/vt/topo"
)
/*
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 {
data, err := json.MarshalIndent(vschema, "", " ")
if err != nil {
return err
}
_, err = s.getGlobal().Set(vschemaFilePath(keyspace), string(data), 0 /* ttl */)
if err != nil {
return convertError(err)
}
return nil
}
// GetVSchema fetches the vschema from the topo.
func (s *Server) GetVSchema(ctx context.Context, keyspace string) (*vschemapb.Keyspace, error) {
resp, err := s.getGlobal().Get(vschemaFilePath(keyspace), false /* sort */, false /* recursive */)
if err != nil {
err = convertError(err)
if err == topo.ErrNoNode {
return nil, topo.ErrNoNode
}
return nil, err
}
if resp.Node == nil {
return nil, ErrBadResponse
}
var vs vschemapb.Keyspace
if err := json.Unmarshal([]byte(resp.Node.Value), &vs); err != nil {
return nil, fmt.Errorf("bad vschema data (%v): %q", err, resp.Node.Value)
}
return &vs, nil
}