forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryDatabase.cs
More file actions
177 lines (141 loc) · 5.79 KB
/
Copy pathInMemoryDatabase.cs
File metadata and controls
177 lines (141 loc) · 5.79 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.Data.Entity.ChangeTracking.Internal;
using Microsoft.Data.Entity.InMemory.Metadata;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.Logging;
namespace Microsoft.Data.Entity.InMemory
{
public class InMemoryDatabase : IInMemoryDatabase
{
private readonly ILogger _logger;
private readonly ThreadSafeLazyRef<ImmutableDictionary<IEntityType, InMemoryTable>> _tables
= new ThreadSafeLazyRef<ImmutableDictionary<IEntityType, InMemoryTable>>(
() => ImmutableDictionary<IEntityType, InMemoryTable>.Empty.WithComparers(new EntityTypeNameEqualityComparer()));
public InMemoryDatabase([NotNull] ILoggerFactory loggerFactory)
{
Check.NotNull(loggerFactory, nameof(loggerFactory));
_logger = loggerFactory.CreateLogger<InMemoryDatabase>();
}
/// <summary>
/// Returns true just after the database has been created, false thereafter
/// </summary>
/// <returns>
/// true if the database has just been created, false otherwise
/// </returns>
public virtual bool EnsureCreated(IModel model)
{
Check.NotNull(model, nameof(model));
var returnValue = !_tables.HasValue;
// ReSharper disable once UnusedVariable
var _ = _tables.Value;
return returnValue;
}
public virtual void Clear()
{
_tables.ExchangeValue(ts => ImmutableDictionary<IEntityType, InMemoryTable>.Empty);
}
public virtual IEnumerable<InMemoryTable> GetTables(IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));
if (!_tables.HasValue)
{
yield break;
}
foreach (var et in entityType.GetConcreteTypesInHierarchy())
{
InMemoryTable table;
if (_tables.Value.TryGetValue(et, out table))
{
yield return table;
}
}
}
public virtual int ExecuteTransaction(IEnumerable<InternalEntityEntry> entries)
{
Check.NotNull(entries, nameof(entries));
var rowsAffected = 0;
_tables.ExchangeValue(ts =>
{
rowsAffected = 0;
foreach (var entry in entries)
{
var entityType = entry.EntityType;
Debug.Assert(!entityType.IsAbstract);
InMemoryTable table;
if (!ts.TryGetValue(entityType, out table))
{
ts = ts.Add(entityType, table = new InMemoryTable(entityType));
}
switch (entry.EntityState)
{
case EntityState.Added:
table.Create(entry);
break;
case EntityState.Deleted:
table.Delete(entry);
break;
case EntityState.Modified:
table.Update(entry);
break;
}
rowsAffected++;
}
return ts;
});
_logger.LogInformation(rowsAffected, ra => Strings.LogSavedChanges(ra));
return rowsAffected;
}
public virtual IEnumerator<InMemoryTable> GetEnumerator()
{
return _tables.HasValue
? _tables.Value.Values.GetEnumerator()
: Enumerable.Empty<InMemoryTable>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public class InMemoryTable : IEnumerable<object[]>
{
private readonly ThreadSafeLazyRef<ImmutableDictionary<EntityKey, object[]>> _rows
= new ThreadSafeLazyRef<ImmutableDictionary<EntityKey, object[]>>(
() => ImmutableDictionary<EntityKey, object[]>.Empty);
public InMemoryTable([NotNull] IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));
EntityType = entityType;
}
public virtual IEntityType EntityType { get; private set; }
internal void Create(InternalEntityEntry entry)
{
_rows.ExchangeValue(rs => rs.Add(entry.GetPrimaryKeyValue(), entry.GetValueBuffer()));
}
internal void Delete(InternalEntityEntry entry)
{
_rows.ExchangeValue(rs => rs.Remove(entry.GetPrimaryKeyValue()));
}
internal void Update(InternalEntityEntry entry)
{
_rows.ExchangeValue(rs => rs.SetItem(entry.GetPrimaryKeyValue(), entry.GetValueBuffer()));
}
public virtual IEnumerator<object[]> GetEnumerator()
{
return _rows.HasValue
? _rows.Value.Values.GetEnumerator()
: Enumerable.Empty<object[]>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}