Skip to content

Commit

Permalink
add namespace list limit-offset (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
asim authored May 17, 2021
1 parent f7807bd commit 5c78c61
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 62 deletions.
36 changes: 29 additions & 7 deletions namespaces/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/micro/micro/v3/service/events"
mevents "github.com/micro/micro/v3/service/events"
log "github.com/micro/micro/v3/service/logger"
mstore "github.com/micro/micro/v3/service/store"
"github.com/micro/micro/v3/service/store"

"github.com/sethvargo/go-diceware/diceware"
)
Expand Down Expand Up @@ -117,15 +117,15 @@ func writeNamespace(ns *NamespaceModel) error {
if err != nil {
return err
}
if err := mstore.Write(&mstore.Record{
if err := store.Write(&store.Record{
Key: prefixNs + ns.ID,
Value: b,
}); err != nil {
return err
}
// index by owner
for _, owner := range ns.Owners {
if err := mstore.Write(&mstore.Record{
if err := store.Write(&store.Record{
Key: prefixOwner + owner + "/" + ns.ID,
Value: b,
}); err != nil {
Expand All @@ -134,7 +134,7 @@ func writeNamespace(ns *NamespaceModel) error {
}
// index by user
for _, user := range ns.Users {
if err := mstore.Write(&mstore.Record{
if err := store.Write(&store.Record{
Key: prefixUser + user + "/" + ns.ID,
Value: b,
}); err != nil {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (n Namespaces) Read(ctx context.Context, request *namespace.ReadRequest, re
}

func readNamespace(id string) (*NamespaceModel, error) {
recs, err := mstore.Read(prefixNs + id)
recs, err := store.Read(prefixNs + id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -260,8 +260,30 @@ func (n Namespaces) List(ctx context.Context, request *namespace.ListRequest, re
key = prefixUser + request.User + "/"
}

recs, err := mstore.Read(key, mstore.ReadPrefix())
if err != nil && err != mstore.ErrNotFound {
opts := []store.ReadOption{
store.ReadPrefix(),
}

if request.Limit > 0 {
opts = append(opts, store.ReadLimit(uint(request.Limit)))
}

if request.Offset > 0 {
opts = append(opts, store.ReadOffset(uint(request.Offset)))
}

if request.Order != "" {
order := store.OrderAsc

if request.Order == "desc" {
order = store.OrderDesc
}

opts = append(opts, store.ReadOrder(order))
}

recs, err := store.Read(key, opts...)
if err != nil && err != store.ErrNotFound {
return err
}
res := []*namespace.Namespace{}
Expand Down
131 changes: 77 additions & 54 deletions namespaces/proto/namespaces.pb.go

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

5 changes: 4 additions & 1 deletion namespaces/proto/namespaces.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package namespaces;

option go_package = "proto;namespaces";
option go_package = "./proto;namespaces";

service Namespaces {
rpc Create(CreateRequest) returns (CreateResponse) {}
Expand Down Expand Up @@ -48,6 +48,9 @@ message DeleteResponse {}
message ListRequest {
string owner = 1;
string user = 2;
int64 limit = 3;
int64 offset = 4;
string order = 5;
}

message ListResponse {
Expand Down

0 comments on commit 5c78c61

Please sign in to comment.