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

Add Codec.ScanBinary #271

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions benchmark_test.go
Expand Up @@ -85,3 +85,16 @@ func BenchmarkNativeFromTextualUsingV2(b *testing.B) {
_ = nativeFromTextUsingV2(b, codec, textData)
}
}

func BenchmarkScanBinaryUsingV2(b *testing.B) {
avroBlob, err := os.ReadFile("fixtures/quickstop-null.avro")
if err != nil {
b.Fatal(err)
}
nativeData, codec := nativeFromAvroUsingV2(b, avroBlob)
binaryData := binaryFromNativeUsingV2(b, codec, nativeData)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = scanBinaryUsingV2(b, codec, binaryData)
}
}
51 changes: 51 additions & 0 deletions codec.go
Expand Up @@ -57,6 +57,8 @@ type Codec struct {
nativeFromBinary func([]byte) (interface{}, []byte, error)
textualFromNative func([]byte, interface{}) ([]byte, error)

scanBinary func([]byte, ...interface{}) ([]byte, error)

Rabin uint64
}

Expand Down Expand Up @@ -583,6 +585,55 @@ func (c *Codec) TextualFromNative(buf []byte, datum interface{}) ([]byte, error)
return newBuf, nil
}

// ScanBinary copies the values from the binary encoded byte slice into the
// values pointed to by dest in the order of the fields of the Avro schema
// supplied when creating the Codec. On success, it returns a byte slice
// containing the remaining undecoded bytes, and a nil error value. On error, it
// returns the original byte slice, and the error message.
//
// func ExampleCodec_ScanBinary_avro() {
// codec, err := NewCodec(`
// {
// "type": "record",
// "name": "r1",
// "fields" : [
// {"name": "f1", "type": "string"},
// {"name": "f2", "type": "int"}
// ]
// }
// `)
//
// if err != nil {
// log.Fatal(err)
// }
//
// binary := []byte{
// 0x10, // field1 size = 8
// 't', 'h', 'i', 'r', 't', 'e', 'e', 'n',
// 0x1a, // field2 == 13
// }
//
// var f1 string
// var f2 int
// if _, err = codec.ScanBinary(binary, &f1, &f2); err != nil {
// log.Fatal(err)
// }
//
// fmt.Printf("f1: %v, f2: %v", f1, f2)
// // Output: f1: thirteen, f2: 13
// }
func (c *Codec) ScanBinary(buf []byte, dest ...interface{}) ([]byte, error) {
// TODO: implement for every type and remove
if c.scanBinary == nil {
return buf, fmt.Errorf("ScanBinary not implemented for codec with schema: %s", c.schemaOriginal)
}
newBuf, err := c.scanBinary(buf, dest...)
if err != nil {
return buf, err // if error, return original byte slice
}
return newBuf, nil
}

// Schema returns the original schema used to create the Codec.
func (c *Codec) Schema() string {
return c.schemaOriginal
Expand Down