@@ -49,88 +49,6 @@ func TestFromAttributeIndices(t *testing.T) {
4949	assert .Equal (t , attrs .AsRaw (), m )
5050}
5151
52- func  testPutAttribute (t  * testing.T , record  attributable ) {
53- 	dic  :=  NewProfilesDictionary ()
54- 	dic .StringTable ().Append ("" )
55- 	table  :=  NewKeyValueAndUnitSlice ()
56- 
57- 	// Put a first attribute. 
58- 	require .NoError (t , PutAttribute (table , record , dic , "hello" , pcommon .NewValueStr ("world" )))
59- 	assert .Equal (t , 1 , table .Len ())
60- 	assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
61- 
62- 	// Put an attribute, same key, same value. 
63- 	// This should be a no-op. 
64- 	require .NoError (t , PutAttribute (table , record , dic , "hello" , pcommon .NewValueStr ("world" )))
65- 	assert .Equal (t , 1 , table .Len ())
66- 	assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
67- 
68- 	// Special case: removing and adding again should not change the table as 
69- 	// this can lead to multiple identical attributes in the table. 
70- 	record .AttributeIndices ().FromRaw ([]int32 {})
71- 	require .NoError (t , PutAttribute (table , record , dic , "hello" , pcommon .NewValueStr ("world" )))
72- 	assert .Equal (t , 1 , table .Len ())
73- 	assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
74- 
75- 	// Put an attribute, same key, different value. 
76- 	// This updates the index and adds to the table. 
77- 	require .NoError (t , PutAttribute (table , record , dic , "hello" , pcommon .NewValueStr ("world2" )))
78- 	assert .Equal (t , 2 , table .Len ())
79- 	assert .Equal (t , []int32 {1 }, record .AttributeIndices ().AsRaw ())
80- 
81- 	// Put an attribute that already exists in the table. 
82- 	// This updates the index and does not add to the table. 
83- 	require .NoError (t , PutAttribute (table , record , dic , "hello" , pcommon .NewValueStr ("world" )))
84- 	assert .Equal (t , 2 , table .Len ())
85- 	assert .Equal (t , []int32 {0 }, record .AttributeIndices ().AsRaw ())
86- 
87- 	// Put a new attribute. 
88- 	// This adds an index and adds to the table. 
89- 	require .NoError (t , PutAttribute (table , record , dic , "good" , pcommon .NewValueStr ("day" )))
90- 	assert .Equal (t , 3 , table .Len ())
91- 	assert .Equal (t , []int32 {0 , 2 }, record .AttributeIndices ().AsRaw ())
92- 
93- 	// Add multiple distinct attributes. 
94- 	for  i  :=  range  100  {
95- 		require .NoError (t , PutAttribute (table , record , dic , fmt .Sprintf ("key_%d" , i ), pcommon .NewValueStr ("day" )))
96- 		assert .Equal (t , i + 4 , table .Len ())
97- 		assert .Equal (t , i + 3 , record .AttributeIndices ().Len ())
98- 	}
99- 
100- 	// Add a negative index to the record. 
101- 	record .AttributeIndices ().Append (- 1 )
102- 	tableLen  :=  table .Len ()
103- 	indicesLen  :=  record .AttributeIndices ().Len ()
104- 	// Try putting a new attribute, make sure it fails, and that table/indices didn't change. 
105- 	require .Error (t , PutAttribute (table , record , dic , "newKey" , pcommon .NewValueStr ("value" )))
106- 	require .Equal (t , tableLen , table .Len ())
107- 	require .Equal (t , indicesLen , record .AttributeIndices ().Len ())
108- 
109- 	// Set the last index to the table length, which is out of range. 
110- 	record .AttributeIndices ().SetAt (indicesLen - 1 , int32 (tableLen )) //nolint:gosec 
111- 	// Try putting a new attribute, make sure it fails, and that table/indices didn't change. 
112- 	require .Error (t , PutAttribute (table , record , dic , "newKey" , pcommon .NewValueStr ("value" )))
113- 	require .Equal (t , tableLen , table .Len ())
114- 	require .Equal (t , indicesLen , record .AttributeIndices ().Len ())
115- }
116- 
117- func  TestPutAttribute (t  * testing.T ) {
118- 	// Test every existing record type. 
119- 	for  _ , tt  :=  range  []struct  {
120- 		name  string 
121- 		attr  attributable 
122- 	}{
123- 		{"Profile" , NewProfile ()},
124- 		{"Sample" , NewSample ()},
125- 		{"Mapping" , NewMapping ()},
126- 		{"Location" , NewLocation ()},
127- 	} {
128- 		t .Run (tt .name , func (t  * testing.T ) {
129- 			testPutAttribute (t , tt .attr )
130- 		})
131- 	}
132- }
133- 
13452func  BenchmarkFromAttributeIndices (b  * testing.B ) {
13553	dic  :=  NewProfilesDictionary ()
13654	table  :=  NewKeyValueAndUnitSlice ()
@@ -153,80 +71,6 @@ func BenchmarkFromAttributeIndices(b *testing.B) {
15371	}
15472}
15573
156- func  BenchmarkPutAttribute (b  * testing.B ) {
157- 	for  _ , bb  :=  range  []struct  {
158- 		name   string 
159- 		key    string 
160- 		value  pcommon.Value 
161- 
162- 		runBefore  func (* testing.B , KeyValueAndUnitSlice , attributable , ProfilesDictionary )
163- 	}{
164- 		{
165- 			name :  "with a new string attribute" ,
166- 			key :   "attribute" ,
167- 			value : pcommon .NewValueStr ("test" ),
168- 		},
169- 		{
170- 			name :  "with an existing attribute" ,
171- 			key :   "attribute" ,
172- 			value : pcommon .NewValueStr ("test" ),
173- 
174- 			runBefore : func (_  * testing.B , table  KeyValueAndUnitSlice , _  attributable , dic  ProfilesDictionary ) {
175- 				dic .StringTable ().Append ("attribute" )
176- 				entry  :=  table .AppendEmpty ()
177- 				entry .SetKeyStrindex (int32 (dic .StringTable ().Len ())) //nolint:gosec // overflow impossible in test 
178- 				entry .Value ().SetStr ("test" )
179- 			},
180- 		},
181- 		{
182- 			name :  "with a duplicate attribute" ,
183- 			key :   "attribute" ,
184- 			value : pcommon .NewValueStr ("test" ),
185- 
186- 			runBefore : func (_  * testing.B , table  KeyValueAndUnitSlice , obj  attributable , dic  ProfilesDictionary ) {
187- 				require .NoError (b , PutAttribute (table , obj , dic , "attribute" , pcommon .NewValueStr ("test" )))
188- 			},
189- 		},
190- 		{
191- 			name :  "with a hundred attributes to loop through" ,
192- 			key :   "attribute" ,
193- 			value : pcommon .NewValueStr ("test" ),
194- 
195- 			runBefore : func (_  * testing.B , table  KeyValueAndUnitSlice , _  attributable , dic  ProfilesDictionary ) {
196- 				for  i  :=  range  100  {
197- 					dic .StringTable ().Append (fmt .Sprintf ("attr_%d" , i ))
198- 
199- 					entry  :=  table .AppendEmpty ()
200- 					entry .SetKeyStrindex (int32 (dic .StringTable ().Len ())) //nolint:gosec // overflow impossible in test 
201- 					entry .Value ().SetStr ("test" )
202- 				}
203- 
204- 				dic .StringTable ().Append ("attribute" )
205- 				entry  :=  table .AppendEmpty ()
206- 				entry .SetKeyStrindex (int32 (dic .StringTable ().Len ())) //nolint:gosec // overflow impossible in test 
207- 				entry .Value ().SetStr ("test" )
208- 			},
209- 		},
210- 	} {
211- 		b .Run (bb .name , func (b  * testing.B ) {
212- 			dic  :=  NewProfilesDictionary ()
213- 			table  :=  NewKeyValueAndUnitSlice ()
214- 			obj  :=  NewLocation ()
215- 
216- 			if  bb .runBefore  !=  nil  {
217- 				bb .runBefore (b , table , obj , dic )
218- 			}
219- 
220- 			b .ResetTimer ()
221- 			b .ReportAllocs ()
222- 
223- 			for  b .Loop () {
224- 				_  =  PutAttribute (table , obj , dic , bb .key , bb .value )
225- 			}
226- 		})
227- 	}
228- }
229- 
23074func  TestSetAttribute (t  * testing.T ) {
23175	table  :=  NewKeyValueAndUnitSlice ()
23276	attr  :=  NewKeyValueAndUnit ()
0 commit comments