package engine import ( "context" "slices" "strings" "testing" "github.com/seaweedfs/seaweedfs/weed/mq/topic" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb" ) type unflushedEntriesBroker struct { BrokerClientInterface entries []*filer_pb.LogEntry } func (b *unflushedEntriesBroker) GetUnflushedMessages(context.Context, string, string, topic.Partition, int64) ([]*filer_pb.LogEntry, error) { return b.entries, nil } // A query sees the rows compaction keeps: a close marker, an entry with // neither key nor value, is not one, and in a schematized topic neither is a // message without a value. func TestScanSkipsWhatCompactionSkips(t *testing.T) { // a JSON string whose bytes parse as a DataMessage carrying a Ctrl jsonString := []byte(`" ` + strings.Repeat("h1", 15) + `h"`) entries := []*filer_pb.LogEntry{ {TsNs: 1, Data: []byte("keyless")}, {TsNs: 2, Key: []byte("tombstone")}, {TsNs: 3, Key: []byte("json"), Data: jsonString}, {TsNs: 4}, {TsNs: 5, Key: []byte("k"), Data: []byte("v")}, } bytesType := &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{ScalarType: schema_pb.ScalarType_BYTES}} for _, tc := range []struct { name string fields []*schema_pb.Field want []int64 }{ {"schemaless", []*schema_pb.Field{{Name: SW_COLUMN_NAME_VALUE, Type: bytesType}}, []int64{1, 2, 3, 5}}, {"schematized", []*schema_pb.Field{{Name: "payload", Type: bytesType}}, []int64{1, 3, 5}}, } { hms := &HybridMessageScanner{ brokerClient: &unflushedEntriesBroker{entries: entries}, topic: topic.Topic{Namespace: "ns", Name: "t"}, recordSchema: &schema_pb.RecordType{Fields: tc.fields}, } results, _, err := hms.scanUnflushedDataWithStats(context.Background(), topic.Partition{}, HybridScanOptions{}) if err != nil { t.Fatal(err) } var got []int64 for _, r := range results { got = append(got, r.Timestamp) } if !slices.Equal(got, tc.want) { t.Errorf("%s: scanned messages at %v, want %v", tc.name, got, tc.want) } var rows []int64 for _, e := range entries { if hms.isRow(e) { rows = append(rows, e.TsNs) } } if !slices.Equal(rows, tc.want) { t.Errorf("%s: counted rows at %v, want %v", tc.name, rows, tc.want) } // COUNT(*) adds the broker's unflushed entries by the same rule if got := hms.countRows(entries); got != int64(len(tc.want)) { t.Errorf("%s: %d unflushed rows counted, the scan reads %d", tc.name, got, len(tc.want)) } } }