forked from vitessio/vitess
/
vschema.go
51 lines (42 loc) · 1.44 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
49
50
51
// Copyright 2015, 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 zk2topo
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 zktopo.Server.
// Eventually this will be moved to the go/vt/topo package.
// SaveVSchema saves the vschema into the topo.
func (zs *Server) SaveVSchema(ctx context.Context, keyspace string, vschema *vschemapb.Keyspace) error {
zkPath := 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 = zs.Delete(ctx, topo.GlobalCell, zkPath, nil)
} else {
_, err = zs.Update(ctx, topo.GlobalCell, zkPath, data, nil)
}
return err
}
// GetVSchema fetches the vschema from the topo.
func (zs *Server) GetVSchema(ctx context.Context, keyspace string) (*vschemapb.Keyspace, error) {
zkPath := path.Join(keyspacesPath, keyspace, topo.VSchemaFile)
data, _, err := zs.Get(ctx, topo.GlobalCell, zkPath)
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
}