Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: add Len() API for better performance when dumping KVS
Browse files Browse the repository at this point in the history
AsterDY committed Jul 27, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
chrismaddalena Christopher Maddalena
1 parent 8555f20 commit ce0647c
Showing 2 changed files with 100 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cloud/metainfo/info.go
Original file line number Diff line number Diff line change
@@ -211,6 +211,24 @@ func getValue(kvs []string, i int) string {
return kvs[i*2+1]
}

// CountPersistentValues counts the length of persisten KV pairs
func CountPersistentValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.persistent)
}
}

// CountValues counts the length of transient KV pairs
func CountValues(ctx context.Context) int {
if n := getNode(ctx); n == nil {
return 0
} else {
return len(n.transient)
}
}

// WithPersistentValues sets the values into the context by the given keys.
// This value will be propagated to the services along the RPC call chain.
func WithPersistentValues(ctx context.Context, kvs ...string) context.Context {
82 changes: 82 additions & 0 deletions cloud/metainfo/info_test.go
Original file line number Diff line number Diff line change
@@ -776,3 +776,85 @@ func BenchmarkAllParallel(b *testing.B) {
}
}
}

func TestValuesCount(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want int
}{
{
name: "0",
args: args{
ctx: ctx,
},
want: 0,
},
{
name: "0",
args: args{
ctx: metainfo.WithPersistentValues(ctx, "1", "1", "2", "2"),
},
want: 0,
},
{
name: "2",
args: args{
ctx: metainfo.WithValues(ctx, "1", "1", "2", "2"),
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := metainfo.CountValues(tt.args.ctx); got != tt.want {
t.Errorf("ValuesCount() = %v, want %v", got, tt.want)
}
})
}
}

func TestPersistentValuesCount(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want int
}{
{
name: "0",
args: args{
ctx: ctx,
},
want: 0,
},
{
name: "0",
args: args{
ctx: metainfo.WithValues(ctx, "1", "1", "2", "2"),
},
want: 0,
},
{
name: "2",
args: args{
ctx: metainfo.WithPersistentValues(ctx, "1", "1", "2", "2"),
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := metainfo.CountPersistentValues(tt.args.ctx); got != tt.want {
t.Errorf("ValuesCount() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ce0647c

Please sign in to comment.