-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
FieldData.cs
153 lines (133 loc) · 4.35 KB
/
FieldData.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
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace HotChocolate.Execution
{
public class FieldData
: IReadOnlyDictionary<string, object>
, IEnumerable<FieldValue>
, IDisposable
{
private static readonly ArrayPool<FieldValue> _pool =
ArrayPool<FieldValue>.Create(1024, 64);
private FieldValue[] _rented;
private int _capacity;
public FieldData(int fields)
{
_capacity = fields;
_rented = _pool.Rent(fields);
}
public object GetFieldValue(int index)
{
if (_capacity <= index)
{
throw new ArgumentException(
"The specified index does not exist.",
nameof(index));
}
return _rented[index].Value;
}
public void SetFieldValue(int index, string key, object value)
{
if (_capacity <= index)
{
throw new ArgumentException(
"The specified index does not exist.",
nameof(index));
}
_rented[index] = new FieldValue(key, value);
}
public void Clear()
{
Dispose();
_rented = Array.Empty<FieldValue>();
_capacity = 0;
}
public void Dispose()
{
if (_rented.Length > 0)
{
for (var i = 0; i < _capacity; i++)
{
DisposeNested(_rented[i].Value);
}
_pool.Return(_rented, true);
}
}
private void DisposeNested(object o)
{
if (o is FieldData data)
{
data.Dispose();
}
else if (o is IList list)
{
for (var i = 0; i < list.Count; i++)
{
DisposeNested(list[i]);
}
}
}
private bool TryGetFieldValue(string key, out FieldValue fieldValue)
{
fieldValue = Array.Find(
_rented,
t => string.Equals(t.Key, key, StringComparison.Ordinal));
return fieldValue.HasValue;
}
object IReadOnlyDictionary<string, object>.this[string key]
{
get
{
if (TryGetFieldValue(key, out FieldValue value))
{
return value.Value;
}
throw new KeyNotFoundException($"The key {key} does not exist.");
}
}
bool IReadOnlyDictionary<string, object>.TryGetValue(string key, out object value)
{
if (TryGetFieldValue(key, out FieldValue fieldValue))
{
value = fieldValue.Value;
return true;
}
value = null;
return false;
}
bool IReadOnlyDictionary<string, object>.ContainsKey(string key)
{
for (int i = 0; i < _capacity; i++)
{
FieldValue value = _rented[i];
if (value.HasValue && string.Equals(value.Key, key, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
IEnumerable<string> IReadOnlyDictionary<string, object>.Keys =>
_rented.Take(_capacity)
.Where(t => t.HasValue)
.Select(t => t.Key);
IEnumerable<object> IReadOnlyDictionary<string, object>.Values =>
_rented.Take(_capacity)
.Where(t => t.HasValue)
.Select(t => t.Value);
int IReadOnlyCollection<KeyValuePair<string, object>>.Count => _capacity;
public IEnumerator<FieldValue> GetEnumerator() =>
_rented.Take(_capacity)
.Where(t => t.HasValue)
.GetEnumerator();
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() =>
_rented.Take(_capacity)
.Where(t => t.HasValue)
.Select(t => new KeyValuePair<string, object>(t.Key, t.Value))
.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}