github.com/weaviate/weaviate@v1.24.6/usecases/objects/head.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 objects
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  
    18  	"github.com/go-openapi/strfmt"
    19  	"github.com/weaviate/weaviate/entities/additional"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  )
    22  
    23  // HeadObject check object's existence in the connected DB
    24  func (m *Manager) HeadObject(ctx context.Context, principal *models.Principal, class string,
    25  	id strfmt.UUID, repl *additional.ReplicationProperties, tenant string,
    26  ) (bool, *Error) {
    27  	path := fmt.Sprintf("objects/%s", id)
    28  	if class != "" {
    29  		path = fmt.Sprintf("objects/%s/%s", class, id)
    30  	}
    31  	if err := m.authorizer.Authorize(principal, "head", path); err != nil {
    32  		return false, &Error{path, StatusForbidden, err}
    33  	}
    34  
    35  	unlock, err := m.locks.LockConnector()
    36  	if err != nil {
    37  		return false, &Error{"cannot lock", StatusInternalServerError, err}
    38  	}
    39  	defer unlock()
    40  
    41  	m.metrics.HeadObjectInc()
    42  	defer m.metrics.HeadObjectDec()
    43  
    44  	ok, err := m.vectorRepo.Exists(ctx, class, id, repl, tenant)
    45  	if err != nil {
    46  		switch err.(type) {
    47  		case ErrMultiTenancy:
    48  			return false, &Error{"repo.exists", StatusUnprocessableEntity, err}
    49  		default:
    50  			return false, &Error{"repo.exists", StatusInternalServerError, err}
    51  		}
    52  	}
    53  	return ok, nil
    54  }