[#13] interface: Add methods to CachedChainStorage interface

* Introduce GetOverride, RemoveOverride, ListOverrides
* Implement them in inmemory struct

Signed-off-by: aarifullin <aarifullin@yadro.com>
This commit is contained in:
aarifullin 2023-10-31 02:03:28 +03:00
parent 35f24627f0
commit 76372aac04
2 changed files with 40 additions and 6 deletions

View file

@ -1,12 +1,9 @@
package policyengine package policyengine
import "sync/atomic"
type inmemory struct { type inmemory struct {
chainCounter atomic.Uint32 namespace map[Name][]chain
namespace map[Name][]chain resource map[Name][]chain
resource map[Name][]chain local map[Name][]*Chain
local map[Name][]*Chain
} }
type chain struct { type chain struct {
@ -77,3 +74,34 @@ func (s *inmemory) AddNameSpaceChain(name Name, namespace string, c *Chain) {
func (s *inmemory) AddOverride(name Name, c *Chain) { func (s *inmemory) AddOverride(name Name, c *Chain) {
s.local[name] = append(s.local[name], c) s.local[name] = append(s.local[name], c)
} }
func (s *inmemory) GetOverride(name Name, chainID ChainID) (chain *Chain, found bool) {
chains := s.local[name]
for _, chain = range chains {
if chain.ID == chainID {
found = true
return
}
}
return
}
func (s *inmemory) RemoveOverride(name Name, chainID ChainID) (found bool) {
chains := s.local[name]
for i, chain := range chains {
if chain.ID == chainID {
s.local[name] = append(chains[:i], chains[i+1:]...)
found = true
return
}
}
return
}
func (s *inmemory) ListOverrides(name Name) []*Chain {
return s.local[name]
}

View file

@ -9,4 +9,10 @@ type CachedChainStorage interface {
AddNameSpaceChain(name Name, namespace string, c *Chain) AddNameSpaceChain(name Name, namespace string, c *Chain)
// Adds a local policy chain used for all operations with this service. // Adds a local policy chain used for all operations with this service.
AddOverride(name Name, c *Chain) AddOverride(name Name, c *Chain)
// Gets the local override with given chain id.
GetOverride(name Name, chainID ChainID) (chain *Chain, found bool)
// Remove the local override with given chain id.
RemoveOverride(name Name, chainID ChainID) (removed bool)
// ListOverrides returns the list of local overrides.
ListOverrides(name Name) []*Chain
} }