refactored back

This commit is contained in:
Radu Ioan Fericean
2012-02-18 11:06:13 +02:00
parent 8bef3cc11b
commit c2841e966d
4 changed files with 68 additions and 126 deletions

View File

@@ -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)
}
}

View File

@@ -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;")
}
}

View File

@@ -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
}

View File

@@ -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
}