Skip to content

Commit 36e2dbd

Browse files
authored
Merge pull request #21708 from fuweid/gogo-part-4-update-events
client/v3: move Event helper methods into pb package
2 parents c867abd + 2214d9f commit 36e2dbd

4 files changed

Lines changed: 29 additions & 23 deletions

File tree

api/mvccpb/extension.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2026 The etcd Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mvccpb
16+
17+
// IsCreate returns true if the event tells that the key is newly created.
18+
func (e *Event) IsCreate() bool {
19+
return e.GetType() == PUT && e.GetKv().GetCreateRevision() == e.GetKv().GetModRevision()
20+
}
21+
22+
// IsModify returns true if the event tells that a new value is put on existing key.
23+
func (e *Event) IsModify() bool {
24+
return e.GetType() == PUT && e.GetKv().GetCreateRevision() != e.GetKv().GetModRevision()
25+
}

client/v3/watch.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
InvalidWatchID = -1
4747
)
4848

49-
type Event mvccpb.Event
49+
type Event = mvccpb.Event
5050

5151
type WatchChan <-chan WatchResponse
5252

@@ -116,16 +116,6 @@ type WatchResponse struct {
116116
CancelReason string
117117
}
118118

119-
// IsCreate returns true if the event tells that the key is newly created.
120-
func (e *Event) IsCreate() bool {
121-
return e.Type == EventTypePut && e.Kv.CreateRevision == e.Kv.ModRevision
122-
}
123-
124-
// IsModify returns true if the event tells that a new value is put on existing key.
125-
func (e *Event) IsModify() bool {
126-
return e.Type == EventTypePut && e.Kv.CreateRevision != e.Kv.ModRevision
127-
}
128-
129119
// Err is the error value if this WatchResponse holds an error.
130120
func (wr *WatchResponse) Err() error {
131121
switch {
@@ -719,14 +709,10 @@ func (w *watchGRPCStream) nextResume() *watcherStream {
719709

720710
// dispatchEvent sends a WatchResponse to the appropriate watcher stream
721711
func (w *watchGRPCStream) dispatchEvent(pbresp *pb.WatchResponse) bool {
722-
events := make([]*Event, len(pbresp.Events))
723-
for i, ev := range pbresp.Events {
724-
events[i] = (*Event)(ev)
725-
}
726712
// TODO: return watch ID?
727713
wr := &WatchResponse{
728714
Header: ensureWatchHeader(pbresp.Header),
729-
Events: events,
715+
Events: pbresp.Events,
730716
CompactRevision: pbresp.CompactRevision,
731717
Created: pbresp.Created,
732718
Canceled: pbresp.Canceled,

etcdctl/ctlv3/command/printer_protobuf.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"os"
2020

2121
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
22-
mvccpb "go.etcd.io/etcd/api/v3/mvccpb"
2322
v3 "go.etcd.io/etcd/client/v3"
2423
"go.etcd.io/etcd/pkg/v3/cobrautl"
2524
)
@@ -37,13 +36,9 @@ func newPBPrinter() printer {
3736
}
3837

3938
func (p *pbPrinter) Watch(r v3.WatchResponse) {
40-
evs := make([]*mvccpb.Event, len(r.Events))
41-
for i, ev := range r.Events {
42-
evs[i] = (*mvccpb.Event)(ev)
43-
}
4439
wr := pb.WatchResponse{
4540
Header: r.Header,
46-
Events: evs,
41+
Events: r.Events,
4742
CompactRevision: r.CompactRevision,
4843
Canceled: r.Canceled,
4944
Created: r.Created,

server/proxy/grpcproxy/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (w *watcher) send(wr clientv3.WatchResponse) {
6868

6969
var lastRev int64
7070
for i := range wr.Events {
71-
ev := (*mvccpb.Event)(wr.Events[i])
71+
ev := wr.Events[i]
7272
if ev.Kv.ModRevision < w.nextrev {
7373
continue
7474
}

0 commit comments

Comments
 (0)