implement dumb non-truncating oplog for cache-service
parent
3f85a61e5e
commit
b7822498b1
|
|
@ -0,0 +1,47 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"render231011/internal/thestore"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Oplog struct {
|
||||||
|
events []timestampedEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
type timestampedEvent struct {
|
||||||
|
t time.Time
|
||||||
|
e thestore.Event
|
||||||
|
}
|
||||||
|
|
||||||
|
// gotcha duration? bigger N? whatever
|
||||||
|
func NewOplog() Oplog {
|
||||||
|
return Oplog{
|
||||||
|
events: make([]timestampedEvent, 0, 1024),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo circular queue if we got time
|
||||||
|
func (oplog *Oplog) Push(event thestore.Event) {
|
||||||
|
oplog.push(time.Now(), event)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (oplog *Oplog) push(t time.Time, event thestore.Event) {
|
||||||
|
oplog.events = append(oplog.events, timestampedEvent{
|
||||||
|
t: t,
|
||||||
|
e: event,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (oplog *Oplog) Since(t time.Time) []thestore.Event {
|
||||||
|
result := make([]thestore.Event, 0, 5)
|
||||||
|
for i := len(oplog.events) - 1; i >= 0; i-- {
|
||||||
|
if !oplog.events[i].t.After(t) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
result = append(result, oplog.events[i].e)
|
||||||
|
}
|
||||||
|
slices.Reverse(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"render231011/internal/thestore"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOplog(t *testing.T) {
|
||||||
|
oplog := NewOplog()
|
||||||
|
oplog.push(time.Unix(1, 0), thestore.Event{ID: "1"})
|
||||||
|
oplog.push(time.Unix(2, 0), thestore.Event{ID: "2"})
|
||||||
|
oplog.push(time.Unix(3, 0), thestore.Event{ID: "3"})
|
||||||
|
oplog.push(time.Unix(4, 0), thestore.Event{ID: "4"})
|
||||||
|
|
||||||
|
got := oplog.Since(time.Unix(2, 0))
|
||||||
|
if len(got) != 2 {
|
||||||
|
t.Error(got)
|
||||||
|
} else if got[0].ID != "3" {
|
||||||
|
t.Error(got[0])
|
||||||
|
} else if got[1].ID != "4" {
|
||||||
|
t.Error(got[1])
|
||||||
|
}
|
||||||
|
t.Logf("%+v", got)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue