-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from yourbasic/tip
Adds binary format
- Loading branch information
Showing
4 changed files
with
107 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package bloom | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
) | ||
|
||
const binVer0 = 0 | ||
|
||
// Data to be included in binary representation of Filter. | ||
type marshalFilter struct { | ||
Version int | ||
Data []uint64 | ||
Lookups int | ||
Count int64 | ||
} | ||
|
||
// MarshalBinary returns a binary representation of the filter. | ||
// | ||
// This method implements the encoding.BinaryMarshaler interface. | ||
// The packages encoding/gob, encoding/json, and encoding/xml | ||
// all check for this interface. | ||
func (f *Filter) MarshalBinary() ([]byte, error) { | ||
mf := marshalFilter{ | ||
Version: binVer0, | ||
Data: f.data, | ||
Lookups: f.lookups, | ||
Count: f.count, | ||
} | ||
var b bytes.Buffer | ||
err := gob.NewEncoder(&b).Encode(mf) | ||
return b.Bytes(), err | ||
} | ||
|
||
// UnmarshalBinary imports binary data created by MarshalBinary | ||
// into an empty filter. If the filter is not empty, all previous | ||
// entries are overwritten. | ||
func (f *Filter) UnmarshalBinary(data []byte) error { | ||
var mf marshalFilter | ||
err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&mf) | ||
if err != nil { | ||
return err | ||
} | ||
f.data = mf.Data | ||
f.lookups = mf.Lookups | ||
f.count = mf.Count | ||
return nil | ||
} |
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