Skip to content

Commit

Permalink
Various attributes and edges refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Dec 15, 2023
1 parent 40f0f04 commit 53ea2e3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 51 deletions.
46 changes: 28 additions & 18 deletions modules/engine/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type mergeapproverinfo struct {
}

var mergeapprovers []mergeapproverinfo
var attributenums []attributeinfo
var attributeinfos []attributeinfo

var (
NonExistingAttribute = ^Attribute(0)
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
56 changes: 31 additions & 25 deletions modules/engine/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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
}

Expand All @@ -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++ {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions modules/engine/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions modules/engine/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53ea2e3

Please sign in to comment.