Reduce gaps/rewrites in the same cell with offset

A new buffer remembers the timestamp of the first write.
Instead of cutting of cells relative to that time, have
a little "time buffer" so that rewriting the same cell twice
happens less often.
This commit is contained in:
Lou Knauer
2021-12-02 12:57:35 +01:00
parent becf41f98c
commit 5d89d87a2d
2 changed files with 52 additions and 1 deletions

View File

@@ -59,7 +59,13 @@ func (b *buffer) write(ts int64, value Float) (*buffer, error) {
return nil, errors.New("cannot write value to buffer from past")
}
idx := int((ts - b.start) / b.frequency)
// When a new buffer is created, it starts at ts. If we would
// use the same index calculation as for a read here, even a very
// slight drift in the timestamps of values will cause cases where
// a cell is re-written. Adding any value smaller than half the frequency
// here creates a time buffer around the cutoff from one cell to the next
// with the same semantics as before.
idx := int((ts - b.start + (b.frequency / 3)) / b.frequency)
if idx >= cap(b.data) {
newbuf := newBuffer(ts, b.frequency)
newbuf.prev = b