2022-08-04 21:07:10 -06:00
|
|
|
package memstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/sour-is/ev/pkg/es"
|
|
|
|
"github.com/sour-is/ev/pkg/es/driver"
|
|
|
|
"github.com/sour-is/ev/pkg/es/event"
|
|
|
|
"github.com/sour-is/ev/pkg/locker"
|
|
|
|
"github.com/sour-is/ev/pkg/math"
|
|
|
|
)
|
|
|
|
|
|
|
|
type state struct {
|
2022-08-09 16:23:33 -06:00
|
|
|
streams map[string]*locker.Locked[event.Events]
|
|
|
|
}
|
|
|
|
type eventLog struct {
|
|
|
|
streamID string
|
|
|
|
events *locker.Locked[event.Events]
|
2022-08-04 21:07:10 -06:00
|
|
|
}
|
|
|
|
type memstore struct {
|
|
|
|
state *locker.Locked[state]
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
const AppendOnly = es.AppendOnly
|
|
|
|
const AllEvents = es.AllEvents
|
2022-08-04 21:07:10 -06:00
|
|
|
|
|
|
|
func Init(ctx context.Context) {
|
|
|
|
es.Register(ctx, "mem", &memstore{})
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var _ driver.Driver = (*memstore)(nil)
|
|
|
|
|
|
|
|
func (memstore) Open(_ context.Context, name string) (driver.Driver, error) {
|
|
|
|
s := &state{streams: make(map[string]*locker.Locked[event.Events])}
|
2022-08-04 21:07:10 -06:00
|
|
|
return &memstore{locker.New(s)}, nil
|
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
func (m *memstore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
|
|
|
el := &eventLog{streamID: streamID}
|
|
|
|
|
|
|
|
err := m.state.Modify(ctx, func(state *state) error {
|
|
|
|
l, ok := state.streams[streamID]
|
|
|
|
if !ok {
|
|
|
|
l = locker.New(&event.Events{})
|
|
|
|
state.streams[streamID] = l
|
|
|
|
}
|
|
|
|
el.events = l
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return el, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.EventLog = (*eventLog)(nil)
|
2022-08-04 21:07:10 -06:00
|
|
|
|
|
|
|
// Append implements driver.EventStore
|
2022-08-09 16:23:33 -06:00
|
|
|
func (m *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
|
|
|
event.SetStreamID(m.streamID, events...)
|
|
|
|
|
|
|
|
return uint64(len(events)), m.events.Modify(ctx, func(stream *event.Events) error {
|
|
|
|
last := uint64(len(*stream))
|
|
|
|
if version != AppendOnly && version != last {
|
|
|
|
return fmt.Errorf("current version wrong %d != %d", version, last)
|
|
|
|
}
|
2022-08-04 21:07:10 -06:00
|
|
|
|
|
|
|
for i := range events {
|
|
|
|
pos := last + uint64(i) + 1
|
|
|
|
event.SetPosition(events[i], pos)
|
2022-08-09 16:23:33 -06:00
|
|
|
*stream = append(*stream, events[i])
|
2022-08-04 21:07:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements driver.EventStore
|
2022-08-09 16:23:33 -06:00
|
|
|
func (es *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Events, error) {
|
|
|
|
var events event.Events
|
2022-08-04 21:07:10 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
err := es.events.Modify(ctx, func(stream *event.Events) error {
|
|
|
|
first := stream.First().EventMeta().Position
|
|
|
|
last := stream.Last().EventMeta().Position
|
|
|
|
// ---
|
2022-08-04 21:07:10 -06:00
|
|
|
if first == 0 || last == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
if count == AllEvents {
|
|
|
|
count = int64(first - last)
|
|
|
|
}
|
|
|
|
|
|
|
|
var start uint64
|
|
|
|
|
2022-08-04 21:07:10 -06:00
|
|
|
switch {
|
2022-08-09 16:23:33 -06:00
|
|
|
case pos >= 0 && count > 0:
|
2022-08-04 21:07:10 -06:00
|
|
|
start = first + uint64(pos)
|
2022-08-09 16:23:33 -06:00
|
|
|
case pos < 0 && count > 0:
|
2022-08-04 21:07:10 -06:00
|
|
|
start = uint64(int64(last) + pos + 1)
|
2022-08-09 16:23:33 -06:00
|
|
|
|
|
|
|
case pos >= 0 && count < 0:
|
|
|
|
start = first + uint64(pos)
|
|
|
|
if pos > 1 {
|
|
|
|
start -= 2 // if pos is positive and count negative start before
|
|
|
|
}
|
|
|
|
if pos <= 1 {
|
|
|
|
return nil // if pos is one or zero and negative count nothing to return
|
2022-08-04 21:07:10 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
case pos < 0 && count < 0:
|
|
|
|
start = uint64(int64(last) + pos)
|
|
|
|
}
|
|
|
|
if start >= last {
|
|
|
|
return nil // if start is after last and positive count nothing to return
|
2022-08-04 21:07:10 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
events = make([]event.Event, math.Abs(count))
|
2022-08-04 21:07:10 -06:00
|
|
|
for i := range events {
|
2022-08-09 16:23:33 -06:00
|
|
|
// ---
|
|
|
|
events[i] = (*stream)[start-1]
|
|
|
|
// ---
|
2022-08-04 21:07:10 -06:00
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
start += 1
|
|
|
|
} else {
|
|
|
|
start -= 1
|
|
|
|
}
|
|
|
|
if start < first || start > last {
|
|
|
|
events = events[:i+1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
event.SetStreamID(es.streamID, events...)
|
2022-08-04 21:07:10 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
return events, nil
|
2022-08-04 21:07:10 -06:00
|
|
|
}
|
2022-08-07 11:55:49 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
// FirstIndex for the streamID
|
|
|
|
func (m *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
|
|
|
events, err := m.events.Copy(ctx)
|
|
|
|
return events.First().EventMeta().Position, err
|
2022-08-07 11:55:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
// LastIndex for the streamID
|
|
|
|
func (m *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
|
|
|
events, err := m.events.Copy(ctx)
|
|
|
|
return events.Last().EventMeta().Position, err
|
2022-08-07 11:55:49 -06:00
|
|
|
}
|