Skip to content

Commit

Permalink
implement kv keys method for gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Feb 28, 2024
1 parent eda49f9 commit 26b5835
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion cloud/gcp/runtime/keyvalue/firestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package keyvalue
import (
"context"
"fmt"
"strings"

"github.com/nitrictech/nitric/core/pkg/decorators/keyvalue"
grpc_errors "github.com/nitrictech/nitric/core/pkg/grpc/errors"
v1 "github.com/nitrictech/nitric/core/pkg/proto/keyvalue/v1"

"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/structpb"

Expand Down Expand Up @@ -164,6 +166,53 @@ func (s *FirestoreDocService) Delete(ctx context.Context, req *v1.KeyValueDelete
return &v1.KeyValueDeleteResponse{}, nil
}

func (s *FirestoreDocService) Keys(req *v1.KeyValueKeysRequest, stream v1.KeyValue_KeysServer) error {
newErr := grpc_errors.ErrorsWithScope("FirestoreDocService.Keys")
storeName := req.GetStore().GetName()

if storeName == "" {
return newErr(
codes.InvalidArgument,
"store name is required",
nil,
)
}

iter := s.getCollectionRef(storeName).DocumentRefs(stream.Context())

for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return newErr(
codes.Internal,
"error iterating over firestore collection",
err,
)
}

// Where <= doesn't appear to work where querying based on document ID.
// so instead we filter the results here
if !strings.HasPrefix(doc.ID, req.Prefix) {
continue
}

if err := stream.Send(&v1.KeyValueKeysResponse{
Key: doc.ID,
}); err != nil {
return newErr(
codes.Internal,
"failed to send response",
err,
)
}
}

return nil
}

func New() (v1.KeyValueServer, error) {
ctx := context.Background()

Expand All @@ -189,5 +238,9 @@ func NewWithClient(client *firestore.Client) (v1.KeyValueServer, error) {
}

func (s *FirestoreDocService) getDocRef(ref *v1.ValueRef) *firestore.DocumentRef {
return s.client.Collection(ref.Store).Doc(ref.Key)
return s.getCollectionRef(ref.Store).Doc(ref.Key)
}

func (s *FirestoreDocService) getCollectionRef(store string) *firestore.CollectionRef {
return s.client.Collection(store)
}

0 comments on commit 26b5835

Please sign in to comment.