This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from jetbasrawi/go.geteventstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamreader.go
209 lines (182 loc) · 5.76 KB
/
streamreader.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2016 Jet Basrawi. All rights reserved.
//
// Use of this source code is governed by a permissive BSD 3 Clause License
// that can be found in the license file.
package goes
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/billy-inc/go.geteventstore/atom"
)
// StreamReader provides methods for reading events and event metadata.
type StreamReader struct {
streamName string
client *Client
version int
nextVersion int
index int
currentURL string
pageSize int
eventResponse *EventResponse
feedPage *atom.Feed
lasterr error
loadFeedPage bool
}
// Err returns any error that is raised as a result of a call to Next().
func (s *StreamReader) Err() error {
return s.lasterr
}
// Version returns the current stream version of the reader.
func (s *StreamReader) Version() int {
return s.version
}
// NextVersion is the version of the stream that will be returned by a call to Next().
func (s *StreamReader) NextVersion(version int) {
s.nextVersion = version
}
// EventResponse returns the container for the event that is returned from a call to Next().
func (s *StreamReader) EventResponse() *EventResponse {
return s.eventResponse
}
// Next gets the next event on the stream.
//
// Next should be treated more like a cursor over the stream rather than an
// enumerator over a collection of results. Individual events are retrieved
// on each call to Next().
//
// The boolean returned is intended to provide a convenient mechanism to
// to enumerate and process events, it should not be considered an indication
// of the status of a call to Next(). To undertand the outcomes of operations the
// stream's Err() field should be inspected. It is left to the user to determine
// under what conditions to exit the loop.
//
// When next is called, it will go to the eventstore and get a single event at the
// current reader's stream version.
func (s *StreamReader) Next() bool {
s.lasterr = nil
numEntries := 0
if s.feedPage != nil {
numEntries = len(s.feedPage.Entry)
}
// The feed page will be nil when the stream reader is first created.
// The initial feed page url will be constructed based on the current
// version number.
if s.feedPage == nil {
s.index = -1
url, err := s.client.GetFeedPath(s.streamName, "forward", s.nextVersion, s.pageSize)
if err != nil {
s.lasterr = err
return false
}
s.currentURL = url
}
// If the index is less than 0 load the previous feed page.
// GetEventStore uses previous to point to more recent feed pages and uses
// next to point to older feed pages. A stream starts at the most recent
// event and ends at the oldest event.
if s.index < 0 {
if s.feedPage != nil {
// Get the url for the previous feed page. If the reader is at the head
// of the stream, the previous link in the feedpage will be nil.
if l := s.feedPage.GetLink("previous"); l != nil {
s.currentURL = l.Href
}
}
//Read the feedpage at the current url
f, _, err := s.client.ReadFeed(s.currentURL)
if err != nil {
s.lasterr = err
return true
}
s.feedPage = f
numEntries = len(f.Entry)
s.index = numEntries - 1
}
//If there are no events returned at the url return an error
if numEntries <= 0 {
s.eventResponse = nil
s.lasterr = &ErrNoMoreEvents{}
return true
}
//There are events returned, get the event for the current version
entry := s.feedPage.Entry[s.index]
url := strings.TrimRight(entry.Link[1].Href, "/")
e, _, err := s.client.GetEvent(url)
if err != nil {
s.lasterr = err
return true
}
s.eventResponse = e
s.version = s.nextVersion
s.nextVersion++
s.index--
return true
}
// Scan deserializes event and event metadata into the types passed in
// as arguments e and m.
func (s *StreamReader) Scan(e interface{}, m interface{}) error {
if s.lasterr != nil {
return s.lasterr
}
if s.eventResponse == nil {
return &ErrNoMoreEvents{}
}
if e != nil {
data, ok := s.eventResponse.Event.Data.(*json.RawMessage)
if !ok {
return fmt.Errorf("Could not unmarshal the event. Event data is not of type *json.RawMessage")
}
if err := json.Unmarshal(*data, e); err != nil {
return err
}
}
if m != nil && s.EventResponse().Event.MetaData != nil {
meta, ok := s.eventResponse.Event.MetaData.(*json.RawMessage)
if !ok {
return fmt.Errorf("Could not unmarshal the event. Event data is not of type *json.RawMessage")
}
if err := json.Unmarshal(*meta, &m); err != nil {
return err
}
}
return nil
}
// LongPoll causes the server to wait up to the number of seconds specified
// for results to become available at the URL requested.
//
// LongPoll is useful when polling the end of the stream as it returns quickly
// after new events are found which means that it is more responsive than polling
// over some arbitrary time interval. It also reduces the number of
// unproductive calls to the server polling for events when there are no new
// events to return.
//
// Setting the argument seconds to any integer value above 0 will cause the
// request to be made with ES-LongPoll set to that value. Any value 0 or below
// will cause the request to be made without ES-LongPoll and the server will not
// wait to return.
func (s *StreamReader) LongPoll(seconds int) {
if seconds > 0 {
s.client.SetHeader("ES-LongPoll", strconv.Itoa(seconds))
} else {
s.client.DeleteHeader("ES-LongPoll")
}
}
// MetaData gets the metadata for a stream.
//
// Stream metadata is retured as an EventResponse.
//
// For more information on stream metadata see:
// http://docs.geteventstore.com/http-api/3.7.0/stream-metadata/
func (s *StreamReader) MetaData() (*EventResponse, error) {
url, _, err := s.client.GetMetadataURL(s.streamName)
if err != nil {
return nil, err
}
ev, _, err := s.client.GetEvent(url)
if err != nil {
return nil, err
}
return ev, nil
}