github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/converter/converter_raw.go (about)

     1  // Copyright 2022 iLogtail 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 protocol
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/alibaba/ilogtail/pkg/models"
    22  )
    23  
    24  func (c *Converter) ConvertToRawStream(groupEvents *models.PipelineGroupEvents, targetFields []string) (stream [][]byte, values []map[string]string, err error) {
    25  	if len(groupEvents.Events) == 0 {
    26  		return nil, nil, nil
    27  	}
    28  
    29  	var targetValues map[string]string
    30  	if len(targetFields) > 0 {
    31  		targetValues = findTargetFieldsInGroup(targetFields, groupEvents.Group)
    32  	}
    33  
    34  	if len(c.Separator) == 0 {
    35  		return getByteStream(groupEvents, targetValues)
    36  	}
    37  
    38  	return getByteStreamWithSep(groupEvents, targetValues, c.Separator)
    39  }
    40  
    41  func getByteStreamWithSep(groupEvents *models.PipelineGroupEvents, targetValues map[string]string, sep string) (stream [][]byte, values []map[string]string, err error) {
    42  	joinedStream := *GetPooledByteBuf()
    43  	for idx, event := range groupEvents.Events {
    44  		eventType := event.GetType()
    45  		if eventType != models.EventTypeByteArray {
    46  			return nil, nil, fmt.Errorf("unsupported event type %v", eventType)
    47  		}
    48  		if idx != 0 {
    49  			joinedStream = append(joinedStream, sep...)
    50  		}
    51  		joinedStream = append(joinedStream, event.(models.ByteArray)...)
    52  	}
    53  	return [][]byte{joinedStream}, []map[string]string{targetValues}, nil
    54  }
    55  
    56  func getByteStream(groupEvents *models.PipelineGroupEvents, targetValues map[string]string) (stream [][]byte, values []map[string]string, err error) {
    57  	byteGroup := make([][]byte, 0, len(groupEvents.Events))
    58  	valueGroup := make([]map[string]string, 0, len(groupEvents.Events))
    59  	for _, event := range groupEvents.Events {
    60  		eventType := event.GetType()
    61  		if eventType != models.EventTypeByteArray {
    62  			return nil, nil, fmt.Errorf("unsupported event type %v", eventType)
    63  		}
    64  
    65  		byteStream := *GetPooledByteBuf()
    66  		byteStream = append(byteStream, event.(models.ByteArray)...)
    67  		byteGroup = append(byteGroup, byteStream)
    68  		valueGroup = append(valueGroup, targetValues)
    69  	}
    70  	return byteGroup, valueGroup, nil
    71  }
    72  
    73  func findTargetFieldsInGroup(targetFields []string, group *models.GroupInfo) map[string]string {
    74  	if len(targetFields) == 0 {
    75  		return nil
    76  	}
    77  
    78  	targetKVs := make(map[string]string, len(targetFields))
    79  
    80  	for _, field := range targetFields {
    81  		var tagName, tagValue string
    82  		if strings.HasPrefix(field, targetGroupMetadataPrefix) {
    83  			tagName = field[len(targetGroupMetadataPrefix):]
    84  			tagValue = group.GetMetadata().Get(tagName)
    85  		} else if strings.HasPrefix(field, targetTagPrefix) {
    86  			tagName = field[len(targetTagPrefix):]
    87  			tagValue = group.GetTags().Get(tagName)
    88  		}
    89  		targetKVs[field] = tagValue
    90  	}
    91  
    92  	return targetKVs
    93  }