Skip to content

Commit bc57119

Browse files
authored
Fix MoveTo when moving to the same destination (#12898)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Moving the data between same source and destination causes the destination to be cleared. <!-- Issue number if applicable --> #### Link to tracking issue Fixes #12887 Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 2526b02 commit bc57119

File tree

167 files changed

+689
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+689
-0
lines changed

.chloggen/fix-moveto.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix MoveTo when moving to the same destination
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12887]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

pdata/internal/cmd/pdatagen/internal/templates/message.go.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func New{{ .structName }}() {{ .structName }} {
5050
func (ms {{ .structName }}) MoveTo(dest {{ .structName }}) {
5151
ms.{{ .stateAccessor }}.AssertMutable()
5252
dest.{{ .stateAccessor }}.AssertMutable()
53+
// If they point to the same data, they are the same, nothing to do.
54+
if ms.{{ .origAccessor }} == dest.{{ .origAccessor }} {
55+
return
56+
}
5357
*dest.{{ .origAccessor }} = *ms.{{ .origAccessor }}
5458
*ms.{{ .origAccessor }} = {{ .originName }}{}
5559
}

pdata/internal/cmd/pdatagen/internal/templates/message_test.go.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func Test{{ .structName }}_MoveTo(t *testing.T) {
1818
ms.MoveTo(dest)
1919
assert.Equal(t, New{{ .structName }}(), ms)
2020
assert.Equal(t, generateTest{{ .structName }}(), dest)
21+
dest.MoveTo(dest)
22+
assert.Equal(t, generateTest{{ .structName }}(), dest)
2123
sharedState := internal.StateReadOnly
2224
assert.Panics(t, func() { ms.MoveTo(new{{ .structName }}(&{{ .originName }}{}, &sharedState)) })
2325
assert.Panics(t, func() { new{{ .structName }}(&{{ .originName }}{}, &sharedState).MoveTo(dest) })

pdata/internal/cmd/pdatagen/internal/templates/primitive_slice.go.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ func (ms {{ .structName }}) Append(elms ...{{ .itemType }}) {
105105
func (ms {{ .structName }}) MoveTo(dest {{ .structName }}) {
106106
ms.getState().AssertMutable()
107107
dest.getState().AssertMutable()
108+
// If they point to the same data, they are the same, nothing to do.
109+
if ms.getOrig() == dest.getOrig() {
110+
return
111+
}
108112
*dest.getOrig() = *ms.getOrig()
109113
*ms.getOrig() = nil
110114
}

pdata/internal/cmd/pdatagen/internal/templates/primitive_slice_test.go.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ func TestNew{{ .structName }}(t *testing.T) {
6565
{{- else }}
6666
assert.Equal(t, {{ .itemType }}({{index .testInterfaceOrigVal 0 }}), mv.At(0))
6767
{{- end }}
68+
mv.MoveTo(mv)
69+
assert.Equal(t, 3, mv.Len())
70+
{{- if eq .itemType "float64" }}
71+
assert.InDelta(t, {{ .itemType }}({{index .testInterfaceOrigVal 0 }}), mv.At(0), 0.01)
72+
{{- else }}
73+
assert.Equal(t, {{ .itemType }}({{index .testInterfaceOrigVal 0 }}), mv.At(0))
74+
{{- end }}
6875
}
6976

7077
func Test{{ .structName }}ReadOnly(t *testing.T) {

pdata/internal/cmd/pdatagen/internal/templates/slice.go.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func (es {{ .structName }}) AppendEmpty() {{ .elementName }} {
113113
func (es {{ .structName }}) MoveAndAppendTo(dest {{ .structName }}) {
114114
es.{{ .stateAccessor }}.AssertMutable()
115115
dest.{{ .stateAccessor }}.AssertMutable()
116+
// If they point to the same data, they are the same, nothing to do.
117+
if es.{{ .origAccessor }} == dest.{{ .origAccessor }} {
118+
return
119+
}
116120
if *dest.{{ .origAccessor }} == nil {
117121
// We can simply move the entire vector and avoid any allocations.
118122
*dest.{{ .origAccessor }} = *es.{{ .origAccessor }}

pdata/internal/cmd/pdatagen/internal/templates/slice_test.go.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func Test{{ .structName }}_MoveAndAppendTo(t *testing.T) {
107107
assert.Equal(t, expectedSlice.At(i), dest.At(i))
108108
assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len()))
109109
}
110+
111+
dest.MoveAndAppendTo(dest)
112+
assert.Equal(t, 2*expectedSlice.Len(), dest.Len())
113+
for i := 0; i < expectedSlice.Len(); i++ {
114+
assert.Equal(t, expectedSlice.At(i), dest.At(i))
115+
assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len()))
116+
}
110117
}
111118

112119
func Test{{ .structName }}_RemoveIf(t *testing.T) {

pdata/pcommon/generated_byteslice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_byteslice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_instrumentationscope.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_instrumentationscope_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_resource.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_resource_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_stringslice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_stringslice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_uint64slice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_uint64slice_test.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ func (m Map) All() iter.Seq2[string, Value] {
248248
func (m Map) MoveTo(dest Map) {
249249
m.getState().AssertMutable()
250250
dest.getState().AssertMutable()
251+
// If they point to the same data, they are the same, nothing to do.
252+
if m.getOrig() == dest.getOrig() {
253+
return
254+
}
251255
*dest.getOrig() = *m.getOrig()
252256
*m.getOrig() = nil
253257
}

pdata/pcommon/map_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ func TestMap_MoveTo(t *testing.T) {
416416
// Test MoveTo from empty to non-empty
417417
NewMap().MoveTo(dest)
418418
assert.Equal(t, 0, dest.Len())
419+
420+
dest.PutStr("k", "v")
421+
dest.MoveTo(dest)
422+
assert.Equal(t, 1, dest.Len())
423+
assert.Equal(t, map[string]any{"k": "v"}, dest.AsRaw())
419424
}
420425

421426
func TestMap_CopyTo(t *testing.T) {

pdata/pcommon/trace_state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (ms TraceState) FromRaw(v string) {
4242
func (ms TraceState) MoveTo(dest TraceState) {
4343
ms.getState().AssertMutable()
4444
dest.getState().AssertMutable()
45+
// If they point to the same data, they are the same, nothing to do.
46+
if ms.getOrig() == dest.getOrig() {
47+
return
48+
}
4549
*dest.getOrig() = *ms.getOrig()
4650
*ms.getOrig() = ""
4751
}

pdata/pcommon/trace_state_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func TestTraceState_MoveTo(t *testing.T) {
1717
ms.MoveTo(dest)
1818
assert.Equal(t, NewTraceState(), ms)
1919
assert.Equal(t, TraceState(internal.GenerateTestTraceState()), dest)
20+
21+
dest.MoveTo(dest)
22+
assert.Equal(t, TraceState(internal.GenerateTestTraceState()), dest)
2023
}
2124

2225
func TestTraceState_CopyTo(t *testing.T) {

pdata/pcommon/value.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ func (v Value) SetEmptySlice() Slice {
324324
func (v Value) MoveTo(dest Value) {
325325
v.getState().AssertMutable()
326326
dest.getState().AssertMutable()
327+
// If they point to the same data, they are the same, nothing to do.
328+
if v.getOrig() == dest.getOrig() {
329+
return
330+
}
327331
*dest.getOrig() = *v.getOrig()
328332
v.getOrig().Value = nil
329333
}

pdata/pcommon/value_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ func TestValue_MoveTo(t *testing.T) {
244244
expected := NewValueMap()
245245
expected.Map().PutStr("key", "value")
246246
assert.True(t, dest.Equal(expected))
247+
248+
dest.MoveTo(dest)
249+
assert.True(t, dest.Equal(expected))
247250
}
248251

249252
func TestValue_CopyTo(t *testing.T) {

pdata/plog/generated_logrecord.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_logrecord_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_logrecordslice.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_logrecordslice_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_resourcelogs.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/plog/generated_resourcelogs_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)