-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic plan serialization (#7785)
- Loading branch information
1 parent
8b12f30
commit 0276674
Showing
10 changed files
with
176 additions
and
103 deletions.
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
...tChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/IOperationPlanNodeProvider.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/IPlanNodeProvider.cs
This file contains 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,8 @@ | ||
namespace HotChocolate.Fusion.Planning.Nodes; | ||
|
||
public interface IPlanNodeProvider | ||
{ | ||
public IReadOnlyList<PlanNode> Nodes { get; } | ||
|
||
public void AddChildNode(PlanNode node); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/ISerializablePlanNode.cs
This file contains 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,10 @@ | ||
using System.Text.Json; | ||
|
||
namespace HotChocolate.Fusion.Planning; | ||
|
||
public interface ISerializablePlanNode | ||
{ | ||
PlanNodeKind Kind { get; } | ||
|
||
void Serialize(Utf8JsonWriter writer); | ||
} |
This file contains 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
7 changes: 7 additions & 0 deletions
7
src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/PlanNodeKind.cs
This file contains 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,7 @@ | ||
namespace HotChocolate.Fusion.Planning; | ||
|
||
public enum PlanNodeKind | ||
{ | ||
Root, | ||
Operation | ||
} |
54 changes: 28 additions & 26 deletions
54
src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/RootPlanNode.cs
This file contains 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 |
---|---|---|
@@ -1,42 +1,44 @@ | ||
using System.Collections.Immutable; | ||
using HotChocolate.Language; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace HotChocolate.Fusion.Planning.Nodes; | ||
|
||
public sealed class RootPlanNode : PlanNode, IOperationPlanNodeProvider | ||
public sealed class RootPlanNode : PlanNode, IPlanNodeProvider, ISerializablePlanNode | ||
{ | ||
private readonly List<OperationPlanNode> _operations = new(); | ||
private static readonly JsonWriterOptions SerializerOptions = new() | ||
{ | ||
Indented = true, | ||
}; | ||
private readonly List<PlanNode> _nodes = []; | ||
|
||
public IReadOnlyList<OperationPlanNode> Operations | ||
=> _operations; | ||
public IReadOnlyList<PlanNode> Nodes => _nodes; | ||
|
||
public void AddOperation(OperationPlanNode operation) | ||
public void AddChildNode(PlanNode node) | ||
{ | ||
ArgumentNullException.ThrowIfNull(operation); | ||
_operations.Add(operation); | ||
operation.Parent = this; | ||
ArgumentNullException.ThrowIfNull(node); | ||
_nodes.Add(node); | ||
node.Parent = this; | ||
} | ||
|
||
public DocumentNode ToSyntaxNode() | ||
public PlanNodeKind Kind => PlanNodeKind.Root; | ||
|
||
public string Serialize() | ||
{ | ||
var backlog = new Queue<OperationPlanNode>(); | ||
var definitions = ImmutableArray.CreateBuilder<IDefinitionNode>(); | ||
using var memoryStream = new MemoryStream(); | ||
var jsonWriter = new Utf8JsonWriter(memoryStream, SerializerOptions); | ||
|
||
foreach(var operation in _operations) | ||
{ | ||
backlog.Enqueue(operation); | ||
} | ||
Serialize(jsonWriter); | ||
|
||
while(backlog.TryDequeue(out var operation)) | ||
{ | ||
definitions.Add(operation.ToSyntaxNode()); | ||
return Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
} | ||
|
||
foreach(var child in operation.Operations) | ||
{ | ||
backlog.Enqueue(child); | ||
} | ||
} | ||
public void Serialize(Utf8JsonWriter writer) | ||
{ | ||
writer.WriteStartObject(); | ||
SerializationHelper.WriteKind(writer, this); | ||
SerializationHelper.WriteChildNodes(writer, this); | ||
writer.WriteEndObject(); | ||
|
||
return new DocumentNode(definitions.ToImmutable()); | ||
writer.Flush(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Nodes/SerializationHelper.cs
This file contains 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,29 @@ | ||
using System.Text.Json; | ||
|
||
namespace HotChocolate.Fusion.Planning.Nodes; | ||
|
||
public static class SerializationHelper | ||
{ | ||
public static void WriteChildNodes(Utf8JsonWriter writer, IPlanNodeProvider planNodeProvider) | ||
{ | ||
if (planNodeProvider.Nodes.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
writer.WritePropertyName("nodes"); | ||
writer.WriteStartArray(); | ||
|
||
foreach (var node in planNodeProvider.Nodes.OfType<ISerializablePlanNode>()) | ||
{ | ||
node.Serialize(writer); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
|
||
public static void WriteKind(Utf8JsonWriter writer, ISerializablePlanNode serializablePlanNode) | ||
{ | ||
writer.WriteString("kind", serializablePlanNode.Kind.ToString()); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.