Skip to content

Commit

Permalink
change to receiving and passing context.Context by value rather than …
Browse files Browse the repository at this point in the history
…a pointer in IndexConnection and its methods, update README
  • Loading branch information
austin-denoble committed May 1, 2024
1 parent 666cf72 commit 94df002
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
```
Expand Down Expand Up @@ -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
Expand All @@ -76,13 +78,14 @@ func main() {
```

## Support

To get help using go-pinecone, reach out to [email protected].

## 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/)

Expand All @@ -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
```
Expand Down
43 changes: 22 additions & 21 deletions pinecone/index_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
35 changes: 18 additions & 17 deletions pinecone/index_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -189,45 +190,45 @@ 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
}

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
}

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
}

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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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)
}

0 comments on commit 94df002

Please sign in to comment.