|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "tiny-bitcask/entity" |
| 11 | +) |
| 12 | + |
| 13 | +func TestHintFile_WriteReadRoundtrip(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + key []byte |
| 17 | + value []byte |
| 18 | + wantKeySize uint32 |
| 19 | + wantValueSize uint32 |
| 20 | + wantOffset int64 |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "ascii_key_value", |
| 24 | + key: []byte("hello"), |
| 25 | + value: []byte("world"), |
| 26 | + wantKeySize: 5, |
| 27 | + wantValueSize: 5, |
| 28 | + wantOffset: 0, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "empty_value", |
| 32 | + key: []byte("k"), |
| 33 | + value: []byte{}, |
| 34 | + wantKeySize: 1, |
| 35 | + wantValueSize: 0, |
| 36 | + wantOffset: 0, |
| 37 | + }, |
| 38 | + } |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + dir := t.TempDir() |
| 42 | + fid := 1 |
| 43 | + datPath := getFilePath(dir, fid) |
| 44 | + f, err := os.OpenFile(datPath, os.O_CREATE|os.O_RDWR, 0o644) |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + e := entity.NewEntryWithData(tt.key, tt.value) |
| 48 | + buf := e.Encode() |
| 49 | + _, err = f.WriteAt(buf, 0) |
| 50 | + require.NoError(t, err) |
| 51 | + require.NoError(t, f.Close()) |
| 52 | + |
| 53 | + require.NoError(t, WriteHintFileForDataFile(dir, fid, true)) |
| 54 | + |
| 55 | + assert.True(t, HintFileExists(dir, fid)) |
| 56 | + |
| 57 | + recs, err := ReadHintFile(dir, fid) |
| 58 | + require.NoError(t, err) |
| 59 | + require.Len(t, recs, 1) |
| 60 | + r := recs[0] |
| 61 | + assert.Equal(t, e.Meta.TimeStamp, r.Timestamp) |
| 62 | + assert.Equal(t, tt.wantKeySize, r.KeySize) |
| 63 | + assert.Equal(t, tt.wantValueSize, r.ValueSize) |
| 64 | + assert.Equal(t, tt.wantOffset, r.RecordOffset) |
| 65 | + assert.Equal(t, byte(0), r.Flag) |
| 66 | + assert.Equal(t, string(tt.key), string(r.Key)) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestHintFile_SkipsTombstone(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + setup func(t *testing.T, f *os.File) error |
| 75 | + wantLen int |
| 76 | + wantKey string |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "live_then_tombstone", |
| 80 | + setup: func(t *testing.T, f *os.File) error { |
| 81 | + t.Helper() |
| 82 | + live := entity.NewEntryWithData([]byte("k1"), []byte("v1")) |
| 83 | + off := int64(0) |
| 84 | + if _, err := f.WriteAt(live.Encode(), off); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + off += live.Size() |
| 88 | + tomb := entity.NewTombstoneEntry([]byte("k2")) |
| 89 | + _, err := f.WriteAt(tomb.Encode(), off) |
| 90 | + return err |
| 91 | + }, |
| 92 | + wantLen: 1, |
| 93 | + wantKey: "k1", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "only_tombstone", |
| 97 | + setup: func(t *testing.T, f *os.File) error { |
| 98 | + t.Helper() |
| 99 | + tomb := entity.NewTombstoneEntry([]byte("k2")) |
| 100 | + _, err := f.WriteAt(tomb.Encode(), 0) |
| 101 | + return err |
| 102 | + }, |
| 103 | + wantLen: 0, |
| 104 | + wantKey: "", |
| 105 | + }, |
| 106 | + } |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + dir := t.TempDir() |
| 110 | + fid := 2 |
| 111 | + datPath := getFilePath(dir, fid) |
| 112 | + f, err := os.OpenFile(datPath, os.O_CREATE|os.O_RDWR, 0o644) |
| 113 | + require.NoError(t, err) |
| 114 | + require.NoError(t, tt.setup(t, f)) |
| 115 | + require.NoError(t, f.Close()) |
| 116 | + |
| 117 | + require.NoError(t, WriteHintFileForDataFile(dir, fid, true)) |
| 118 | + recs, err := ReadHintFile(dir, fid) |
| 119 | + require.NoError(t, err) |
| 120 | + require.Len(t, recs, tt.wantLen) |
| 121 | + if tt.wantLen > 0 { |
| 122 | + assert.Equal(t, tt.wantKey, string(recs[0].Key)) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestHintFile_InvalidHeader(t *testing.T) { |
| 129 | + tests := []struct { |
| 130 | + name string |
| 131 | + fid int |
| 132 | + content []byte |
| 133 | + }{ |
| 134 | + { |
| 135 | + name: "wrong_magic", |
| 136 | + fid: 9, |
| 137 | + content: []byte("BAD!"), |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "too_short", |
| 141 | + fid: 10, |
| 142 | + content: []byte("TBH"), |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "bad_version", |
| 146 | + fid: 11, |
| 147 | + content: append([]byte("TBHK"), 0xFF, 0, 0, 0), |
| 148 | + }, |
| 149 | + } |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + dir := t.TempDir() |
| 153 | + p := filepath.Join(dir, filepath.Base(HintFilePath(dir, tt.fid))) |
| 154 | + require.NoError(t, os.WriteFile(p, tt.content, 0o644)) |
| 155 | + _, err := ReadHintFile(dir, tt.fid) |
| 156 | + assert.ErrorIs(t, err, ErrInvalidHintFile) |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
0 commit comments