Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
best implementation of period and marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed May 15, 2015
1 parent daa64bb commit 31207ff
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
13 changes: 10 additions & 3 deletions aggregator.go
Expand Up @@ -29,7 +29,7 @@ func NewTimeAggregator(units ...unit) (*TimeAggregator, error) {
}

func (a *TimeAggregator) Add(date time.Time, value int64) {
p := pack(a.flags, date)
p := NewPeriod(a.flags, date)
if _, ok := a.Values[p]; !ok {
a.Values[p] = a.buildAggregator()
}
Expand All @@ -38,7 +38,7 @@ func (a *TimeAggregator) Add(date time.Time, value int64) {
}

func (a *TimeAggregator) Get(date time.Time) int64 {
p := pack(a.flags, date)
p := NewPeriod(a.flags, date)

if _, ok := a.Values[p]; !ok {
return -1
Expand All @@ -63,8 +63,9 @@ func (a *TimeAggregator) Marshal() []byte {
}

func (a *TimeAggregator) Unmarshal(v []byte) error {
buf := bytes.NewBuffer(v)
a.Values = make(map[Period]*aggregator, 0)

buf := bytes.NewBuffer(v)
for {
var p Period
if err := binary.Read(buf, binary.LittleEndian, &p); err != nil {
Expand All @@ -75,6 +76,12 @@ func (a *TimeAggregator) Unmarshal(v []byte) error {
return err
}

if a.flags == 0 {
a.flags = p.Flag()
us := p.Units()
a.kind = us[len(us)-1]
}

a.Values[p] = a.buildAggregator()
if err := a.Values[p].Unmarshal(buf); err != nil {
return err
Expand Down
40 changes: 21 additions & 19 deletions aggregator_test.go
Expand Up @@ -18,25 +18,6 @@ func (s *UtilsSuite) TestNewTimeAggregator(c *C) {
c.Assert(err, Equals, InvalidOrderError)
}

func (s *UtilsSuite) TestTimeAggregator_Add(c *C) {
d := time.Now()

a, _ := NewTimeAggregator(Year, Hour)
a.Add(d, 10)
a.Add(d, 10)

c.Assert(a.Values, HasLen, 1)
c.Assert(a.Get(d), Equals, int64(20))

m := a.Marshal()

b, _ := NewTimeAggregator(Year, Hour)
err := b.Unmarshal(m)
c.Assert(err, IsNil)
c.Assert(b.Values, HasLen, 1)
c.Assert(b.Get(d), Equals, int64(20))
}

func (s *UtilsSuite) TestTimeAggregator_Add_YearHour(c *C) {
a, _ := NewTimeAggregator(Year, Hour)
a.Add(date2014November, 15)
Expand Down Expand Up @@ -84,6 +65,27 @@ func (s *UtilsSuite) TestTimeAggregator_Add_Only(c *C) {
c.Assert(a.Get(h21), Equals, int64(40))
}

func (s *UtilsSuite) TestTimeAggregator_MarshalAndUnmarshal(c *C) {
d := time.Now()

a, _ := NewTimeAggregator(Year, Hour)
a.Add(d, 10)
a.Add(d, 10)

c.Assert(a.Values, HasLen, 1)
c.Assert(a.Get(d), Equals, int64(20))

m := a.Marshal()

b := &TimeAggregator{}
err := b.Unmarshal(m)

c.Assert(err, IsNil)
c.Assert(a.flags, Equals, b.flags)
c.Assert(b.Values, HasLen, 1)
c.Assert(b.Get(d), Equals, int64(20))
}

var date2013December = time.Date(2013, time.December, 12, 23, 59, 59, 0, time.UTC)
var date2013November = time.Date(2013, time.November, 12, 23, 59, 59, 0, time.UTC)
var date2014February = time.Date(2014, time.February, 12, 23, 59, 59, 0, time.UTC)
Expand Down
20 changes: 14 additions & 6 deletions periods.go
Expand Up @@ -4,8 +4,6 @@ import (
"time"
)

type Period uint64

type unit int

const (
Expand Down Expand Up @@ -106,7 +104,9 @@ var defs = map[unit]periodDefinition{
},
}

func pack(flag unit, date time.Time) Period {
type Period uint64

func NewPeriod(flag unit, date time.Time) Period {
us := getUnitsFromFlag(flag)

t := binaryVersion
Expand All @@ -121,10 +121,10 @@ func pack(flag unit, date time.Time) Period {
return Period(t)
}

func unpack(total Period) map[string]uint64 {
t := uint64(total)
func (p Period) ToMap() map[string]uint64 {
t := uint64(p)

us := getUnitsFromFlag(unit(t % 1e3))
us := p.Units()
t = t / 1e3

result := make(map[string]uint64, 0)
Expand All @@ -141,6 +141,14 @@ func unpack(total Period) map[string]uint64 {
return result
}

func (p Period) Flag() unit {
return unit(uint64(p) % 1e3)
}

func (p Period) Units() []unit {
return getUnitsFromFlag(p.Flag())
}

func getUnitsFromFlag(flag unit) []unit {
us := make([]unit, 0)
for _, u := range units {
Expand Down
10 changes: 5 additions & 5 deletions periods_test.go
Expand Up @@ -4,12 +4,12 @@ import (
. "gopkg.in/check.v1"
)

func (s *UtilsSuite) Test_pack(c *C) {
flags := Year | Month | Hour
c.Assert(pack(flags, date2015November), Equals, Period(1201511067))
func (s *UtilsSuite) Test_NewPeriod(c *C) {
p := NewPeriod(Year|Month|Hour, date2015November)
c.Assert(p, Equals, Period(1201511067))
}

func (s *UtilsSuite) Test_unpack(c *C) {
p := unpack(Period(1201511067))
c.Assert(p, DeepEquals, map[string]uint64{"year": 2015, "month": 11})
p := Period(1201511067)
c.Assert(p.ToMap(), DeepEquals, map[string]uint64{"year": 2015, "month": 11})
}

0 comments on commit 31207ff

Please sign in to comment.