github.com/weaviate/weaviate@v1.24.6/usecases/schema/get.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package schema
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  
    18  	"github.com/weaviate/weaviate/adapters/repos/db/inverted"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  )
    22  
    23  // GetSchema retrieves a locally cached copy of the schema
    24  func (m *Manager) GetSchema(principal *models.Principal) (schema.Schema, error) {
    25  	err := m.Authorizer.Authorize(principal, "list", "schema/*")
    26  	if err != nil {
    27  		return schema.Schema{}, err
    28  	}
    29  
    30  	return m.getSchema(), nil
    31  }
    32  
    33  // GetSchemaSkipAuth can never be used as a response to a user request as it
    34  // could leak the schema to an unauthorized user, is intended to be used for
    35  // non-user triggered processes, such as regular updates / maintenance / etc
    36  func (m *Manager) GetSchemaSkipAuth() schema.Schema { return m.getSchema() }
    37  
    38  func (m *Manager) getSchema() schema.Schema {
    39  	return schema.Schema{
    40  		Objects: m.schemaCache.readOnlySchema(),
    41  	}
    42  }
    43  
    44  func (m *Manager) IndexedInverted(className, propertyName string) bool {
    45  	class := m.getClassByName(className)
    46  	if class == nil {
    47  		return false
    48  	}
    49  	prop, _ := schema.GetPropertyByName(class, propertyName)
    50  	if prop == nil {
    51  		return false
    52  	}
    53  	return inverted.HasInvertedIndex(prop)
    54  }
    55  
    56  func (m *Manager) GetClass(ctx context.Context, principal *models.Principal,
    57  	name string,
    58  ) (*models.Class, error) {
    59  	err := m.Authorizer.Authorize(principal, "list", "schema/*")
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return m.getClassByName(name), nil
    64  }
    65  
    66  func (m *Manager) getClassByName(name string) *models.Class {
    67  	c, _ := m.schemaCache.readOnlyClass(name)
    68  	return c
    69  }
    70  
    71  // ResolveParentNodes gets all replicas for a specific class shard and resolves their names
    72  //
    73  // it returns map[node_name] node_address where node_address = "" if can't resolve node_name
    74  func (m *Manager) ResolveParentNodes(class, shardName string) (map[string]string, error) {
    75  	nodes, err := m.ShardReplicas(class, shardName)
    76  	if err != nil {
    77  		return nil, fmt.Errorf("get replicas from schema: %w", err)
    78  	}
    79  
    80  	if len(nodes) == 0 {
    81  		return nil, nil
    82  	}
    83  
    84  	name2Addr := make(map[string]string, len(nodes))
    85  	for _, node := range nodes {
    86  		if node != "" {
    87  			host, _ := m.clusterState.NodeHostname(node)
    88  			name2Addr[node] = host
    89  		}
    90  	}
    91  	return name2Addr, nil
    92  }
    93  
    94  func (m *Manager) Nodes() []string {
    95  	return m.clusterState.AllNames()
    96  }
    97  
    98  func (m *Manager) NodeName() string {
    99  	return m.clusterState.LocalName()
   100  }
   101  
   102  func (m *Manager) ClusterHealthScore() int {
   103  	return m.clusterState.ClusterHealthScore()
   104  }
   105  
   106  func (m *Manager) GetShardsStatus(ctx context.Context, principal *models.Principal,
   107  	className, tenant string,
   108  ) (models.ShardStatusList, error) {
   109  	err := m.Authorizer.Authorize(principal, "list", fmt.Sprintf("schema/%s/shards", className))
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	shardsStatus, err := m.migrator.GetShardsStatus(ctx, className, tenant)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	shardsQueueSize, err := m.migrator.GetShardsQueueSize(ctx, className, tenant)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	resp := models.ShardStatusList{}
   124  
   125  	for name, status := range shardsStatus {
   126  		resp = append(resp, &models.ShardStatusGetResponse{
   127  			Name:            name,
   128  			Status:          status,
   129  			VectorQueueSize: shardsQueueSize[name],
   130  		})
   131  	}
   132  
   133  	return resp, nil
   134  }