github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/metastore/mock/metastore_client.go (about) 1 package mock 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "strings" 8 "testing" 9 10 "github.com/treeverse/lakefs/pkg/metastore" 11 mserrors "github.com/treeverse/lakefs/pkg/metastore/errors" 12 ) 13 14 var ( 15 ErrNotFound = errors.New("not found") 16 ErrAlreadyExists = errors.New("already exists") 17 ) 18 19 type MSClient struct { 20 t *testing.T 21 Databases map[string]*metastore.Database 22 Tables map[string]*metastore.Table 23 partitionMap map[string]*metastore.Partition 24 } 25 26 func (m *MSClient) GetDBLocation(dbName string) string { 27 return dbName 28 } 29 30 func (m *MSClient) NormalizeDBName(name string) string { 31 return name 32 } 33 34 func NewMSClient(t *testing.T, initialDatabases map[string]*metastore.Database, initialTables map[string]*metastore.Table, initialPartitions map[string]*metastore.Partition) *MSClient { 35 if initialPartitions == nil { 36 initialPartitions = make(map[string]*metastore.Partition) 37 } 38 if initialTables == nil { 39 initialTables = make(map[string]*metastore.Table) 40 } 41 if initialDatabases == nil { 42 initialDatabases = make(map[string]*metastore.Database) 43 } 44 45 return &MSClient{ 46 t: t, 47 Tables: initialTables, 48 partitionMap: initialPartitions, 49 Databases: initialDatabases, 50 } 51 } 52 53 func (m *MSClient) GetTable(_ context.Context, dbname string, tableName string) (*metastore.Table, error) { 54 m.t.Helper() 55 table := m.Tables[GetKey(dbname, tableName)] 56 if table == nil { 57 return nil, ErrNotFound 58 } 59 return table, nil 60 } 61 62 func (m *MSClient) HasTable(_ context.Context, dbname string, tableName string) (bool, error) { 63 _, hasTable := m.Tables[GetKey(dbname, tableName)] 64 return hasTable, nil 65 } 66 67 func (m *MSClient) GetPartitions(_ context.Context, dbName string, tableName string) ([]*metastore.Partition, error) { 68 key := GetKey(dbName, tableName) 69 return GetByPrefix(m.partitionMap, key), nil 70 } 71 72 func (m *MSClient) GetPartition(_ context.Context, dbName string, tableName string, values []string) (*metastore.Partition, error) { 73 return m.partitionMap[GetPartitionKey(dbName, tableName, values)], nil 74 } 75 76 func (m *MSClient) GetDatabase(_ context.Context, name string) (*metastore.Database, error) { 77 db := m.Databases[name] 78 if db == nil { 79 return nil, ErrNotFound 80 } 81 return db, nil 82 } 83 84 func (m *MSClient) GetDatabases(_ context.Context, name string) ([]*metastore.Database, error) { 85 panic("implement me") 86 } 87 88 func (m *MSClient) GetTables(_ context.Context, _ string, _ string) ([]*metastore.Table, error) { 89 panic("implement me") 90 } 91 92 func (m *MSClient) GetPartitionCollection(_ context.Context, _ string, _ string) (metastore.Collection, error) { 93 panic("implement me") 94 } 95 96 func (m *MSClient) GetTableCollection(_ context.Context, _ string, _ string) (metastore.Collection, error) { 97 panic("implement me") 98 } 99 100 func (m *MSClient) CreateTable(_ context.Context, table *metastore.Table) error { 101 key := GetKey(table.DBName, table.TableName) 102 if m.Tables[key] != nil { 103 return fmt.Errorf("table %w - key %s", ErrAlreadyExists, key) 104 } 105 m.Tables[key] = table 106 return nil 107 } 108 109 func (m *MSClient) AlterTable(_ context.Context, dbName string, tableName string, newTable *metastore.Table) error { 110 key := GetKey(dbName, tableName) 111 if m.Tables[key] == nil { 112 return fmt.Errorf("table %w - key %s", ErrNotFound, key) // TODO(Guys): consider using m.t.Fatal() 113 } 114 m.Tables[key] = newTable 115 return nil 116 } 117 118 func (m *MSClient) AddPartitions(ctx context.Context, tableName string, dbName string, newParts []*metastore.Partition) error { 119 for _, partition := range newParts { 120 err := m.AddPartition(ctx, tableName, dbName, partition) 121 if err != nil { 122 return err 123 } 124 } 125 return nil 126 } 127 128 func (m *MSClient) AlterPartitions(ctx context.Context, dbName string, tableName string, newPartitions []*metastore.Partition) error { 129 for _, partition := range newPartitions { 130 err := m.AlterPartition(ctx, dbName, tableName, partition) 131 if err != nil { 132 return err 133 } 134 } 135 return nil 136 } 137 138 func (m *MSClient) AlterPartition(_ context.Context, dbName string, tableName string, partition *metastore.Partition) error { 139 key := GetPartitionKey(dbName, tableName, partition.Values) 140 if m.partitionMap[key] == nil { 141 return fmt.Errorf("partition %w - key %s", ErrNotFound, key) 142 } 143 m.partitionMap[key] = partition 144 return nil 145 } 146 147 func (m *MSClient) AddPartition(_ context.Context, tableName string, dbName string, newPartition *metastore.Partition) error { 148 key := GetPartitionKey(dbName, tableName, newPartition.Values) 149 if m.partitionMap[key] != nil { 150 return fmt.Errorf("partition %w - key %s", ErrAlreadyExists, key) 151 } 152 m.partitionMap[key] = newPartition 153 return nil 154 } 155 156 func (m *MSClient) DropPartition(_ context.Context, dbName string, tableName string, values []string) error { 157 key := GetPartitionKey(dbName, tableName, values) 158 if m.partitionMap[key] == nil { 159 return fmt.Errorf("partition %w - key %s", ErrNotFound, key) 160 } 161 delete(m.partitionMap, key) 162 return nil 163 } 164 165 func (m *MSClient) CreateDatabase(_ context.Context, db *metastore.Database) error { 166 if _, ok := m.Databases[db.Name]; ok { 167 return mserrors.ErrSchemaExists 168 } 169 m.Databases[db.Name] = db 170 return nil 171 } 172 173 func PartitionsListToMap(partitions []*metastore.Partition) map[string]*metastore.Partition { 174 res := make(map[string]*metastore.Partition) 175 for _, partition := range partitions { 176 res[GetPartitionKey(partition.DBName, partition.TableName, partition.Values)] = partition 177 } 178 return res 179 } 180 181 func AddColumn(m *metastore.Partition, col *metastore.FieldSchema) { 182 m.Sd.Cols = append(m.Sd.Cols, col) 183 } 184 185 func GetByPrefix(m map[string]*metastore.Partition, prefix string) []*metastore.Partition { 186 res := make([]*metastore.Partition, 0) 187 for key, val := range m { 188 if strings.HasPrefix(key, prefix) { 189 res = append(res, val) 190 } 191 } 192 return res 193 } 194 195 func GetKey(dbName, tableName string) string { 196 return fmt.Sprintf("%s-%s", dbName, tableName) 197 } 198 199 func GetPartitionKey(dbName, tableName string, partition []string) string { 200 return fmt.Sprintf("%s-%s-%s", dbName, tableName, strings.Join(partition, "-")) 201 }