File size: 3,247 Bytes
95d599c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package schema

import (
	"encoding/json"
	"fmt"

	command "github.com/weaviate/weaviate/cluster/proto/api"
	gproto "google.golang.org/protobuf/proto"
)

func (s *SchemaManager) CreateAlias(cmd *command.ApplyRequest) error {
	req := &command.CreateAliasRequest{}
	if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
		return fmt.Errorf("%w: %w", ErrBadRequest, err)
	}

	return s.apply(
		applyOp{
			op:                   cmd.GetType().String(),
			updateSchema:         func() error { return s.schema.createAlias(req.Collection, req.Alias) },
			updateStore:          func() error { return nil /* nothing do to here */ },
			enableSchemaCallback: true,
		},
	)
}

func (s *SchemaManager) ReplaceAlias(cmd *command.ApplyRequest) error {
	req := &command.ReplaceAliasRequest{}
	if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
		return fmt.Errorf("%w: %w", ErrBadRequest, err)
	}

	return s.apply(
		applyOp{
			op:                   cmd.GetType().String(),
			updateSchema:         func() error { return s.schema.replaceAlias(req.Collection, req.Alias) },
			updateStore:          func() error { return nil /* nothing do to here */ },
			enableSchemaCallback: true,
		},
	)
}

func (s *SchemaManager) DeleteAlias(cmd *command.ApplyRequest) error {
	req := &command.DeleteAliasRequest{}
	if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
		return fmt.Errorf("%w: %w", ErrBadRequest, err)
	}

	return s.apply(
		applyOp{
			op:                   cmd.GetType().String(),
			updateSchema:         func() error { return s.schema.deleteAlias(req.Alias) },
			updateStore:          func() error { return nil /* nothing do to here */ },
			enableSchemaCallback: true,
		},
	)
}

func (s *SchemaManager) ResolveAlias(req *command.QueryRequest) ([]byte, error) {
	subCommand := command.QueryResolveAliasRequest{}
	if err := json.Unmarshal(req.SubCommand, &subCommand); err != nil {
		return []byte{}, fmt.Errorf("%w: %w", ErrBadRequest, err)
	}

	rootClass := s.schema.ResolveAlias(subCommand.Alias)
	if rootClass == "" {
		return nil, fmt.Errorf("resolve alias: %s, %w", subCommand.Alias, ErrAliasNotFound)
	}

	response := command.QueryResolveAliasResponse{
		Class: rootClass,
	}
	payload, err := json.Marshal(&response)
	if err != nil {
		return []byte{}, fmt.Errorf("could not marshal resolve alias response: %w", err)
	}
	return payload, nil
}

func (s *SchemaManager) GetAliases(req *command.QueryRequest) ([]byte, error) {
	subCommand := command.QueryGetAliasesRequest{}
	if err := json.Unmarshal(req.SubCommand, &subCommand); err != nil {
		return []byte{}, fmt.Errorf("%w: %w", ErrBadRequest, err)
	}

	response := command.QueryGetAliasesResponse{
		Aliases: s.schema.getAliases(subCommand.Alias, subCommand.Class),
	}
	payload, err := json.Marshal(&response)
	if err != nil {
		return []byte{}, fmt.Errorf("could not marshal get aliases response: %w", err)
	}
	return payload, nil
}