github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/structs/transporttrie/merge.go (about)

     1  package transporttrie
     2  
     3  import "github.com/pyroscope-io/pyroscope/pkg/structs/merge"
     4  
     5  func (dstTrie *Trie) Merge(srcTrieI merge.Merger) {
     6  	srcTrie := srcTrieI.(*Trie)
     7  	srcNodes := []*trieNode{srcTrie.root}
     8  	dstNodes := []*trieNode{dstTrie.root}
     9  
    10  	for len(srcNodes) > 0 {
    11  		st := srcNodes[0]
    12  		srcNodes = srcNodes[1:]
    13  
    14  		dt := dstNodes[0]
    15  		dstNodes = dstNodes[1:]
    16  
    17  		for _, srcChildNode := range st.children {
    18  			dt.findNodeAt(srcChildNode.name, func(dstChildNode *trieNode) {
    19  				if srcChildNode.value > 0 {
    20  					dstChildNode.value = mergeFunc(dstChildNode.value, srcChildNode.value, dstTrie, srcTrie)
    21  				}
    22  				srcNodes = append([]*trieNode{srcChildNode}, srcNodes...)
    23  				dstNodes = append([]*trieNode{dstChildNode}, dstNodes...)
    24  			})
    25  		}
    26  	}
    27  }