Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit d77462c

Browse files
using what the client said to direct the output
we will have to limit the maxcount somehow
1 parent cca53f2 commit d77462c

8 files changed

Lines changed: 215 additions & 118 deletions

File tree

src/SqlStreamStore.HAL/AllStreamResource.cs

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
internal class AllStreamResource
1212
{
13-
private const string StreamId = "stream";
14-
1513
private readonly IReadonlyStreamStore _streamStore;
1614

1715
public AllStreamResource(IReadonlyStreamStore streamStore)
@@ -38,10 +36,10 @@ public async Task<Response> GetPage(
3836
var response = new Response(
3937
new HALResponse(new object())
4038
.AddLinks(Links.SelfFeed(options))
41-
.AddLinks(Links.Navigation(page, options.Self))
42-
.AddLinks(Links.Feed)
39+
.AddLinks(Links.Navigation(page, options))
40+
.AddLinks(Links.Feed(options))
4341
.AddEmbeddedCollection(
44-
Relations.Message,
42+
Constants.Relations.Message,
4543
page.Messages.Zip(payloads,
4644
(message, payload) => new HALResponse(new
4745
{
@@ -79,7 +77,7 @@ public async Task<Response> GetMessage(
7977
{
8078
return new Response(
8179
new HALResponse(new HALModelConfig())
82-
.AddLinks(Links.Feed),
80+
.AddLinks(Links.Feed(options)),
8381
404);
8482
}
8583

@@ -97,46 +95,86 @@ public async Task<Response> GetMessage(
9795
payload
9896
}).AddLinks(
9997
Links.SelfAll(message),
100-
Links.Feed));
98+
Links.Feed(options)));
10199
}
102100

103101
private static class Links
104102
{
105103
public static Link SelfFeed(ReadAllStreamOptions options)
106-
=> new Link(Relations.Self, options.Self);
104+
=> new Link(Constants.Relations.Self, options.Self);
107105

108106
public static Link Self(StreamMessage message) => new Link(
109-
Relations.Self,
107+
Constants.Relations.Self,
110108
$"streams/{message.StreamId}/{message.StreamVersion}");
111109

112-
public static Link SelfAll(StreamMessage message) => new Link(Relations.Self, $"/{StreamId}/{message.Position}");
113-
114-
public static Link First => new Link(Relations.First,
115-
LinkFormatter.FormatForwardLink(StreamId, 20, Position.Start));
116-
117-
public static Link Last => new Link(Relations.Last, LinkFormatter.FormatBackwardLink(StreamId, 20, Position.End));
118-
119-
public static Link Feed => new Link(Relations.Feed, Last.Href);
120-
121-
public static Link Previous(ReadAllPage page)
122-
=> new Link(Relations.Previous,
123-
LinkFormatter.FormatBackwardLink(StreamId, 20, page.Messages.Min(m => m.Position) - 1));
124-
125-
public static Link Next(ReadAllPage page)
126-
=> new Link(Relations.Next,
127-
LinkFormatter.FormatForwardLink(StreamId, 20, page.Messages.Max(m => m.Position) + 1));
128-
129-
public static IEnumerable<Link> Navigation(ReadAllPage page, string self)
110+
public static Link SelfAll(StreamMessage message)
111+
=> new Link(Constants.Relations.Self, $"/{Constants.Streams.All}/{message.Position}");
112+
113+
public static Link First(ReadAllStreamOptions options)
114+
=> new Link(
115+
Constants.Relations.First,
116+
LinkFormatter.FormatForwardLink(
117+
Constants.Streams.All,
118+
options.MaxCount,
119+
Position.Start,
120+
options.EmbedPayload));
121+
122+
public static Link Last(ReadAllStreamOptions options)
123+
=> new Link(
124+
Constants.Relations.Last,
125+
LinkFormatter.FormatBackwardLink(
126+
Constants.Streams.All,
127+
options.MaxCount,
128+
Position.End,
129+
options.EmbedPayload));
130+
131+
public static Link Last(ReadAllStreamMessageOptions options)
132+
=> new Link(
133+
Constants.Relations.Last,
134+
LinkFormatter.FormatBackwardLink(
135+
Constants.Streams.All,
136+
Constants.MaxCount,
137+
Position.End,
138+
false));
139+
140+
public static Link Feed(ReadAllStreamOptions options)
141+
=> new Link(Constants.Relations.Feed, Last(options).Href);
142+
143+
public static Link Feed(ReadAllStreamMessageOptions options)
144+
=> new Link(Constants.Relations.Feed, Last(options).Href);
145+
146+
public static Link Previous(ReadAllPage page, ReadAllStreamOptions options)
147+
=> new Link(
148+
Constants.Relations.Previous,
149+
LinkFormatter.FormatBackwardLink(
150+
Constants.Streams.All,
151+
options.MaxCount,
152+
page.Messages.Min(m => m.Position) - 1,
153+
options.EmbedPayload));
154+
155+
public static Link Next(ReadAllPage page, ReadAllStreamOptions options)
156+
=> new Link(
157+
Constants.Relations.Next,
158+
LinkFormatter.FormatForwardLink(
159+
Constants.Streams.All,
160+
options.MaxCount,
161+
page.Messages.Max(m => m.Position) + 1,
162+
options.EmbedPayload));
163+
164+
public static IEnumerable<Link> Navigation(ReadAllPage page, ReadAllStreamOptions options)
130165
{
131-
yield return First;
166+
var first = First(options);
167+
var last = Last(options);
168+
169+
yield return first;
132170

133-
if(self != First.Href && !page.IsEnd)
134-
yield return Previous(page);
171+
if(options.Self != first.Href && !page.IsEnd)
172+
yield return Previous(page, options);
135173

136-
if(self != Last.Href && !page.IsEnd)
137-
yield return Next(page);
174+
if(options.Self != last.Href && !page.IsEnd)
175+
yield return Next(page, options);
138176

139-
yield return Last;
177+
yield return last;
140178
}
141179
}
142180
}

src/SqlStreamStore.HAL/Constants.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ public static class ContentTypes
1818
public const string Any = "*/*";
1919
}
2020
}
21+
22+
public static class Relations
23+
{
24+
public const string Self = "self";
25+
public const string First = "first";
26+
public const string Previous = "previous";
27+
public const string Next = "next";
28+
public const string Last = "last";
29+
public const string Feed = "streamStore:feed";
30+
public const string Message = "streamStore:message";
31+
}
32+
33+
public static class Streams
34+
{
35+
public const string All = "stream";
36+
}
2137

2238
public static IReadOnlyDictionary<int, string> ReasonPhrases { get; }
2339
= new ReadOnlyDictionary<int, string>(new Dictionary<int, string>
@@ -36,5 +52,7 @@ public static class ReadDirection
3652
public const int Forwards = 1;
3753
public const int Backwards = -1;
3854
}
55+
56+
public const int MaxCount = 20;
3957
}
4058
}

src/SqlStreamStore.HAL/LinkFormatter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ namespace SqlStreamStore.HAL
22
{
33
internal static class LinkFormatter
44
{
5-
private static string FormatLink(string baseAddress, string direction, int maxCount, long position)
6-
=> $"{baseAddress}?d={direction}&m={maxCount}&p={position}";
5+
private static string FormatLink(string baseAddress, string direction, int maxCount, long position, bool prefetch)
6+
=> $"{baseAddress}?d={direction}&m={maxCount}&p={position}{(prefetch ? "&e=1" : string.Empty)}";
77

8-
public static string FormatForwardLink(string baseAddress, int maxCount, long position)
9-
=> FormatLink(baseAddress, "f", maxCount, position);
8+
public static string FormatForwardLink(string baseAddress, int maxCount, long position, bool prefetch)
9+
=> FormatLink(baseAddress, "f", maxCount, position, prefetch);
1010

11-
public static string FormatBackwardLink(string baseAddress, int maxCount, long position)
12-
=> FormatLink(baseAddress, "b", maxCount, position);
11+
public static string FormatBackwardLink(string baseAddress, int maxCount, long position, bool prefetch)
12+
=> FormatLink(baseAddress, "b", maxCount, position, prefetch);
1313
}
1414
}

src/SqlStreamStore.HAL/ReadAllStreamOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ReadAllStreamOptions(IOwinRequest request)
2626

2727
if(!int.TryParse(request.Query.Get("m"), out _maxCount))
2828
{
29-
_maxCount = 20;
29+
_maxCount = Constants.MaxCount;
3030
}
3131
}
3232

@@ -36,8 +36,8 @@ public ReadAllStreamOptions(IOwinRequest request)
3636
public int ReadDirection { get; }
3737

3838
public string Self => ReadDirection == Constants.ReadDirection.Forwards
39-
? LinkFormatter.FormatForwardLink("stream", MaxCount, FromPositionInclusive)
40-
: LinkFormatter.FormatBackwardLink("stream", MaxCount, FromPositionInclusive);
39+
? LinkFormatter.FormatForwardLink(Constants.Streams.All, MaxCount, FromPositionInclusive, EmbedPayload)
40+
: LinkFormatter.FormatBackwardLink(Constants.Streams.All, MaxCount, FromPositionInclusive, EmbedPayload);
4141

4242
public Func<IReadonlyStreamStore, CancellationToken, Task<ReadAllPage>> GetReadOperation()
4343
=> (streamStore, ct) => ReadDirection == Constants.ReadDirection.Forwards

src/SqlStreamStore.HAL/ReadStreamOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ReadStreamOptions(IOwinRequest request)
3030

3131
if(!int.TryParse(request.Query.Get("m"), out _maxCount))
3232
{
33-
_maxCount = 20;
33+
_maxCount = Constants.MaxCount;
3434
}
3535
}
3636

@@ -41,8 +41,8 @@ public ReadStreamOptions(IOwinRequest request)
4141
public string StreamId { get; }
4242

4343
public string Self => ReadDirection == Constants.ReadDirection.Forwards
44-
? LinkFormatter.FormatForwardLink(StreamId, MaxCount, FromVersionInclusive)
45-
: LinkFormatter.FormatBackwardLink(StreamId, MaxCount, FromVersionInclusive);
44+
? LinkFormatter.FormatForwardLink(StreamId, MaxCount, FromVersionInclusive, EmbedPayload)
45+
: LinkFormatter.FormatBackwardLink(StreamId, MaxCount, FromVersionInclusive, EmbedPayload);
4646

4747
public Func<IReadonlyStreamStore, CancellationToken, Task<ReadStreamPage>> GetReadOperation()
4848
=> (streamStore, ct) => ReadDirection == Constants.ReadDirection.Forwards

src/SqlStreamStore.HAL/Relations.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ private static MidFunc MethodsNotAllowed(params string[] methods) => next => env
7979
return next(env);
8080
}
8181

82-
var response = new Response(new HALResponse(null).AddLinks(new Link("streamStore:stream", "stream")));
82+
var response = new Response(new HALResponse(null)
83+
.AddLinks(new Link(
84+
Constants.Relations.Feed,
85+
"stream")));
8386

8487
return context.WriteHalResponse(response);
8588
};

0 commit comments

Comments
 (0)