This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new 17b8b78  delete POC code, new api to get kv by id (#128)
17b8b78 is described below

commit 17b8b78aa01c78403a57fc8e5f71c54bb548a4ba
Author: Shawn <[email protected]>
AuthorDate: Thu Mar 26 16:05:45 2020 +0800

    delete POC code, new api to get kv by id (#128)
---
 server/resource/v1/kv_resource.go      |  6 +--
 server/resource/v1/kv_resource_test.go |  4 +-
 server/service/mongo/kv/kv_service.go  | 48 ++++--------------
 server/service/mongo/kv/kv_test.go     | 38 +-------------
 server/service/mongo/kv/tool.go        | 92 ++--------------------------------
 server/service/service.go              |  4 +-
 6 files changed, 21 insertions(+), 171 deletions(-)

diff --git a/server/resource/v1/kv_resource.go 
b/server/resource/v1/kv_resource.go
index 2c42bc5..86ef153 100644
--- a/server/resource/v1/kv_resource.go
+++ b/server/resource/v1/kv_resource.go
@@ -127,6 +127,7 @@ func (r *KVResource) List(rctx *restful.Context) {
                WriteErrResponse(rctx, http.StatusBadRequest, err.Error(), 
common.ContentTypeText)
                return
        }
+
        offsetStr := rctx.ReadQueryParameter(common.QueryParamOffset)
        limitStr := rctx.ReadQueryParameter(common.QueryParamLimit)
        offset, limit, err := checkPagination(offsetStr, limitStr)
@@ -217,8 +218,7 @@ func (r *KVResource) Delete(context *restful.Context) {
                WriteErrResponse(context, http.StatusBadRequest, 
common.ErrKvIDMustNotEmpty, common.ContentTypeText)
                return
        }
-       result, err := service.KVService.FindKV(context.Ctx, domain.(string), 
project,
-               service.WithID(kvID))
+       result, err := service.KVService.Get(context.Ctx, domain.(string), 
project, kvID)
        if err != nil && err != service.ErrKeyNotExists {
                WriteErrResponse(context, http.StatusInternalServerError, 
err.Error(), common.ContentTypeText)
                return
@@ -226,7 +226,7 @@ func (r *KVResource) Delete(context *restful.Context) {
                context.WriteHeader(http.StatusNoContent)
                return
        }
-       kv := result[0].Data[0]
+       kv := result.Data[0]
        err = service.KVService.Delete(context.Ctx, kvID, domain.(string), 
project)
        if err != nil {
                openlogging.Error("delete failed ,", 
openlogging.WithTags(openlogging.Tags{
diff --git a/server/resource/v1/kv_resource_test.go 
b/server/resource/v1/kv_resource_test.go
index e0450d7..fcd789e 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -117,7 +117,8 @@ func TestKVResource_Put(t *testing.T) {
        t.Run("put kv,label is service and version", func(t *testing.T) {
                kv := &model.KVDoc{
                        Value: "1s",
-                       Labels: map[string]string{"service": "utService",
+                       Labels: map[string]string{
+                               "service": "utService",
                                "version": "1.0.0"},
                }
                j, _ := json.Marshal(kv)
@@ -155,6 +156,7 @@ func TestKVResource_List(t *testing.T) {
                err = json.Unmarshal(body, result)
                assert.NoError(t, err)
                assert.Equal(t, 3, len(result.Data))
+
        })
        var rev string
        t.Run("list kv by service label, exact match,should return 2 kv", 
func(t *testing.T) {
diff --git a/server/service/mongo/kv/kv_service.go 
b/server/service/mongo/kv/kv_service.go
index e169dcc..5b01e0b 100644
--- a/server/service/mongo/kv/kv_service.go
+++ b/server/service/mongo/kv/kv_service.go
@@ -123,19 +123,19 @@ func (s *Service) Exist(ctx context.Context, domain, key 
string, project string,
                }
                return kvs[0], nil
        }
-       kvs, err := s.FindKV(ctx, domain, project,
+       kvs, err := s.List(ctx, domain, project,
                service.WithExactLabels(),
                service.WithLabels(opts.Labels),
                service.WithKey(key))
        if err != nil {
-               openlogging.Error(err.Error())
+               openlogging.Error("check kv exist: " + err.Error())
                return nil, err
        }
-       if len(kvs) != 1 {
+       if len(kvs.Data) != 1 {
                return nil, session.ErrTooMany
        }
 
-       return kvs[0].Data[0], nil
+       return kvs.Data[0], nil
 
 }
 
@@ -193,10 +193,8 @@ func (s *Service) List(ctx context.Context, domain, 
project string, options ...s
        return result, nil
 }
 
-//FindKV get kvs by key, labels
-//because labels has a a lot of combination,
-//you can use WithDepth(0) to return only one kv which's labels exactly match 
the criteria
-func (s *Service) FindKV(ctx context.Context, domain string, project string, 
options ...service.FindOption) ([]*model.KVResponse, error) {
+//Get get kvs by id
+func (s *Service) Get(ctx context.Context, domain, project, id string, options 
...service.FindOption) (*model.KVResponse, error) {
        opts := service.FindOptions{}
        for _, o := range options {
                o(&opts)
@@ -210,36 +208,8 @@ func (s *Service) FindKV(ctx context.Context, domain 
string, project string, opt
        if project == "" {
                return nil, session.ErrMissingProject
        }
-
-       if opts.ID != "" {
-               openlogging.Debug(MsgFindOneKeyByID, 
openlogging.WithTags(openlogging.Tags{
-                       "id":     opts.ID,
-                       "key":    opts.Key,
-                       "labels": opts.Labels,
-               }))
-               return findKVByID(ctx, domain, project, opts.ID)
+       if id == "" {
+               return nil, session.ErrIDIsNil
        }
-
-       cur, _, err := findKV(ctx, domain, project, opts)
-       if err != nil {
-               return nil, err
-       }
-       defer cur.Close(ctx)
-
-       if opts.Depth == 0 && opts.Key != "" {
-               openlogging.Debug(MsgFindOneKey, openlogging.WithTags(
-                       map[string]interface{}{
-                               "key":    opts.Key,
-                               "label":  opts.Labels,
-                               "domain": domain,
-                       },
-               ))
-               return cursorToOneKV(ctx, cur, opts.Labels)
-       }
-       openlogging.Debug(MsgFindMoreKey, openlogging.WithTags(openlogging.Tags{
-               "depth":  opts.Depth,
-               "key":    opts.Key,
-               "labels": opts.Labels,
-       }))
-       return findMoreKV(ctx, cur, &opts)
+       return findKVByID(ctx, domain, project, id)
 }
diff --git a/server/service/mongo/kv/kv_test.go 
b/server/service/mongo/kv/kv_test.go
index b08c1c0..f43888d 100644
--- a/server/service/mongo/kv/kv_test.go
+++ b/server/service/mongo/kv/kv_test.go
@@ -72,7 +72,7 @@ func TestService_CreateOrUpdate(t *testing.T) {
                assert.NotEmpty(t, oid)
        })
        t.Run("put kv timeout,with labels app,and update value", func(t 
*testing.T) {
-               beforeKV, err := kvsvc.CreateOrUpdate(context.Background(), 
&model.KVDoc{
+               _, err := kvsvc.CreateOrUpdate(context.Background(), 
&model.KVDoc{
                        Key:   "timeout",
                        Value: "1s",
                        Labels: map[string]string{
@@ -82,13 +82,6 @@ func TestService_CreateOrUpdate(t *testing.T) {
                        Project: "kv-test",
                })
                assert.NoError(t, err)
-               kvs1, err := kvsvc.FindKV(context.Background(), "default", 
"kv-test",
-                       service.WithKey("timeout"),
-                       service.WithLabels(map[string]string{
-                               "app": "mall",
-                       }),
-                       service.WithExactLabels())
-               assert.Equal(t, beforeKV.Value, kvs1[0].Data[0].Value)
                afterKV, err := kvsvc.CreateOrUpdate(context.Background(), 
&model.KVDoc{
                        Key:   "timeout",
                        Value: "3s",
@@ -104,39 +97,10 @@ func TestService_CreateOrUpdate(t *testing.T) {
                }))
                assert.NoError(t, err)
                assert.Equal(t, afterKV.Value, savedKV.Value)
-               kvs, err := kvsvc.FindKV(context.Background(), "default", 
"kv-test",
-                       service.WithKey("timeout"),
-                       service.WithLabels(map[string]string{
-                               "app": "mall",
-                       }),
-                       service.WithExactLabels())
-               assert.Equal(t, afterKV.Value, kvs[0].Data[0].Value)
        })
 
 }
 
-func TestService_FindKV(t *testing.T) {
-       kvsvc := &kv.Service{}
-       t.Run("exact find by kv and labels with label app", func(t *testing.T) {
-               kvs, err := kvsvc.FindKV(context.Background(), "default", 
"kv-test",
-                       service.WithKey("timeout"),
-                       service.WithLabels(map[string]string{
-                               "app": "mall",
-                       }),
-                       service.WithExactLabels())
-               assert.NoError(t, err)
-               assert.Equal(t, 1, len(kvs))
-       })
-       t.Run("greedy find by labels,with labels app ans service ", func(t 
*testing.T) {
-               kvs, err := kvsvc.FindKV(context.Background(), "default", 
"kv-test",
-                       service.WithLabels(map[string]string{
-                               "app":     "mall",
-                               "service": "cart",
-                       }))
-               assert.NoError(t, err)
-               assert.Equal(t, 1, len(kvs))
-       })
-}
 func TestService_Delete(t *testing.T) {
        kvsvc := &kv.Service{}
        t.Run("delete key by kvID", func(t *testing.T) {
diff --git a/server/service/mongo/kv/tool.go b/server/service/mongo/kv/tool.go
index cbeb5af..de9d0e3 100644
--- a/server/service/mongo/kv/tool.go
+++ b/server/service/mongo/kv/tool.go
@@ -19,13 +19,7 @@ package kv
 
 import (
        "context"
-       "fmt"
-       "reflect"
-
        "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/server/service"
-       "github.com/go-mesh/openlogging"
-       "go.mongodb.org/mongo-driver/mongo"
 )
 
 //clearAll clean attr which don't need to return to client side
@@ -38,94 +32,14 @@ func clearPart(kv *model.KVDoc) {
        kv.Domain = ""
        kv.Project = ""
 }
-func cursorToOneKV(ctx context.Context, cur *mongo.Cursor, labels 
map[string]string) ([]*model.KVResponse, error) {
-       result := make([]*model.KVResponse, 0)
-       //check label length to get the exact match
-       for cur.Next(ctx) { //although complexity is O(n), but there won't be 
so much labels for one key
-               if cur.Err() != nil {
-                       return nil, cur.Err()
-               }
-               curKV := &model.KVDoc{}
-               err := cur.Decode(curKV)
-               if err != nil {
-                       openlogging.Error("decode error: " + err.Error())
-                       return nil, err
-               }
-               if reflect.DeepEqual(curKV.Labels, labels) {
-                       openlogging.Debug(MsgHitExactLabels)
-                       labelGroup := &model.KVResponse{
-                               LabelDoc: &model.LabelDocResponse{
-                                       Labels:  labels,
-                                       LabelID: curKV.LabelID,
-                               },
-                               Data: make([]*model.KVDoc, 0),
-                       }
-                       clearAll(curKV)
-                       labelGroup.Data = append(labelGroup.Data, curKV)
-                       result = append(result, labelGroup)
-                       return result, nil
-               }
-
-       }
-       return nil, service.ErrKeyNotExists
-}
 
-func findKVByID(ctx context.Context, domain, project, kvID string) 
([]*model.KVResponse, error) {
-       kvResp := make([]*model.KVResponse, 0)
+func findKVByID(ctx context.Context, domain, project, kvID string) 
(*model.KVResponse, error) {
        kv, err := findKVDocByID(ctx, domain, project, kvID)
        if err != nil {
                return nil, err
        }
-       kvResp = append(kvResp, &model.KVResponse{
+       return &model.KVResponse{
                Total: 1,
                Data:  []*model.KVDoc{kv},
-       })
-       return kvResp, nil
-}
-
-func findMoreKV(ctx context.Context, cur *mongo.Cursor, opts 
*service.FindOptions) ([]*model.KVResponse, error) {
-       kvResp := make([]*model.KVResponse, 0)
-       for cur.Next(ctx) {
-               curKV := &model.KVDoc{}
-
-               if err := cur.Decode(curKV); err != nil {
-                       openlogging.Error("decode to KVs error: " + err.Error())
-                       return nil, err
-               }
-               if (len(curKV.Labels) - len(opts.Labels)) > opts.Depth {
-                       //because it is query by labels, so result can not be 
minus
-                       //so many labels,then continue
-                       openlogging.Debug("so deep, skip this key")
-                       continue
-               }
-               openlogging.Debug(fmt.Sprintf("%v", curKV))
-               var groupExist bool
-               var labelGroup *model.KVResponse
-               for _, labelGroup = range kvResp {
-                       if reflect.DeepEqual(labelGroup.LabelDoc.Labels, 
curKV.Labels) {
-                               groupExist = true
-                               clearAll(curKV)
-                               labelGroup.Data = append(labelGroup.Data, curKV)
-                               break
-                       }
-
-               }
-               if !groupExist {
-                       labelGroup = &model.KVResponse{
-                               LabelDoc: &model.LabelDocResponse{
-                                       Labels:  curKV.Labels,
-                                       LabelID: curKV.LabelID,
-                               },
-                               Data: []*model.KVDoc{curKV},
-                       }
-                       clearAll(curKV)
-                       openlogging.Debug("add new label group")
-                       kvResp = append(kvResp, labelGroup)
-               }
-
-       }
-       if len(kvResp) == 0 {
-               return nil, service.ErrKeyNotExists
-       }
-       return kvResp, nil
+       }, nil
 }
diff --git a/server/service/service.go b/server/service/service.go
index 5720a6c..a0d238d 100644
--- a/server/service/service.go
+++ b/server/service/service.go
@@ -46,8 +46,8 @@ type KV interface {
        CreateOrUpdate(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, 
error)
        List(ctx context.Context, domain, project string, options 
...FindOption) (*model.KVResponse, error)
        Delete(ctx context.Context, kvID string, domain, project string) error
-       //FindKV is usually for service to pull configs
-       FindKV(ctx context.Context, domain, project string, options 
...FindOption) ([]*model.KVResponse, error)
+       //Get return kv by id
+       Get(ctx context.Context, domain, project, id string, options 
...FindOption) (*model.KVResponse, error)
 }
 
 //History provide api of History entity

Reply via email to