Skip to content

Commit bc317a2

Browse files
committed
Refactored Node.ID()
1 parent da3bde8 commit bc317a2

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

modules/aql/bloom.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const totalBits = bloomSize * bloomItemSize
99
type bloom [bloomSize]byte
1010

1111
// Calculate a bit to set or test
12-
func (b *bloom) hash(item engine.ObjectID) int {
12+
func (b *bloom) hash(item engine.NodeID) int {
1313
// incoming item value is a pointer, so mix it up a bit
1414
hash := int(item ^ item>>17 ^ item>>31)
1515

@@ -18,13 +18,13 @@ func (b *bloom) hash(item engine.ObjectID) int {
1818
return bit
1919
}
2020

21-
func (b *bloom) Add(item engine.ObjectID) {
21+
func (b *bloom) Add(item engine.NodeID) {
2222
// hash the value to a given bit
2323
bit := b.hash(item)
2424
b[bit/bloomItemSize] |= 1 << (bit % bloomItemSize)
2525
}
2626

27-
func (b *bloom) Has(item engine.ObjectID) bool {
27+
func (b *bloom) Has(item engine.NodeID) bool {
2828
// hash the value to a given bit
2929
bit := b.hash(item)
3030
return b[bit/bloomItemSize]&(1<<(bit%bloomItemSize)) != 0

modules/engine/graph.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77
"sync/atomic"
88
"time"
9-
"unsafe"
109

1110
gsync "github.com/SaveTheRbtz/generic-sync-map-go"
1211
"github.com/akyoto/cache"
@@ -377,13 +376,8 @@ func (os *IndexedGraph) NodeIndex(node *Node) (NodeIndexType, bool) {
377376
return os.nodeLookup.Load(node)
378377
}
379378

380-
func (os *IndexedGraph) LookupNodeByID(id ObjectID) (*Node, bool) {
381-
// It's a uintptr ... shoot me!
382-
fakenode := (*Node)(unsafe.Pointer(uintptr(id)))
383-
if os.Contains(fakenode) {
384-
return fakenode, true
385-
}
386-
return nil, false
379+
func (os *IndexedGraph) LookupNodeByID(id NodeID) (*Node, bool) {
380+
return os.Find(AttributeNodeId, AttributeValueInt(id))
387381
}
388382

389383
// Attemps to merge the object into the objects

modules/engine/node.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sync"
1212
"sync/atomic"
1313
"time"
14-
"unsafe"
1514

1615
"github.com/gofrs/uuid"
1716
"github.com/icza/gox/stringsx"
@@ -23,6 +22,9 @@ import (
2322
var threadbuckets = runtime.NumCPU() * runtime.NumCPU() * 64
2423
var threadsafeobjectmutexes = make([]sync.RWMutex, threadbuckets)
2524

25+
var AttributeNodeId = NewAttribute("nodeID").Single().Hidden()
26+
var uniqueNodeID atomic.Uint32
27+
2628
var UnknownGUID = uuid.UUID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
2729

2830
var BlankSID = windowssecurity.SID("")
@@ -48,10 +50,14 @@ func NewNode(flexinit ...any) *Node {
4850
}
4951

5052
// Temporary workaround
51-
type ObjectID uintptr
53+
type NodeID uint32
5254

53-
func (o *Node) ID() ObjectID {
54-
return ObjectID(uintptr(unsafe.Pointer(o)))
55+
func (o *Node) ID() NodeID {
56+
n := o.OneAttr(AttributeNodeId)
57+
if n == nil {
58+
return 0
59+
}
60+
return NodeID(n.(AttributeValueInt))
5561
}
5662

5763
func (o *Node) lockbucket() int {
@@ -686,6 +692,7 @@ func (o *Node) Meta() map[string]string {
686692

687693
func (o *Node) init() {
688694
o.values.init()
695+
o.Set(AttributeNodeId, AttributeValueInt(NodeID(uniqueNodeID.Add(1))))
689696
}
690697

691698
func (o *Node) String() string {

modules/frontend/ui.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func AddUIEndpoints(ws *WebService) {
190190
}
191191

192192
type APINodeDetails struct {
193-
ID engine.ObjectID `json:"id"`
193+
ID engine.NodeID `json:"id"`
194194
Label string `json:"label"`
195195
DistinguishedName string `json:"distinguishedname"`
196196
Attributes map[string][]string `json:"attributes"`
@@ -284,7 +284,7 @@ func AddDataEndpoints(ws *WebService) {
284284
c.String(500, "Error parsing ID")
285285
return
286286
}
287-
o, found = ws.SuperGraph.LookupNodeByID(engine.ObjectID(id))
287+
o, found = ws.SuperGraph.LookupNodeByID(engine.NodeID(id))
288288
// o, found = ws.SuperGraph.Find(engine.UniqueID, engine.NewAttributeValueGUID(id))
289289
case "dn", "distinguishedname":
290290
o, found = ws.SuperGraph.Find(activedirectory.DistinguishedName, engine.AttributeValueString(c.Param("id")))
@@ -329,7 +329,7 @@ func AddDataEndpoints(ws *WebService) {
329329
c.String(500, "Error parsing ID")
330330
return
331331
}
332-
o, found = ws.SuperGraph.LookupNodeByID(engine.ObjectID(thisId))
332+
o, found = ws.SuperGraph.LookupNodeByID(engine.NodeID(thisId))
333333
if !found {
334334
c.AbortWithStatus(404)
335335
return
@@ -407,7 +407,7 @@ func AddDataEndpoints(ws *WebService) {
407407
return
408408
}
409409

410-
if parent, found := ws.SuperGraph.LookupNodeByID(engine.ObjectID(id)); found {
410+
if parent, found := ws.SuperGraph.LookupNodeByID(engine.NodeID(id)); found {
411411
// if parent, found := ws.SuperGraph.Find(engine.UniqueID, engine.NewAttributeValueGUID(id)); found {
412412
children = parent.Children()
413413
} else {
@@ -416,10 +416,10 @@ func AddDataEndpoints(ws *WebService) {
416416
}
417417
}
418418
type treeData struct {
419-
Label string `json:"text"`
420-
Type string `json:"type,omitempty"`
421-
ID engine.ObjectID `json:"id"`
422-
Children bool `json:"children,omitempty"`
419+
Label string `json:"text"`
420+
Type string `json:"type,omitempty"`
421+
ID engine.NodeID `json:"id"`
422+
Children bool `json:"children,omitempty"`
423423
}
424424
var results []treeData
425425
children.Iterate(func(object *engine.Node) bool {

modules/frontend/xgmml.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package frontend
22

33
import (
44
"encoding/xml"
5+
56
"github.com/lkarlslund/adalanche/modules/engine"
67
)
78

@@ -37,15 +38,15 @@ type XGMMLNode struct {
3738
XMLName xml.Name `xml:"node"`
3839
Label string `xml:"label,attr"`
3940
Attributes []XGMMLAttribute
40-
Weight int `xml:"weight,attr,omitempty"`
41-
Id engine.ObjectID `xml:"id,attr"`
41+
Weight int `xml:"weight,attr,omitempty"`
42+
Id engine.NodeID `xml:"id,attr"`
4243
}
4344
type XGMMLEdge struct {
4445
XMLName xml.Name `xml:"edge"`
4546
Label string `xml:"label,attr"`
4647
Attributes []XGMMLAttribute
47-
Source engine.ObjectID `xml:"source,attr"`
48-
Target engine.ObjectID `xml:"target,attr"`
48+
Source engine.NodeID `xml:"source,attr"`
49+
Target engine.NodeID `xml:"target,attr"`
4950
}
5051
type XGMMLAttribute struct {
5152
XMLName xml.Name `xml:"att"`

0 commit comments

Comments
 (0)