Extend DocumentBuffer with apply interface

The existing DocumentBuffer interface provided no facility
to iterate over events in order without caching them all in memory
and pushing/popping them. This was deemed significantly unoptimal and
so Apply() was born.

When Apply() is called, DocumentBuffer will recieve a filter callable
(functor, lambda, or closure) and will call it on all documents in order
throughout the entire cache and store, without caching stored events
or modifying the cache either.

The filter Apply() uses will recieve one document and return true or
false: true to continue iterating and false to stop iterating.

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2024-11-26 19:18:02 -08:00
parent 1ef8ff042f
commit 609c50ff7d
2 changed files with 120 additions and 15 deletions

View file

@ -302,3 +302,36 @@ func TestInitialize(t *testing.T) {
t.Fatalf("error reading: %e", err)
}
}
func TestApply(t *testing.T) {
backingStore := NewReadWriteSeekString()
backingStore.Write([]byte("MQo=\nMgo=\nMwo=\nNAo=\nNQo=\nNgo="))
buf, e := NewDocumentBuffer(3, &backingStore)
if e != nil {
t.Fatalf("error making documentbuffer: %e", e)
}
// test cached
if buf.Cached() != 3 {
t.Fatalf("expected 3 docs in cache")
}
count := 0
if err := buf.Apply(func(doc string) bool {
count += 1
return true
}); err != nil || count != 6 {
t.Fatalf("error applying: %e", err)
}
count = 0
if err := buf.Apply(func(doc string) bool {
if doc == "2\n" {
return false
}
count += 1
return true
}); err != nil || count != 4 {
t.Fatalf("error applying: %e", err)
}
}