Skip to content

Commit

Permalink
fix(embedded/tbtree): add marker entry to store timestamp when tree i…
Browse files Browse the repository at this point in the history
…s empty

Signed-off-by: Stefano Scafiti <[email protected]>
  • Loading branch information
ostafen committed Feb 26, 2025
1 parent cdd00cb commit 47c38ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
18 changes: 15 additions & 3 deletions embedded/tbtree/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,23 @@ func (l *leafNode) writeTo(nw, hw io.Writer, writeOpts *WriteOpts, buf []byte) (
buf[bi] = LeafNodeType
bi++

binary.BigEndian.PutUint16(buf[bi:], uint16(len(l.values)))
bi += 2
if len(l.values) == 0 && l._ts > 0 {
// NOTE: we store a marker entry to remember the highest timestamp seen

accH := int64(0)
binary.BigEndian.PutUint16(buf[bi:], 1)
bi += 2

binary.BigEndian.PutUint16(buf[bi:], 0)
bi += 2

binary.BigEndian.PutUint64(buf[bi:], l._ts)
bi += 8
} else {
binary.BigEndian.PutUint16(buf[bi:], uint16(len(l.values)))
bi += 2
}

accH := int64(0)
for _, v := range l.values {
timedValue := v.timedValues[0]

Expand Down
14 changes: 12 additions & 2 deletions embedded/tbtree/tbtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ func (t *TBtree) readNodeFrom(r *appendable.Reader) (node, error) {
n.off = off
return n, nil
}

return nil, ErrReadingFileContent
}

Expand Down Expand Up @@ -914,6 +913,18 @@ func (t *TBtree) readLeafNodeFrom(r *appendable.Reader) (*leafNode, error) {
return nil, err
}

if ksize == 0 {
ts, err := r.ReadUint64()
if err != nil {
return nil, err
}

return &leafNode{
t: t,
_ts: ts,
}, nil
}

key := make([]byte, ksize)
_, err = r.Read(key)
if err != nil {
Expand Down Expand Up @@ -1607,7 +1618,6 @@ func (t *TBtree) IncreaseTs(ts uint64) error {
_, _, err := t.flushTree(t.cleanupPercentage, false, false, "increaseTs")
return err
}

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions embedded/tbtree/tbtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,30 @@ func TestTBTreeIncreaseTs(t *testing.T) {
require.ErrorIs(t, err, ErrAlreadyClosed)
}

func TestTBTreeFlushWithNoActualData(t *testing.T) {
dir := t.TempDir()
t.Cleanup(func() {
os.RemoveAll(dir)
})

tbtree, err := Open(dir, DefaultOptions())
require.NoError(t, err)

err = tbtree.IncreaseTs(100)
require.NoError(t, err)

_, _, err = tbtree.Flush()
require.NoError(t, err)

err = tbtree.Close()
require.NoError(t, err)

tbtree, err = Open(dir, DefaultOptions())
require.NoError(t, err)

require.Equal(t, tbtree.Ts(), uint64(100))
}

func BenchmarkRandomInsertion(b *testing.B) {
seed := rand.NewSource(time.Now().UnixNano())
rnd := rand.New(seed)
Expand Down

0 comments on commit 47c38ac

Please sign in to comment.