Slight improvements to some Clone functions

This commit is contained in:
arberkatellari
2025-04-07 18:25:03 +02:00
committed by Dan Christian Bogos
parent de349e2482
commit 0e5c5a9e60
14 changed files with 301 additions and 93 deletions

View File

@@ -86,6 +86,9 @@ func (apl *ActionPlan) RemoveAccountID(accID string) (found bool) {
// Clone clones *ActionPlan
func (apl *ActionPlan) Clone() *ActionPlan {
if apl == nil {
return nil
}
cln := &ActionPlan{
Id: apl.Id,
AccountIDs: apl.AccountIDs.Clone(),

View File

@@ -114,6 +114,9 @@ type DispatcherProfile struct {
// Clone method for DispatcherProfile
func (dp *DispatcherProfile) Clone() *DispatcherProfile {
if dp == nil {
return nil
}
clone := &DispatcherProfile{
Tenant: dp.Tenant,
ID: dp.ID,
@@ -230,6 +233,9 @@ func (dHPrflIDs DispatcherHostIDs) Shuffle() {
}
func (dHPrflIDs DispatcherHostIDs) Clone() (cln DispatcherHostIDs) {
if dHPrflIDs == nil {
return
}
cln = make(DispatcherHostIDs, len(dHPrflIDs))
copy(cln, dHPrflIDs)
return

View File

@@ -205,6 +205,9 @@ type Filter struct {
// Clone method for Filter
func (fltr *Filter) Clone() *Filter {
if fltr == nil {
return nil
}
clone := &Filter{
Tenant: fltr.Tenant,
ID: fltr.ID,

View File

@@ -67,6 +67,9 @@ type AttributeProfile struct {
// Clone method for AttributeProfile struct
func (ap *AttributeProfile) Clone() *AttributeProfile {
if ap == nil {
return nil
}
clone := &AttributeProfile{
Tenant: ap.Tenant,
ID: ap.ID,

View File

@@ -37,6 +37,9 @@ type ChargerProfile struct {
// Clone method for ChargerProfile
func (cp *ChargerProfile) Clone() *ChargerProfile {
if cp == nil {
return nil
}
clone := &ChargerProfile{
Tenant: cp.Tenant,
ID: cp.ID,

View File

@@ -53,11 +53,15 @@ func (rkp *RankingProfile) TenantID() string {
// Clone will clone a RankingProfile
func (rkP *RankingProfile) Clone() (cln *RankingProfile) {
if rkP == nil {
return nil
}
cln = &RankingProfile{
Tenant: rkP.Tenant,
ID: rkP.ID,
Schedule: rkP.Schedule,
Sorting: rkP.Sorting,
Stored: rkP.Stored,
}
if rkP.StatIDs != nil {
cln.StatIDs = make([]string, len(rkP.StatIDs))
@@ -68,7 +72,6 @@ func (rkP *RankingProfile) Clone() (cln *RankingProfile) {
copy(cln.MetricIDs, rkP.MetricIDs)
}
if rkP.SortingParameters != nil {
cln.SortingParameters = make([]string, len(rkP.SortingParameters))
copy(cln.SortingParameters, rkP.SortingParameters)
}
@@ -127,27 +130,38 @@ type Ranking struct {
// Clone clones *Ranking
func (r *Ranking) Clone() *Ranking {
if r == nil {
return nil
}
r.rMux.RLock()
defer r.rMux.RUnlock()
cln := &Ranking{
Tenant: r.Tenant,
ID: r.ID,
LastUpdate: r.LastUpdate,
Sorting: r.Sorting,
SortingParameters: make([]string, len(r.SortingParameters)),
SortedStatIDs: make([]string, len(r.SortedStatIDs)),
Tenant: r.Tenant,
ID: r.ID,
LastUpdate: r.LastUpdate,
Sorting: r.Sorting,
}
copy(cln.SortingParameters, r.SortingParameters)
copy(cln.SortedStatIDs, r.SortedStatIDs)
cln.Metrics = make(map[string]map[string]float64)
for statID, metricMap := range r.Metrics {
cln.Metrics[statID] = make(map[string]float64)
maps.Copy(cln.Metrics[statID], metricMap)
if r.SortingParameters != nil {
cln.SortingParameters = make([]string, len(r.SortingParameters))
copy(cln.SortingParameters, r.SortingParameters)
}
if r.SortedStatIDs != nil {
cln.SortedStatIDs = make([]string, len(r.SortedStatIDs))
copy(cln.SortedStatIDs, r.SortedStatIDs)
}
if r.Metrics != nil {
cln.Metrics = make(map[string]map[string]float64)
for statID, metricMap := range r.Metrics {
cln.Metrics[statID] = make(map[string]float64)
maps.Copy(cln.Metrics[statID], metricMap)
}
}
if r.rkPrfl != nil {
cln.rkPrfl = r.rkPrfl.Clone()
}
cln.metricIDs = r.metricIDs.Clone()
if r.metricIDs != nil {
cln.metricIDs = r.metricIDs.Clone()
}
return cln
}

View File

@@ -25,6 +25,8 @@ import (
"strings"
"time"
"maps"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/guardian"
"github.com/cgrates/cgrates/utils"
@@ -48,7 +50,7 @@ type StatQueueProfile struct {
lkID string // holds the reference towards guardian lock key
}
// Clone clones *StatQueueProfile
// Clone clones *StatQueueProfile (lkID excluded)
func (sqp *StatQueueProfile) Clone() *StatQueueProfile {
if sqp == nil {
return nil
@@ -62,7 +64,6 @@ func (sqp *StatQueueProfile) Clone() *StatQueueProfile {
Stored: sqp.Stored,
Blocker: sqp.Blocker,
Weight: sqp.Weight,
lkID: sqp.lkID,
}
if sqp.FilterIDs != nil {
result.FilterIDs = make([]string, len(sqp.FilterIDs))
@@ -253,16 +254,23 @@ type StatQueue struct {
ttl *time.Duration // timeToLeave, picked on each init
}
// Clone clones *StatQueue
// Clone clones *StatQueue (lkID excluded)
func (sq *StatQueue) Clone() *StatQueue {
result := &StatQueue{
Tenant: sq.Tenant,
ID: sq.ID,
SQItems: make([]SQItem, len(sq.SQItems)),
SQMetrics: sq.SQMetrics,
lkID: sq.lkID,
if sq == nil {
return nil
}
result := &StatQueue{
Tenant: sq.Tenant,
ID: sq.ID,
}
if sq.SQItems != nil {
result.SQItems = make([]SQItem, len(sq.SQItems))
copy(result.SQItems, sq.SQItems)
}
if sq.SQMetrics != nil {
result.SQMetrics = make(map[string]StatMetric)
maps.Copy(result.SQMetrics, sq.SQMetrics)
}
copy(result.SQItems, sq.SQItems)
if sq.sqPrfl != nil {
result.sqPrfl = sq.sqPrfl.Clone()
}

View File

@@ -46,6 +46,9 @@ type TrendProfile struct {
// Clone will clone the TrendProfile so it can be used by scheduler safely
func (tP *TrendProfile) Clone() (clnTp *TrendProfile) {
if tP == nil {
return nil
}
clnTp = &TrendProfile{
Tenant: tP.Tenant,
ID: tP.ID,
@@ -124,6 +127,9 @@ type Trend struct {
}
func (t *Trend) Clone() (tC *Trend) {
if t == nil {
return nil
}
tC = &Trend{
Tenant: t.Tenant,
ID: t.ID,

View File

@@ -48,7 +48,7 @@ type ResourceProfile struct {
lkID string // holds the reference towards guardian lock key
}
// Clone clones *ResourceProfile
// Clone clones *ResourceProfile (lkID excluded)
func (rp *ResourceProfile) Clone() *ResourceProfile {
if rp == nil {
return nil
@@ -62,7 +62,6 @@ func (rp *ResourceProfile) Clone() *ResourceProfile {
Blocker: rp.Blocker,
Stored: rp.Stored,
Weight: rp.Weight,
lkID: rp.lkID,
}
if rp.FilterIDs != nil {
clone.FilterIDs = make([]string, len(rp.FilterIDs))
@@ -73,10 +72,7 @@ func (rp *ResourceProfile) Clone() *ResourceProfile {
copy(clone.ThresholdIDs, rp.ThresholdIDs)
}
if rp.ActivationInterval != nil {
clone.ActivationInterval = &utils.ActivationInterval{
ActivationTime: rp.ActivationInterval.ActivationTime,
ExpiryTime: rp.ActivationInterval.ExpiryTime,
}
clone.ActivationInterval = rp.ActivationInterval.Clone()
}
return clone
}
@@ -169,7 +165,7 @@ type Resource struct {
rPrf *ResourceProfile // for ordering purposes
}
// Clone clones *Resource
// Clone clones *Resource (lkID excluded)
func (r *Resource) Clone() *Resource {
if r == nil {
return nil
@@ -177,7 +173,6 @@ func (r *Resource) Clone() *Resource {
clone := &Resource{
Tenant: r.Tenant,
ID: r.ID,
lkID: r.lkID,
}
if r.Usages != nil {
clone.Usages = make(map[string]*ResourceUsage, len(r.Usages))
@@ -207,6 +202,11 @@ func (r *Resource) Clone() *Resource {
return clone
}
// CacheClone returns a clone of ActionPlan used by ltcache CacheCloner
func (apl *Resource) CacheClone() any {
return apl.Clone()
}
// resourceLockKey returns the ID used to lock a resource with guardian
func resourceLockKey(tnt, id string) string {
return utils.ConcatenatedKey(utils.CacheResources, tnt, id)

View File

@@ -104,6 +104,9 @@ type RouteProfile struct {
// Clone method for RouteProfile
func (rp *RouteProfile) Clone() *RouteProfile {
if rp == nil {
return nil
}
clone := &RouteProfile{
Tenant: rp.Tenant,
ID: rp.ID,

View File

@@ -120,16 +120,17 @@ type SMCost struct {
// Clone clones SMCost
func (s *SMCost) Clone() *SMCost {
clone := &SMCost{
CGRID: s.CGRID,
RunID: s.RunID,
OriginHost: s.OriginHost,
OriginID: s.OriginID,
CostSource: s.CostSource,
Usage: s.Usage,
if s == nil {
return nil
}
if s.CostDetails != nil {
clone.CostDetails = s.CostDetails.Clone()
clone := &SMCost{
CGRID: s.CGRID,
RunID: s.RunID,
OriginHost: s.OriginHost,
OriginID: s.OriginID,
CostSource: s.CostSource,
Usage: s.Usage,
CostDetails: s.CostDetails.Clone(),
}
return clone
}

View File

@@ -54,8 +54,11 @@ type ThresholdProfile struct {
lkID string // holds the reference towards guardian lock key
}
// Clone clones *ThresholdProfile
// Clone clones *ThresholdProfile (lkID excluded)
func (tp *ThresholdProfile) Clone() *ThresholdProfile {
if tp == nil {
return nil
}
clone := &ThresholdProfile{
Tenant: tp.Tenant,
ID: tp.ID,
@@ -65,7 +68,6 @@ func (tp *ThresholdProfile) Clone() *ThresholdProfile {
Blocker: tp.Blocker,
Weight: tp.Weight,
Async: tp.Async,
lkID: tp.lkID,
}
if tp.FilterIDs != nil {
clone.FilterIDs = make([]string, len(tp.FilterIDs))
@@ -139,14 +141,16 @@ type Threshold struct {
dirty *bool // needs save
}
// Clone clones *Threshold
// Clone clones *Threshold (lkID excluded)
func (t *Threshold) Clone() *Threshold {
if t == nil {
return nil
}
clone := &Threshold{
Tenant: t.Tenant,
ID: t.ID,
Hits: t.Hits,
Snooze: t.Snooze,
lkID: t.lkID,
}
if t.tPrfl != nil {
clone.tPrfl = t.tPrfl.Clone()