|
| 1 | +package streamhash |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stellar/streamhash/internal/sherr" |
| 12 | +) |
| 13 | + |
| 14 | +// buildEmptyIndex builds a zero-key index via the requested builder type and |
| 15 | +// options, returning the file path. |
| 16 | +func buildEmptyIndex(t *testing.T, unsorted bool, opts ...BuildOption) string { |
| 17 | + t.Helper() |
| 18 | + ctx := context.Background() |
| 19 | + dir := t.TempDir() |
| 20 | + path := filepath.Join(dir, "empty.idx") |
| 21 | + |
| 22 | + if unsorted { |
| 23 | + b, err := NewUnsortedBuilder(ctx, path, 0, dir, opts...) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("NewUnsortedBuilder(0): %v", err) |
| 26 | + } |
| 27 | + if err := b.Finish(); err != nil { |
| 28 | + t.Fatalf("unsorted Finish: %v", err) |
| 29 | + } |
| 30 | + return path |
| 31 | + } |
| 32 | + |
| 33 | + b, err := NewSortedBuilder(ctx, path, 0, opts...) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("NewSortedBuilder(0): %v", err) |
| 36 | + } |
| 37 | + if err := b.Finish(); err != nil { |
| 38 | + t.Fatalf("sorted Finish: %v", err) |
| 39 | + } |
| 40 | + return path |
| 41 | +} |
| 42 | + |
| 43 | +// TestEmptyIndexWithWorkers checks that a zero-key build requested with |
| 44 | +// WithWorkers still succeeds: newBuilder forces single-threaded for an empty |
| 45 | +// build (nothing to parallelize), so it must not reach the parallel finish |
| 46 | +// paths, which assume real block work. |
| 47 | +func TestEmptyIndexWithWorkers(t *testing.T) { |
| 48 | + for _, unsorted := range []bool{false, true} { |
| 49 | + name := "sorted" |
| 50 | + if unsorted { |
| 51 | + name = "unsorted" |
| 52 | + } |
| 53 | + t.Run(name, func(t *testing.T) { |
| 54 | + path := buildEmptyIndex(t, unsorted, WithWorkers(4)) |
| 55 | + idx, err := Open(path) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Open: %v", err) |
| 58 | + } |
| 59 | + defer idx.Close() |
| 60 | + if got := idx.NumKeys(); got != 0 { |
| 61 | + t.Errorf("NumKeys() = %d, want 0", got) |
| 62 | + } |
| 63 | + key := make([]byte, MinKeySize) |
| 64 | + if _, err := idx.QueryRank(key); !errors.Is(err, sherr.ErrNotFound) { |
| 65 | + t.Errorf("QueryRank = %v, want ErrNotFound", err) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// TestEmptyIndex pins the zero-key contract across both algorithms, both |
| 72 | +// builder types, and with/without payload+fingerprint: the build succeeds, the |
| 73 | +// index opens and passes integrity Verify, reports zero keys, and every |
| 74 | +// QueryRank resolves to ErrNotFound (never a spurious hit or a decode error). |
| 75 | +func TestEmptyIndex(t *testing.T) { |
| 76 | + algos := []struct { |
| 77 | + name string |
| 78 | + opt BuildOption |
| 79 | + }{ |
| 80 | + {"bijection", WithAlgorithm(AlgoBijection)}, |
| 81 | + {"ptrhash", WithAlgorithm(AlgoPTRHash)}, |
| 82 | + } |
| 83 | + builders := []struct { |
| 84 | + name string |
| 85 | + unsorted bool |
| 86 | + }{ |
| 87 | + {"sorted", false}, |
| 88 | + {"unsorted", true}, |
| 89 | + } |
| 90 | + // mphf-only vs the txhash-style payload+fingerprint layout, which gives the |
| 91 | + // header a non-zero PayloadSize/FingerprintSize even though the payload |
| 92 | + // region is empty. |
| 93 | + optionSets := []struct { |
| 94 | + name string |
| 95 | + opts []BuildOption |
| 96 | + hasPayload bool |
| 97 | + }{ |
| 98 | + {"mphf-only", nil, false}, |
| 99 | + {"payload+fingerprint", []BuildOption{WithPayload(8), WithFingerprint(4)}, true}, |
| 100 | + } |
| 101 | + |
| 102 | + for _, a := range algos { |
| 103 | + for _, bt := range builders { |
| 104 | + for _, optSet := range optionSets { |
| 105 | + t.Run(a.name+"/"+bt.name+"/"+optSet.name, func(t *testing.T) { |
| 106 | + opts := append([]BuildOption{a.opt}, optSet.opts...) |
| 107 | + path := buildEmptyIndex(t, bt.unsorted, opts...) |
| 108 | + |
| 109 | + idx, err := Open(path) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("Open empty index: %v", err) |
| 112 | + } |
| 113 | + defer idx.Close() |
| 114 | + |
| 115 | + if got := idx.NumKeys(); got != 0 { |
| 116 | + t.Errorf("NumKeys() = %d, want 0", got) |
| 117 | + } |
| 118 | + if err := idx.Verify(); err != nil { |
| 119 | + t.Errorf("Verify() on empty index: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + // Every key must miss cleanly. |
| 123 | + for i := range 200 { |
| 124 | + var key [16]byte |
| 125 | + binary.LittleEndian.PutUint64(key[0:], uint64(i)*0x9e3779b97f4a7c15) |
| 126 | + binary.LittleEndian.PutUint64(key[8:], uint64(i)^0xdeadbeef) |
| 127 | + if _, qerr := idx.QueryRank(key[:]); !errors.Is(qerr, sherr.ErrNotFound) { |
| 128 | + t.Fatalf("QueryRank(key %d) = %v, want ErrNotFound", i, qerr) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // The payload read path (used by the txhash cold store) must |
| 133 | + // also miss cleanly rather than read the zero-size region. |
| 134 | + if optSet.hasPayload { |
| 135 | + pi, perr := OpenPayload(path) |
| 136 | + if perr != nil { |
| 137 | + t.Fatalf("OpenPayload empty index: %v", perr) |
| 138 | + } |
| 139 | + var key [16]byte |
| 140 | + binary.LittleEndian.PutUint64(key[0:], 0x1234) |
| 141 | + if _, _, qerr := pi.QueryPayload(key[:]); !errors.Is(qerr, sherr.ErrNotFound) { |
| 142 | + t.Errorf("QueryPayload on empty index = %v, want ErrNotFound", qerr) |
| 143 | + } |
| 144 | + _ = pi.Close() |
| 145 | + } |
| 146 | + |
| 147 | + // The in-memory open path must accept it too. |
| 148 | + data, err := os.ReadFile(path) |
| 149 | + if err != nil { |
| 150 | + t.Fatal(err) |
| 151 | + } |
| 152 | + bIdx, err := OpenBytes(data) |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("OpenBytes empty index: %v", err) |
| 155 | + } |
| 156 | + if got := bIdx.NumKeys(); got != 0 { |
| 157 | + t.Errorf("OpenBytes NumKeys() = %d, want 0", got) |
| 158 | + } |
| 159 | + if err := bIdx.Close(); err != nil { |
| 160 | + t.Errorf("Close: %v", err) |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments