From 32391bcb7b862570139e272797ade4ecca50aab3 Mon Sep 17 00:00:00 2001 From: Austin DeNoble Date: Wed, 1 May 2024 17:24:28 -0400 Subject: [PATCH] Receive and pass `context.Context` by value in `IndexConnection` methods (#20) ## Problem From this issue: https://github.com/pinecone-io/go-pinecone/issues/19 We're receiving and passing `*context.Context` in `IndexConnection` methods. This differs from our `Client` methods and is non-standard for how most libraries handle `Context`. ## Solution - Swap to receiving and passing `context.Context` by value rather than a pointer in `IndexConnection` methods, update README example to account. ## Type of Change - [X] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [ ] None of the above: (explain here) ## Test Plan CI Testing, make sure you can `just test` locally or `go build` --- README.md | 18 ++++++++----- pinecone/index_connection.go | 43 ++++++++++++++++--------------- pinecone/index_connection_test.go | 35 +++++++++++++------------ 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 7efc051..cdc7c40 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ > **Warning** -> +> > **Under active development** This SDK is pre-1.0 and should be considered unstable. Before a 1.0 release, there are > no guarantees of backward compatibility between minor versions. @@ -8,18 +8,20 @@ Official Pinecone Go Client ## Features -go-pinecone contains -* gRPC bindings for Data Plane operations on Vectors -* REST bindings for Control Plane operations on Indexes and Collections +go-pinecone contains + +- gRPC bindings for Data Plane operations on Vectors +- REST bindings for Control Plane operations on Indexes and Collections See [Pinecone API Docs](https://docs.pinecone.io/reference/) for more info. - ## Installation + go-pinecone requires a Go version with [modules](https://github.com/golang/go/wiki/Modules) support. To add a dependency on go-pinecone: + ```shell go get github.com/pinecone-io/go-pinecone/pinecone ``` @@ -65,7 +67,7 @@ func main() { return } - res, err := idx.DescribeIndexStats(&ctx) + res, err := idx.DescribeIndexStats(ctx) if err != nil { fmt.Println("Error:", err) return @@ -76,13 +78,14 @@ func main() { ``` ## Support + To get help using go-pinecone, reach out to support@pinecone.io. ## Development ### Prereqs -1. A [current version of Go](https://go.dev/doc/install) (recommended 1.21+) +1. A [current version of Go](https://go.dev/doc/install) (recommended 1.21+) 2. The [just](https://github.com/casey/just?tab=readme-ov-file#installation) command runner 3. The [protobuf-compiler](https://grpc.io/docs/protoc-installation/) @@ -96,6 +99,7 @@ and one serverless index. Copy the api key and index names to a `.env` file. See ### API Definitions submodule The API Definitions are in a private submodule. To checkout or update the submodules execute in the root of the project: + ```shell git submodule update --init --recursive ``` diff --git a/pinecone/index_connection.go b/pinecone/index_connection.go index 59fc5c8..9a2131e 100644 --- a/pinecone/index_connection.go +++ b/pinecone/index_connection.go @@ -4,12 +4,13 @@ import ( "context" "crypto/tls" "fmt" + "log" + "github.com/pinecone-io/go-pinecone/internal/gen/data" "github.com/pinecone-io/go-pinecone/internal/useragent" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/metadata" - "log" ) type IndexConnection struct { @@ -46,7 +47,7 @@ func (idx *IndexConnection) Close() error { return err } -func (idx *IndexConnection) UpsertVectors(ctx *context.Context, in []*Vector) (uint32, error) { +func (idx *IndexConnection) UpsertVectors(ctx context.Context, in []*Vector) (uint32, error) { vectors := make([]*data.Vector, len(in)) for i, v := range in { vectors[i] = vecToGrpc(v) @@ -57,7 +58,7 @@ func (idx *IndexConnection) UpsertVectors(ctx *context.Context, in []*Vector) (u Namespace: idx.Namespace, } - res, err := (*idx.dataClient).Upsert(idx.akCtx(*ctx), req) + res, err := (*idx.dataClient).Upsert(idx.akCtx(ctx), req) if err != nil { return 0, err } @@ -69,13 +70,13 @@ type FetchVectorsResponse struct { Usage *Usage } -func (idx *IndexConnection) FetchVectors(ctx *context.Context, ids []string) (*FetchVectorsResponse, error) { +func (idx *IndexConnection) FetchVectors(ctx context.Context, ids []string) (*FetchVectorsResponse, error) { req := &data.FetchRequest{ Ids: ids, Namespace: idx.Namespace, } - res, err := (*idx.dataClient).Fetch(idx.akCtx(*ctx), req) + res, err := (*idx.dataClient).Fetch(idx.akCtx(ctx), req) if err != nil { return nil, err } @@ -103,14 +104,14 @@ type ListVectorsResponse struct { NextPaginationToken *string } -func (idx *IndexConnection) ListVectors(ctx *context.Context, in *ListVectorsRequest) (*ListVectorsResponse, error) { +func (idx *IndexConnection) ListVectors(ctx context.Context, in *ListVectorsRequest) (*ListVectorsResponse, error) { req := &data.ListRequest{ Prefix: in.Prefix, Limit: in.Limit, PaginationToken: in.PaginationToken, Namespace: idx.Namespace, } - res, err := (*idx.dataClient).List(idx.akCtx(*ctx), req) + res, err := (*idx.dataClient).List(idx.akCtx(ctx), req) if err != nil { return nil, err } @@ -141,7 +142,7 @@ type QueryVectorsResponse struct { Usage *Usage } -func (idx *IndexConnection) QueryByVectorValues(ctx *context.Context, in *QueryByVectorValuesRequest) (*QueryVectorsResponse, error) { +func (idx *IndexConnection) QueryByVectorValues(ctx context.Context, in *QueryByVectorValuesRequest) (*QueryVectorsResponse, error) { req := &data.QueryRequest{ Namespace: idx.Namespace, TopK: in.TopK, @@ -164,7 +165,7 @@ type QueryByVectorIdRequest struct { SparseValues *SparseValues } -func (idx *IndexConnection) QueryByVectorId(ctx *context.Context, in *QueryByVectorIdRequest) (*QueryVectorsResponse, error) { +func (idx *IndexConnection) QueryByVectorId(ctx context.Context, in *QueryByVectorIdRequest) (*QueryVectorsResponse, error) { req := &data.QueryRequest{ Id: in.VectorId, Namespace: idx.Namespace, @@ -178,7 +179,7 @@ func (idx *IndexConnection) QueryByVectorId(ctx *context.Context, in *QueryByVec return idx.query(ctx, req) } -func (idx *IndexConnection) DeleteVectorsById(ctx *context.Context, ids []string) error { +func (idx *IndexConnection) DeleteVectorsById(ctx context.Context, ids []string) error { req := data.DeleteRequest{ Ids: ids, Namespace: idx.Namespace, @@ -187,7 +188,7 @@ func (idx *IndexConnection) DeleteVectorsById(ctx *context.Context, ids []string return idx.delete(ctx, &req) } -func (idx *IndexConnection) DeleteVectorsByFilter(ctx *context.Context, filter *Filter) error { +func (idx *IndexConnection) DeleteVectorsByFilter(ctx context.Context, filter *Filter) error { req := data.DeleteRequest{ Filter: filter, Namespace: idx.Namespace, @@ -196,7 +197,7 @@ func (idx *IndexConnection) DeleteVectorsByFilter(ctx *context.Context, filter * return idx.delete(ctx, &req) } -func (idx *IndexConnection) DeleteAllVectorsInNamespace(ctx *context.Context) error { +func (idx *IndexConnection) DeleteAllVectorsInNamespace(ctx context.Context) error { req := data.DeleteRequest{ Namespace: idx.Namespace, DeleteAll: true, @@ -212,7 +213,7 @@ type UpdateVectorRequest struct { Metadata *Metadata } -func (idx *IndexConnection) UpdateVector(ctx *context.Context, in *UpdateVectorRequest) error { +func (idx *IndexConnection) UpdateVector(ctx context.Context, in *UpdateVectorRequest) error { req := &data.UpdateRequest{ Id: in.Id, Values: in.Values, @@ -221,7 +222,7 @@ func (idx *IndexConnection) UpdateVector(ctx *context.Context, in *UpdateVectorR Namespace: idx.Namespace, } - _, err := (*idx.dataClient).Update(idx.akCtx(*ctx), req) + _, err := (*idx.dataClient).Update(idx.akCtx(ctx), req) return err } @@ -232,15 +233,15 @@ type DescribeIndexStatsResponse struct { Namespaces map[string]*NamespaceSummary } -func (idx *IndexConnection) DescribeIndexStats(ctx *context.Context) (*DescribeIndexStatsResponse, error) { +func (idx *IndexConnection) DescribeIndexStats(ctx context.Context) (*DescribeIndexStatsResponse, error) { return idx.DescribeIndexStatsFiltered(ctx, nil) } -func (idx *IndexConnection) DescribeIndexStatsFiltered(ctx *context.Context, filter *Filter) (*DescribeIndexStatsResponse, error) { +func (idx *IndexConnection) DescribeIndexStatsFiltered(ctx context.Context, filter *Filter) (*DescribeIndexStatsResponse, error) { req := &data.DescribeIndexStatsRequest{ Filter: filter, } - res, err := (*idx.dataClient).DescribeIndexStats(idx.akCtx(*ctx), req) + res, err := (*idx.dataClient).DescribeIndexStats(idx.akCtx(ctx), req) if err != nil { return nil, err } @@ -260,8 +261,8 @@ func (idx *IndexConnection) DescribeIndexStatsFiltered(ctx *context.Context, fil }, nil } -func (idx *IndexConnection) query(ctx *context.Context, req *data.QueryRequest) (*QueryVectorsResponse, error) { - res, err := (*idx.dataClient).Query(idx.akCtx(*ctx), req) +func (idx *IndexConnection) query(ctx context.Context, req *data.QueryRequest) (*QueryVectorsResponse, error) { + res, err := (*idx.dataClient).Query(idx.akCtx(ctx), req) if err != nil { return nil, err } @@ -277,8 +278,8 @@ func (idx *IndexConnection) query(ctx *context.Context, req *data.QueryRequest) }, nil } -func (idx *IndexConnection) delete(ctx *context.Context, req *data.DeleteRequest) error { - _, err := (*idx.dataClient).Delete(idx.akCtx(*ctx), req) +func (idx *IndexConnection) delete(ctx context.Context, req *data.DeleteRequest) error { + _, err := (*idx.dataClient).Delete(idx.akCtx(ctx), req) return err } diff --git a/pinecone/index_connection_test.go b/pinecone/index_connection_test.go index 7449160..ffdca5f 100644 --- a/pinecone/index_connection_test.go +++ b/pinecone/index_connection_test.go @@ -3,11 +3,12 @@ package pinecone import ( "context" "fmt" + "os" + "testing" + "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "os" - "testing" ) type IndexConnectionTests struct { @@ -124,14 +125,14 @@ func (ts *IndexConnectionTests) TestNewIndexConnectionNamespace() { func (ts *IndexConnectionTests) TestFetchVectors() { ctx := context.Background() - res, err := ts.idxConn.FetchVectors(&ctx, ts.vectorIds) + res, err := ts.idxConn.FetchVectors(ctx, ts.vectorIds) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } func (ts *IndexConnectionTests) TestFetchVectorsSourceTag() { ctx := context.Background() - res, err := ts.idxConnSourceTag.FetchVectors(&ctx, ts.vectorIds) + res, err := ts.idxConnSourceTag.FetchVectors(ctx, ts.vectorIds) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -148,7 +149,7 @@ func (ts *IndexConnectionTests) TestQueryByVector() { } ctx := context.Background() - res, err := ts.idxConn.QueryByVectorValues(&ctx, req) + res, err := ts.idxConn.QueryByVectorValues(ctx, req) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -165,7 +166,7 @@ func (ts *IndexConnectionTests) TestQueryByVectorSourceTag() { } ctx := context.Background() - res, err := ts.idxConnSourceTag.QueryByVectorValues(&ctx, req) + res, err := ts.idxConnSourceTag.QueryByVectorValues(ctx, req) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -177,7 +178,7 @@ func (ts *IndexConnectionTests) TestQueryById() { } ctx := context.Background() - res, err := ts.idxConn.QueryByVectorId(&ctx, req) + res, err := ts.idxConn.QueryByVectorId(ctx, req) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -189,14 +190,14 @@ func (ts *IndexConnectionTests) TestQueryByIdSourceTag() { } ctx := context.Background() - res, err := ts.idxConnSourceTag.QueryByVectorId(&ctx, req) + res, err := ts.idxConnSourceTag.QueryByVectorId(ctx, req) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } func (ts *IndexConnectionTests) TestDeleteVectorsById() { ctx := context.Background() - err := ts.idxConn.DeleteVectorsById(&ctx, ts.vectorIds) + err := ts.idxConn.DeleteVectorsById(ctx, ts.vectorIds) assert.NoError(ts.T(), err) ts.loadData() //reload deleted data @@ -204,7 +205,7 @@ func (ts *IndexConnectionTests) TestDeleteVectorsById() { func (ts *IndexConnectionTests) TestDeleteVectorsByFilter() { ctx := context.Background() - err := ts.idxConn.DeleteVectorsByFilter(&ctx, &Filter{}) + err := ts.idxConn.DeleteVectorsByFilter(ctx, &Filter{}) assert.NoError(ts.T(), err) ts.loadData() //reload deleted data @@ -212,7 +213,7 @@ func (ts *IndexConnectionTests) TestDeleteVectorsByFilter() { func (ts *IndexConnectionTests) TestDeleteAllVectorsInNamespace() { ctx := context.Background() - err := ts.idxConn.DeleteAllVectorsInNamespace(&ctx) + err := ts.idxConn.DeleteAllVectorsInNamespace(ctx) assert.NoError(ts.T(), err) ts.loadData() //reload deleted data @@ -220,14 +221,14 @@ func (ts *IndexConnectionTests) TestDeleteAllVectorsInNamespace() { func (ts *IndexConnectionTests) TestDescribeIndexStats() { ctx := context.Background() - res, err := ts.idxConn.DescribeIndexStats(&ctx) + res, err := ts.idxConn.DescribeIndexStats(ctx) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } func (ts *IndexConnectionTests) TestDescribeIndexStatsFiltered() { ctx := context.Background() - res, err := ts.idxConn.DescribeIndexStatsFiltered(&ctx, &Filter{}) + res, err := ts.idxConn.DescribeIndexStatsFiltered(ctx, &Filter{}) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -237,7 +238,7 @@ func (ts *IndexConnectionTests) TestListVectors() { req := &ListVectorsRequest{} ctx := context.Background() - res, err := ts.idxConn.ListVectors(&ctx, req) + res, err := ts.idxConn.ListVectors(ctx, req) assert.NoError(ts.T(), err) assert.NotNil(ts.T(), res) } @@ -263,7 +264,7 @@ func (ts *IndexConnectionTests) loadData() { } ctx := context.Background() - _, err := ts.idxConn.UpsertVectors(&ctx, vectors) + _, err := ts.idxConn.UpsertVectors(ctx, vectors) assert.NoError(ts.T(), err) } @@ -288,12 +289,12 @@ func (ts *IndexConnectionTests) loadDataSourceTag() { } ctx := context.Background() - _, err := ts.idxConnSourceTag.UpsertVectors(&ctx, vectors) + _, err := ts.idxConnSourceTag.UpsertVectors(ctx, vectors) assert.NoError(ts.T(), err) } func (ts *IndexConnectionTests) truncateData() { ctx := context.Background() - err := ts.idxConn.DeleteAllVectorsInNamespace(&ctx) + err := ts.idxConn.DeleteAllVectorsInNamespace(ctx) assert.NoError(ts.T(), err) }