Skip to content

Commit

Permalink
fix(embedded/store): handle replication of empty values
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Jun 20, 2023
1 parent d4da35f commit 14b2ed8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 1 addition & 5 deletions embedded/store/immustore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2868,10 +2868,6 @@ func (s *ImmuStore) ReadValue(entry *TxEntry) ([]byte, error) {
func (s *ImmuStore) readValueAt(b []byte, off int64, hvalue [sha256.Size]byte, skipIntegrityCheck bool) (n int, err error) {
vLogID, offset := decodeOffset(off)

if len(b) == 0 && vLogID > 0 {
return 0, fmt.Errorf("%w: attempt to read empty value from a non-embedded vlog", ErrCorruptedTxData)
}

if !s.embeddedValues && vLogID == 0 && len(b) > 0 {
// it means value was not stored on any vlog i.e. a truncated transaction was replicated
return 0, io.EOF
Expand Down Expand Up @@ -2922,7 +2918,7 @@ func (s *ImmuStore) readValueAt(b []byte, off int64, hvalue [sha256.Size]byte, s
}
}

// either value was empty (n == 0 && vLogID == 0)
// either value was empty (n == 0)
// or a non-empty value (n > 0) was read from cache or disk

if !skipIntegrityCheck && (len(b) != n || hvalue != sha256.Sum256(b[:n])) {
Expand Down
27 changes: 27 additions & 0 deletions embedded/store/immustore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4937,3 +4937,30 @@ func TestCommitOfEmptyTxWithMetadata(t *testing.T) {
require.NoError(t, err)
require.Empty(t, txholder.Entries())
}

func TestImmudbStore_ExportTxWithEmptyValues(t *testing.T) {
opts := DefaultOptions().WithEmbeddedValues(false)

st, err := Open(t.TempDir(), opts)
require.NoError(t, err)
require.NotNil(t, st)

defer immustoreClose(t, st)

tx, err := st.NewWriteOnlyTx(context.Background())
require.NoError(t, err)

err = tx.Set([]byte("my-key"), nil, nil)
require.NoError(t, err)

hdr, err := tx.Commit(context.Background())
require.NoError(t, err)

txholder, err := st.fetchAllocTx()
require.NoError(t, err)

defer st.releaseAllocTx(txholder)

_, err = st.ExportTx(hdr.ID, false, false, txholder)
require.NoError(t, err)
}

0 comments on commit 14b2ed8

Please sign in to comment.