From 4d917719ec799272f7abff13e5a7393fb7bff246 Mon Sep 17 00:00:00 2001 From: tornado-ssy <64736788+tornado-ssy@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:56:40 +0800 Subject: [PATCH] =?UTF-8?q?kvdoc=E5=A2=9E=E5=8A=A0=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=9F=A5=E8=AF=A2=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=94=AF=E6=8C=81=E4=BC=98=E5=85=88=E7=BA=A7=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=20(#296)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: songshiyuan 00649746 --- pkg/model/db_schema.go | 1 + server/datasource/etcd/history/history_dao.go | 5 +++-- server/datasource/etcd/kv/kv_dao.go | 9 +++++---- server/datasource/kv_sort.go | 7 +++++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/model/db_schema.go b/pkg/model/db_schema.go index 748f2c3b..28b98d34 100644 --- a/pkg/model/db_schema.go +++ b/pkg/model/db_schema.go @@ -36,6 +36,7 @@ type KVDoc struct { Key string `json:"key" yaml:"key" validate:"min=1,max=2048,key"` Value string `json:"value" yaml:"value" validate:"max=131072,value"` //128K ValueType string `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty" validate:"valueType"` //ini,json,text,yaml,properties,xml + Priority int `json:"priority,omitempty" yaml:"priority,omitempty"` //the smaller value,the higher priority Checker string `json:"check,omitempty" yaml:"check,omitempty" validate:"max=1048576,check"` //python script CreateRevision int64 `json:"create_revision,omitempty" bson:"create_revision," yaml:"create_revision,omitempty"` UpdateRevision int64 `json:"update_revision,omitempty" bson:"update_revision," yaml:"update_revision,omitempty"` diff --git a/server/datasource/etcd/history/history_dao.go b/server/datasource/etcd/history/history_dao.go index bebc4eae..f1ce04d1 100644 --- a/server/datasource/etcd/history/history_dao.go +++ b/server/datasource/etcd/history/history_dao.go @@ -21,10 +21,11 @@ import ( "context" "encoding/json" - "github.com/apache/servicecomb-kie/server/datasource/auth" "github.com/go-chassis/openlog" "github.com/little-cui/etcdadpt" + "github.com/apache/servicecomb-kie/server/datasource/auth" + "github.com/apache/servicecomb-kie/pkg/model" "github.com/apache/servicecomb-kie/server/datasource" "github.com/apache/servicecomb-kie/server/datasource/etcd/key" @@ -80,7 +81,7 @@ func pagingResult(histories []*model.KVDoc, offset, limit int64) []*model.KVDoc return []*model.KVDoc{} } - datasource.ReverseByUpdateRev(histories) + datasource.ReverseByPriorityAndUpdateRev(histories) if limit == 0 { return histories diff --git a/server/datasource/etcd/kv/kv_dao.go b/server/datasource/etcd/kv/kv_dao.go index 6c1745d8..84c6958f 100644 --- a/server/datasource/etcd/kv/kv_dao.go +++ b/server/datasource/etcd/kv/kv_dao.go @@ -23,14 +23,15 @@ import ( "regexp" "strings" + "github.com/go-chassis/cari/sync" + "github.com/go-chassis/openlog" + "github.com/little-cui/etcdadpt" + "github.com/apache/servicecomb-kie/pkg/model" "github.com/apache/servicecomb-kie/pkg/util" "github.com/apache/servicecomb-kie/server/datasource" "github.com/apache/servicecomb-kie/server/datasource/auth" "github.com/apache/servicecomb-kie/server/datasource/etcd/key" - "github.com/go-chassis/cari/sync" - "github.com/go-chassis/openlog" - "github.com/little-cui/etcdadpt" ) // Dao operate data in mongodb @@ -609,7 +610,7 @@ func toRegex(opts datasource.FindOptions) (*regexp.Regexp, error) { } func pagingResult(result *model.KVResponse, opts datasource.FindOptions) *model.KVResponse { - datasource.ReverseByUpdateRev(result.Data) + datasource.ReverseByPriorityAndUpdateRev(result.Data) if opts.Limit == 0 { return result diff --git a/server/datasource/kv_sort.go b/server/datasource/kv_sort.go index f181e790..3cae128c 100644 --- a/server/datasource/kv_sort.go +++ b/server/datasource/kv_sort.go @@ -32,14 +32,17 @@ func (k *KVDocSorter) Len() int { } func (k *KVDocSorter) Less(i, j int) bool { - return k.KVs[i].UpdateRevision > k.KVs[j].UpdateRevision + if k.KVs[i].Priority == k.KVs[j].Priority { + return k.KVs[i].UpdateRevision > k.KVs[j].UpdateRevision + } + return k.KVs[i].Priority > k.KVs[j].Priority } func (k *KVDocSorter) Swap(i, j int) { k.KVs[i], k.KVs[j] = k.KVs[j], k.KVs[i] } -func ReverseByUpdateRev(kvs []*model.KVDoc) { +func ReverseByPriorityAndUpdateRev(kvs []*model.KVDoc) { sorter := &KVDocSorter{KVs: kvs} sort.Sort(sorter) }