-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add schema serde utilities * update go version * move avro builder into avro package, rename avro.Schema to avro.Serde * resolve lint warnings * do not expose Sort method * use bytes instead of string * clarify comment * add comment about sorting
- Loading branch information
1 parent
d68a4ca
commit d973cba
Showing
20 changed files
with
2,324 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package rabin provides a Rabin fingerprint hash.Hash64 implementation compatible | ||
// with the Avro spec: https://avro.apache.org/docs/1.8.2/spec.html#schema_fingerprints. | ||
package rabin | ||
|
||
import "hash" | ||
|
||
type digest uint64 | ||
|
||
// New constructs a new Rabin fingerprint hash.Hash64 initialized to the empty | ||
// state according to the Avro spec: https://avro.apache.org/docs/1.8.2/spec.html#schema_fingerprints. | ||
func New() hash.Hash64 { | ||
var d digest | ||
d.Reset() | ||
return &d | ||
} | ||
|
||
func (d *digest) Write(p []byte) (n int, err error) { | ||
*d = update(*d, p) | ||
return len(p), nil | ||
} | ||
|
||
func (d *digest) Sum64() uint64 { | ||
return uint64(*d) | ||
} | ||
|
||
func (d *digest) Sum(in []byte) []byte { | ||
s := d.Sum64() | ||
return append(in, byte(s>>56), byte(s>>48), byte(s>>40), byte(s>>32), byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) | ||
} | ||
|
||
func (d *digest) Reset() { | ||
*d = digest(rabinEmpty) | ||
} | ||
|
||
func (d *digest) Size() int { return 8 } | ||
func (d *digest) BlockSize() int { return 1 } | ||
|
||
const rabinEmpty = uint64(0xc15d213aa4d7a795) | ||
|
||
// rabinTable is used to compute the CRC-64-AVRO fingerprint. | ||
var rabinTable = newRabinFingerprintTable() | ||
|
||
// newRabinFingerprintTable initializes the fingerprint table according to the | ||
// spec: https://avro.apache.org/docs/1.8.2/spec.html#schema_fingerprints | ||
func newRabinFingerprintTable() [256]uint64 { | ||
fpTable := [256]uint64{} | ||
for i := 0; i < 256; i++ { | ||
fp := uint64(i) | ||
for j := 0; j < 8; j++ { | ||
fp = (fp >> 1) ^ (rabinEmpty & -(fp & 1)) | ||
} | ||
fpTable[i] = fp | ||
} | ||
return fpTable | ||
} | ||
|
||
// Bytes creates a Rabin fingerprint according to the spec: | ||
// https://avro.apache.org/docs/1.8.2/spec.html#schema_fingerprints | ||
func Bytes(buf []byte) uint64 { | ||
h := New() | ||
_, _ = h.Write(buf) // it never returns an error | ||
return h.Sum64() | ||
} | ||
|
||
// update adds p to the running checksum d. | ||
func update(d digest, p []byte) digest { | ||
fp := uint64(d) | ||
for i := 0; i < len(p); i++ { | ||
fp = (fp >> 8) ^ rabinTable[(byte(fp)^p[i])&0xff] | ||
} | ||
return digest(fp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rabin | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/matryer/is" | ||
) | ||
|
||
func TestRabin(t *testing.T) { | ||
testCases := []struct { | ||
have string | ||
want uint64 | ||
}{ | ||
{have: `"int"`, want: 0x7275d51a3f395c8f}, | ||
{have: `"string"`, want: 0x8f014872634503c7}, | ||
{have: `"bool"`, want: 0x4a1c6b80ca0bcf48}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("Bytes_%v", tc.have), func(t *testing.T) { | ||
is := is.New(t) | ||
got := Bytes([]byte(tc.have)) | ||
is.Equal(tc.want, got) | ||
}) | ||
t.Run(fmt.Sprintf("Hash_%v", tc.have), func(t *testing.T) { | ||
is := is.New(t) | ||
d := New() | ||
|
||
n, err := d.Write([]byte(tc.have)) | ||
is.NoErr(err) | ||
is.Equal(n, len(tc.have)) | ||
|
||
got := d.Sum64() | ||
is.Equal(tc.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package avro | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrUnsupportedType = errors.New("unsupported avro type") | ||
ErrSchemaValueMismatch = errors.New("avro schema doesn't match supplied value") | ||
) |
Oops, something went wrong.