Skip to content

Commit

Permalink
kvdoc增加优先级字段,查询结果支持优先级排序 (#296)
Browse files Browse the repository at this point in the history
Co-authored-by: songshiyuan 00649746 <[email protected]>
  • Loading branch information
tornado-ssy and songshiyuan 00649746 authored Sep 16, 2023
1 parent 3baf916 commit 4d91771
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/model/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 3 additions & 2 deletions server/datasource/etcd/history/history_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions server/datasource/etcd/kv/kv_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions server/datasource/kv_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4d91771

Please sign in to comment.