-
Notifications
You must be signed in to change notification settings - Fork 64
/
S05_Read_from_stream.cs
79 lines (63 loc) · 2.09 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
using System;
using System.Linq;
using Streamstone;
namespace Example.Scenarios
{
public class S05_Read_from_stream : Scenario
{
public override void Run()
{
Prepare();
ReadSlice();
ReadAll();
}
void Prepare()
{
var events = Enumerable
.Range(1, 10)
.Select(Event)
.ToArray();
Stream.Write(new Stream(Partition), events);
}
void ReadSlice()
{
Console.WriteLine("Reading single slice from specified start version and using specified slice size");
var slice = Stream.Read<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();
}
void 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;
int nextSliceStart = 1;
do
{
slice = Stream.Read<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.NextEventNumber;
}
while (!slice.IsEndOfStream);
}
static EventData Event(int id)
{
var data = new
{
Id = id,
Type = "<type>",
Data = "{some}"
};
return new EventData(id.ToString(), data.Props());
}
class EventEntity
{
public string Id { get; set; }
public string Type { get; set; }
public string Data { get; set; }
public int Version { get; set; }
}
}
}