-
Notifications
You must be signed in to change notification settings - Fork 8
feat: BED-7498 batch node update #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
483a59e
initial implementation
bsheth711 488df1a
fix sql
bsheth711 54ea6a9
add batch size option to batchoperation
bsheth711 5c177f7
update
bsheth711 dde02f3
update
bsheth711 444edaf
update
bsheth711 130d20a
added unit tests
bsheth711 482e1c4
change StripAllPropertiesExcept to respect deleted properties; simpli…
bsheth711 2da2db9
update comment
bsheth711 ea70c59
update comment
bsheth711 14010b8
feat: neo4j batched node updates
zinic 4840ed8
check rows.Err()
bsheth711 60f7db7
update graph test package
bsheth711 9cb3225
update
bsheth711 4864959
chore: add testing to update nodes cypher statement creation
zinic 3649653
fix: integration test the update node strategy for neo4j
zinic 66a72f4
safety improvements; improve pattern for passing options
bsheth711 d9faf72
fix test
bsheth711 21082bd
coderabbit suggestions
bsheth711 1ac2011
chore+fix: fix bug in key creation for kind batching; pr cleanup
zinic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package pgsql_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/specterops/dawgs/cypher/models/pgsql" | ||
| "github.com/specterops/dawgs/graph" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestDeletedPropertiesToString(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| setup func() *graph.Properties | ||
| check func(t *testing.T, result string) | ||
| }{ | ||
| { | ||
| name: "no deleted properties returns empty braces", | ||
| setup: func() *graph.Properties { | ||
| return graph.NewProperties() | ||
| }, | ||
| check: func(t *testing.T, result string) { | ||
| assert.Equal(t, "{}", result) | ||
| }, | ||
| }, | ||
| { | ||
| name: "single deleted property is wrapped in braces", | ||
| setup: func() *graph.Properties { | ||
| props := graph.NewProperties() | ||
| props.Set("mykey", "myvalue") | ||
| props.Delete("mykey") | ||
| return props | ||
| }, | ||
| check: func(t *testing.T, result string) { | ||
| assert.Equal(t, "{\"mykey\"}", result) | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple deleted properties are all present in output", | ||
| setup: func() *graph.Properties { | ||
| props := graph.NewProperties() | ||
| props.Set("alpha", 1) | ||
| props.Set("beta", 2) | ||
| props.Delete("alpha") | ||
| props.Delete("beta") | ||
| return props | ||
| }, | ||
| check: func(t *testing.T, result string) { | ||
| // Map iteration order is non-deterministic; accept either ordering. | ||
| assert.True(t, result == "{\"alpha\",\"beta\"}" || result == "{\"beta\",\"alpha\"}", | ||
| "unexpected result: %s", result) | ||
| }, | ||
| }, | ||
| { | ||
| name: "non-deleted properties are not included", | ||
| setup: func() *graph.Properties { | ||
| props := graph.NewProperties() | ||
| props.Set("active", "yes") | ||
| props.Set("removed", "no") | ||
| props.Delete("removed") | ||
| return props | ||
| }, | ||
| check: func(t *testing.T, result string) { | ||
| assert.Equal(t, "{\"removed\"}", result) | ||
| assert.NotContains(t, result, "active") | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| props := tc.setup() | ||
| result := pgsql.DeletedPropertiesToString(props) | ||
| tc.check(t, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| //go:build integration | ||
|
|
||
| package neo4j_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "os" | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/specterops/dawgs" | ||
| "github.com/specterops/dawgs/drivers/neo4j" | ||
| "github.com/specterops/dawgs/graph" | ||
| "github.com/specterops/dawgs/ops" | ||
| "github.com/specterops/dawgs/query" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| const Neo4jConnectionStringEnv = "NEO4J_CONNECTION" | ||
|
|
||
| var ( | ||
| NodeKind1 = graph.StringKind("NodeKind1") | ||
| NodeKind2 = graph.StringKind("NodeKind2") | ||
|
|
||
| NameProperty = "name" | ||
| HeatProperty = "heat" | ||
| NewProperty = "new" | ||
| ) | ||
|
|
||
| func prepareNode(index int) *graph.Node { | ||
| return graph.PrepareNode( | ||
| graph.AsProperties(map[string]any{ | ||
| NameProperty: "Node " + strconv.Itoa(index), | ||
| HeatProperty: 10 + index, | ||
| }), | ||
| NodeKind1, | ||
| ) | ||
| } | ||
|
|
||
| func TestBatchTransaction_NodeUpdate(t *testing.T) { | ||
| const ( | ||
| numNodes = 1_000 | ||
| ) | ||
|
|
||
| var ( | ||
| neo4jConnectionStr = os.Getenv(Neo4jConnectionStringEnv) | ||
| ctx, done = context.WithCancel(context.Background()) | ||
| ) | ||
|
|
||
| defer done() | ||
|
|
||
| if neo4jConnectionStr == "" { | ||
| t.Fatalf("No Neo4j connection string specified. Test requires a valid Neo4j connection string present in the %s environment variable.", Neo4jConnectionStringEnv) | ||
| } | ||
|
|
||
| graphDB, err := dawgs.Open(ctx, neo4j.DriverName, dawgs.Config{ | ||
| ConnectionString: neo4jConnectionStr, | ||
| GraphQueryMemoryLimit: 0, | ||
| }) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Regsiter a cleanup step to wipe the database | ||
|
zinic marked this conversation as resolved.
|
||
| t.Cleanup(func() { | ||
| cleanupCtx, done := context.WithTimeout(context.Background(), time.Minute) | ||
| defer done() | ||
|
|
||
| err := graphDB.WriteTransaction(cleanupCtx, func(tx graph.Transaction) error { | ||
| return tx.Nodes().Filter(query.Kind(query.Node(), NodeKind1)).Delete() | ||
| }) | ||
|
|
||
| if err != nil { | ||
| slog.Error("Failed to cleanup after test.", slog.String("err", err.Error())) | ||
| } | ||
| }) | ||
|
|
||
| // Insert nodes to batch update afterward | ||
| assert.NoError(t, | ||
| graphDB.BatchOperation(ctx, func(batch graph.Batch) error { | ||
| for idx := range numNodes { | ||
| if err := batch.CreateNode(prepareNode(idx)); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }), | ||
| ) | ||
|
|
||
| // Update the nodes in batch | ||
| assert.NoError(t, | ||
| graphDB.BatchOperation(ctx, func(batch graph.Batch) error { | ||
| // Fetch all of the nodes and ensure that they the number of nodes expected matches | ||
| if nodes, err := ops.FetchNodes(batch.Nodes().Filter(query.Kind(query.Node(), NodeKind1))); err != nil { | ||
| return err | ||
| } else { | ||
| assert.Equal(t, numNodes, len(nodes)) | ||
|
|
||
| for _, node := range nodes { | ||
| node.AddKinds(NodeKind2) | ||
|
|
||
| node.Properties.Set(NewProperty, true) | ||
| node.Properties.Delete(HeatProperty) | ||
| } | ||
|
|
||
| return batch.UpdateNodes(nodes) | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| // Assert the node update | ||
| assert.NoError(t, | ||
| graphDB.ReadTransaction(ctx, func(tx graph.Transaction) error { | ||
| // Fetch all of the nodes and ensure that they have been updated | ||
| if nodes, err := ops.FetchNodes(tx.Nodes().Filter(query.Kind(query.Node(), NodeKind1))); err != nil { | ||
| return err | ||
| } else { | ||
| assert.Equal(t, numNodes, len(nodes)) | ||
|
|
||
| for _, node := range nodes { | ||
| assert.True(t, node.Properties.Exists(HeatProperty)) | ||
| assert.Equal(t, true, node.Properties.Get(NewProperty).Any()) | ||
| } | ||
|
zinic marked this conversation as resolved.
|
||
|
|
||
| return nil | ||
| } | ||
| }), | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.