Skip to content

Commit a9006a0

Browse files
committed
Added OptimizedDictionary
1 parent aeed10c commit a9006a0

11 files changed

Lines changed: 330 additions & 20 deletions

Simple.Data.Ado/DataReaderExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Data;
4+
using Simple.Data.Extensions;
45

56
namespace Simple.Data.Ado
67
{
@@ -33,9 +34,10 @@ private static IEnumerable<IEnumerable<IDictionary<string,object>>> ToMultipleDi
3334

3435
private static IEnumerable<IDictionary<string,object>> ToDictionariesImpl(IDataReader reader)
3536
{
37+
var index = OptimizedDictionary.CreateIndex(reader.GetFieldNames().Select(n => n.Homogenize()));
3638
while (reader.Read())
3739
{
38-
yield return reader.ToDictionary();
40+
yield return reader.ToDictionary(index);
3941
}
4042
}
4143

Simple.Data.Ado/DataRecordExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,44 @@ public static dynamic ToDynamicRecord(this IDataRecord dataRecord)
1111
return ToDynamicRecord(dataRecord, null, null);
1212
}
1313

14+
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index)
15+
{
16+
return ToDynamicRecord(dataRecord, index, null, null);
17+
}
18+
1419
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, string tableName, Database database)
1520
{
1621
return new SimpleRecord(dataRecord.ToDictionary(), tableName, database);
1722
}
1823

24+
public static dynamic ToDynamicRecord(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index, string tableName, Database database)
25+
{
26+
return new SimpleRecord(dataRecord.ToDictionary(index), tableName, database);
27+
}
28+
1929
public static Dictionary<string, object> ToDictionary(this IDataRecord dataRecord)
2030
{
2131
return dataRecord.GetFieldNames().ToDictionary(fieldName => fieldName, fieldName => dataRecord[fieldName]);
2232
}
2333

34+
public static IDictionary<string, object> ToDictionary(this IDataRecord dataRecord, OptimizedDictionaryIndex<string> index)
35+
{
36+
return OptimizedDictionary.Create(index,dataRecord.GetValues());
37+
}
38+
2439
public static IEnumerable<string> GetFieldNames(this IDataRecord dataRecord)
2540
{
2641
for (int i = 0; i < dataRecord.FieldCount; i++)
2742
{
2843
yield return dataRecord.GetName(i);
2944
}
3045
}
46+
47+
public static IEnumerable<object> GetValues(this IDataRecord dataRecord)
48+
{
49+
var values = new object[dataRecord.FieldCount];
50+
dataRecord.GetValues(values);
51+
return values;
52+
}
3153
}
3254
}

Simple.Data/Commands/ExecuteFunctionCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public bool Execute(out object result)
2929
private SimpleResultSet ToMultipleResultSets(object source)
3030
{
3131
if (source == null) return SimpleResultSet.Empty;
32-
var resultSets = source as IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>>;
32+
var resultSets = source as IEnumerable<IEnumerable<IDictionary<string, object>>>;
3333
if (resultSets == null) throw new InvalidOperationException("Adapter returned incorrect Type.");
3434

3535
return ToMultipleDynamicEnumerables(resultSets);
3636
}
3737

38-
private SimpleResultSet ToMultipleDynamicEnumerables(IEnumerable<IEnumerable<IEnumerable<KeyValuePair<string, object>>>> resultSets)
38+
private SimpleResultSet ToMultipleDynamicEnumerables(IEnumerable<IEnumerable<IDictionary<string, object>>> resultSets)
3939
{
4040
var result = new SimpleResultSet(resultSets.Select(resultSet => resultSet.Select(dict => new SimpleRecord(dict))));
4141
result.SetOutputValues(_arguments);
@@ -46,7 +46,7 @@ private static SimpleResultSet ToResultSet(object source)
4646
{
4747
if (source == null) return SimpleResultSet.Empty;
4848

49-
var dicts = source as IEnumerable<IEnumerable<KeyValuePair<string, object>>>;
49+
var dicts = source as IEnumerable<IDictionary<string, object>>;
5050
if (dicts == null) throw new InvalidOperationException("Adapter returned incorrect Type.");
5151

5252
return new SimpleResultSet(dicts.Select(dict => new SimpleRecord(dict)));

Simple.Data/ConcreteObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class ConcreteObject
1212
private static readonly object CastFailureObject = new object();
1313
private WeakReference _concreteObject;
1414

15-
public object Get(Type type, HomogenizedKeyDictionary data)
15+
public object Get(Type type, IDictionary<string,object> data)
1616
{
1717
if (_concreteObject == null || !_concreteObject.IsAlive)
1818
{
@@ -27,7 +27,7 @@ public object Get(Type type, HomogenizedKeyDictionary data)
2727
return null;
2828
}
2929

30-
private object ConvertAndCacheReference(Type type, HomogenizedKeyDictionary data)
30+
private object ConvertAndCacheReference(Type type, IDictionary<string, object> data)
3131
{
3232
_concreteObject = null;
3333
object result;

Simple.Data/ConcreteTypeCreator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text;
7+
using Simple.Data.Extensions;
78

89
namespace Simple.Data
910
{
@@ -29,13 +30,13 @@ public static ConcreteTypeCreator Get(Type concreteType)
2930
return Cache.GetOrAdd(concreteType, type => new ConcreteTypeCreator(type));
3031
}
3132

32-
public bool TryCreate(HomogenizedKeyDictionary data, out object result)
33+
public bool TryCreate(IDictionary<string, object> data, out object result)
3334
{
3435
bool anyPropertiesSet = false;
3536
object obj = Activator.CreateInstance(_concreteType);
3637
foreach (var propertyInfo in _concreteType.GetProperties().Where(pi => CanSetProperty(pi, data)))
3738
{
38-
propertyInfo.SetValue(obj, data[propertyInfo.Name], null);
39+
propertyInfo.SetValue(obj, data[propertyInfo.Name.Homogenize()], null);
3940
anyPropertiesSet = true;
4041
}
4142

@@ -44,10 +45,10 @@ public bool TryCreate(HomogenizedKeyDictionary data, out object result)
4445
return anyPropertiesSet;
4546
}
4647

47-
private static bool CanSetProperty(PropertyInfo propertyInfo, HomogenizedKeyDictionary data)
48+
private static bool CanSetProperty(PropertyInfo propertyInfo, IDictionary<string, object> data)
4849
{
49-
return data.ContainsKey(propertyInfo.Name) &&
50-
!(propertyInfo.PropertyType.IsValueType && data[propertyInfo.Name] == null);
50+
return data.ContainsKey(propertyInfo.Name.Homogenize()) &&
51+
!(propertyInfo.PropertyType.IsValueType && data[propertyInfo.Name.Homogenize()] == null);
5152
}
5253
}
5354
}

Simple.Data/OptimizedDictionary.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Simple.Data
5+
{
6+
public static class OptimizedDictionary
7+
{
8+
public static OptimizedDictionaryIndex<T> CreateIndex<T>(IEnumerable<T> keys)
9+
{
10+
var index = keys.Select((key, i) => new KeyValuePair<T, int>(key, i)).ToDictionary(kvp => kvp.Key,
11+
kvp => kvp.Value);
12+
return new OptimizedDictionaryIndex<T>(index);
13+
}
14+
15+
public static OptimizedDictionary<TKey,TValue> Create<TKey,TValue>(OptimizedDictionaryIndex<TKey> index, IEnumerable<TValue> values)
16+
{
17+
return new OptimizedDictionary<TKey, TValue>(index, values);
18+
}
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
3+
namespace Simple.Data
4+
{
5+
public class OptimizedDictionaryIndex<T>
6+
{
7+
private readonly Dictionary<T, int> _index;
8+
9+
internal OptimizedDictionaryIndex(IDictionary<T,int> index)
10+
{
11+
_index = new Dictionary<T, int>(index);
12+
}
13+
14+
public int this[T key]
15+
{
16+
get { return _index[key]; }
17+
}
18+
19+
public IEnumerable<T> GetKeys()
20+
{
21+
return _index.Keys;
22+
}
23+
24+
public bool ContainsKey(T key)
25+
{
26+
return _index.ContainsKey(key);
27+
}
28+
29+
public bool TryGetIndex(T key, out int index)
30+
{
31+
return _index.TryGetValue(key, out index);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)