github.com/kiali/kiali@v1.84.0/tracing/jaeger/model/converter/json/process_hashtable.go (about)

     1  // Copyright (c) 2019 The Jaeger Authors.
     2  // Copyright (c) 2017 Uber Technologies, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package json
    17  
    18  import (
    19  	"strconv"
    20  
    21  	"github.com/kiali/kiali/tracing/jaeger/model"
    22  )
    23  
    24  type processHashtable struct {
    25  	count     int
    26  	processes map[uint64][]processKey
    27  	extHash   func(*model.Process) uint64
    28  }
    29  
    30  type processKey struct {
    31  	process *model.Process
    32  	key     string
    33  }
    34  
    35  // getKey assigns a new unique string key to the process, or returns
    36  // a previously assigned value if the process has already been seen.
    37  func (ph *processHashtable) getKey(process *model.Process) string {
    38  	if ph.processes == nil {
    39  		ph.processes = make(map[uint64][]processKey)
    40  	}
    41  	hash := ph.hash(process)
    42  	if keys, ok := ph.processes[hash]; ok {
    43  		for _, k := range keys {
    44  			if k.process.Equal(process) {
    45  				return k.key
    46  			}
    47  		}
    48  		key := ph.nextKey()
    49  		keys = append(keys, processKey{process: process, key: key})
    50  		ph.processes[hash] = keys
    51  		return key
    52  	}
    53  	key := ph.nextKey()
    54  	ph.processes[hash] = []processKey{{process: process, key: key}}
    55  	return key
    56  }
    57  
    58  // getMapping returns the accumulated mapping of string keys to processes.
    59  func (ph *processHashtable) getMapping() map[string]*model.Process {
    60  	out := make(map[string]*model.Process)
    61  	for _, keys := range ph.processes {
    62  		for _, key := range keys {
    63  			out[key.key] = key.process
    64  		}
    65  	}
    66  	return out
    67  }
    68  
    69  func (ph *processHashtable) nextKey() string {
    70  	ph.count++
    71  	key := "p" + strconv.Itoa(ph.count)
    72  	return key
    73  }
    74  
    75  func (ph processHashtable) hash(process *model.Process) uint64 {
    76  	if ph.extHash != nil {
    77  		// for testing collisions
    78  		return ph.extHash(process)
    79  	}
    80  	hc, _ := model.HashCode(process)
    81  	return hc
    82  }