Magic as numbers are faster to encode and compare

master
Anand Babu (AB) Periasamy 10 years ago
parent eab19e3ce4
commit ec36a598c9
  1. 58
      pkg/storage/donut/donut.go

@ -25,7 +25,7 @@ import (
DONUT v1 Spec DONUT v1 Spec
********************** **********************
BlockStart [4]byte // Magic="MINI" BlockStart [4]byte // Magic="MINI"=1229867341
VersionMajor uint16 VersionMajor uint16
VersionMinor uint16 VersionMinor uint16
VersionPatch uint16 VersionPatch uint16
@ -33,15 +33,21 @@ import (
Reserved uint64 Reserved uint64
GobHeaderLen uint32 GobHeaderLen uint32
GobHeader io.Reader // matches length GobHeader io.Reader // matches length
BlockData [4]byte // Magic="DATA" BlockData [4]byte // Magic="DATA"=1096040772
Data io.Reader // matches length Data io.Reader // matches length
BlockLen uint64 // length to block start BlockLen uint64 // length to block start
BlockEnd [4]byte // Magic="INIM" BlockEnd [4]byte // Magic="INIM"=1229867341
*/ */
type DonutStructure struct { var (
BlockStart [4]byte // Magic="MINI" MagicMINI = binary.LittleEndian.Uint32([]byte{'M', 'I', 'N', 'I'})
MagicDATA = binary.LittleEndian.Uint32([]byte{'D', 'A', 'T', 'A'})
MagicINIM = binary.LittleEndian.Uint32([]byte{'I', 'N', 'I', 'M'})
)
type DonutFormat struct {
BlockStart uint32 // Magic="MINI"=1229867341
VersionMajor uint16 VersionMajor uint16
VersionMinor uint16 VersionMinor uint16
VersionPatch uint16 VersionPatch uint16
@ -49,15 +55,15 @@ type DonutStructure struct {
Reserved uint64 Reserved uint64
GobHeaderLen uint32 GobHeaderLen uint32
GobHeader GobHeader GobHeader GobHeader
BlockData [4]byte BlockData uint32 // Magic="DATA"=1096040772
Data io.Reader Data io.Reader
BlockLen uint64 BlockLen uint64
BlockEnd [4]byte BlockEnd uint32
} }
type DonutFooter struct { type DonutFooter struct {
BlockLen uint64 BlockLen uint64
BlockEnd uint32 // Magic="INIM" BlockEnd uint32 // Magic="INIM"=1229867341
} }
type Donut struct { type Donut struct {
@ -70,8 +76,8 @@ type GobHeader struct{}
func (donut *Donut) Write(gobHeader GobHeader, object io.Reader) error { func (donut *Donut) Write(gobHeader GobHeader, object io.Reader) error {
// TODO mutex // TODO mutex
// Create bytes buffer representing the new object // Create bytes buffer representing the new object
donutStructure := DonutStructure{ donutFormat := DonutFormat{
BlockStart: [4]byte{'M', 'I', 'N', 'I'}, BlockStart: MagicMINI,
VersionMajor: 1, VersionMajor: 1,
VersionMinor: 0, VersionMinor: 0,
VersionPatch: 0, VersionPatch: 0,
@ -79,30 +85,30 @@ func (donut *Donut) Write(gobHeader GobHeader, object io.Reader) error {
Reserved: 0, Reserved: 0,
GobHeaderLen: 0, GobHeaderLen: 0,
GobHeader: gobHeader, GobHeader: gobHeader,
BlockData: [4]byte{'D', 'A', 'T', 'A'}, BlockData: MagicDATA,
Data: object, Data: object,
BlockLen: 0, BlockLen: 0,
BlockEnd: [4]byte{'I', 'N', 'I', 'M'}, BlockEnd: MagicINIM,
} }
if err := donut.WriteStructure(donut.file, donutStructure); err != nil { if err := donut.WriteFormat(donut.file, donutFormat); err != nil {
return err return err
} }
return nil return nil
} }
func (donut *Donut) WriteStructure(target io.Writer, donutStructure DonutStructure) error { func (donut *Donut) WriteFormat(target io.Writer, donutFormat DonutFormat) error {
err := binary.Write(target, binary.LittleEndian, donutStructure.BlockStart) err := binary.Write(target, binary.LittleEndian, donutFormat.BlockStart)
err = binary.Write(target, binary.LittleEndian, donutStructure.VersionMajor) err = binary.Write(target, binary.LittleEndian, donutFormat.VersionMajor)
err = binary.Write(target, binary.LittleEndian, donutStructure.VersionMinor) err = binary.Write(target, binary.LittleEndian, donutFormat.VersionMinor)
err = binary.Write(target, binary.LittleEndian, donutStructure.VersionPatch) err = binary.Write(target, binary.LittleEndian, donutFormat.VersionPatch)
err = binary.Write(target, binary.LittleEndian, donutStructure.VersionReserved) err = binary.Write(target, binary.LittleEndian, donutFormat.VersionReserved)
err = binary.Write(target, binary.LittleEndian, donutStructure.Reserved) err = binary.Write(target, binary.LittleEndian, donutFormat.Reserved)
err = binary.Write(target, binary.LittleEndian, donutStructure.GobHeaderLen) err = binary.Write(target, binary.LittleEndian, donutFormat.GobHeaderLen)
err = binary.Write(target, binary.LittleEndian, donutStructure.GobHeader) err = binary.Write(target, binary.LittleEndian, donutFormat.GobHeader)
err = binary.Write(target, binary.LittleEndian, donutStructure.BlockData) err = binary.Write(target, binary.LittleEndian, donutFormat.BlockData)
err = binary.Write(target, binary.LittleEndian, donutStructure.Data) err = binary.Write(target, binary.LittleEndian, donutFormat.Data)
err = binary.Write(target, binary.LittleEndian, donutStructure.BlockLen) err = binary.Write(target, binary.LittleEndian, donutFormat.BlockLen)
err = binary.Write(target, binary.LittleEndian, donutStructure.BlockEnd) err = binary.Write(target, binary.LittleEndian, donutFormat.BlockEnd)
return err return err
} }

Loading…
Cancel
Save