Skip to content

Commit

Permalink
efficiency improvement for saving nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Solender committed Dec 15, 2019
1 parent 4b36fff commit 11b8527
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 619 deletions.
92 changes: 50 additions & 42 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ func saveDepth(sess *driver.BoltConn, obj interface{}, depth int) error {
nodeRef := map[string]*reflect.Value{}

newNodes := []*string{}
visited := []string{}

rootVal := reflect.ValueOf(obj)

err := parseStruct("", "", false, 0, nil, &rootVal, 0, depth, &nodes, &relations, &oldRels, &newNodes, &nodeRef)
err := parseStruct("", "", false, dsl.DirectionBoth, nil, &rootVal, 0, depth, &nodes, &relations, &oldRels, &newNodes, &nodeRef, &visited)
if err != nil {
return err
}
Expand Down Expand Up @@ -604,7 +605,7 @@ func generateCurRels(parentId string, current *reflect.Value, currentDepth, maxD
// parses tree of structs
func parseStruct(parentId, edgeLabel string, parentIsStart bool, direction dsl.Direction, edgeParams map[string]interface{}, current *reflect.Value,
currentDepth, maxDepth int, nodesPtr *map[string]map[string]nodeCreateConf, relationsPtr *map[string][]relCreateConf, oldRels *map[string]map[string]*RelationConfig,
newNodes *[]*string, nodeRef *map[string]*reflect.Value) error {
newNodes *[]*string, nodeRef *map[string]*reflect.Value, visited *[]string) error {
//check if its done
if currentDepth > maxDepth {
return nil
Expand Down Expand Up @@ -642,6 +643,42 @@ func parseStruct(parentId, edgeLabel string, parentIsStart bool, direction dsl.D
return err
}

//set edge
if parentId != "" && parentIsStart {
if _, ok := (*relationsPtr)[edgeLabel]; !ok {
(*relationsPtr)[edgeLabel] = []relCreateConf{}
}

start := ""
end := ""

//if parentIsStart {
start = parentId
end = id
//} else {
// start = id
// end = parentId
//}

if edgeParams == nil {
edgeParams = map[string]interface{}{}
}

(*relationsPtr)[edgeLabel] = append((*relationsPtr)[edgeLabel], relCreateConf{
Direction: direction,
Params: edgeParams,
StartNodeUUID: start,
EndNodeUUID: end,
})
}

//check if this has been visited yet, do this after edge is intentional
if stringSliceContains(*visited, id) {
return nil
} else {
*visited = append(*visited, id)
}

if !isNewNode {
if _, ok := (*oldRels)[id]; !ok {
(*oldRels)[id] = relConf
Expand Down Expand Up @@ -677,35 +714,6 @@ func parseStruct(parentId, edgeLabel string, parentIsStart bool, direction dsl.D
Params: params,
}

//set edge
if parentId != "" {
if _, ok := (*relationsPtr)[edgeLabel]; !ok {
(*relationsPtr)[edgeLabel] = []relCreateConf{}
}

start := ""
end := ""

if parentIsStart {
start = parentId
end = id
} else {
start = id
end = parentId
}

if edgeParams == nil {
edgeParams = map[string]interface{}{}
}

(*relationsPtr)[edgeLabel] = append((*relationsPtr)[edgeLabel], relCreateConf{
Direction: direction,
Params: edgeParams,
StartNodeUUID: start,
EndNodeUUID: end,
})
}

for _, conf := range currentConf.Fields {
if conf.Relationship == "" {
continue
Expand All @@ -727,32 +735,32 @@ func parseStruct(parentId, edgeLabel string, parentIsStart bool, direction dsl.D
for i := 0; i < slLen; i++ {
relVal := relField.Index(i)

newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, skip, err := processStruct(conf, &relVal, id, parentId)
newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, _, err := processStruct(conf, &relVal, id, parentId)
if err != nil {
return err
}

//makes us go backwards
if skip {
continue
}
////makes us go backwards
//if skip {
// continue
//}

err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef)
err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef, visited)
if err != nil {
return err
}
}
} else {
newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, skip, err := processStruct(conf, &relField, id, parentId)
newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, _, _, err := processStruct(conf, &relField, id, parentId)
if err != nil {
return err
}

if skip {
continue
}
//if skip {
// continue
//}

err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef)
err = parseStruct(newParentId, newEdgeLabel, newParentIdStart, newDirection, newEdgeParams, followVal, currentDepth+1, maxDepth, nodesPtr, relationsPtr, oldRels, newNodes, nodeRef, visited)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package gogm

import (
dsl "github.com/mindstand/go-cypherdsl"
"github.com/stretchr/testify/require"
"reflect"
"testing"
Expand Down Expand Up @@ -87,7 +88,7 @@ func parseO2O(req *require.Assertions) {
val := reflect.ValueOf(comp1)
nodeRef := map[string]*reflect.Value{}

req.Nil(parseStruct("", "", false, 0, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef))
req.Nil(parseStruct("", "", false, dsl.DirectionBoth, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef, &[]string{}))
req.Nil(generateCurRels("", &val, 0, 5, &curRels))
req.Equal(2, len(nodes))
req.Equal(1, len(nodes["a"]))
Expand Down Expand Up @@ -144,7 +145,7 @@ func parseM2O(req *require.Assertions) {

val := reflect.ValueOf(a1)
nodeRef := map[string]*reflect.Value{}
req.Nil(parseStruct("", "", false, 0, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef))
req.Nil(parseStruct("", "", false, dsl.DirectionBoth, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef, &[]string{}))
req.Nil(generateCurRels("", &val, 0, 5, &curRels))
req.Equal(2, len(nodes))
req.Equal(1, len(nodes["a"]))
Expand Down Expand Up @@ -201,7 +202,7 @@ func parseM2M(req *require.Assertions) {

val := reflect.ValueOf(a1)

req.Nil(parseStruct("", "", false, 0, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef))
req.Nil(parseStruct("", "", false, dsl.DirectionBoth, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef, &[]string{}))
req.Nil(generateCurRels("", &val, 0, 5, &curRels))
req.Equal(2, len(nodes))
req.Equal(1, len(nodes["a"]))
Expand Down Expand Up @@ -262,7 +263,7 @@ func TestCalculateCurRels(t *testing.T) {

val := reflect.ValueOf(a1)

req.Nil(parseStruct("", "", false, 0, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef))
req.Nil(parseStruct("", "", false, dsl.DirectionBoth, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef, &[]string{}))
req.Nil(generateCurRels("", &val, 0, 5, &curRels))
req.Equal(1, len(curRels))
}
Expand Down
47 changes: 0 additions & 47 deletions testing_/integration_test.go

This file was deleted.

Loading

0 comments on commit 11b8527

Please sign in to comment.