forked from vitessio/vitess
/
vschema.go
51 lines (43 loc) · 1.39 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 zktopo
import (
"encoding/json"
"fmt"
"path"
zookeeper "github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
"github.com/youtube/vitess/go/zk"
)
/*
This file contains the vschema management code for zktopo.Server
*/
const (
vschemaPath = "vschema"
)
// SaveVSchema saves the vschema into the topo.
func (zkts *Server) SaveVSchema(ctx context.Context, keyspace string, vschema *vschemapb.Keyspace) error {
data, err := json.MarshalIndent(vschema, "", " ")
if err != nil {
return err
}
vschemaPath := path.Join(GlobalKeyspacesPath, keyspace, vschemaPath)
_, err = zk.CreateOrUpdate(zkts.zconn, vschemaPath, data, 0, zookeeper.WorldACL(zookeeper.PermAll), true)
return convertError(err)
}
// GetVSchema fetches the JSON vschema from the topo.
func (zkts *Server) GetVSchema(ctx context.Context, keyspace string) (*vschemapb.Keyspace, error) {
vschemaPath := path.Join(GlobalKeyspacesPath, keyspace, vschemaPath)
data, _, err := zkts.zconn.Get(vschemaPath)
if err != nil {
return nil, convertError(err)
}
var vs vschemapb.Keyspace
err = json.Unmarshal(data, &vs)
if err != nil {
return nil, fmt.Errorf("bad vschema data (%v): %q", err, data)
}
return &vs, nil
}