forked from yevhen/Streamstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S05_Read_from_stream.cs
83 lines (68 loc) · 2.51 KB
/
S05_Read_from_stream.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
using System;
using System.Linq;
using System.Threading.Tasks;
using Streamstone;
namespace Example.Scenarios
{
public class S05_Read_from_stream : Scenario
{
public override async Task RunAsync()
{
await Prepare();
await ReadSlice();
await ReadAll();
}
async Task Prepare()
{
var events = Enumerable
.Range(1, 10)
.Select(Event)
.ToArray();
var existent = await Stream.TryOpenAsync(Partition);
var stream = existent.Found ? existent.Stream : new Stream(Partition);
await Stream.WriteAsync(stream, events);
}
async Task ReadSlice()
{
Console.WriteLine("Reading single slice from specified start version and using specified slice size");
var slice = await Stream.ReadAsync<EventEntity>(Partition, startVersion: 2, sliceSize: 2);
foreach (var @event in slice.Events)
Console.WriteLine("{0}: {1}-{2}", @event.Version, @event.Type, @event.Data);
Console.WriteLine();
}
async Task ReadAll()
{
Console.WriteLine("Reading all events in a stream");
Console.WriteLine("If slice size is > than WATS limit, continuation token will be managed automatically");
StreamSlice<EventEntity> slice;
var nextSliceStart = 1;
do
{
slice = await Stream.ReadAsync<EventEntity>(Partition, nextSliceStart, sliceSize: 1);
foreach (var @event in slice.Events)
Console.WriteLine("{0}:{1} {2}-{3}", @event.Id, @event.Version, @event.Type, @event.Data);
nextSliceStart = slice.HasEvents
? slice.Events.Last().Version + 1
: -1;
}
while (!slice.IsEndOfStream);
}
static EventData Event(int id)
{
var properties = new
{
Id = id,
Type = "<type>",
Data = "{some}"
};
return new EventData(EventId.From(id.ToString()), EventProperties.From(properties));
}
class EventEntity
{
public int Id { get; set; }
public string Type { get; set; }
public string Data { get; set; }
public int Version { get; set; }
}
}
}