-
Notifications
You must be signed in to change notification settings - Fork 15
/
InMemoryPartition.cs
184 lines (152 loc) · 5.96 KB
/
InMemoryPartition.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NStore.Core.Persistence;
namespace NStore.Core.InMemory
{
internal class InMemoryPartition
{
private readonly ReaderWriterLockSlim _lockSlim = new ReaderWriterLockSlim();
public InMemoryPartition(string partitionId, INetworkSimulator networkSimulator, Func<MemoryChunk, MemoryChunk> clone)
{
this.Id = partitionId;
_networkSimulator = networkSimulator;
Clone = clone;
}
private Func<MemoryChunk, MemoryChunk> Clone { get; }
public string Id { get; set; }
private IEnumerable<MemoryChunk> Chunks => _sortedChunks.Values;
private readonly SortedDictionary<long, MemoryChunk> _sortedChunks =
new SortedDictionary<long, MemoryChunk>();
private readonly IDictionary<string, MemoryChunk> _operations = new Dictionary<string, MemoryChunk>();
private readonly INetworkSimulator _networkSimulator;
public async Task ReadForward(
long fromLowerIndexInclusive,
ISubscription subscription,
long toUpperIndexInclusive,
int limit,
CancellationToken cancellationToken)
{
_lockSlim.EnterReadLock();
var result = Chunks
.Where(x => x.Index >= fromLowerIndexInclusive && x.Index <= toUpperIndexInclusive)
.Take(limit)
.ToArray();
_lockSlim.ExitReadLock();
await PushToSubscriber(fromLowerIndexInclusive, subscription, result, cancellationToken).ConfigureAwait(false);
}
public async Task ReadForwardByPosition(
long fromLowerPositionInclusive,
ISubscription subscription,
long toUpperPositionInclusive,
int limit,
CancellationToken cancellationToken)
{
_lockSlim.EnterReadLock();
var result = Chunks
.Where(x => x.Position >= fromLowerPositionInclusive && x.Position <= toUpperPositionInclusive)
.Take(limit)
.ToArray();
_lockSlim.ExitReadLock();
await PushToSubscriber(fromLowerPositionInclusive, subscription, result, cancellationToken).ConfigureAwait(false);
}
public Task ReadBackward(
long fromUpperIndexInclusive,
ISubscription subscription,
long toLowerIndexInclusive,
int limit,
CancellationToken cancellationToken
)
{
_lockSlim.EnterReadLock();
var result = Chunks.Reverse()
.Where(x => x.Index <= fromUpperIndexInclusive && x.Index >= toLowerIndexInclusive)
.Take(limit)
.ToArray();
_lockSlim.ExitReadLock();
return PushToSubscriber(fromUpperIndexInclusive, subscription, result, cancellationToken);
}
public Task<IChunk> Peek(long maxValue, CancellationToken cancellationToken)
{
_lockSlim.EnterReadLock();
var chunk = Chunks.Reverse()
.Where(x => x.Index <= maxValue)
.Take(1)
.SingleOrDefault();
_lockSlim.ExitReadLock();
return Task.FromResult((IChunk)Clone(chunk));
}
private async Task PushToSubscriber(
long start,
ISubscription subscription,
IEnumerable<MemoryChunk> chunks,
CancellationToken cancellationToken)
{
long index = 0;
await subscription.OnStartAsync(start).ConfigureAwait(false);
try
{
foreach (var chunk in chunks)
{
index = chunk.Index;
await _networkSimulator.WaitFast().ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if (!await subscription.OnNextAsync(Clone(chunk)).ConfigureAwait(false))
{
await subscription.CompletedAsync(index).ConfigureAwait(false);
return;
}
}
}
catch (Exception e)
{
await subscription.OnErrorAsync(index, e).ConfigureAwait(false);
return;
}
await subscription.CompletedAsync(index).ConfigureAwait(false);
}
public Boolean Write(MemoryChunk chunk)
{
_lockSlim.EnterWriteLock();
try
{
if (_operations.ContainsKey(chunk.OperationId))
return false;
if (_sortedChunks.ContainsKey(chunk.Index))
{
throw new DuplicateStreamIndexException(this.Id, chunk.Index);
}
_operations.Add(chunk.OperationId, chunk);
_sortedChunks.Add(chunk.Index, chunk);
}
finally
{
_lockSlim.ExitWriteLock();
}
return true;
}
public MemoryChunk[] Delete(long fromIndex, long toIndex)
{
_lockSlim.EnterReadLock();
var toDelete = Chunks.Where(x => x.Index >= fromIndex && x.Index <= toIndex).ToArray();
_lockSlim.ExitReadLock();
_lockSlim.EnterWriteLock();
foreach (var chunk in toDelete)
{
this._sortedChunks.Remove(chunk.Index);
this._operations.Remove(chunk.OperationId);
}
_lockSlim.ExitWriteLock();
return toDelete;
}
public Task<IChunk> GetByOperationId(string operationId)
{
_lockSlim.EnterReadLock();
_operations.TryGetValue(operationId, out MemoryChunk chunk);
_lockSlim.ExitReadLock();
return Task.FromResult((IChunk)Clone(chunk));
}
}
}