Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
148 lines (129 loc) · 5.88 KB

File metadata and controls

148 lines (129 loc) · 5.88 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
namespace Simple.Data.Ado
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Extensions;
using QueryPolyfills;
public class EagerLoadingEnumerable : IEnumerable<IDictionary<string,object>>
{
private readonly IEnumerable<IDictionary<string, object>> _source;
private readonly object _listLock = new object();
private IList<IDictionary<string, object>> _list;
public EagerLoadingEnumerable(IEnumerable<IDictionary<string, object>> source)
{
_source = source;
}
public IEnumerator<IDictionary<string, object>> GetEnumerator()
{
lock (_listLock)
{
if (_list == null)
{
_list = CreateObjectGraphs().ToList();
}
}
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private IEnumerable<IDictionary<string,object>> CreateObjectGraphs()
{
var load = BuildLoadDictionary();
IDictionary<string,int> index = null;
foreach (var kvp in load)
{
if (index == null)
{
index = kvp.Key.Keys.Select((k, i) => new KeyValuePair<string, int>(k, i)).ToDictionary(HomogenizedEqualityComparer.DefaultInstance);
}
var row = new OptimizedDictionary<string, object>(index, kvp.Key.Values);
foreach (var sub in kvp.Value)
{
if (sub.Value.Single != null)
{
row[sub.Key] = sub.Value.Single;
}
else if (sub.Value.Collection != null)
{
row[sub.Key] = sub.Value.Collection.ToList();
}
}
yield return row;
}
}
private Dictionary<IDictionary<string, object>, IDictionary<string, WithContainer>> BuildLoadDictionary()
{
var load =
new Dictionary<IDictionary<string, object>, IDictionary<string, WithContainer>>(
new DictionaryEqualityComparer());
foreach (var dict in _source)
{
IDictionary<string, WithContainer> withContainers;
var main = new SubDictionary<string, object>(dict, s => !s.StartsWith("__with"));
if (!load.TryGetValue(main, out withContainers))
{
withContainers = new Dictionary<string, WithContainer>();
load.Add(main, withContainers);
foreach (var tuple in ExtractSingleObjects(dict))
{
var withContainer = new WithContainer();
withContainer.SetSingle(tuple.Item2);
if (!ReferenceEquals(withContainer.Single, null))
{
withContainers.Add(tuple.Item1, withContainer);
}
}
}
foreach (var tuple in ExtractCollectionObjects(dict))
{
if (!withContainers.ContainsKey(tuple.Item1))
{
withContainers.Add(tuple.Item1, new WithContainer());
}
withContainers[tuple.Item1].AddToCollection(tuple.Item2);
}
}
return load;
}
private class WithContainer
{
public HashSet<IDictionary<string, object>> Collection { get; private set; }
public IDictionary<string, object> Single { get; private set; }
public void AddToCollection(IDictionary<string,object> row)
{
if (row.All(kvp => ReferenceEquals(null, kvp.Value))) return;
if (Collection == null) Collection = new HashSet<IDictionary<string, object>>(new DictionaryEqualityComparer());
Collection.Add(row);
}
public void SetSingle(IDictionary<string,object> row)
{
if (row.All(kvp => ReferenceEquals(null, kvp.Value))) return;
Single = row;
}
}
private IEnumerable<Tuple<string,Dictionary<string,object>>> ExtractSingleObjects(IDictionary<string,object> source)
{
var names =
source.Keys.Where(k => k.StartsWith("__with1__")).Select(
k => k.Split(new[] {"__"}, StringSplitOptions.RemoveEmptyEntries)[1]).Distinct().ToList();
return from name in names
let pattern = "__with1__" + name + "__"
select Tuple.Create(name, source.Where(kvp => kvp.Key.StartsWith(pattern))
.ToDictionary(kvp => kvp.Key.Replace(pattern, ""), kvp => kvp.Value, HomogenizedEqualityComparer.DefaultInstance));
}
private IEnumerable<Tuple<string,Dictionary<string,object>>> ExtractCollectionObjects(IDictionary<string,object> source)
{
var names =
source.Keys.Where(k => k.StartsWith("__withn__")).Select(
k => k.Split(new[] {"__"}, StringSplitOptions.RemoveEmptyEntries)[1]).Distinct().ToList();
return from name in names
let pattern = "__withn__" + name + "__"
select Tuple.Create(name, source.Where(kvp => kvp.Key.StartsWith(pattern))
.ToDictionary(kvp => kvp.Key.Replace(pattern, ""), kvp => kvp.Value, HomogenizedEqualityComparer.DefaultInstance));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.