-
Notifications
You must be signed in to change notification settings - Fork 8
/
JobController.cs
142 lines (123 loc) · 4.37 KB
/
JobController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Diagnostics;
using System.IO.Pipelines;
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
namespace JsonWebApp.Controllers;
[ApiController]
public class JobController : ControllerBase
{
// Change to #if false to switch to low-allocation version from Example 21
#if false
[HttpPost]
[Route("/jobs/create")]
public void CreateJob([FromBody] JobDescription requestBody)
{
switch (requestBody.JobCategory)
{
case "arduous":
CreateArduousJob(requestBody.DepartmentId);
break;
case "tedious":
CreateTediousJob(requestBody.DepartmentId);
break;
}
}
public record JobDescription(int DepartmentId, string JobCategory);
#else
[HttpPost]
[Route("/jobs/create")]
public async ValueTask CreateJobFrugalAsync()
{
bool inDepartmentIdProperty = false;
bool inJobCategoryProperty = false;
int? departmentId = null;
bool? isArduous = null;
PipeReader reader = this.Request.BodyReader;
JsonReaderState jsonState = default;
while (true)
{
ReadResult result = await reader.ReadAsync().ConfigureAwait(false);
jsonState = ProcessBuffer(
result,
jsonState,
out SequencePosition position);
if (departmentId.HasValue && isArduous.HasValue)
{
if (isArduous.Value)
{
CreateArduousJob(departmentId.Value);
}
else
{
CreateTediousJob(departmentId.Value);
}
return;
}
reader.AdvanceTo(position);
if (result.IsCompleted)
{
break;
}
}
JsonReaderState ProcessBuffer(
in ReadResult result,
in JsonReaderState jsonState,
out SequencePosition position)
{
// This is a ref struct, so this has no GC overhead
var r = new Utf8JsonReader(result.Buffer, result.IsCompleted, jsonState);
while (r.Read())
{
if (inDepartmentIdProperty)
{
if (r.TokenType == JsonTokenType.Number)
{
if (r.TryGetInt32(out int v))
{
departmentId = v;
}
}
}
else if (inJobCategoryProperty)
{
if (r.TokenType == JsonTokenType.String)
{
if (r.ValueSpan.SequenceEqual("arduous"u8))
{
isArduous = true;
}
else if (r.ValueSpan.SequenceEqual("tedious"u8))
{
isArduous = false;
}
}
}
inDepartmentIdProperty = false;
inJobCategoryProperty = false;
if (r.TokenType == JsonTokenType.PropertyName)
{
if (r.ValueSpan.SequenceEqual("JobCategory"u8))
{
inJobCategoryProperty = true;
}
else if (r.ValueSpan.SequenceEqual("DepartmentId"u8))
{
inDepartmentIdProperty = true;
}
}
}
position = r.Position;
return r.CurrentState;
}
}
#endif
private static void CreateTediousJob(int departmentId)
{
Debug.WriteLine($"Real code would be creating a tedious job for {departmentId} at this point");
}
private static void CreateArduousJob(int departmentId)
{
Debug.WriteLine($"Real code would be creating an arduous job for {departmentId} at this point");
}
}