Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get multi records by keys #410

Merged
18 commits merged into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,665 changes: 993 additions & 672 deletions api/open_saves.pb.go

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions api/open_saves.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ syntax = "proto3";

package opensaves;

import "google/protobuf/any.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

Expand All @@ -42,6 +43,9 @@ service OpenSaves {
// GetRecord returns a record with the specified key.
rpc GetRecord(GetRecordRequest) returns (Record) {}

// GetRecords fetches multiple records by keys.
rpc GetRecords(GetRecordsRequest) returns (GetRecordsResponse) {}

// QueryRecords performs a query and returns matching records.
rpc QueryRecords(QueryRecordsRequest) returns (QueryRecordsResponse) {}

Expand Down Expand Up @@ -343,6 +347,14 @@ message GetRecordRequest {
Hint hint = 3;
}

message GetRecordsRequest {
// The keys of the stores the records belongs to.
repeated string store_keys = 1;

// The keys of the records to get.
repeated string keys = 2;
}

// QueryRecordsRequest contains conditions to search particular records.
// Multiple conditions are AND'ed together.
message QueryRecordsRequest {
Expand Down Expand Up @@ -430,6 +442,21 @@ message SortOrder {
string user_property_name = 4;
}

message Status {
uint32 code = 1;
string message = 2;
repeated google.protobuf.Any details = 3;
}

message GetRecordsResponse {
message Result {
Status status = 1;
string store_key = 2;
Record record = 3;
}
repeated Result results = 1;
}

message QueryRecordsResponse {
// List of records that match the criteria.
repeated Record records = 1;
Expand Down
40 changes: 39 additions & 1 deletion api/open_saves_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
- [GetBlobRequest](#opensaves-GetBlobRequest)
- [GetBlobResponse](#opensaves-GetBlobResponse)
- [GetRecordRequest](#opensaves-GetRecordRequest)
- [GetRecordsRequest](#opensaves-GetRecordsRequest)
- [GetRecordsResponse](#opensaves-GetRecordsResponse)
- [GetRecordsResponse.Result](#opensaves-GetRecordsResponse-Result)
- [GetStoreRequest](#opensaves-GetStoreRequest)
- [Hint](#opensaves-Hint)
- [ListStoresRequest](#opensaves-ListStoresRequest)
Expand All @@ -39,6 +42,7 @@
- [Record](#opensaves-Record)
- [Record.PropertiesEntry](#opensaves-Record-PropertiesEntry)
- [SortOrder](#opensaves-SortOrder)
- [Status](#opensaves-Status)
- [Store](#opensaves-Store)
- [UpdateRecordRequest](#opensaves-UpdateRecordRequest)
- [UploadChunkRequest](#opensaves-UploadChunkRequest)
Expand Down Expand Up @@ -452,6 +456,54 @@ field.



<a name="opensaves-GetRecordsRequest"></a>

### GetRecordsRequest



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| store_keys | [string](#string) | repeated | The keys of the stores the records belongs to. |
| keys | [string](#string) | repeated | The keys of the records to get. |






<a name="opensaves-GetRecordsResponse"></a>

### GetRecordsResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| results | [GetRecordsResponse.Result](#opensaves-GetRecordsResponse-Result) | repeated | |






<a name="opensaves-GetRecordsResponse-Result"></a>

### GetRecordsResponse.Result



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| status | [Status](#opensaves-Status) | | |
| store_key | [string](#string) | | |
| record | [Record](#opensaves-Record) | | |






<a name="opensaves-GetStoreRequest"></a>

### GetStoreRequest
Expand Down Expand Up @@ -682,6 +734,23 @@ SortOrder is a way to order records returned by QueryRecords.



<a name="opensaves-Status"></a>

### Status



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| code | [uint32](#uint32) | | |
| message | [string](#string) | | |
| details | [google.protobuf.Any](#google-protobuf-Any) | repeated | |






<a name="opensaves-Store"></a>

### Store
Expand Down Expand Up @@ -811,6 +880,7 @@ Public interface of the Open Saves service.
| DeleteStore | [DeleteStoreRequest](#opensaves-DeleteStoreRequest) | [.google.protobuf.Empty](#google-protobuf-Empty) | DeleteStore deletes a single store with the specified key. |
| CreateRecord | [CreateRecordRequest](#opensaves-CreateRecordRequest) | [Record](#opensaves-Record) | CreateRecord creates a new record. This returns an error if the specified key already exists. |
| GetRecord | [GetRecordRequest](#opensaves-GetRecordRequest) | [Record](#opensaves-Record) | GetRecord returns a record with the specified key. |
| GetRecords | [GetRecordsRequest](#opensaves-GetRecordsRequest) | [GetRecordsResponse](#opensaves-GetRecordsResponse) | GetRecords fetches multiple records by keys. |
| QueryRecords | [QueryRecordsRequest](#opensaves-QueryRecordsRequest) | [QueryRecordsResponse](#opensaves-QueryRecordsResponse) | QueryRecords performs a query and returns matching records. |
| UpdateRecord | [UpdateRecordRequest](#opensaves-UpdateRecordRequest) | [Record](#opensaves-Record) | UpdateRecord updates an existing record. This returns an error and does not create a new record if the key doesn&#39;t exist. |
| DeleteRecord | [DeleteRecordRequest](#opensaves-DeleteRecordRequest) | [.google.protobuf.Empty](#google-protobuf-Empty) | DeleteRecord deletes a single record with the specified key. |
Expand Down
50 changes: 50 additions & 0 deletions internal/app/server/open_saves.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server

import (
"bytes"
"cloud.google.com/go/datastore"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -250,6 +251,55 @@ func (s *openSavesServer) QueryRecords(ctx context.Context, req *pb.QueryRecords
}, nil
}

func (s *openSavesServer) GetRecords(ctx context.Context, req *pb.GetRecordsRequest) (*pb.GetRecordsResponse, error) {
records, err := s.metaDB.GetRecords(ctx, req.GetStoreKeys(), req.GetKeys())

// Check if there was an unexpected error
var errors datastore.MultiError
if err != nil {
var ok bool
if errors, ok = err.(datastore.MultiError); !ok {
log.Errorf("GetRecords unable to retrieve records: %v", err)
return nil, err
}
}

response := &pb.GetRecordsResponse{
Results: []*pb.GetRecordsResponse_Result{},
}
statusOk := &pb.Status{
Code: uint32(codes.OK),
Message: codes.OK.String(),
}
for i, rec := range records {
var protoRec *pb.Record
var storeKey string
var pbStatus *pb.Status

if rec != nil {
protoRec = rec.ToProto()
storeKey = rec.StoreKey
}
if errors != nil && errors[i] != nil {
statusCode := status.Code(errors[i])
pbStatus = &pb.Status{
Code: uint32(statusCode),
Message: errors[i].Error(),
}
} else {
pbStatus = statusOk
}

result := &pb.GetRecordsResponse_Result{
Record: protoRec,
StoreKey: storeKey,
Status: pbStatus,
}
response.Results = append(response.Results, result)
}
return response, nil
}

func (s *openSavesServer) insertInlineBlob(ctx context.Context, stream pb.OpenSaves_CreateBlobServer, meta *pb.BlobMetadata) error {
log.Debugf("Inserting inline blob: %v\n", meta)
// Receive the blob
Expand Down
Loading