// getMatchingCpes returns matching CPEs  by pseudo-CPE
489184
func (r *RDBDriver) getMatchingCpes(uri string) ([]models.Cpe, error) {

	// parse wfn, get vendor, product
	specified, err := naming.UnbindURI(uri)
	if err != nil {
		return nil, err
	}
	// sleect from cpe by vendor, product
	cpes := []models.Cpe{}
	err = r.conn.Where(&models.Cpe{
		CpeBase: models.CpeBase{
			CpeWFN: models.CpeWFN{
				Vendor:  fmt.Sprintf("%s", specified.Get(common.AttributeVendor)),
				Product: fmt.Sprintf("%s", specified.Get(common.AttributeProduct)),
			},
		}}).Find(&cpes).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	log.Debugf("specified: %s", uri)
	filtered := []models.Cpe{}
	checkedIDs := map[uint]bool{}
	for _, cpe := range cpes {
		match, err := match(uri, cpe)
		if err != nil {
			log.Debugf("Failed to compare the version:%s %s cpe_id:%d %#v",
				err, uri, cpe.ID, cpe)

			// Try to exact match by vendor, product and version if the version in CPE is not a semVer style.
			if cpe.NvdJSONID != 0 {
				if _, ok := checkedIDs[cpe.NvdJSONID]; ok {
					continue
				}
				affects := []models.Affect{}
				result := r.conn.Where(&models.Affect{NvdJSONID: cpe.NvdJSONID}).Find(&affects)
				if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
					return nil, result.Error
				}

				ok, err := matchExactByAffects(uri, affects)
				if err != nil {
					return nil, err
				}
				if ok {
					filtered = append(filtered, cpe)
				}
				checkedIDs[cpe.NvdJSONID] = true
			}
		} else if match {
			filtered = append(filtered, cpe)
		}
	}

	return filtered, nil
}
1229816
func (s *Store) UpdateMatching(dataType interface{}, query *Query, update func(record interface{}) error) error {
	return s.Bolt().Update(func(tx *bolt.Tx) error {
		return s.TxUpdateMatching(tx, dataType, query, update)
	})
}
1093030
func (cpcodes *CpCodes) GetCpCodes() error {
	if cpcodes.Contract == nil {
		cpcodes.Contract = NewContract(NewContracts())
		cpcodes.Contract.ContractID = cpcodes.Group.ContractIDs[0]
	}

	req, err := client.NewRequest(
		Config,
		"GET",
		fmt.Sprintf(
			"/papi/v1/cpcodes?groupId=%s&contractId=%s",
			cpcodes.Group.GroupID,
			cpcodes.Contract.ContractID,
		),
		nil,
	)
	if err != nil {
		return err
	}

	res, err := client.Do(Config, req)
	if err != nil {
		return err
	}

	if client.IsError(res) {
		return client.NewAPIError(res)
	}

	if err = client.BodyJSON(res, cpcodes); err != nil {
		return err
	}

	return nil
}
1184330
func (f *ToolsFinder) matchingStorageTools(args params.FindToolsParams) (coretools.List, error) {
	storage, err := f.toolsStorageGetter.ToolsStorage()
	if err != nil {
		return nil, err
	}
	defer storage.Close()
	allMetadata, err := storage.AllMetadata()
	if err != nil {
		return nil, err
	}
	list := make(coretools.List, len(allMetadata))
	for i, m := range allMetadata {
		vers, err := version.ParseBinary(m.Version)
		if err != nil {
			return nil, errors.Annotatef(err, "unexpected bad version %q of agent binary in storage", m.Version)
		}
		list[i] = &coretools.Tools{
			Version: vers,
			Size:    m.Size,
			SHA256:  m.SHA256,
		}
	}
	list, err = list.Match(toolsFilter(args))
	if err != nil {
		return nil, err
	}
	var matching coretools.List
	for _, tools := range list {
		if args.MajorVersion != -1 && tools.Version.Major != args.MajorVersion {
			continue
		}
		if args.MinorVersion != -1 && tools.Version.Minor != args.MinorVersion {
			continue
		}
		matching = append(matching, tools)
	}
	if len(matching) == 0 {
		return nil, coretools.ErrNoMatches
	}
	return matching, nil
}
1035408
func getMatchingGlobalIP(addrs []net.Addr, family AddressFamily) (net.IP, error) {
	if len(addrs) > 0 {
		for i := range addrs {
			klog.V(4).Infof("Checking addr  %s.", addrs[i].String())
			ip, _, err := net.ParseCIDR(addrs[i].String())
			if err != nil {
				return nil, err
			}
			if memberOf(ip, family) {
				if ip.IsGlobalUnicast() {
					klog.V(4).Infof("IP found %v", ip)
					return ip, nil
				} else {
					klog.V(4).Infof("Non-global unicast address found %v", ip)
				}
			} else {
				klog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
			}

		}
	}
	return nil, nil
}
663797
func MemberMatchesPredicates(member Member, predicates ...MemberPredicate) bool {
	for _, p := range predicates {
		if !p(member) {
			return false
		}
	}
	return true
}
1082051
func Matchkeys(values, keys, searchset cty.Value) (cty.Value, error) {
	return MatchkeysFunc.Call([]cty.Value{values, keys, searchset})
}
636855
func (p *podEvaluator) MatchingScopes(item runtime.Object, scopeSelectors []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
	matchedScopes := []corev1.ScopedResourceSelectorRequirement{}
	for _, selector := range scopeSelectors {
		match, err := podMatchesScopeFunc(selector, item)
		if err != nil {
			return []corev1.ScopedResourceSelectorRequirement{}, fmt.Errorf("error on matching scope %v: %v", selector, err)
		}
		if match {
			matchedScopes = append(matchedScopes, selector)
		}
	}
	return matchedScopes, nil
}
971732
func (c *EventHistoryCollector) eventMatches(event types.BaseEvent) bool {
	spec := c.Filter.(types.EventFilterSpec)

	if !c.typeMatches(event, &spec) {
		return false
	}

	// TODO: spec.Time, spec.UserName, etc

	return c.entityMatches(event, &spec)
}
399917
func (c *MatchingCache) GetMatchingObject(labelObj objectWithMeta) (controller interface{}, exists bool) {
	key := keyFunc(labelObj)
	// NOTE: we use Lock() instead of RLock() here because lru's Get() method also modifies state(
	// it need update the least recently usage information). So we can not call it concurrently.
	c.mutex.Lock()
	defer c.mutex.Unlock()
	return c.cache.Get(key)
}
320061
func (p *serviceEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
	return []corev1.ScopedResourceSelectorRequirement{}, nil
}
487687
func (c *DirConstraint) hasMatchingParent(ctx context.Context, s *search, parents map[blob.Ref]struct{}) (bool, error) {
	for parent := range parents {
		meta, err := s.blobMeta(ctx, parent)
		if err != nil {
			if os.IsNotExist(err) {
				continue
			}
			return false, err
		}
		ok, err := c.blobMatches(ctx, s, parent, meta)
		if err != nil {
			return false, err
		}
		if ok {
			return true, nil
		}
	}
	return false, nil
}
1136437
func (o *objectCountEvaluator) MatchingScopes(item runtime.Object, scopes []corev1.ScopedResourceSelectorRequirement) ([]corev1.ScopedResourceSelectorRequirement, error) {
	return []corev1.ScopedResourceSelectorRequirement{}, nil
}
1024935
func (r *Rules) FindMatchingRule(c context.Context, q *RulesQuery) (*admin.DelegationRule, error) {
	var matches []*admin.DelegationRule
	for _, rule := range r.rules {
		switch yes, err := rule.matchesQuery(c, q); {
		case err != nil:
			return nil, err // usually transient
		case yes:
			matches = append(matches, rule.rule)
		}
	}

	if len(matches) == 0 {
		return nil, fmt.Errorf("no matching delegation rules in the config")
	}

	if len(matches) > 1 {
		names := make([]string, len(matches))
		for i, m := range matches {
			names[i] = fmt.Sprintf("%q", m.Name)
		}
		return nil, fmt.Errorf(
			"ambiguous request, multiple delegation rules match (%s)",
			strings.Join(names, ", "))
	}

	return matches[0], nil
}
1217836
func (p *Repository) getMatchingRules(securityIdentity *identity.Identity) (ingressMatch bool, egressMatch bool, matchingRules ruleSlice) {
	matchingRules = []*rule{}
	ingressMatch = false
	egressMatch = false
	for _, r := range p.rules {
		if ruleMatches := r.matches(securityIdentity); ruleMatches {
			// Don't need to update whether ingressMatch is true if it already
			// has been determined to be true - allows us to not have to check
			// lenth of slice.
			if !ingressMatch && len(r.Ingress) > 0 {
				ingressMatch = true
			}
			if !egressMatch && len(r.Egress) > 0 {
				egressMatch = true
			}
			matchingRules = append(matchingRules, r)
		}
	}
	return
}
621982
func (db *DB) GetCertPaths(cert *certificate.Certificate) (paths certificate.Paths, err error) {
	// check if we have a path in the cache
	if paths, ok := db.paths.Get(cert.Issuer.String()); ok {
		// the end-entity in the cache is likely another cert, so replace
		// it with the current one
		newpaths := paths.(certificate.Paths)
		newpaths.Cert = cert
		return newpaths, nil
	}
	// nothing in the cache, go to the database, recursively
	var ancestors []string
	paths, err = db.getCertPaths(cert, ancestors)
	if err != nil {
		return
	}
	// add the path into the cache
	db.paths.Add(cert.Issuer.String(), paths)
	return
}
477666
func compileMatcher(s string) Matcher {
	cs, err := cascadia.Compile(s)
	if err != nil {
		return invalidMatcher{}
	}
	return cs
}