github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/database.go (about)

     1  // Licensed to the LF AI & Data foundation under one
     2  // or more contributor license agreements. See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership. The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License. You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/cockroachdb/errors"
    23  	"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    24  	"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
    25  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    26  )
    27  
    28  // UsingDatabase for database operation after this function call.
    29  // All request in any goroutine will be applied to new database on the same client. e.g.
    30  // 1. goroutine A access DB1.
    31  // 2. goroutine B call UsingDatabase(ctx, "DB2").
    32  // 3. goroutine A access DB2 after 2.
    33  func (c *GrpcClient) UsingDatabase(ctx context.Context, dbName string) error {
    34  	c.config.useDatabase(dbName)
    35  	err := c.connectInternal(ctx)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // CreateDatabase creates a new database for remote Milvus cluster.
    44  // TODO:New options can be added as expanding parameters.
    45  func (c *GrpcClient) CreateDatabase(ctx context.Context, dbName string, opts ...CreateDatabaseOption) error {
    46  	if c.Service == nil {
    47  		return ErrClientNotReady
    48  	}
    49  	if c.config.hasFlags(disableDatabase) {
    50  		return ErrFeatureNotSupported
    51  	}
    52  	req := &milvuspb.CreateDatabaseRequest{
    53  		DbName: dbName,
    54  	}
    55  	for _, opt := range opts {
    56  		opt(req)
    57  	}
    58  	resp, err := c.Service.CreateDatabase(ctx, req)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	return handleRespStatus(resp)
    63  }
    64  
    65  // ListDatabases list all database in milvus cluster.
    66  func (c *GrpcClient) ListDatabases(ctx context.Context) ([]entity.Database, error) {
    67  	if c.Service == nil {
    68  		return nil, ErrClientNotReady
    69  	}
    70  	if c.config.hasFlags(disableDatabase) {
    71  		return nil, ErrFeatureNotSupported
    72  	}
    73  
    74  	req := &milvuspb.ListDatabasesRequest{}
    75  	resp, err := c.Service.ListDatabases(ctx, req)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	if err = handleRespStatus(resp.GetStatus()); err != nil {
    80  		return nil, err
    81  	}
    82  	databases := make([]entity.Database, len(resp.GetDbNames()))
    83  	for i, dbName := range resp.GetDbNames() {
    84  		databases[i] = entity.Database{
    85  			Name: dbName,
    86  		}
    87  	}
    88  	return databases, nil
    89  }
    90  
    91  // DropDatabase drop all database in milvus cluster.
    92  func (c *GrpcClient) DropDatabase(ctx context.Context, dbName string, opts ...DropDatabaseOption) error {
    93  	if c.Service == nil {
    94  		return ErrClientNotReady
    95  	}
    96  	if c.config.hasFlags(disableDatabase) {
    97  		return ErrFeatureNotSupported
    98  	}
    99  
   100  	req := &milvuspb.DropDatabaseRequest{
   101  		DbName: dbName,
   102  	}
   103  	for _, opt := range opts {
   104  		opt(req)
   105  	}
   106  	resp, err := c.Service.DropDatabase(ctx, req)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	return handleRespStatus(resp)
   111  }
   112  
   113  // AlterDatabase changes the database attribute.
   114  func (c *GrpcClient) AlterDatabase(ctx context.Context, dbName string, attrs ...entity.DatabaseAttribute) error {
   115  	if c.Service == nil {
   116  		return ErrClientNotReady
   117  	}
   118  
   119  	if c.config.hasFlags(disableDatabase) {
   120  		return ErrFeatureNotSupported
   121  	}
   122  
   123  	if len(attrs) == 0 {
   124  		return errors.New("no database attribute provided")
   125  	}
   126  
   127  	keys := make(map[string]struct{})
   128  	props := make([]*commonpb.KeyValuePair, 0, len(attrs))
   129  	for _, attr := range attrs {
   130  		k, v := attr.KeyValue()
   131  		if _, exists := keys[k]; exists {
   132  			return errors.New("duplicated attributed received")
   133  		}
   134  		keys[k] = struct{}{}
   135  		props = append(props, &commonpb.KeyValuePair{
   136  			Key:   k,
   137  			Value: v,
   138  		})
   139  	}
   140  
   141  	req := &milvuspb.AlterDatabaseRequest{
   142  		DbName:     dbName,
   143  		Properties: props,
   144  	}
   145  
   146  	resp, err := c.Service.AlterDatabase(ctx, req)
   147  	if err != nil {
   148  		return err
   149  	}
   150  	return handleRespStatus(resp)
   151  }
   152  
   153  // DropDatabase drop all database in milvus cluster.
   154  func (c *GrpcClient) DescribeDatabase(ctx context.Context, dbName string) (*entity.Database, error) {
   155  	if c.Service == nil {
   156  		return nil, ErrClientNotReady
   157  	}
   158  	if c.config.hasFlags(disableDatabase) {
   159  		return nil, ErrFeatureNotSupported
   160  	}
   161  
   162  	req := &milvuspb.DescribeDatabaseRequest{
   163  		DbName: dbName,
   164  	}
   165  
   166  	resp, err := c.Service.DescribeDatabase(ctx, req)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	err = handleRespStatus(resp.GetStatus())
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	database := &entity.Database{
   175  		Name:       resp.GetDbName(),
   176  		Properties: resp.GetProperties(),
   177  	}
   178  
   179  	return database, nil
   180  }