From 53ea2e317d79c0b1acf2e41b3115e36c699bb5fb Mon Sep 17 00:00:00 2001 From: Lars Karlslund Date: Fri, 15 Dec 2023 15:09:11 +0100 Subject: [PATCH] Various attributes and edges refactoring --- modules/engine/attributes.go | 46 +++++++++++++++++------------ modules/engine/edge.go | 56 ++++++++++++++++++++---------------- modules/engine/object.go | 6 ++-- modules/engine/objects.go | 2 +- modules/engine/processing.go | 8 +++--- 5 files changed, 67 insertions(+), 51 deletions(-) diff --git a/modules/engine/attributes.go b/modules/engine/attributes.go index c9f4e63..898d4e3 100644 --- a/modules/engine/attributes.go +++ b/modules/engine/attributes.go @@ -51,7 +51,7 @@ type mergeapproverinfo struct { } var mergeapprovers []mergeapproverinfo -var attributenums []attributeinfo +var attributeinfos []attributeinfo var ( NonExistingAttribute = ^Attribute(0) @@ -171,10 +171,10 @@ func NewAttribute(name string) Attribute { return attribute } - newindex := Attribute(len(attributenums)) + newindex := Attribute(len(attributeinfos)) attributenames[lowername] = newindex attributenames[name] = newindex - attributenums = append(attributenums, attributeinfo{ + attributeinfos = append(attributeinfos, attributeinfo{ name: name, }) attributemutex.Unlock() @@ -187,62 +187,62 @@ func (a Attribute) String() string { return "N/A" } attributemutex.RLock() - result := attributenums[a].name + result := attributeinfos[a].name attributemutex.RUnlock() return result } func (a Attribute) Type(t AttributeType) Attribute { attributemutex.Lock() - attributenums[a].atype = t + attributeinfos[a].atype = t attributemutex.Unlock() return a } func (a Attribute) Single() Attribute { attributemutex.Lock() - attributenums[a].single = true + attributeinfos[a].single = true attributemutex.Unlock() return a } func (a Attribute) IsSingle() bool { attributemutex.RLock() - result := attributenums[a].single + result := attributeinfos[a].single attributemutex.RUnlock() return result } func (a Attribute) Unique() Attribute { attributemutex.Lock() - attributenums[a].unique = true + attributeinfos[a].unique = true attributemutex.Unlock() return a } func (a Attribute) IsNonUnique() bool { attributemutex.RLock() - result := !attributenums[a].unique + result := !attributeinfos[a].unique attributemutex.RUnlock() return result } func (a Attribute) IsUnique() bool { attributemutex.RLock() - result := attributenums[a].unique + result := attributeinfos[a].unique attributemutex.RUnlock() return result } func (a Attribute) Hidden() Attribute { attributemutex.Lock() - attributenums[a].hidden = true + attributeinfos[a].hidden = true attributemutex.Unlock() return a } func (a Attribute) IsHidden() bool { - return attributenums[a].hidden + return attributeinfos[a].hidden } var ErrDontMerge = errors.New("Dont merge objects using any methods") @@ -256,7 +256,7 @@ func StandardMerge(attr Attribute, a, b *Object) (*Object, error) { func (a Attribute) Merge() Attribute { attributemutex.Lock() - attributenums[a].merge = true + attributeinfos[a].merge = true attributemutex.Unlock() return a } @@ -272,28 +272,28 @@ func AddMergeApprover(name string, mf mergefunc) { func (a Attribute) Tag(t string) Attribute { attributemutex.Lock() - attributenums[a].tags = append(attributenums[a].tags, t) + attributeinfos[a].tags = append(attributeinfos[a].tags, t) attributemutex.Unlock() return a } func (a Attribute) SetDescription(t string) Attribute { attributemutex.Lock() - attributenums[a].description = t + attributeinfos[a].description = t attributemutex.Unlock() return a } func (a Attribute) OnSet(onset AttributeSetFunc) Attribute { attributemutex.Lock() - attributenums[a].onset = onset + attributeinfos[a].onset = onset attributemutex.Unlock() return a } func (a Attribute) OnGet(onget AttributeGetFunc) Attribute { attributemutex.Lock() - attributenums[a].onget = onget + attributeinfos[a].onget = onget attributemutex.Unlock() return a } @@ -318,9 +318,19 @@ func (a Attribute) IsMeta() bool { func Attributes() []Attribute { var results []Attribute attributemutex.RLock() - for i := range attributenums { + for i := range attributeinfos { results = append(results, Attribute(i)) } attributemutex.RUnlock() return results } + +func AttributeInfos() []attributeinfo { + result := make([]attributeinfo, len(attributeinfos)) + attributemutex.RLock() + for i := 0; i < len(attributeinfos); i++ { + result[i] = attributeinfos[i] + } + attributemutex.RUnlock() + return result +} diff --git a/modules/engine/edge.go b/modules/engine/edge.go index 184adf3..d822113 100644 --- a/modules/engine/edge.go +++ b/modules/engine/edge.go @@ -196,12 +196,12 @@ type edgeInfo struct { probability ProbabilityCalculatorFunction Name string Description string - tags map[string]struct{} - multi bool // If true, this attribute can have multiple values - nonunique bool // Doing a Find on this attribute will return multiple results - merge bool // If true, objects can be merged on this attribute - hidden bool // If true, this attribute is not shown in the UI - defaultf, defaultm, defaultl bool + Tags map[string]struct{} + Multi bool // If true, this attribute can have multiple values + Nonunique bool // Doing a Find on this attribute will return multiple results + Merge bool // If true, objects can be merged on this attribute + Hidden bool // If true, this attribute is not shown in the UI + DefaultF, DefaultM, DefaultL bool } func NewEdge(name string) Edge { @@ -228,9 +228,9 @@ func NewEdge(name string) Edge { edgeInfos = append(edgeInfos, &edgeInfo{ Name: name, - defaultf: true, - defaultm: true, - defaultl: true, + DefaultF: true, + DefaultM: true, + DefaultL: true, }) edgeNames[lowername] = newindex edgeMutex.Unlock() @@ -246,49 +246,49 @@ func (p Edge) String() string { } func (p Edge) DefaultF() bool { - return edgeInfos[p].defaultf + return edgeInfos[p].DefaultF } func (p Edge) DefaultM() bool { - return edgeInfos[p].defaultm + return edgeInfos[p].DefaultM } func (p Edge) DefaultL() bool { - return edgeInfos[p].defaultl + return edgeInfos[p].DefaultL } func (p Edge) SetDefault(f, m, l bool) Edge { edgeMutex.Lock() - edgeInfos[p].defaultf = f - edgeInfos[p].defaultm = m - edgeInfos[p].defaultl = l + edgeInfos[p].DefaultF = f + edgeInfos[p].DefaultM = m + edgeInfos[p].DefaultL = l edgeMutex.Unlock() return p } func (p Edge) Hidden() Edge { edgeMutex.Lock() - edgeInfos[p].hidden = true + edgeInfos[p].Hidden = true edgeMutex.Unlock() return p } func (p Edge) IsHidden() bool { - return edgeInfos[p].hidden + return edgeInfos[p].Hidden } func (p Edge) Tag(t string) Edge { - tags := edgeInfos[p].tags + tags := edgeInfos[p].Tags if tags == nil { tags = make(map[string]struct{}) - edgeInfos[p].tags = tags + edgeInfos[p].Tags = tags } tags[t] = struct{}{} return p } func (p Edge) HasTag(t string) bool { - _, found := edgeInfos[p].tags[t] + _, found := edgeInfos[p].Tags[t] return found } @@ -301,11 +301,7 @@ func LookupEdge(name string) Edge { return NonExistingEdgeType } -func E(name string) Edge { - return LookupEdge(name) -} - -func AllEdgesSlice() []Edge { +func Edges() []Edge { result := make([]Edge, len(edgeInfos)) edgeMutex.RLock() for i := 0; i < len(edgeInfos); i++ { @@ -315,6 +311,16 @@ func AllEdgesSlice() []Edge { return result } +func EdgeInfos() []edgeInfo { + result := make([]edgeInfo, len(edgeInfos)) + edgeMutex.RLock() + for i := 0; i < len(edgeInfos); i++ { + result[i] = *edgeInfos[i] + } + edgeMutex.RUnlock() + return result +} + var ( NonExistingEdgeType = Edge(10000) AnyEdgeType = Edge(9999) diff --git a/modules/engine/object.go b/modules/engine/object.go index b588949..1499518 100644 --- a/modules/engine/object.go +++ b/modules/engine/object.go @@ -485,8 +485,8 @@ func (o *Object) get(attr Attribute) (AttributeValues, bool) { if attr == NonExistingAttribute { return NoValues{}, false } - if attributenums[attr].onget != nil { - return attributenums[attr].onget(o, attr) + if attributeinfos[attr].onget != nil { + return attributeinfos[attr].onget(o, attr) } return o.values.Get(attr) } @@ -948,7 +948,7 @@ func (o *Object) String() string { if attr == NTSecurityDescriptor { return true // continue } - result += " " + attributenums[attr].name + ":\n" + result += " " + attributeinfos[attr].name + ":\n" values.Iterate(func(value AttributeValue) bool { cleanval := stringsx.Clean(value.String()) if cleanval != value.String() { diff --git a/modules/engine/objects.go b/modules/engine/objects.go index d59ca6b..97bbe97 100644 --- a/modules/engine/objects.go +++ b/modules/engine/objects.go @@ -387,7 +387,7 @@ func (os *Objects) Merge(attrtomerge []Attribute, source *Object) bool { } // ui.Trace().Msgf("Merging %v with %v on attribute %v", o.Label(), mergetarget.Label(), mergeattr.String()) - attributenums[int(mergeattr)].mergeSuccesses.Add(1) + attributeinfos[int(mergeattr)].mergeSuccesses.Add(1) target.Absorb(source) os.ReindexObject(target, false) diff --git a/modules/engine/processing.go b/modules/engine/processing.go index 5bf91f9..0986724 100644 --- a/modules/engine/processing.go +++ b/modules/engine/processing.go @@ -12,15 +12,15 @@ import ( func getMergeAttributes() []Attribute { var mergeon []Attribute attributemutex.RLock() - for i := range attributenums { - if attributenums[i].merge { + for i := range attributeinfos { + if attributeinfos[i].merge { mergeon = append(mergeon, Attribute(i)) } } attributemutex.RUnlock() sort.Slice(mergeon, func(i, j int) bool { - isuccess := attributenums[mergeon[i]].mergeSuccesses.Load() - jsuccess := attributenums[mergeon[j]].mergeSuccesses.Load() + isuccess := attributeinfos[mergeon[i]].mergeSuccesses.Load() + jsuccess := attributeinfos[mergeon[j]].mergeSuccesses.Load() return jsuccess < isuccess }) return mergeon