summaryrefslogtreecommitdiff
path: root/rrdformat/unmarshal_binary.go
blob: b9bafde76b193051583bcebe9af76fbf4096504b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package rrdformat

import (
	"encoding"

	"git.lukeshu.com/go/librrd/rrdformat/rrdbinary"
)

type RRD struct {
	Architecture rrdbinary.Architecture
	Data         RRDv0005
}

func (rrd *RRD) UnmarshalBinary(data []byte) error {
	arch, err := SniffArchitecture(data)
	if err != nil {
		return err
	}
	var header Header
	if err := rrdbinary.Unmarshal(arch, data, &header); err != nil {
		return err
	}

	var parsed RRDv0005
	parsed.DSDefs = make([]DSDef, header.DSCnt)
	parsed.DSDefs = make([]DSDef, header.DSCnt)
	parsed.RRADefs = make([]RRADef, header.RRACnt)
	//LastUpdated
	parsed.PDPPreps = make([]PDPPrep, header.DSCnt)
	parsed.CDPPreps = make([]CDPPrep, header.DSCnt*header.RRACnt)
	parsed.RRAPtrs = make([]RRAPtr, header.RRACnt)
	//Values

	switch header.Version {
	case "0001", "0002":
		_parsed := &RRDv0001{} //(*RRDv0001)(&parsed)
		if err := rrdbinary.Unmarshal(arch, data, _parsed); err != nil {
			return err
		}
	case "0003", "0004", "0005":
		if err := rrdbinary.Unmarshal(arch, data, &parsed); err != nil {
			return err
		}
	default:
		// version number already validated by
		// SniffArchitecture
		panic("should not happen")
	}

	val_cnt := 0
	for i := range parsed.RRADefs {
		val_cnt += int(parsed.RRADefs[i].RowCnt * header.DSCnt)
	}
	parsed.Values = make([]RRDValue, val_cnt)
	if err := rrdbinary.Unmarshal(arch, data, &parsed.Values); err != nil {
		return err
	}

	*rrd = RRD{
		Architecture: arch,
		Data:         parsed,
	}
	return nil
}

//var _ encoding.BinaryMarshaler = &RRD{}
var _ encoding.BinaryUnmarshaler = &RRD{}