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

Append index for CSV #41

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 18 additions & 8 deletions pkg/appendable/csv_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ type CSVHandler struct {
io.ReadSeeker
}

func (c CSVHandler) Synchronize(f *IndexFile) error {

func (c CSVHandler) Synchronize(f *IndexFile, headerData []string) error {
friendlymatthew marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("===SYNCHRONIZE===")
var headers []string
var err error

isHeader := true
isHeader := false
if headerData == nil {
isHeader = true
} else {
headers = headerData
}

scanner := bufio.NewScanner(f.data)

for i := 0; scanner.Scan(); i++ {
Expand All @@ -40,7 +46,7 @@ func (c CSVHandler) Synchronize(f *IndexFile) error {

f.Checksums = append(f.Checksums, xxhash.Sum64(line))

if i == 0 {
if i == 0 && isHeader {
fmt.Printf("Header %d - StartOffset: %d, EndOffset: %d, Checksum: %d\n\n", i, start, start+uint64(len(line))+1, xxhash.Sum64(line))

} else {
Expand All @@ -58,10 +64,10 @@ func (c CSVHandler) Synchronize(f *IndexFile) error {
}

dec := csv.NewReader(bytes.NewReader(line))
f.handleCSVLine(dec, headers, []string{}, uint64(existingCount), start)
f.handleCSVLine(dec, headers, []string{}, uint64(existingCount)-1, start)
}

fmt.Printf("%v\n\n", f.Checksums)
fmt.Printf("%v\n\n===END===", f.Checksums)
return nil
}

Expand Down Expand Up @@ -113,16 +119,20 @@ func inferCSVField(fieldValue string) (interface{}, protocol.FieldType) {
}

func (i *IndexFile) handleCSVLine(dec *csv.Reader, headers []string, path []string, dataIndex, dataOffset uint64) error {
fmt.Println("\t===reached handleCsv line===")

record, err := dec.Read()

if err != nil {
return fmt.Errorf("failed to read CSV record at index %d: %w", dataIndex, err)
}

fmt.Printf("\tRecord looks like %v", record)

cumulativeLength := uint64(0)

for fieldIndex, fieldValue := range record {
fmt.Printf("\n\theaders: %v\n", headers)
if fieldIndex >= len(headers) {
return fmt.Errorf("field index %d is out of bounds with header", fieldIndex)
}
Expand All @@ -135,7 +145,7 @@ func (i *IndexFile) handleCSVLine(dec *csv.Reader, headers []string, path []stri

value, fieldType := inferCSVField(fieldValue)

fmt.Printf("Field '%s' - Offset: %d, Length: %d, Value: %v, Type: %v\n", fieldName, fieldOffset, fieldLength, value, fieldType)
fmt.Printf("\tField '%s' - Offset: %d, Length: %d, Value: %v, Type: %v\n", fieldName, fieldOffset, fieldLength, value, fieldType)

switch fieldType {
case protocol.FieldTypeBoolean, protocol.FieldTypeString, protocol.FieldTypeNumber:
Expand All @@ -161,7 +171,7 @@ func (i *IndexFile) handleCSVLine(dec *csv.Reader, headers []string, path []stri
cumulativeLength += fieldLength
}

fmt.Printf("\n")
fmt.Printf("\t===end===\n")

return nil
}
92 changes: 45 additions & 47 deletions pkg/appendable/index_file_csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func TestAppendDataRowCSV(t *testing.T) {

var mockCsv string = "test\ntest1\n"
var mockCsv2 string = "test\ntest1\ntest3\n"
var mockCsv string = "header1\ntest1\n"
var mockCsv2 string = "header1\ntest1\ntest3\n"

t.Run("no schema changes", func(t *testing.T) {

Expand Down Expand Up @@ -49,63 +49,61 @@ func TestAppendDataRowCSV(t *testing.T) {
}
})

/*
t.Run("correctly sets field offset", func(t *testing.T) {
i, err := NewIndexFile(CSVHandler{ReadSeeker: strings.NewReader(mockCsv)})
if err != nil {
t.Fatal(err)
}
t.Run("append index to existing", func(t *testing.T) {
i, err := NewIndexFile(CSVHandler{ReadSeeker: strings.NewReader(mockCsv)})
if err != nil {
t.Fatal(err)
}

buf := &bytes.Buffer{}
buf := &bytes.Buffer{}

if err := i.Serialize(buf); err != nil {
t.Fatal(err)
}
if err := i.Serialize(buf); err != nil {
t.Fatal(err)
}

j, err := ReadIndexFile(buf, CSVHandler{ReadSeeker: strings.NewReader(mockCsv2)})
if err != nil {
t.Fatal(err)
}
j, err := ReadIndexFile(buf, CSVHandler{ReadSeeker: strings.NewReader(mockCsv2)})
if err != nil {
t.Fatal(err)
}

// check that the index file now has the additional data ranges but same number of indices
if len(j.Indexes) != 1 {
t.Errorf("got len(j.Indexes) = %d, want 1", len(j.Indexes))
}
// check that the index file now has the additional data ranges but same number of indices
if len(j.Indexes) != 1 {
t.Errorf("got len(j.Indexes) = %d, want 1", len(j.Indexes))
}

t.Logf("len: %v", j.Indexes[0].IndexRecords)
if len(j.Indexes[0].IndexRecords) != 2 {
t.Errorf("got len(j.Indexes[0].IndexRecords) = %d, want 2", len(j.Indexes[0].IndexRecords))
}

if len(j.Indexes[0].IndexRecords) != 2 {
t.Errorf("got len(j.Indexes[0].IndexRecords) = %d, want 2", len(j.Indexes[0].IndexRecords))
if len(j.Indexes[0].IndexRecords["test1"]) != 1 {
t.Errorf("got len(j.Indexes[0].IndexRecords[\"test1\"]) = %d, want 1", len(j.Indexes[0].IndexRecords["test1"]))
}
if len(j.Indexes[0].IndexRecords["test3"]) != 1 {
for key, records := range j.Indexes[0].IndexRecords {
t.Errorf("\n\n\nKey: %v, Records: %+v", key, records)
}
t.Errorf("got len(j.Indexes[0].IndexRecords[\"test3\"]) = %d, want 1", len(j.Indexes[0].IndexRecords["test3"]))
}

if len(j.Indexes[0].IndexRecords["test1"]) != 1 {
t.Errorf("got len(j.Indexes[0].IndexRecords[\"test1\"]) = %d, want 1", len(j.Indexes[0].IndexRecords["test1"]))
}
if len(j.Indexes[0].IndexRecords["test3"]) != 1 {
for key, records := range j.Indexes[0].IndexRecords {
t.Errorf("\n\n\nKey: %v, Records: %+v", key, records)
}
t.Errorf("got len(j.Indexes[0].IndexRecords[\"test3\"]) = %d, want 1", len(j.Indexes[0].IndexRecords["test3"]))
}
if j.Indexes[0].IndexRecords["test1"][0].DataNumber != 0 {
t.Errorf("got i.Indexes[0].IndexRecords[\"test1\"][0].DataNumber = %d, want 0", j.Indexes[0].IndexRecords["test1"][0].DataNumber)
}
if j.Indexes[0].IndexRecords["test1"][0].FieldStartByteOffset != uint64(len("{\"test\":")) {
t.Errorf("got i.Indexes[0].IndexRecords[\"test1\"][0].FieldStartByteOffset = %d, want 10", j.Indexes[0].IndexRecords["test1"][0].FieldStartByteOffset)
}

if j.Indexes[0].IndexRecords["test1"][0].DataNumber != 0 {
t.Errorf("got i.Indexes[0].IndexRecords[\"test1\"][0].DataNumber = %d, want 0", j.Indexes[0].IndexRecords["test1"][0].DataNumber)
}
if j.Indexes[0].IndexRecords["test1"][0].FieldStartByteOffset != uint64(len("{\"test\":")) {
t.Errorf("got i.Indexes[0].IndexRecords[\"test1\"][0].FieldStartByteOffset = %d, want 10", j.Indexes[0].IndexRecords["test1"][0].FieldStartByteOffset)
}
if j.Indexes[0].IndexRecords["test3"][0].DataNumber != 0 {
t.Errorf("got i.Indexes[0].IndexRecords[\"test3\"][0].DataNumber = %d, want 1", j.Indexes[0].IndexRecords["test3"][0].DataNumber)
}

if j.Indexes[0].IndexRecords["test3"][0].DataNumber != 1 {
t.Errorf("got i.Indexes[0].IndexRecords[\"test3\"][1].DataNumber = %d, want 1", j.Indexes[0].IndexRecords["test3"][1].DataNumber)
}
if j.Indexes[0].IndexRecords["test3"][0].FieldStartByteOffset != uint64(len("test\ntest1\n")) {
t.Errorf("got i.Indexes[0].IndexRecords[\"test3\"][1].FieldStartByteOffset = %d, want 10", j.Indexes[0].IndexRecords["test3"][1].FieldStartByteOffset)
}
})
*/
// verify byte offset calculation
if j.Indexes[0].IndexRecords["test3"][0].FieldStartByteOffset != uint64(len("header\ntest1\n")+1) {
t.Errorf("got i.Indexes[0].IndexRecords[\"test3\"][0].FieldStartByteOffset = %d, want 14", j.Indexes[0].IndexRecords["test3"][0].FieldStartByteOffset)
}
})

t.Run("generate index file", func(t *testing.T) {
i, err := NewIndexFile(CSVHandler{ReadSeeker: strings.NewReader(mockCsv2)})
i, err := NewIndexFile(CSVHandler{ReadSeeker: strings.NewReader("")})

if err != nil {
t.Fatal(err)
Expand Down
40 changes: 32 additions & 8 deletions pkg/appendable/io.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package appendable

import (
"bufio"
"bytes"
"encoding/binary"
"encoding/csv"
"fmt"
"io"
"sort"
Expand All @@ -15,7 +17,7 @@ import (

type DataHandler interface {
io.ReadSeeker
Synchronize(f *IndexFile) error
Synchronize(f *IndexFile, headerData []string) error
}

func NewIndexFile(data DataHandler) (*IndexFile, error) {
Expand All @@ -24,7 +26,7 @@ func NewIndexFile(data DataHandler) (*IndexFile, error) {
Indexes: []Index{},
data: data,
}
return f, data.Synchronize(f)
return f, data.Synchronize(f, nil)
}

func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
Expand All @@ -33,8 +35,6 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {

f.data = data

fmt.Printf("data looks like %v\n", data)

// read the version
version, err := encoding.ReadByte(r)
if err != nil {
Expand Down Expand Up @@ -80,6 +80,8 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
return nil, fmt.Errorf("expected to read %d bytes, read %d bytes", ifh.IndexLength, br)
}

fmt.Printf("headers look like: %v\n", f.Indexes)

// read the index records
for i, index := range f.Indexes {

Expand Down Expand Up @@ -111,6 +113,7 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {

switch value.(type) {
case nil, bool, int, int8, int16, int32, int64, float32, float64, string:
fmt.Printf("appending: %v", value)
index.IndexRecords[value] = append(index.IndexRecords[value], ir)
default:
return nil, fmt.Errorf("unsupported type: %T", value)
Expand Down Expand Up @@ -140,9 +143,10 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
start = f.EndByteOffsets[0]
startIndex = 1
}
fmt.Printf("\ndatacount: %v\n", ifh.DataCount)
for i := startIndex; i < int(ifh.DataCount); i++ {

fmt.Printf("Current start idx: %d, position: %d\n", i, start)
fmt.Printf("\n\nCurrent start idx: %d, position: %d\n", i, start)

if _, isCsv := data.(CSVHandler); isCsv {
if i > 1 {
Expand All @@ -159,7 +163,7 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
if _, err := io.CopyN(buf, data, int64(f.EndByteOffsets[i]-start-1)); err != nil {
return nil, fmt.Errorf("failed to read data file: %w", err)
}
fmt.Printf("string: %v and \n bytes look like: %v\n", buf.String(), buf.Bytes())
fmt.Printf("string: %v and bytes look like: %v\n", buf.String(), buf.Bytes())

if xxhash.Sum64(buf.Bytes()) != f.Checksums[i] {
return nil, fmt.Errorf("checksum mismatch a %d, b %d", xxhash.Sum64(buf.Bytes()), f.Checksums[i])
Expand All @@ -169,7 +173,25 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
default:
return nil, fmt.Errorf("unsupported version: %d", version)
}
var headers []string
if _, isCSV := data.(CSVHandler); isCSV {

if _, err := data.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("failed to seek data file: %w", err)
}

buf := bufio.NewReader(data)
headerLine, err := buf.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("failed to read header line: %w", err)
}

dec := csv.NewReader(strings.NewReader(headerLine))
headers, err = dec.Read()
if err != nil {
return nil, fmt.Errorf("failed to parse CSV header: %w", err)
}
}
fmt.Printf("endbyteoffsets: %v\n", f.EndByteOffsets)

// we've deserialized the underlying file, seek to the end of the last data range to prepare for appending
Expand All @@ -184,8 +206,10 @@ func ReadIndexFile(r io.Reader, data DataHandler) (*IndexFile, error) {
}
}

fmt.Println("End of ReadIndexFile, calling Synchronize")
return f, data.Synchronize(f)
// extract headers from 0 -> endByteOffsets[0]

fmt.Printf("\n===End of ReadIndexFile, calling Synchronize===\n")
return f, data.Synchronize(f, headers)
}

func (f *IndexFile) Serialize(w io.Writer) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/appendable/jsonl_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type JSONLHandler struct {
io.ReadSeeker
}

func (j JSONLHandler) Synchronize(f *IndexFile) error {
func (j JSONLHandler) Synchronize(f *IndexFile, headerData []string) error {

// read until the next newline
scanner := bufio.NewScanner(f.data)
Expand Down
2 changes: 0 additions & 2 deletions pkg/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ func (i IndexRecord) CSVField(r io.ReadSeeker) (any, error) {
return nil, fmt.Errorf("failed to decode field: %w", err)
}

fmt.Printf("Fields read at offset %d: %v\n", i.FieldStartByteOffset, fields)

if _, err := r.Seek(offset, io.SeekStart); err != nil {
return nil, fmt.Errorf("failed to seek to original offset: %w", err)
}
Expand Down