2022-10-30 10:00:53 -06:00
|
|
|
package resolvelinks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-20 10:15:51 -07:00
|
|
|
"errors"
|
2022-10-30 10:00:53 -06:00
|
|
|
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev"
|
2023-07-12 17:35:02 -06:00
|
|
|
"go.sour.is/ev/pkg/driver"
|
|
|
|
"go.sour.is/ev/pkg/event"
|
|
|
|
"go.sour.is/pkg/lg"
|
2022-10-30 10:00:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type resolvelinks struct {
|
|
|
|
up driver.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *resolvelinks {
|
|
|
|
return &resolvelinks{}
|
|
|
|
}
|
2022-12-19 10:50:38 -07:00
|
|
|
|
2023-01-09 11:30:02 -07:00
|
|
|
func (r *resolvelinks) Apply(es *ev.EventStore) {
|
2022-10-30 10:00:53 -06:00
|
|
|
r.up = es.Driver
|
|
|
|
es.Driver = r
|
|
|
|
}
|
2022-12-19 10:50:38 -07:00
|
|
|
|
2022-10-30 10:00:53 -06:00
|
|
|
func (r *resolvelinks) Unwrap() driver.Driver {
|
|
|
|
return r.up
|
|
|
|
}
|
2022-12-19 10:50:38 -07:00
|
|
|
|
2022-10-30 10:00:53 -06:00
|
|
|
func (r *resolvelinks) Open(ctx context.Context, dsn string) (driver.Driver, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return r.up.Open(ctx, dsn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolvelinks) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
l, err := r.up.EventLog(ctx, streamID)
|
|
|
|
return &wrapper{l, r}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
up driver.EventLog
|
|
|
|
resolvelinks *resolvelinks
|
|
|
|
}
|
|
|
|
|
2022-11-20 10:15:51 -07:00
|
|
|
func (r *wrapper) Unwrap() driver.EventLog {
|
|
|
|
return r.up
|
|
|
|
}
|
2022-12-19 10:50:38 -07:00
|
|
|
|
2022-10-30 10:00:53 -06:00
|
|
|
func (w *wrapper) Read(ctx context.Context, after int64, count int64) (event.Events, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
events, err := w.up.Read(ctx, after, count)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:50:38 -07:00
|
|
|
idx := make(map[string][]uint64)
|
|
|
|
ptrs := make(map[string][]int)
|
|
|
|
for i := range events {
|
|
|
|
e := events[i]
|
|
|
|
if e, ok := e.(*event.EventPtr); ok {
|
|
|
|
idx[e.StreamID] = append(idx[e.StreamID], e.Pos)
|
|
|
|
ptrs[e.StreamID] = append(ptrs[e.StreamID], i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for streamID, ids := range idx {
|
|
|
|
d, err := w.resolvelinks.EventLog(ctx, streamID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ptr := ptrs[streamID]
|
|
|
|
lis, err := d.ReadN(ctx, ids...)
|
2023-01-09 11:30:02 -07:00
|
|
|
if err != nil && !errors.Is(err, ev.ErrNotFound) {
|
2022-12-19 10:50:38 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range lis {
|
|
|
|
meta := lis[i].EventMeta()
|
|
|
|
actual := events[ptr[i]].EventMeta()
|
|
|
|
meta.ActualPosition = actual.Position
|
|
|
|
meta.ActualStreamID = actual.ActualStreamID
|
|
|
|
lis[i].SetEventMeta(meta)
|
|
|
|
events[i] = lis[i]
|
2022-10-30 10:00:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:50:38 -07:00
|
|
|
// for i, e := range events {
|
|
|
|
// switch e := e.(type) {
|
|
|
|
// case *event.EventPtr:
|
|
|
|
// d, err := w.resolvelinks.EventLog(ctx, e.StreamID)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// lis, err := d.ReadN(ctx, e.Pos)
|
|
|
|
// if err != nil && !errors.Is(err, es.ErrNotFound) {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if ne := lis.First(); ne != event.NilEvent {
|
|
|
|
// meta := ne.EventMeta()
|
|
|
|
// actual := e.EventMeta()
|
|
|
|
// meta.ActualPosition = actual.Position
|
|
|
|
// meta.ActualStreamID = actual.ActualStreamID
|
|
|
|
// ne.SetEventMeta(meta)
|
|
|
|
// events[i] = ne
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2022-10-30 10:00:53 -06:00
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) ReadN(ctx context.Context, index ...uint64) (event.Events, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
events, err := w.up.ReadN(ctx, index...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range events {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *event.EventPtr:
|
|
|
|
d, err := w.resolvelinks.EventLog(ctx, e.StreamID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lis, err := d.ReadN(ctx, e.Pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-20 10:15:51 -07:00
|
|
|
ne := lis.First()
|
|
|
|
meta := ne.EventMeta()
|
|
|
|
actual := e.EventMeta()
|
|
|
|
meta.ActualPosition = actual.Position
|
|
|
|
meta.ActualStreamID = actual.ActualStreamID
|
|
|
|
ne.SetEventMeta(meta)
|
|
|
|
|
|
|
|
events[i] = ne
|
2022-10-30 10:00:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return w.up.Append(ctx, events, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) FirstIndex(ctx context.Context) (uint64, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return w.up.FirstIndex(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) LastIndex(ctx context.Context) (uint64, error) {
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return w.up.LastIndex(ctx)
|
|
|
|
}
|