-
Notifications
You must be signed in to change notification settings - Fork 279
/
database.go
170 lines (144 loc) · 5.35 KB
/
database.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package models
import (
"fmt"
"strconv"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/lindb/lindb/pkg/option"
)
// DatabaseNames represents the database name list.
type DatabaseNames []string
// ToTable returns database name list as table if it has value, else return empty string.
func (dbs DatabaseNames) ToTable() (rows int, tableStr string) {
if len(dbs) == 0 {
return 0, ""
}
writer := NewTableFormatter()
writer.AppendHeader(table.Row{"Database"})
for i := range dbs {
r := dbs[i]
writer.AppendRow(table.Row{r})
}
return len(dbs), writer.Render()
}
// Databases represents the database list.
type Databases []Database
// ToTable returns database list as table if it has value, else return empty string.
func (dbs Databases) ToTable() (rows int, tableStr string) {
if len(dbs) == 0 {
return 0, ""
}
writer := NewTableFormatter()
writer.AppendHeader(table.Row{"Name", "Storage", "Desc"})
for i := range dbs {
r := dbs[i]
writer.AppendRow(table.Row{r.Name, r.Storage, r.Desc})
}
return len(dbs), writer.Render()
}
// ShardID represents type for shard id.
type ShardID int
func (s ShardID) String() string { return strconv.Itoa(int(s)) }
func (s ShardID) Int() int { return int(s) }
// ParseShardID returns ShardID by given shard string value.
func ParseShardID(shard string) ShardID {
shardID, _ := strconv.Atoi(shard)
return ShardID(shardID)
}
// DatabaseConfig represents a database configuration about config and families
type DatabaseConfig struct {
ShardIDs []ShardID `toml:"shardIDs" json:"shardIDs"`
Option *option.DatabaseOption `toml:"option" json:"option"`
}
// Router represents the router of database.
type Router struct {
Key string `json:"key" validate:"required"` // routing key
Values []string `json:"values" validate:"required"` // routing values
Broker string `json:"broker" validate:"required"` // target broker
Database string `json:"database,omitempty"` // target database
}
// LogicDatabase defines database logic config, database can include multi-cluster.
type LogicDatabase struct {
Name string `json:"name" validate:"required"` // database's name
Routers []Router `json:"routers" validate:"required"` // database router
Desc string `json:"desc,omitempty"`
}
// Database defines database config.
type Database struct {
Name string `json:"name" validate:"required"` // database's name
Storage string `json:"storage" validate:"required"` // storage cluster's name
NumOfShard int `json:"numOfShard" validate:"gt=0"` // num. of shard
ReplicaFactor int `json:"replicaFactor" validate:"gt=0"` // replica refactor
Option *option.DatabaseOption `json:"option"` // time series database option
Desc string `json:"desc,omitempty"`
}
// String returns the database's description.
func (db *Database) String() string {
result := "create database " + db.Name + " with "
result += "shard " + fmt.Sprintf("%d", db.NumOfShard) + ", replica " + fmt.Sprintf("%d", db.ReplicaFactor)
result += ", intervals " + db.Option.Intervals.String()
return result
}
type DatabaseAssignment struct {
ShardAssignment *ShardAssignment `json:"shardAssignment"`
Option *option.DatabaseOption `json:"option"`
}
// Replica defines replica list for spec shard of database.
type Replica struct {
Replicas []NodeID `json:"replicas"`
}
// Contain returns if replica include node id.
func (r Replica) Contain(nodeID NodeID) bool {
for _, id := range r.Replicas {
if id == nodeID {
return true
}
}
return false
}
// ShardAssignment defines shard assignment for database.
type ShardAssignment struct {
Name string `json:"name"` // database's name
Shards map[ShardID]*Replica `json:"shards"`
replicaFactor int // for storage recover
}
// NewShardAssignment returns empty shard assignment instance.
func NewShardAssignment(name string) *ShardAssignment {
return &ShardAssignment{
Name: name,
Shards: make(map[ShardID]*Replica),
}
}
// AddReplica adds replica id to replica list of spec shard.
func (s *ShardAssignment) AddReplica(shardID ShardID, replicaID NodeID) {
replica, ok := s.Shards[shardID]
if !ok {
replica = &Replica{}
s.Shards[shardID] = replica
}
if !replica.Contain(replicaID) {
replica.Replicas = append(replica.Replicas, replicaID)
}
if len(replica.Replicas) > s.replicaFactor {
s.replicaFactor = len(replica.Replicas)
}
}
// GetReplicaFactor returns the factor of replica.
func (s *ShardAssignment) GetReplicaFactor() int {
return s.replicaFactor
}