diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2020-02-01 10:01:22 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2020-02-01 10:01:22 -0500 |
commit | be65ef54f7d2371602ae5999d9955d4bf9f48cd5 (patch) | |
tree | ca3b3beb94aab5b84fe14bccbdd9d5f79f912cf3 /rrdformat | |
parent | a14b9983db45f7b20a654d85ca580bc729e20874 (diff) |
Rename some of the rrdbinary types to be more C-like
Diffstat (limited to 'rrdformat')
-rw-r--r-- | rrdformat/format.go | 22 | ||||
-rw-r--r-- | rrdformat/rrdbinary/decode.go | 68 | ||||
-rw-r--r-- | rrdformat/rrdbinary/types.go | 34 | ||||
-rw-r--r-- | rrdformat/sniff.go | 54 |
4 files changed, 89 insertions, 89 deletions
diff --git a/rrdformat/format.go b/rrdformat/format.go index 8277089..d004e86 100644 --- a/rrdformat/format.go +++ b/rrdformat/format.go @@ -39,28 +39,28 @@ import ( // // - "0001" (the original in rrdtool 1.0.0, 1999-07-15) -type Value = rrdbinary.Float +type Value = rrdbinary.Double type Header struct { Cookie rrdbinary.String `rrdbinary:"size=4" xml:"-"` Version rrdbinary.String `rrdbinary:"size=5" xml:"version"` - FloatCookie rrdbinary.Float `xml:"-"` - DSCnt rrdbinary.Uint `xml:"-"` - RRACnt rrdbinary.Uint `xml:"-"` - PDPStep rrdbinary.Uint `xml:"step"` + FloatCookie rrdbinary.Double `xml:"-"` + DSCnt rrdbinary.ULong `xml:"-"` + RRACnt rrdbinary.ULong `xml:"-"` + PDPStep rrdbinary.ULong `xml:"step"` Parameters [10]rrdbinary.Unival `xml:"-"` } type DSDef struct { - DSName rrdbinary.String `rrdbinary:"size=20" xml:"name"` - DSType rrdbinary.String `rrdbinary:"size=20" xml:"type"` + DSName rrdbinary.String `rrdbinary:"size=20" xml:"name"` + DSType rrdbinary.String `rrdbinary:"size=20" xml:"type"` Parameters [10]rrdbinary.Unival `xml:"params>item"` } type RRADef struct { CFName rrdbinary.String `rrdbinary:"size=20" xml:"cf"` - RowCnt rrdbinary.Uint `xml:"-"` - PDPCnt rrdbinary.Uint `xml:"pdp_per_row"` + RowCnt rrdbinary.ULong `xml:"-"` + PDPCnt rrdbinary.ULong `xml:"pdp_per_row"` Parameters [10]rrdbinary.Unival `xml:"params>item"` CDPPrep *CDPPrep `rrdbinary:"-" xml:"cdp_prep"` Database [][]Value `rrdbinary:"-" xml:"database>row>v"` @@ -68,7 +68,7 @@ type RRADef struct { type TimeWithUsec struct { Sec rrdbinary.Time - Usec rrdbinary.Int // signed, but always >= 0 + Usec rrdbinary.Long // signed, but always >= 0 } func (ts TimeWithUsec) Time() time.Time { @@ -93,7 +93,7 @@ type CDPPrep struct { } type RRAPtr struct { - CurRow rrdbinary.Uint + CurRow rrdbinary.ULong } type RRDv0005 = RRDv0004 diff --git a/rrdformat/rrdbinary/decode.go b/rrdformat/rrdbinary/decode.go index cdaf49e..08c9141 100644 --- a/rrdformat/rrdbinary/decode.go +++ b/rrdformat/rrdbinary/decode.go @@ -153,9 +153,9 @@ func (obj *String) unmarshalRRD(d *Decoder, tag string) error { return nil } -func (obj *Float) unmarshalRRD(d *Decoder, tag string) error { - if d.arch.FloatWidth != 8 { - return archErrorf("rrdbinary does not support FloatWidth=%d; only supports 8", d.arch.FloatWidth) +func (obj *Double) unmarshalRRD(d *Decoder, tag string) error { + if d.arch.DoubleWidth != 8 { + return archErrorf("rrdbinary does not support DoubleWidth=%d; only supports 8", d.arch.DoubleWidth) } if tag != "" { return typeErrorf("invalid rrdbinary struct tag for float: %q", tag) @@ -164,26 +164,26 @@ func (obj *Float) unmarshalRRD(d *Decoder, tag string) error { data := d.data[d.pos:] padding := 0 - if d.pos%d.arch.FloatAlign != 0 { - padding = d.arch.FloatAlign - (d.pos % d.arch.FloatAlign) + if d.pos%d.arch.DoubleAlign != 0 { + padding = d.arch.DoubleAlign - (d.pos % d.arch.DoubleAlign) } if len(data) < padding { - return d.binErrorf(padding+d.arch.FloatWidth, "unexpected end-of-file in %d-byte padding-before-float", padding) + return d.binErrorf(padding+d.arch.DoubleWidth, "unexpected end-of-file in %d-byte padding-before-float", padding) } data = data[padding:] - if len(data) < d.arch.FloatWidth { - return d.binErrorf(d.arch.FloatWidth, "unexpected end-of-file in %d-byte float", d.arch.FloatWidth) + if len(data) < d.arch.DoubleWidth { + return d.binErrorf(d.arch.DoubleWidth, "unexpected end-of-file in %d-byte float", d.arch.DoubleWidth) } - *obj = Float(math.Float64frombits(d.arch.ByteOrder.Uint64(data))) - d.pos += padding + d.arch.FloatWidth + *obj = Double(math.Float64frombits(d.arch.ByteOrder.Uint64(data))) + d.pos += padding + d.arch.DoubleWidth return nil } -func (obj *Uint) unmarshalRRD(d *Decoder, tag string) error { - if d.arch.IntWidth != 4 && d.arch.IntWidth != 8 { - return archErrorf("rrdbinary does not support IntWidth=%d; only supports 4 or 8", d.arch.IntWidth) +func (obj *ULong) unmarshalRRD(d *Decoder, tag string) error { + if d.arch.LongWidth != 4 && d.arch.LongWidth != 8 { + return archErrorf("rrdbinary does not support LongWidth=%d; only supports 4 or 8", d.arch.LongWidth) } if tag != "" { return typeErrorf("invalid rrdbinary struct tag for uint: %q", tag) @@ -192,31 +192,31 @@ func (obj *Uint) unmarshalRRD(d *Decoder, tag string) error { data := d.data[d.pos:] padding := 0 - if d.pos%d.arch.IntAlign != 0 { - padding = d.arch.IntAlign - (d.pos % d.arch.IntAlign) + if d.pos%d.arch.LongAlign != 0 { + padding = d.arch.LongAlign - (d.pos % d.arch.LongAlign) } if len(data) < padding { - return d.binErrorf(padding+d.arch.IntWidth, "unexpected end-of-file in %d-byte padding-before-uint", padding) + return d.binErrorf(padding+d.arch.LongWidth, "unexpected end-of-file in %d-byte padding-before-uint", padding) } data = data[padding:] - if len(data) < d.arch.IntWidth { - return d.binErrorf(d.arch.IntWidth, "unexpected end-of-file in %d-byte uint", d.arch.IntWidth) + if len(data) < d.arch.LongWidth { + return d.binErrorf(d.arch.LongWidth, "unexpected end-of-file in %d-byte uint", d.arch.LongWidth) } - switch d.arch.IntWidth { + switch d.arch.LongWidth { case 4: - *obj = Uint(d.arch.ByteOrder.Uint32(data)) + *obj = ULong(d.arch.ByteOrder.Uint32(data)) case 8: - *obj = Uint(d.arch.ByteOrder.Uint64(data)) + *obj = ULong(d.arch.ByteOrder.Uint64(data)) } - d.pos += padding + d.arch.IntWidth + d.pos += padding + d.arch.LongWidth return nil } -func (obj *Int) unmarshalRRD(d *Decoder, tag string) error { - if d.arch.IntWidth != 4 && d.arch.IntWidth != 8 { - return archErrorf("rrdbinary does not support IntWidth=%d; only supports 4 or 8", d.arch.IntWidth) +func (obj *Long) unmarshalRRD(d *Decoder, tag string) error { + if d.arch.LongWidth != 4 && d.arch.LongWidth != 8 { + return archErrorf("rrdbinary does not support LongWidth=%d; only supports 4 or 8", d.arch.LongWidth) } if tag != "" { return typeErrorf("invalid rrdbinary struct tag for int: %q", tag) @@ -225,25 +225,25 @@ func (obj *Int) unmarshalRRD(d *Decoder, tag string) error { data := d.data[d.pos:] padding := 0 - if d.pos%d.arch.IntAlign != 0 { - padding = d.arch.IntAlign - (d.pos % d.arch.IntAlign) + if d.pos%d.arch.LongAlign != 0 { + padding = d.arch.LongAlign - (d.pos % d.arch.LongAlign) } if len(data) < padding { - return d.binErrorf(padding+d.arch.IntWidth, "unexpected end-of-file in %d-byte padding-before-int", padding) + return d.binErrorf(padding+d.arch.LongWidth, "unexpected end-of-file in %d-byte padding-before-int", padding) } data = data[padding:] - if len(data) < d.arch.IntWidth { - return d.binErrorf(d.arch.IntWidth, "unexpected end-of-file in %d-byte int", d.arch.IntWidth) + if len(data) < d.arch.LongWidth { + return d.binErrorf(d.arch.LongWidth, "unexpected end-of-file in %d-byte int", d.arch.LongWidth) } - switch d.arch.IntWidth { + switch d.arch.LongWidth { case 4: - *obj = Int(int32(d.arch.ByteOrder.Uint32(data))) + *obj = Long(int32(d.arch.ByteOrder.Uint32(data))) case 8: - *obj = Int(d.arch.ByteOrder.Uint64(data)) + *obj = Long(d.arch.ByteOrder.Uint64(data)) } - d.pos += padding + d.arch.IntWidth + d.pos += padding + d.arch.LongWidth return nil } diff --git a/rrdformat/rrdbinary/types.go b/rrdformat/rrdbinary/types.go index a6fa881..47df26e 100644 --- a/rrdformat/rrdbinary/types.go +++ b/rrdformat/rrdbinary/types.go @@ -9,32 +9,32 @@ import ( type Architecture struct { ByteOrder binary.ByteOrder // C `double` - FloatWidth int // always 8 -- we assume IEEE 754 doubles - FloatAlign int + DoubleWidth int // always 8 -- we assume IEEE 754 doubles + DoubleAlign int // C `long` or `unsigned long` - IntWidth int - IntAlign int + LongWidth int + LongAlign int // C `union { unsigned long; double; }` - UnivalWidth int // max(FloatWidth, IntWidth) - UnivalAlign int // max(FloatAlign, IntAlign) + UnivalWidth int // max(DoubleWidth, LongWidth) + UnivalAlign int // max(DoubleAlign, LongAlign) // C `time_t` TimeWidth int TimeAlign int } -type String string // \0-terminated -type Float float64 // 8 bytes -type Uint uint64 // 4 or 8 bytes -type Int int64 // 4 or 8 bytes -type Unival uint64 // 8 bytes -type Time int64 // 4 or 8 bytes, only has second-precision -type EOF struct{} // 0 bytes +type String string // \0-terminated +type Double float64 // 8 bytes +type ULong uint64 // 4 or 8 bytes +type Long int64 // 4 or 8 bytes +type Unival uint64 // 8 bytes +type Time int64 // 4 or 8 bytes, only has second-precision +type EOF struct{} // 0 bytes func (u Unival) AsUint64() uint64 { return uint64(u) } func (u Unival) AsFloat64() float64 { return math.Float64frombits(uint64(u)) } // MarshalJSON is for my own debugging. -func (f Float) MarshalJSON() ([]byte, error) { +func (f Double) MarshalJSON() ([]byte, error) { raw := float64(f) if math.IsNaN(raw) { return json.Marshal("NaN") @@ -45,9 +45,9 @@ func (f Float) MarshalJSON() ([]byte, error) { // Statically assert that each of the above types implements the // 'unmarshaler' interface. var _ unmarshaler = func() *String { return nil }() -var _ unmarshaler = func() *Float { return nil }() -var _ unmarshaler = func() *Uint { return nil }() -var _ unmarshaler = func() *Int { return nil }() +var _ unmarshaler = func() *Double { return nil }() +var _ unmarshaler = func() *ULong { return nil }() +var _ unmarshaler = func() *Long { return nil }() var _ unmarshaler = func() *Unival { return nil }() var _ unmarshaler = func() *Time { return nil }() var _ unmarshaler = func() *EOF { return nil }() diff --git a/rrdformat/sniff.go b/rrdformat/sniff.go index 104a8f7..de1e67b 100644 --- a/rrdformat/sniff.go +++ b/rrdformat/sniff.go @@ -38,44 +38,44 @@ func SniffArchitecture(data []byte) (rrdbinary.Architecture, error) { // enough that I'm OK saying "you're going to need to use `rrdtool dump`". This lets us assume that: // - a 'double' is 8 bytes wide // - the value will be exactly equal, and we don't need to worry about weird rounding. - arch.FloatWidth = 8 + arch.DoubleWidth = 8 magicFloat := float64(8.642135e130) floatAddrPacked := 9 floatAddr32 := ((floatAddrPacked + 3) / 4) * 4 floatAddr64 := ((floatAddrPacked + 7) / 8) * 8 var restOffset int switch { - case len(data) < floatAddr32+arch.FloatWidth: - return rrdbinary.Architecture{}, rrdbinary.NewBinError("unexpected end-of-file", data, floatAddrPacked, floatAddr64+arch.FloatWidth-floatAddrPacked) + case len(data) < floatAddr32+arch.DoubleWidth: + return rrdbinary.Architecture{}, rrdbinary.NewBinError("unexpected end-of-file", data, floatAddrPacked, floatAddr64+arch.DoubleWidth-floatAddrPacked) case math.Float64frombits(binary.LittleEndian.Uint64(data[floatAddr32:])) == magicFloat: arch.ByteOrder = binary.LittleEndian - arch.FloatAlign = 4 - restOffset = floatAddr32 + arch.FloatWidth + arch.DoubleAlign = 4 + restOffset = floatAddr32 + arch.DoubleWidth case math.Float64frombits(binary.BigEndian.Uint64(data[floatAddr32:])) == magicFloat: arch.ByteOrder = binary.BigEndian - arch.FloatAlign = 4 - restOffset = floatAddr32 + arch.FloatWidth - case len(data) < floatAddr64+arch.FloatWidth: - return rrdbinary.Architecture{}, rrdbinary.NewBinError("unexpected end-of-file", data, floatAddrPacked, floatAddr64+arch.FloatWidth-floatAddrPacked) + arch.DoubleAlign = 4 + restOffset = floatAddr32 + arch.DoubleWidth + case len(data) < floatAddr64+arch.DoubleWidth: + return rrdbinary.Architecture{}, rrdbinary.NewBinError("unexpected end-of-file", data, floatAddrPacked, floatAddr64+arch.DoubleWidth-floatAddrPacked) case math.Float64frombits(binary.LittleEndian.Uint64(data[floatAddr64:])) == magicFloat: arch.ByteOrder = binary.LittleEndian - arch.FloatAlign = 8 - restOffset = floatAddr64 + arch.FloatWidth + arch.DoubleAlign = 8 + restOffset = floatAddr64 + arch.DoubleWidth case math.Float64frombits(binary.BigEndian.Uint64(data[floatAddr64:])) == magicFloat: arch.ByteOrder = binary.BigEndian - arch.FloatAlign = 8 - restOffset = floatAddr64 + arch.FloatWidth + arch.DoubleAlign = 8 + restOffset = floatAddr64 + arch.DoubleWidth default: return rrdbinary.Architecture{}, rrdbinary.NewBinError("failed to sniff byte-order and float-alignment", - data, floatAddrPacked, floatAddr64+arch.FloatWidth-floatAddrPacked) + data, floatAddrPacked, floatAddr64+arch.DoubleWidth-floatAddrPacked) } // 5, 6. ds_cnt, rra_cnt - switch arch.FloatAlign { + switch arch.DoubleAlign { case 4: // Assume that if floats are only 32-bit aligned, then everything is 32-bit - arch.IntWidth = 4 - arch.IntAlign = 4 + arch.LongWidth = 4 + arch.LongAlign = 4 case 8: // If floats are 64-bit aligned, then this might be all-in on 64-bit, or it might 32-bit ints. @@ -106,18 +106,18 @@ func SniffArchitecture(data []byte) (rrdbinary.Architecture, error) { binary.LittleEndian: restOffset + 4, // 28 in the above diagram }[arch.ByteOrder] if arch.ByteOrder.Uint32(data[offset:]) == 0 { - arch.IntWidth = 8 - arch.IntAlign = 8 + arch.LongWidth = 8 + arch.LongAlign = 8 } else { - arch.IntWidth = 4 - arch.IntAlign = 4 + arch.LongWidth = 4 + arch.LongAlign = 4 } } - // The above just os happens that FloatXXX >= IntXXX, so we - // can just set the Unival stuff to the Float Stuff. - arch.UnivalWidth = arch.FloatWidth // max(FloatWidth, IntWidth) - arch.UnivalAlign = arch.FloatAlign // max(FloatAlign, IntAlign) + // The above just os happens that DoubleXXX >= LongXXX, so we + // can just set the Unival stuff to the Double Stuff. + arch.UnivalWidth = arch.DoubleWidth // max(DoubleWidth, LongWidth) + arch.UnivalAlign = arch.DoubleAlign // max(DoubleAlign, LongAlign) // FIXME: Figure out how to sniff the sizeof(time_t). // @@ -125,8 +125,8 @@ func SniffArchitecture(data []byte) (rrdbinary.Architecture, error) { // which his historically been true, but // - isn't true of proprietary 32-bit Unixen that are 2038-safe // - isn't true of the Linux kernel with the x32 ABI - arch.TimeWidth = arch.IntWidth - arch.TimeAlign = arch.IntAlign + arch.TimeWidth = arch.LongWidth + arch.TimeAlign = arch.LongAlign return arch, nil } |