Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pdata/internal/wrapper_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ func NewMap(orig *[]otlpcommon.KeyValue, state *State) Map {
}

func CopyOrigMap(dest, src []otlpcommon.KeyValue) []otlpcommon.KeyValue {
var newDest []otlpcommon.KeyValue
if cap(dest) < len(src) {
dest = make([]otlpcommon.KeyValue, len(src))
newDest = make([]otlpcommon.KeyValue, len(src))
} else {
newDest = dest[:len(src)]
// Cleanup the rest of the elements so GC can free the memory.
// This can happen when len(src) < len(dest) < cap(dest).
for i := len(src); i < len(dest); i++ {
dest[i] = otlpcommon.KeyValue{}
}
}
dest = dest[:len(src)]
for i := 0; i < len(src); i++ {
dest[i].Key = src[i].Key
CopyOrigValue(&dest[i].Value, &src[i].Value)
for i := range src {
newDest[i].Key = src[i].Key
CopyOrigValue(&newDest[i].Value, &src[i].Value)
}
return dest
return newDest
}

func GenerateTestMap() Map {
Expand Down
17 changes: 12 additions & 5 deletions pdata/internal/wrapper_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ func NewSlice(orig *[]otlpcommon.AnyValue, state *State) Slice {
}

func CopyOrigSlice(dest, src []otlpcommon.AnyValue) []otlpcommon.AnyValue {
var newDest []otlpcommon.AnyValue
if cap(dest) < len(src) {
dest = make([]otlpcommon.AnyValue, len(src))
newDest = make([]otlpcommon.AnyValue, len(src))
} else {
newDest = dest[:len(src)]
// Cleanup the rest of the elements so GC can free the memory.
// This can happen when len(src) < len(dest) < cap(dest).
for i := len(src); i < len(dest); i++ {
dest[i] = otlpcommon.AnyValue{}
}
}
dest = dest[:len(src)]
for i := 0; i < len(src); i++ {
CopyOrigValue(&dest[i], &src[i])
for i := range src {
CopyOrigValue(&newDest[i], &src[i])
}
return dest
return newDest
}

func GenerateTestSlice() Slice {
Expand Down
5 changes: 3 additions & 2 deletions pdata/pcommon/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (m Map) RemoveIf(f func(string, Value) bool) {
m.getState().AssertMutable()
newLen := 0
for i := 0; i < len(*m.getOrig()); i++ {
akv := &(*m.getOrig())[i]
if f(akv.Key, newValue(&akv.Value, m.getState())) {
if f((*m.getOrig())[i].Key, newValue(&(*m.getOrig())[i].Value, m.getState())) {
(*m.getOrig())[i] = otlpcommon.KeyValue{}
continue
}
if newLen == i {
Expand All @@ -104,6 +104,7 @@ func (m Map) RemoveIf(f func(string, Value) bool) {
continue
}
(*m.getOrig())[newLen] = (*m.getOrig())[i]
(*m.getOrig())[i] = otlpcommon.KeyValue{}
newLen++
}
*m.getOrig() = (*m.getOrig())[:newLen]
Expand Down
17 changes: 17 additions & 0 deletions pdata/pcommon/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ func TestMap_CopyTo(t *testing.T) {
assert.Equal(t, Map(internal.GenerateTestMap()), dest)
}

func TestMap_CopyToAndEnsureCapacity(t *testing.T) {
dest := NewMap()
src := Map(internal.GenerateTestMap())
dest.EnsureCapacity(src.Len())
src.CopyTo(dest)
assert.Equal(t, Map(internal.GenerateTestMap()), dest)
}

func TestMap_EnsureCapacity_Zero(t *testing.T) {
am := NewMap()
am.EnsureCapacity(0)
Expand Down Expand Up @@ -532,6 +540,15 @@ func TestMap_RemoveIf(t *testing.T) {
assert.True(t, exists)
}

func TestMap_RemoveIfAll(t *testing.T) {
am := Map(internal.GenerateTestMap())
assert.Equal(t, 7, am.Len())
am.RemoveIf(func(string, Value) bool {
return true
})
assert.Equal(t, 0, am.Len())
}

func generateTestEmptyMap(t *testing.T) Map {
m := NewMap()
assert.NoError(t, m.FromRaw(map[string]any{"k": map[string]any(nil)}))
Expand Down
2 changes: 2 additions & 0 deletions pdata/pcommon/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (es Slice) RemoveIf(f func(Value) bool) {
newLen := 0
for i := 0; i < len(*es.getOrig()); i++ {
if f(es.At(i)) {
(*es.getOrig())[i] = otlpcommon.AnyValue{}
continue
}
if newLen == i {
Expand All @@ -142,6 +143,7 @@ func (es Slice) RemoveIf(f func(Value) bool) {
continue
}
(*es.getOrig())[newLen] = (*es.getOrig())[i]
(*es.getOrig())[i] = otlpcommon.AnyValue{}
newLen++
}
*es.getOrig() = (*es.getOrig())[:newLen]
Expand Down
8 changes: 8 additions & 0 deletions pdata/pcommon/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func TestSlice_CopyTo(t *testing.T) {
assert.Equal(t, Slice(internal.GenerateTestSlice()), dest)
}

func TestSlice_CopyToAndEnsureCapacity(t *testing.T) {
dest := NewSlice()
src := Slice(internal.GenerateTestSlice())
dest.EnsureCapacity(src.Len())
src.CopyTo(dest)
assert.Equal(t, Slice(internal.GenerateTestSlice()), dest)
}

func TestSlice_EnsureCapacity(t *testing.T) {
es := Slice(internal.GenerateTestSlice())
// Test ensure smaller capacity.
Expand Down
Loading