github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/relation.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package manager for managing pipeline scripts
     7  package manager
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/GuanceCloud/cliutils/point"
    13  )
    14  
    15  // var remoteRelation = &PipelineRelation{}
    16  
    17  type ScriptRelation struct {
    18  	// map[<category>]: map[<source>]: <name>
    19  	relation map[point.Category]map[string]string
    20  
    21  	updateAt int64
    22  
    23  	rwMutex sync.RWMutex
    24  }
    25  
    26  func NewPipelineRelation() *ScriptRelation {
    27  	return &ScriptRelation{}
    28  }
    29  
    30  func (relation *ScriptRelation) UpdateAt() int64 {
    31  	relation.rwMutex.RLock()
    32  	defer relation.rwMutex.RUnlock()
    33  
    34  	return relation.updateAt
    35  }
    36  
    37  func (relation *ScriptRelation) UpdateRelation(updateAt int64, rel map[point.Category]map[string]string) {
    38  	relation.rwMutex.Lock()
    39  	defer relation.rwMutex.Unlock()
    40  
    41  	relation.updateAt = updateAt
    42  
    43  	// reset relation
    44  	relation.relation = map[point.Category]map[string]string{}
    45  
    46  	for cat, relat := range rel {
    47  		m := map[string]string{}
    48  		relation.relation[cat] = m
    49  		for source, name := range relat {
    50  			m[source] = name
    51  		}
    52  	}
    53  }
    54  
    55  func (relation *ScriptRelation) Query(cat point.Category, source string) (string, bool) {
    56  	relation.rwMutex.RLock()
    57  	defer relation.rwMutex.RUnlock()
    58  
    59  	if v, ok := relation.relation[cat]; ok {
    60  		if name, ok := v[source]; ok {
    61  			return name, true
    62  		}
    63  	}
    64  
    65  	return "", false
    66  }