forked from vitessio/vitess
/
keyspace.go
90 lines (76 loc) · 2.32 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
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2013, 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 topo
import (
"github.com/youtube/vitess/go/vt/key"
)
// This file contains keyspace utility functions
// Keyspace is the data structure that has data about the Keyspaces in
// the topology. Most fields are optional.
type Keyspace struct {
// name of the column used for sharding
// empty if the keyspace is not sharded
ShardingColumnName string
// type of the column used for sharding
// KIT_UNSET if the keyspace is not sharded
ShardingColumnType key.KeyspaceIdType
// ServedFrom will redirect the appropriate traffic to
// another keyspace
ServedFrom map[TabletType]string
}
// KeyspaceInfo is a meta struct that contains metadata to give the
// data more context and convenience. This is the main way we interact
// with a keyspace.
type KeyspaceInfo struct {
keyspace string
version int64
*Keyspace
}
// KeyspaceName returns the keyspace name
func (ki *KeyspaceInfo) KeyspaceName() string {
return ki.keyspace
}
// Version returns the keyspace version from last time it was read or updated.
func (ki *KeyspaceInfo) Version() int64 {
return ki.version
}
// NewKeyspaceInfo returns a KeyspaceInfo basing on keyspace with the
// keyspace. This function should be only used by Server
// implementations.
func NewKeyspaceInfo(keyspace string, value *Keyspace, version int64) *KeyspaceInfo {
return &KeyspaceInfo{
keyspace: keyspace,
version: version,
Keyspace: value,
}
}
// UpdateKeyspace updates the keyspace data, with the right version
func UpdateKeyspace(ts Server, ki *KeyspaceInfo) error {
var version int64 = -1
if ki.version != 0 {
version = ki.version
}
newVersion, err := ts.UpdateKeyspace(ki, version)
if err == nil {
ki.version = newVersion
}
return err
}
// FindAllShardsInKeyspace reads and returns all the existing shards in
// a keyspace. It doesn't take any lock.
func FindAllShardsInKeyspace(ts Server, keyspace string) (map[string]*ShardInfo, error) {
shards, err := ts.GetShardNames(keyspace)
if err != nil {
return nil, err
}
result := make(map[string]*ShardInfo, len(shards))
for _, shard := range shards {
si, err := ts.GetShard(keyspace, shard)
if err != nil {
return nil, err
}
result[shard] = si
}
return result, nil
}