@@ -102,12 +102,17 @@ func (self streamKey) Edges() iter.Seq2[server.Id, [2]*server.Id] {
102102 }
103103}
104104
105+ // represents a hop through a client from source to destination
105106type StreamHop [48 ]byte
106107
107- func NewStreamHop (sourceId server.Id , destinationId server.Id , streamId server.Id ) StreamHop {
108+ func NewStreamHop (sourceId * server.Id , destinationId * server.Id , streamId server.Id ) StreamHop {
108109 var sh [48 ]byte
109- copy (sh [:], sourceId [:])
110- copy (sh [16 :], destinationId [:])
110+ if sourceId != nil {
111+ copy (sh [:], sourceId [:])
112+ }
113+ if destinationId != nil {
114+ copy (sh [16 :], destinationId [:])
115+ }
111116 copy (sh [32 :], streamId [:])
112117 return sh
113118}
@@ -116,12 +121,20 @@ func (self StreamHop) Bytes() []byte {
116121 return []byte (self [:])
117122}
118123
119- func (self StreamHop ) SourceId () server.Id {
120- return server .Id (self [0 :16 ])
124+ func (self StreamHop ) SourceId () * server.Id {
125+ sourceId := server .Id (self [0 :16 ])
126+ if (sourceId == server.Id {}) {
127+ return nil
128+ }
129+ return & sourceId
121130}
122131
123- func (self StreamHop ) DestinationId () server.Id {
124- return server .Id (self [16 :32 ])
132+ func (self StreamHop ) DestinationId () * server.Id {
133+ destinationId := server .Id (self [16 :32 ])
134+ if (destinationId == server.Id {}) {
135+ return nil
136+ }
137+ return & destinationId
125138}
126139
127140func (self StreamHop ) StreamId () server.Id {
@@ -261,16 +274,9 @@ func AddToStream(
261274 EventId : eventId ,
262275 }
263276
264- if edges [0 ] != nil {
265- streamHop := NewStreamHop (* edges [0 ], clientId , streamId )
266- pipe .SAdd (ctx , streamHopsKey , streamHop .Bytes ())
267- event .StreamHops = append (event .StreamHops , streamHop )
268- }
269- if edges [1 ] != nil {
270- streamHop := NewStreamHop (clientId , * edges [1 ], streamId )
271- pipe .SAdd (ctx , streamHopsKey , streamHop .Bytes ())
272- event .StreamHops = append (event .StreamHops , streamHop )
273- }
277+ streamHop := NewStreamHop (edges [0 ], edges [1 ], streamId )
278+ pipe .SAdd (ctx , streamHopsKey , streamHop .Bytes ())
279+ event .StreamHops = append (event .StreamHops , streamHop )
274280
275281 buf := bytes .NewBuffer (nil )
276282 encoder := gob .NewEncoder (buf )
@@ -298,7 +304,7 @@ func AddToStream(
298304 return
299305}
300306
301- func RemoveFromStream (ctx context.Context , contractId server.Id ) {
307+ func RemoveFromStream (ctx context.Context , contractId server.Id ) ( streamId server. Id , ok bool ) {
302308 server .Redis (ctx , func (r server.RedisClient ) {
303309 pipe := r .TxPipeline ()
304310 streamKeyCmd := pipe .Get (ctx , contractStreamKey (contractId ))
@@ -327,7 +333,7 @@ func RemoveFromStream(ctx context.Context, contractId server.Id) {
327333 if err != nil {
328334 panic (err )
329335 }
330- streamId : = server .Id (streamIdBytes )
336+ streamId = server .Id (streamIdBytes )
331337
332338 // note the keys in eval have to be in the same hash slot
333339 result := r .Eval (
@@ -386,16 +392,9 @@ func RemoveFromStream(ctx context.Context, contractId server.Id) {
386392 EventId : eventId ,
387393 }
388394
389- if edges [0 ] != nil {
390- streamHop := NewStreamHop (* edges [0 ], clientId , streamId )
391- pipe .SRem (ctx , streamHopsKey , streamHop .Bytes ())
392- event .StreamHops = append (event .StreamHops , streamHop )
393- }
394- if edges [1 ] != nil {
395- streamHop := NewStreamHop (clientId , * edges [1 ], streamId )
396- pipe .SRem (ctx , streamHopsKey , streamHop .Bytes ())
397- event .StreamHops = append (event .StreamHops , streamHop )
398- }
395+ streamHop := NewStreamHop (edges [0 ], edges [1 ], streamId )
396+ pipe .SRem (ctx , streamHopsKey , streamHop .Bytes ())
397+ event .StreamHops = append (event .StreamHops , streamHop )
399398
400399 buf := bytes .NewBuffer (nil )
401400 encoder := gob .NewEncoder (buf )
@@ -413,6 +412,8 @@ func RemoveFromStream(ctx context.Context, contractId server.Id) {
413412 }
414413 }
415414 }
415+
416+ ok = true
416417 })
417418 return
418419}
0 commit comments