write.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package client
  17. import (
  18. "context"
  19. "google.golang.org/grpc"
  20. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  21. "github.com/milvus-io/milvus/client/v2/column"
  22. "github.com/milvus-io/milvus/pkg/util/merr"
  23. )
  24. type InsertResult struct {
  25. InsertCount int64
  26. IDs column.Column
  27. }
  28. func (c *Client) Insert(ctx context.Context, option InsertOption, callOptions ...grpc.CallOption) (InsertResult, error) {
  29. result := InsertResult{}
  30. collection, err := c.getCollection(ctx, option.CollectionName())
  31. if err != nil {
  32. return result, err
  33. }
  34. req, err := option.InsertRequest(collection)
  35. if err != nil {
  36. return result, err
  37. }
  38. err = c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
  39. resp, err := milvusService.Insert(ctx, req, callOptions...)
  40. err = merr.CheckRPCCall(resp, err)
  41. if err != nil {
  42. return err
  43. }
  44. result.InsertCount = resp.GetInsertCnt()
  45. result.IDs, err = column.IDColumns(collection.Schema, resp.GetIDs(), 0, -1)
  46. if err != nil {
  47. return err
  48. }
  49. // write back pks if needed
  50. // pks values shall be written back to struct if receiver field exists
  51. return option.WriteBackPKs(collection.Schema, result.IDs)
  52. })
  53. return result, err
  54. }
  55. type DeleteResult struct {
  56. DeleteCount int64
  57. }
  58. func (c *Client) Delete(ctx context.Context, option DeleteOption, callOptions ...grpc.CallOption) (DeleteResult, error) {
  59. req := option.Request()
  60. result := DeleteResult{}
  61. err := c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
  62. resp, err := milvusService.Delete(ctx, req, callOptions...)
  63. if err = merr.CheckRPCCall(resp, err); err != nil {
  64. return err
  65. }
  66. result.DeleteCount = resp.GetDeleteCnt()
  67. return nil
  68. })
  69. return result, err
  70. }
  71. type UpsertResult struct {
  72. UpsertCount int64
  73. IDs column.Column
  74. }
  75. func (c *Client) Upsert(ctx context.Context, option UpsertOption, callOptions ...grpc.CallOption) (UpsertResult, error) {
  76. result := UpsertResult{}
  77. collection, err := c.getCollection(ctx, option.CollectionName())
  78. if err != nil {
  79. return result, err
  80. }
  81. req, err := option.UpsertRequest(collection)
  82. if err != nil {
  83. return result, err
  84. }
  85. err = c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
  86. resp, err := milvusService.Upsert(ctx, req, callOptions...)
  87. if err = merr.CheckRPCCall(resp, err); err != nil {
  88. return err
  89. }
  90. result.UpsertCount = resp.GetUpsertCnt()
  91. result.IDs, err = column.IDColumns(collection.Schema, resp.GetIDs(), 0, -1)
  92. if err != nil {
  93. return err
  94. }
  95. return nil
  96. })
  97. return result, err
  98. }