go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/internal/test/intmeta.go (about)

     1  // Copyright (c) 2018 Cisco and/or its affiliates.
     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 test
    16  
    17  import (
    18  	"strconv"
    19  
    20  	"go.ligato.io/cn-infra/v2/idxmap"
    21  	"go.ligato.io/cn-infra/v2/idxmap/mem"
    22  	"go.ligato.io/cn-infra/v2/logging"
    23  )
    24  
    25  // NameToInteger is a idxmap specialization used in the UTs for scheduler.
    26  // It extends plain metadata with integer exposed as a secondary index.
    27  type NameToInteger interface {
    28  	// LookupByName retrieves a previously stored metadata identified by <name>.
    29  	LookupByName(valName string) (metadata MetaWithInteger, exists bool)
    30  
    31  	// LookupByIndex retrieves a previously stored metadata identified by the given
    32  	// integer index <metaInt>.
    33  	LookupByIndex(metaInt int) (valName string, metadata MetaWithInteger, exists bool)
    34  }
    35  
    36  // NameToIntegerRW extends NameToInteger with write access.
    37  type NameToIntegerRW interface {
    38  	NameToInteger
    39  	idxmap.NamedMappingRW
    40  }
    41  
    42  // MetaWithInteger is interface that metadata for NameToIntMap must implement.
    43  type MetaWithInteger interface {
    44  	// GetInteger returns the integer stored in the metadata.
    45  	GetInteger() int
    46  }
    47  
    48  // OnlyInteger is a minimal implementation of MetaWithInteger.
    49  type OnlyInteger struct {
    50  	Integer int
    51  }
    52  
    53  // GetInteger returns the integer stored in the metadata.
    54  func (idx *OnlyInteger) GetInteger() int {
    55  	return idx.Integer
    56  }
    57  
    58  // nameToInteger implements NameToInteger.
    59  type nameToInteger struct {
    60  	idxmap.NamedMappingRW
    61  }
    62  
    63  const (
    64  	// IntegerKey is a secondary index for the integer value.
    65  	IntegerKey = "integer"
    66  )
    67  
    68  // NewNameToInteger creates a new instance implementing NameToIntegerRW.
    69  func NewNameToInteger(title string) NameToIntegerRW {
    70  	return &nameToInteger{
    71  		NamedMappingRW: mem.NewNamedMapping(logging.DefaultLogger, title, internalIndexFunction),
    72  	}
    73  }
    74  
    75  // LookupByName retrieves a previously stored metadata identified by <name>.
    76  func (metaMap *nameToInteger) LookupByName(valName string) (metadata MetaWithInteger, exists bool) {
    77  	untypedMeta, found := metaMap.GetValue(valName)
    78  	if found {
    79  		if metadata, ok := untypedMeta.(MetaWithInteger); ok {
    80  			return metadata, found
    81  		}
    82  	}
    83  	return nil, false
    84  }
    85  
    86  // LookupByIndex retrieves a previously stored metadata identified by the given
    87  // integer index <metaInt>.
    88  func (metaMap *nameToInteger) LookupByIndex(metaInt int) (valName string, metadata MetaWithInteger, exists bool) {
    89  	res := metaMap.ListNames(IntegerKey, strconv.FormatUint(uint64(metaInt), 10))
    90  	if len(res) != 1 {
    91  		return
    92  	}
    93  	untypedMeta, found := metaMap.GetValue(res[0])
    94  	if found {
    95  		if metadata, ok := untypedMeta.(MetaWithInteger); ok {
    96  			return res[0], metadata, found
    97  		}
    98  	}
    99  	return
   100  }
   101  
   102  func internalIndexFunction(untypedMeta interface{}) map[string][]string {
   103  	indexes := map[string][]string{}
   104  	metadata, ok := untypedMeta.(MetaWithInteger)
   105  	if !ok || metadata == nil {
   106  		return indexes
   107  	}
   108  
   109  	indexes[IntegerKey] = []string{strconv.FormatUint(uint64(metadata.GetInteger()), 10)}
   110  	return indexes
   111  }