File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -399,6 +399,25 @@ func (f *BloomFilter) GobDecode(data []byte) error {
399399 return err
400400}
401401
402+ // MarshalBinary implements binary.BinaryMarshaler interface.
403+ func (f * BloomFilter ) MarshalBinary () ([]byte , error ) {
404+ var buf bytes.Buffer
405+ _ , err := f .WriteTo (& buf )
406+ if err != nil {
407+ return nil , err
408+ }
409+
410+ return buf .Bytes (), nil
411+ }
412+
413+ // UnmarshalBinary implements binary.BinaryUnmarshaler interface.
414+ func (f * BloomFilter ) UnmarshalBinary (data []byte ) error {
415+ buf := bytes .NewBuffer (data )
416+ _ , err := f .ReadFrom (buf )
417+
418+ return err
419+ }
420+
402421// Equal tests for the equality of two Bloom filters
403422func (f * BloomFilter ) Equal (g * BloomFilter ) bool {
404423 return f .m == g .m && f .k == g .k && f .b .Equal (g .b )
Original file line number Diff line number Diff line change @@ -646,3 +646,41 @@ func TestFPP(t *testing.T) {
646646 t .Errorf ("Excessive fpp" )
647647 }
648648}
649+
650+ func TestEncodeDecodeBinary (t * testing.T ) {
651+ f := New (1000 , 4 )
652+ f .Add ([]byte ("one" ))
653+ f .Add ([]byte ("two" ))
654+ f .Add ([]byte ("three" ))
655+ data , err := f .MarshalBinary ()
656+ if err != nil {
657+ t .Fatal (err .Error ())
658+ }
659+
660+ var g BloomFilter
661+ err = g .UnmarshalBinary (data )
662+ if err != nil {
663+ t .Fatal (err .Error ())
664+ }
665+ if g .m != f .m {
666+ t .Error ("invalid m value" )
667+ }
668+ if g .k != f .k {
669+ t .Error ("invalid k value" )
670+ }
671+ if g .b == nil {
672+ t .Fatal ("bitset is nil" )
673+ }
674+ if ! g .b .Equal (f .b ) {
675+ t .Error ("bitsets are not equal" )
676+ }
677+ if ! g .Test ([]byte ("three" )) {
678+ t .Errorf ("missing value 'three'" )
679+ }
680+ if ! g .Test ([]byte ("two" )) {
681+ t .Errorf ("missing value 'two'" )
682+ }
683+ if ! g .Test ([]byte ("one" )) {
684+ t .Errorf ("missing value 'one'" )
685+ }
686+ }
You can’t perform that action at this time.
0 commit comments