-
Notifications
You must be signed in to change notification settings - Fork 342
/
metastore_client.go
186 lines (157 loc) · 5.42 KB
/
metastore_client.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package mock
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/treeverse/lakefs/pkg/metastore"
)
var (
ErrNotFound = errors.New("not found")
ErrAlreadyExists = errors.New("already exists")
)
type MSClient struct {
t *testing.T
Tables map[string]*metastore.Table
partitionMap map[string]*metastore.Partition
}
func (m *MSClient) GetDBLocation(dbName string) string {
return dbName
}
func (m *MSClient) NormalizeDBName(name string) string {
return name
}
func NewMSClient(t *testing.T, initialTable map[string]*metastore.Table, initialPartitions map[string]*metastore.Partition) *MSClient {
if initialPartitions == nil {
initialPartitions = make(map[string]*metastore.Partition)
}
if initialTable == nil {
initialTable = make(map[string]*metastore.Table)
}
return &MSClient{
t: t,
Tables: initialTable,
partitionMap: initialPartitions,
}
}
func (m *MSClient) GetTable(_ context.Context, dbname string, tableName string) (*metastore.Table, error) {
m.t.Helper()
table := m.Tables[GetKey(dbname, tableName)]
if table == nil {
return nil, ErrNotFound
}
return table, nil
}
func (m *MSClient) HasTable(_ context.Context, dbname string, tableName string) (bool, error) {
_, hasTable := m.Tables[GetKey(dbname, tableName)]
return hasTable, nil
}
func (m *MSClient) GetPartitions(_ context.Context, dbName string, tableName string) ([]*metastore.Partition, error) {
key := GetKey(dbName, tableName)
return GetByPrefix(m.partitionMap, key), nil
}
func (m *MSClient) GetPartition(_ context.Context, dbName string, tableName string, values []string) (*metastore.Partition, error) {
return m.partitionMap[GetPartitionKey(dbName, tableName, values)], nil
}
func (m *MSClient) GetDatabase(_ context.Context, _ string) (*metastore.Database, error) {
panic("implement me")
}
func (m *MSClient) GetDatabases(_ context.Context, _ string) ([]*metastore.Database, error) {
panic("implement me")
}
func (m *MSClient) GetTables(_ context.Context, _ string, _ string) ([]*metastore.Table, error) {
panic("implement me")
}
func (m *MSClient) GetPartitionCollection(_ context.Context, _ string, _ string) (metastore.Collection, error) {
panic("implement me")
}
func (m *MSClient) GetTableCollection(_ context.Context, _ string, _ string) (metastore.Collection, error) {
panic("implement me")
}
func (m *MSClient) CreateTable(_ context.Context, table *metastore.Table) error {
key := GetKey(table.DBName, table.TableName)
if m.Tables[key] != nil {
return fmt.Errorf("table %w - key %s", ErrAlreadyExists, key)
}
m.Tables[key] = table
return nil
}
func (m *MSClient) AlterTable(_ context.Context, dbName string, tableName string, newTable *metastore.Table) error {
key := GetKey(dbName, tableName)
if m.Tables[key] == nil {
return fmt.Errorf("table %w - key %s", ErrNotFound, key) // TODO(Guys): consider using m.t.Fatal()
}
m.Tables[key] = newTable
return nil
}
func (m *MSClient) AddPartitions(ctx context.Context, tableName string, dbName string, newParts []*metastore.Partition) error {
for _, partition := range newParts {
err := m.AddPartition(ctx, tableName, dbName, partition)
if err != nil {
return err
}
}
return nil
}
func (m *MSClient) AlterPartitions(ctx context.Context, dbName string, tableName string, newPartitions []*metastore.Partition) error {
for _, partition := range newPartitions {
err := m.AlterPartition(ctx, dbName, tableName, partition)
if err != nil {
return err
}
}
return nil
}
func (m *MSClient) AlterPartition(_ context.Context, dbName string, tableName string, partition *metastore.Partition) error {
key := GetPartitionKey(dbName, tableName, partition.Values)
if m.partitionMap[key] == nil {
return fmt.Errorf("partition %w - key %s", ErrNotFound, key)
}
m.partitionMap[key] = partition
return nil
}
func (m *MSClient) AddPartition(_ context.Context, tableName string, dbName string, newPartition *metastore.Partition) error {
key := GetPartitionKey(dbName, tableName, newPartition.Values)
if m.partitionMap[key] != nil {
return fmt.Errorf("partition %w - key %s", ErrAlreadyExists, key)
}
m.partitionMap[key] = newPartition
return nil
}
func (m *MSClient) DropPartition(_ context.Context, dbName string, tableName string, values []string) error {
key := GetPartitionKey(dbName, tableName, values)
if m.partitionMap[key] == nil {
return fmt.Errorf("partition %w - key %s", ErrNotFound, key)
}
delete(m.partitionMap, key)
return nil
}
func (m *MSClient) CreateDatabaseIfNotExists(_ context.Context, _ *metastore.Database) error {
panic("implement me")
}
func PartitionsListToMap(partitions []*metastore.Partition) map[string]*metastore.Partition {
res := make(map[string]*metastore.Partition)
for _, partition := range partitions {
res[GetPartitionKey(partition.DBName, partition.TableName, partition.Values)] = partition
}
return res
}
func AddColumn(m *metastore.Partition, col *metastore.FieldSchema) {
m.Sd.Cols = append(m.Sd.Cols, col)
}
func GetByPrefix(m map[string]*metastore.Partition, prefix string) []*metastore.Partition {
res := make([]*metastore.Partition, 0)
for key, val := range m {
if strings.HasPrefix(key, prefix) {
res = append(res, val)
}
}
return res
}
func GetKey(dbName, tableName string) string {
return fmt.Sprintf("%s-%s", dbName, tableName)
}
func GetPartitionKey(dbName, tableName string, partition []string) string {
return fmt.Sprintf("%s-%s-%s", dbName, tableName, strings.Join(partition, "-"))
}