File size: 2,636 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package v1

import (
	"fmt"
	"math/big"
	"strings"

	"github.com/weaviate/weaviate/entities/filters"
	"github.com/weaviate/weaviate/entities/schema"
	pb "github.com/weaviate/weaviate/grpc/generated/protocol/v1"
	"github.com/weaviate/weaviate/usecases/objects"
)

func batchDeleteParamsFromProto(req *pb.BatchDeleteRequest, scheme schema.Schema) (objects.BatchDeleteParams, error) {
	params := objects.BatchDeleteParams{}

	// make sure collection exists
	_, err := schema.GetClassByName(scheme.Objects, req.Collection)
	if err != nil {
		return objects.BatchDeleteParams{}, err
	}

	params.ClassName = schema.ClassName(req.Collection)

	if req.Verbose {
		params.Output = "verbose"
	} else {
		params.Output = "minimal"
	}

	params.DryRun = req.DryRun

	if req.Filters == nil {
		return objects.BatchDeleteParams{}, fmt.Errorf("no filters in batch delete request")
	}

	clause, err := extractFilters(req.Filters, scheme, req.Collection)
	if err != nil {
		return objects.BatchDeleteParams{}, err
	}
	filter := &filters.LocalFilter{Root: &clause}
	if err := filters.ValidateFilters(scheme, filter); err != nil {
		return objects.BatchDeleteParams{}, err
	}
	params.Filters = filter

	return params, nil
}

func batchDeleteReplyFromObjects(response objects.BatchDeleteResult, verbose bool) (*pb.BatchDeleteReply, error) {
	var successful, failed int64

	var objs []*pb.BatchDeleteObject
	if verbose {
		objs = make([]*pb.BatchDeleteObject, 0, len(response.Objects))
	}
	for _, obj := range response.Objects {
		if obj.Err == nil {
			successful += 1
		} else {
			failed += 1
		}
		if verbose {
			hexInteger, success := new(big.Int).SetString(strings.Replace(obj.UUID.String(), "-", "", -1), 16)
			if !success {
				return nil, fmt.Errorf("failed to parse hex string to integer")
			}
			errorString := ""
			if obj.Err != nil {
				errorString = obj.Err.Error()
			}

			resultObj := &pb.BatchDeleteObject{
				Uuid:       hexInteger.Bytes(),
				Successful: obj.Err == nil,
				Error:      &errorString,
			}
			objs = append(objs, resultObj)
		}
	}
	reply := &pb.BatchDeleteReply{
		Successful: successful,
		Failed:     failed,
		Matches:    response.Matches,
		Objects:    objs,
	}

	return reply, nil
}