github.com/weaviate/weaviate@v1.24.6/usecases/objects/manager.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 provides managers for all kind-related items, such as objects.
    13  // Manager provides methods for "regular" interaction, such as
    14  // add, get, delete, update, etc. Additionally BatchManager allows for
    15  // efficient batch-adding of object instances and references.
    16  package objects
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/go-openapi/strfmt"
    24  	"github.com/google/uuid"
    25  	"github.com/sirupsen/logrus"
    26  	"github.com/weaviate/weaviate/entities/additional"
    27  	"github.com/weaviate/weaviate/entities/filters"
    28  	"github.com/weaviate/weaviate/entities/models"
    29  	"github.com/weaviate/weaviate/entities/modulecapabilities"
    30  	"github.com/weaviate/weaviate/entities/moduletools"
    31  	"github.com/weaviate/weaviate/entities/schema/crossref"
    32  	"github.com/weaviate/weaviate/entities/search"
    33  	"github.com/weaviate/weaviate/usecases/config"
    34  )
    35  
    36  // Manager manages kind changes at a use-case level, i.e. agnostic of
    37  // underlying databases or storage providers
    38  type Manager struct {
    39  	config            *config.WeaviateConfig
    40  	locks             locks
    41  	schemaManager     schemaManager
    42  	logger            logrus.FieldLogger
    43  	authorizer        authorizer
    44  	vectorRepo        VectorRepo
    45  	timeSource        timeSource
    46  	modulesProvider   ModulesProvider
    47  	autoSchemaManager *autoSchemaManager
    48  	metrics           objectsMetrics
    49  }
    50  
    51  type objectsMetrics interface {
    52  	BatchInc()
    53  	BatchDec()
    54  	BatchRefInc()
    55  	BatchRefDec()
    56  	BatchDeleteInc()
    57  	BatchDeleteDec()
    58  	AddObjectInc()
    59  	AddObjectDec()
    60  	UpdateObjectInc()
    61  	UpdateObjectDec()
    62  	MergeObjectInc()
    63  	MergeObjectDec()
    64  	DeleteObjectInc()
    65  	DeleteObjectDec()
    66  	GetObjectInc()
    67  	GetObjectDec()
    68  	HeadObjectInc()
    69  	HeadObjectDec()
    70  	AddReferenceInc()
    71  	AddReferenceDec()
    72  	UpdateReferenceInc()
    73  	UpdateReferenceDec()
    74  	DeleteReferenceInc()
    75  	DeleteReferenceDec()
    76  	AddUsageDimensions(className, queryType, operation string, dims int)
    77  }
    78  
    79  type timeSource interface {
    80  	Now() int64
    81  }
    82  
    83  type locks interface {
    84  	LockConnector() (func() error, error)
    85  	LockSchema() (func() error, error)
    86  }
    87  
    88  type authorizer interface {
    89  	Authorize(principal *models.Principal, verb, resource string) error
    90  }
    91  
    92  type VectorRepo interface {
    93  	PutObject(ctx context.Context, concept *models.Object, vector []float32, vectors models.Vectors,
    94  		repl *additional.ReplicationProperties) error
    95  	DeleteObject(ctx context.Context, className string, id strfmt.UUID,
    96  		repl *additional.ReplicationProperties, tenant string) error
    97  	// Object returns object of the specified class giving by its id
    98  	Object(ctx context.Context, class string, id strfmt.UUID, props search.SelectProperties,
    99  		additional additional.Properties, repl *additional.ReplicationProperties,
   100  		tenant string) (*search.Result, error)
   101  	// Exists returns true if an object of a giving class exists
   102  	Exists(ctx context.Context, class string, id strfmt.UUID,
   103  		repl *additional.ReplicationProperties, tenant string) (bool, error)
   104  	ObjectByID(ctx context.Context, id strfmt.UUID, props search.SelectProperties,
   105  		additional additional.Properties, tenant string) (*search.Result, error)
   106  	ObjectSearch(ctx context.Context, offset, limit int, filters *filters.LocalFilter,
   107  		sort []filters.Sort, additional additional.Properties, tenant string) (search.Results, error)
   108  	AddReference(ctx context.Context, source *crossref.RefSource,
   109  		target *crossref.Ref, repl *additional.ReplicationProperties, tenant string) error
   110  	Merge(ctx context.Context, merge MergeDocument, repl *additional.ReplicationProperties, tenant string) error
   111  	Query(context.Context, *QueryInput) (search.Results, *Error)
   112  }
   113  
   114  type ModulesProvider interface {
   115  	GetObjectAdditionalExtend(ctx context.Context, in *search.Result,
   116  		moduleParams map[string]interface{}) (*search.Result, error)
   117  	ListObjectsAdditionalExtend(ctx context.Context, in search.Results,
   118  		moduleParams map[string]interface{}) (search.Results, error)
   119  	UsingRef2Vec(className string) bool
   120  	UpdateVector(ctx context.Context, object *models.Object, class *models.Class,
   121  		compFactory moduletools.PropsComparatorFactory, repo modulecapabilities.FindObjectFn,
   122  		logger logrus.FieldLogger) error
   123  	VectorizerName(className string) (string, error)
   124  }
   125  
   126  // NewManager creates a new manager
   127  func NewManager(locks locks, schemaManager schemaManager,
   128  	config *config.WeaviateConfig, logger logrus.FieldLogger,
   129  	authorizer authorizer, vectorRepo VectorRepo,
   130  	modulesProvider ModulesProvider, metrics objectsMetrics,
   131  ) *Manager {
   132  	return &Manager{
   133  		config:            config,
   134  		locks:             locks,
   135  		schemaManager:     schemaManager,
   136  		logger:            logger,
   137  		authorizer:        authorizer,
   138  		vectorRepo:        vectorRepo,
   139  		timeSource:        defaultTimeSource{},
   140  		modulesProvider:   modulesProvider,
   141  		autoSchemaManager: newAutoSchemaManager(schemaManager, vectorRepo, config, logger),
   142  		metrics:           metrics,
   143  	}
   144  }
   145  
   146  func generateUUID() (strfmt.UUID, error) {
   147  	id, err := uuid.NewRandom()
   148  	if err != nil {
   149  		return "", fmt.Errorf("could not generate uuid v4: %v", err)
   150  	}
   151  
   152  	return strfmt.UUID(id.String()), nil
   153  }
   154  
   155  type defaultTimeSource struct{}
   156  
   157  func (ts defaultTimeSource) Now() int64 {
   158  	return time.Now().UnixNano() / int64(time.Millisecond)
   159  }