forked from TrueCloudLab/frostfs-node
Airat Arifullin
2697d4d1fe
* Fix `subjectFromSubjectExtended` converter. Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
|
frostfsidcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/frostfsid"
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
)
|
|
|
|
type morphFrostfsIDCache struct {
|
|
subjProvider frostfsidcore.SubjectProvider
|
|
|
|
subjCache *expirable.LRU[util.Uint160, *client.Subject]
|
|
|
|
subjExtCache *expirable.LRU[util.Uint160, *client.SubjectExtended]
|
|
}
|
|
|
|
func newMorphFrostfsIDCache(subjProvider frostfsidcore.SubjectProvider, size int, ttl time.Duration) frostfsidcore.SubjectProvider {
|
|
return &morphFrostfsIDCache{
|
|
subjProvider: subjProvider,
|
|
|
|
subjCache: expirable.NewLRU(size, func(util.Uint160, *client.Subject) {}, ttl),
|
|
|
|
subjExtCache: expirable.NewLRU(size, func(util.Uint160, *client.SubjectExtended) {}, ttl),
|
|
}
|
|
}
|
|
|
|
func (m *morphFrostfsIDCache) GetSubject(addr util.Uint160) (*client.Subject, error) {
|
|
result, found := m.subjCache.Get(addr)
|
|
if found {
|
|
return result, nil
|
|
}
|
|
|
|
result, err := m.subjProvider.GetSubject(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.subjCache.Add(addr, result)
|
|
return result, nil
|
|
}
|
|
|
|
func (m *morphFrostfsIDCache) GetSubjectExtended(addr util.Uint160) (*client.SubjectExtended, error) {
|
|
subjExt, found := m.subjExtCache.Get(addr)
|
|
if found {
|
|
return subjExt, nil
|
|
}
|
|
|
|
var err error
|
|
subjExt, err = m.subjProvider.GetSubjectExtended(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.subjExtCache.Add(addr, subjExt)
|
|
m.subjCache.Add(addr, subjectFromSubjectExtended(subjExt))
|
|
|
|
return subjExt, nil
|
|
}
|
|
|
|
func subjectFromSubjectExtended(subjExt *client.SubjectExtended) *client.Subject {
|
|
return &client.Subject{
|
|
PrimaryKey: subjExt.PrimaryKey,
|
|
AdditionalKeys: subjExt.AdditionalKeys,
|
|
Namespace: subjExt.Namespace,
|
|
Name: subjExt.Name,
|
|
KV: subjExt.KV,
|
|
}
|
|
}
|