Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify csv header logic #99

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions pkg/handlers/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,20 @@ func (c CSVHandler) Synchronize(f *appendable.IndexFile, df []byte) error {
if err != nil {
return fmt.Errorf("failed to read metadata: %w", err)
}
isHeader := false

isEmpty, err := f.IsEmpty()
fieldNames, err := f.IndexFieldNames()
if err != nil {
return fmt.Errorf("failed to check if index file is empty: %w", err)
}

if isEmpty {
isHeader = true
} else {
fieldNames, err := f.IndexFieldNames()
if err != nil {
return fmt.Errorf("failed to retrieve index field names: %w", err)
}
headers = fieldNames
return fmt.Errorf("failed to retrieve index field names: %w", err)
}
headers = fieldNames

for {
i := bytes.IndexByte(df[metadata.ReadOffset:], '\n')
if i == -1 {
break
}

if isHeader {
if len(headers) == 0 {
slog.Info("Parsing CSV headers")
dec := csv.NewReader(bytes.NewReader(df[metadata.ReadOffset : metadata.ReadOffset+uint64(i)]))
headers, err = dec.Read()
Expand All @@ -67,7 +57,6 @@ func (c CSVHandler) Synchronize(f *appendable.IndexFile, df []byte) error {
return fmt.Errorf("failed to parse CSV header: %w", err)
}
metadata.ReadOffset += uint64(i) + 1
isHeader = false
continue
}

Expand Down
Loading