From c2841e966d1ec4cecba54a3569e866d26d6f11b9 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sat, 18 Feb 2012 11:06:13 +0200 Subject: [PATCH] refactored back --- timespans/activationperiod.go | 56 ++++++++++++++++ ...orage_test.go => activationperiod_test.go} | 10 +-- timespans/kyoto_storage.go | 65 ++----------------- timespans/redis_storage.go | 63 +----------------- 4 files changed, 68 insertions(+), 126 deletions(-) rename timespans/{kyoto_storage_test.go => activationperiod_test.go} (88%) diff --git a/timespans/activationperiod.go b/timespans/activationperiod.go index 1d51b1801..89b972b6b 100644 --- a/timespans/activationperiod.go +++ b/timespans/activationperiod.go @@ -1,6 +1,8 @@ package timespans import ( + "strings" + "strconv" "time" //"log" ) @@ -21,3 +23,57 @@ func (ap *ActivationPeriod) AddInterval(is ...*Interval) { ap.Intervals = append(ap.Intervals, i) } } + +/* +Serializes the activation periods for the storage. Used for key-value storages. +*/ +func (ap *ActivationPeriod) store() (result string) { + result += strconv.FormatInt(ap.ActivationTime.UnixNano(), 10) + ";" + var is string + for _, i := range ap.Intervals { + is = strconv.Itoa(int(i.Month)) + "|" + is += strconv.Itoa(i.MonthDay) + "|" + for _, wd := range i.WeekDays { + is += strconv.Itoa(int(wd)) + "," + } + is = strings.TrimRight(is, ",") + "|" + is += i.StartTime + "|" + is += i.EndTime + "|" + is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|" + is += strconv.FormatFloat(i.ConnectFee, 'f', -1, 64) + "|" + is += strconv.FormatFloat(i.Price, 'f', -1, 64) + "|" + is += strconv.FormatFloat(i.BillingUnit, 'f', -1, 64) + result += is + ";" + } + return +} + +/* +De-serializes the activation periods for the storage. Used for key-value storages. +*/ +func (ap *ActivationPeriod) restore(input string) { + elements := strings.Split(input, ";") + unixNano, _ := strconv.ParseInt(elements[0], 10, 64) + ap.ActivationTime = time.Unix(0, unixNano).In(time.UTC) + for _, is := range elements[1 : len(elements)-1] { + i := &Interval{} + ise := strings.Split(is, "|") + month, _ := strconv.Atoi(ise[0]) + i.Month = time.Month(month) + i.MonthDay, _ = strconv.Atoi(ise[1]) + for _, d := range strings.Split(ise[2], ",") { + if d != "" { + wd, _ := strconv.Atoi(d) + i.WeekDays = append(i.WeekDays, time.Weekday(wd)) + } + } + i.StartTime = ise[3] + i.EndTime = ise[4] + i.Ponder, _ = strconv.ParseFloat(ise[5], 64) + i.ConnectFee, _ = strconv.ParseFloat(ise[6], 64) + i.Price, _ = strconv.ParseFloat(ise[7], 64) + i.BillingUnit, _ = strconv.ParseFloat(ise[8], 64) + + ap.Intervals = append(ap.Intervals, i) + } +} diff --git a/timespans/kyoto_storage_test.go b/timespans/activationperiod_test.go similarity index 88% rename from timespans/kyoto_storage_test.go rename to timespans/activationperiod_test.go index e279ce530..96fe44384 100644 --- a/timespans/kyoto_storage_test.go +++ b/timespans/activationperiod_test.go @@ -15,13 +15,13 @@ func TestApStoreRestore(t *testing.T) { EndTime: "15:00:00"} ap := &ActivationPeriod{ActivationTime: d} ap.AddInterval(i) - storage, _ := NewKyotoStorage("test.kch") - result := storage.store(ap) + result := ap.store() expected := "1328106601000000000;2|1|3,4|14:30:00|15:00:00|0|0|0|0;" if result != expected { t.Errorf("Expected %q was %q", expected, result) } - ap1 := storage.restore(result) + ap1 := ActivationPeriod{} + ap1.restore(result) if ap1.ActivationTime != ap.ActivationTime { t.Errorf("Expected %v was %v", ap.ActivationTime, ap1.ActivationTime) } @@ -58,8 +58,8 @@ func TestApStoreRestore(t *testing.T) { } func BenchmarkActivationPeriodRestore(b *testing.B) { - storage, _ := NewKyotoStorage("test.kch") + ap := ActivationPeriod{} for i := 0; i < b.N; i++ { - storage.restore("1328106601;2|1|3,4|14:30:00|15:00:00|0|0|0|0;") + ap.restore("1328106601;2|1|3,4|14:30:00|15:00:00|0|0|0|0;") } } diff --git a/timespans/kyoto_storage.go b/timespans/kyoto_storage.go index 18fcf2133..a8990e4db 100644 --- a/timespans/kyoto_storage.go +++ b/timespans/kyoto_storage.go @@ -1,10 +1,8 @@ package timespans import ( - "github.com/fsouza/gokabinet/kc" - "time" - "strconv" "strings" + "github.com/fsouza/gokabinet/kc" ) type KyotoStorage struct { @@ -26,7 +24,8 @@ func (ks *KyotoStorage) GetActivationPeriods(key string) (aps []*ActivationPerio if err == nil { for _, ap_string := range strings.Split(values, "\n") { if len(ap_string) > 0 { - ap := ks.restore(ap_string) + ap := &ActivationPeriod{} + ap.restore(ap_string) aps = append(aps, ap) } } @@ -37,63 +36,7 @@ func (ks *KyotoStorage) GetActivationPeriods(key string) (aps []*ActivationPerio func (ks *KyotoStorage) SetActivationPeriods(key string, aps []*ActivationPeriod){ result := "" for _, ap := range aps { - result += ks.store(ap) + "\n" + result += ap.store() + "\n" } ks.db.Set(key, result) } - -/* -Serializes the activation periods for the storage. -*/ -func (ks *KyotoStorage) store(ap *ActivationPeriod) (result string) { - result += strconv.FormatInt(ap.ActivationTime.UnixNano(), 10) + ";" - var is string - for _, i := range ap.Intervals { - is = strconv.Itoa(int(i.Month)) + "|" - is += strconv.Itoa(i.MonthDay) + "|" - for _, wd := range i.WeekDays { - is += strconv.Itoa(int(wd)) + "," - } - is = strings.TrimRight(is, ",") + "|" - is += i.StartTime + "|" - is += i.EndTime + "|" - is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.ConnectFee, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.Price, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.BillingUnit, 'f', -1, 64) - result += is + ";" - } - return -} - -/* -De-serializes the activation periods for the storage. -*/ -func (ks *KyotoStorage) restore(input string) (ap *ActivationPeriod) { - elements := strings.Split(input, ";") - unixNano, _ := strconv.ParseInt(elements[0], 10, 64) - ap = &ActivationPeriod{} - ap.ActivationTime = time.Unix(0, unixNano).In(time.UTC) - for _, is := range elements[1 : len(elements)-1] { - i := &Interval{} - ise := strings.Split(is, "|") - month, _ := strconv.Atoi(ise[0]) - i.Month = time.Month(month) - i.MonthDay, _ = strconv.Atoi(ise[1]) - for _, d := range strings.Split(ise[2], ",") { - if d != "" { - wd, _ := strconv.Atoi(d) - i.WeekDays = append(i.WeekDays, time.Weekday(wd)) - } - } - i.StartTime = ise[3] - i.EndTime = ise[4] - i.Ponder, _ = strconv.ParseFloat(ise[5], 64) - i.ConnectFee, _ = strconv.ParseFloat(ise[6], 64) - i.Price, _ = strconv.ParseFloat(ise[7], 64) - i.BillingUnit, _ = strconv.ParseFloat(ise[8], 64) - - ap.Intervals = append(ap.Intervals, i) - } - return -} diff --git a/timespans/redis_storage.go b/timespans/redis_storage.go index 1ede4befa..1350b36c8 100644 --- a/timespans/redis_storage.go +++ b/timespans/redis_storage.go @@ -2,8 +2,6 @@ package timespans import ( "strings" - "strconv" - "time" "github.com/simonz05/godis" ) @@ -26,7 +24,8 @@ func (rs *RedisStorage) GetActivationPeriods(key string) (aps []*ActivationPerio if err == nil { for _, ap_string := range strings.Split(values, "\n") { if len(ap_string) > 0 { - ap := rs.restore(ap_string) + ap := &ActivationPeriod{} + ap.restore(ap_string) aps = append(aps, ap) } } @@ -37,63 +36,7 @@ func (rs *RedisStorage) GetActivationPeriods(key string) (aps []*ActivationPerio func (rs *RedisStorage) SetActivationPeriods(key string, aps []*ActivationPeriod){ result := "" for _, ap := range aps { - result += rs.store(ap) + "\n" + result += ap.store() + "\n" } rs.db.Set(key, result) } - -/* -Serializes the activation periods for the storage. -*/ -func (rs *RedisStorage) store(ap *ActivationPeriod) (result string) { - result += strconv.FormatInt(ap.ActivationTime.UnixNano(), 10) + ";" - var is string - for _, i := range ap.Intervals { - is = strconv.Itoa(int(i.Month)) + "|" - is += strconv.Itoa(i.MonthDay) + "|" - for _, wd := range i.WeekDays { - is += strconv.Itoa(int(wd)) + "," - } - is = strings.TrimRight(is, ",") + "|" - is += i.StartTime + "|" - is += i.EndTime + "|" - is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.ConnectFee, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.Price, 'f', -1, 64) + "|" - is += strconv.FormatFloat(i.BillingUnit, 'f', -1, 64) - result += is + ";" - } - return -} - -/* -De-serializes the activation periods for the storage. -*/ -func (rs *RedisStorage) restore(input string) (ap *ActivationPeriod) { - elements := strings.Split(input, ";") - unixNano, _ := strconv.ParseInt(elements[0], 10, 64) - ap = &ActivationPeriod{} - ap.ActivationTime = time.Unix(0, unixNano).In(time.UTC) - for _, is := range elements[1 : len(elements)-1] { - i := &Interval{} - ise := strings.Split(is, "|") - month, _ := strconv.Atoi(ise[0]) - i.Month = time.Month(month) - i.MonthDay, _ = strconv.Atoi(ise[1]) - for _, d := range strings.Split(ise[2], ",") { - if d != "" { - wd, _ := strconv.Atoi(d) - i.WeekDays = append(i.WeekDays, time.Weekday(wd)) - } - } - i.StartTime = ise[3] - i.EndTime = ise[4] - i.Ponder, _ = strconv.ParseFloat(ise[5], 64) - i.ConnectFee, _ = strconv.ParseFloat(ise[6], 64) - i.Price, _ = strconv.ParseFloat(ise[7], 64) - i.BillingUnit, _ = strconv.ParseFloat(ise[8], 64) - - ap.Intervals = append(ap.Intervals, i) - } - return -}