github.com/weaviate/weaviate@v1.24.6/usecases/objects/delete.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  // DeleteObject Class Instance from the connected DB
    24  //
    25  // if class == "" it will delete all object with same id regardless of the class name.
    26  // This is due to backward compatibility reasons and should be removed in the future
    27  func (m *Manager) DeleteObject(ctx context.Context,
    28  	principal *models.Principal, class string, id strfmt.UUID,
    29  	repl *additional.ReplicationProperties, tenant string,
    30  ) error {
    31  	path := fmt.Sprintf("objects/%s/%s", class, id)
    32  	if class == "" {
    33  		path = fmt.Sprintf("objects/%s", id)
    34  	}
    35  	err := m.authorizer.Authorize(principal, "delete", path)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	unlock, err := m.locks.LockConnector()
    41  	if err != nil {
    42  		return NewErrInternal("could not acquire lock: %v", err)
    43  	}
    44  	defer unlock()
    45  
    46  	m.metrics.DeleteObjectInc()
    47  	defer m.metrics.DeleteObjectDec()
    48  
    49  	if class == "" { // deprecated
    50  		return m.deleteObjectFromRepo(ctx, id)
    51  	}
    52  
    53  	ok, err := m.vectorRepo.Exists(ctx, class, id, repl, tenant)
    54  	if err != nil {
    55  		switch err.(type) {
    56  		case ErrMultiTenancy:
    57  			return NewErrMultiTenancy(fmt.Errorf("check object existence: %w", err))
    58  		default:
    59  			return NewErrInternal("check object existence: %v", err)
    60  		}
    61  	}
    62  	if !ok {
    63  		return NewErrNotFound("object %v could not be found", path)
    64  	}
    65  
    66  	err = m.vectorRepo.DeleteObject(ctx, class, id, repl, tenant)
    67  	if err != nil {
    68  		return NewErrInternal("could not delete object from vector repo: %v", err)
    69  	}
    70  	return nil
    71  }
    72  
    73  // deleteObjectFromRepo deletes objects with same id and different classes.
    74  //
    75  // Deprecated
    76  func (m *Manager) deleteObjectFromRepo(ctx context.Context, id strfmt.UUID) error {
    77  	// There might be a situation to have UUIDs which are not unique across classes.
    78  	// Added loop in order to delete all of the objects with given UUID across all classes.
    79  	// This change is added in response to this issue:
    80  	// https://github.com/weaviate/weaviate/issues/1836
    81  	deleteCounter := 0
    82  	for {
    83  		objectRes, err := m.getObjectFromRepo(ctx, "", id, additional.Properties{}, nil, "")
    84  		if err != nil {
    85  			_, ok := err.(ErrNotFound)
    86  			if ok {
    87  				if deleteCounter == 0 {
    88  					return err
    89  				}
    90  				return nil
    91  			}
    92  			return err
    93  		}
    94  
    95  		object := objectRes.Object()
    96  		err = m.vectorRepo.DeleteObject(ctx, object.Class, id, nil, "")
    97  		if err != nil {
    98  			return NewErrInternal("could not delete object from vector repo: %v", err)
    99  		}
   100  		deleteCounter++
   101  	}
   102  }