From a49bb1ce2adc434da1d40f3b9720deaffa7c9493 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 1 Mar 2012 11:09:53 +0200 Subject: [PATCH] gob finally seems to work --- timespans/kyoto_storage.go | 20 ++++++++++ timespans/minute_buckets_test.go | 4 +- timespans/redis_storage.go | 61 +++++++++++++++++++++---------- timespans/test.kch | Bin 6301376 -> 6299632 bytes 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/timespans/kyoto_storage.go b/timespans/kyoto_storage.go index 05a433bf8..42899ecb7 100644 --- a/timespans/kyoto_storage.go +++ b/timespans/kyoto_storage.go @@ -53,9 +53,29 @@ func NewKyotoStorage(filaName string) (*KyotoStorage, error) { ks.encTP = gob.NewEncoder(&ks.buf) ks.decUB = gob.NewDecoder(&ks.buf) ks.encUB = gob.NewEncoder(&ks.buf) + ks.trainGobEncodersAndDecoders() return ks, err } +func (ks *KyotoStorage) trainGobEncodersAndDecoders() { + aps := []*ActivationPeriod{&ActivationPeriod{}} + ks.encAP.Encode(aps) + ks.decAP.Decode(&aps) + ks.buf.Reset() + dest := &Destination{} + ks.encDest.Encode(dest) + ks.decDest.Decode(&dest) + ks.buf.Reset() + tp := &TariffPlan{} + ks.encTP.Encode(tp) + ks.decTP.Decode(&tp) + ks.buf.Reset() + ub := &UserBudget{} + ks.encUB.Encode(ub) + ks.decUB.Decode(&ub) + ks.buf.Reset() +} + func (ks *KyotoStorage) Close() { ks.db.Close() } diff --git a/timespans/minute_buckets_test.go b/timespans/minute_buckets_test.go index ee4312df8..78911bc2b 100644 --- a/timespans/minute_buckets_test.go +++ b/timespans/minute_buckets_test.go @@ -41,7 +41,7 @@ func TestMultipleGetDestination(t *testing.T) { if d.Id != "nationale" || len(d.Prefixes) != 4 { t.Error("Got wrong destination: ", d) } - /*mb = &MinuteBucket{DestinationId: "retea"} + mb = &MinuteBucket{DestinationId: "retea"} d = mb.getDestination(getter) d = mb.getDestination(getter) d = mb.getDestination(getter) @@ -54,5 +54,5 @@ func TestMultipleGetDestination(t *testing.T) { d = mb.getDestination(getter) if d.Id != "mobil" || len(d.Prefixes) != 2 { t.Error("Got wrong destination: ", d) - }*/ + } } diff --git a/timespans/redis_storage.go b/timespans/redis_storage.go index 511be6b9f..30c6d2214 100644 --- a/timespans/redis_storage.go +++ b/timespans/redis_storage.go @@ -21,7 +21,7 @@ import ( "bytes" "encoding/gob" "github.com/simonz05/godis" - "log" + // "log" "sync" ) @@ -30,9 +30,13 @@ type RedisStorage struct { db *godis.Client buf bytes.Buffer decAP *gob.Decoder + encAP *gob.Encoder decDest *gob.Decoder + encDest *gob.Encoder decTP *gob.Decoder + encTP *gob.Encoder decUB *gob.Decoder + encUB *gob.Encoder mux sync.Mutex } @@ -41,12 +45,36 @@ func NewRedisStorage(address string, db int) (*RedisStorage, error) { rs := &RedisStorage{db: ndb, dbNb: db} rs.decAP = gob.NewDecoder(&rs.buf) + rs.encAP = gob.NewEncoder(&rs.buf) rs.decDest = gob.NewDecoder(&rs.buf) + rs.encDest = gob.NewEncoder(&rs.buf) rs.decTP = gob.NewDecoder(&rs.buf) + rs.encTP = gob.NewEncoder(&rs.buf) rs.decUB = gob.NewDecoder(&rs.buf) + rs.encUB = gob.NewEncoder(&rs.buf) + rs.trainGobEncodersAndDecoders() return rs, nil } +func (rs *RedisStorage) trainGobEncodersAndDecoders() { + aps := []*ActivationPeriod{&ActivationPeriod{}} + rs.encAP.Encode(aps) + rs.decAP.Decode(&aps) + rs.buf.Reset() + dest := &Destination{} + rs.encDest.Encode(dest) + rs.decDest.Decode(&dest) + rs.buf.Reset() + tp := &TariffPlan{} + rs.encTP.Encode(tp) + rs.decTP.Decode(&tp) + rs.buf.Reset() + ub := &UserBudget{} + rs.encUB.Encode(ub) + rs.decUB.Decode(&ub) + rs.buf.Reset() +} + func (rs *RedisStorage) Close() { rs.db.Quit() } @@ -56,10 +84,9 @@ func (rs *RedisStorage) SetActivationPeriods(key string, aps []*ActivationPeriod rs.mux.Lock() defer rs.mux.Unlock() - var writeBuf bytes.Buffer - encoder := gob.NewEncoder(&writeBuf) - encoder.Encode(aps) - return rs.db.Set(key, writeBuf.Bytes()) + rs.buf.Reset() + rs.encAP.Encode(aps) + return rs.db.Set(key, rs.buf.Bytes()) } func (rs *RedisStorage) GetActivationPeriods(key string) (aps []*ActivationPeriod, err error) { @@ -80,10 +107,9 @@ func (rs *RedisStorage) SetDestination(dest *Destination) error { rs.mux.Lock() defer rs.mux.Unlock() - var writeBuf bytes.Buffer - encoder := gob.NewEncoder(&writeBuf) - encoder.Encode(dest) - return rs.db.Set(dest.Id, writeBuf.Bytes()) + rs.buf.Reset() + rs.encDest.Encode(dest) + return rs.db.Set(dest.Id, rs.buf.Bytes()) } func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) { @@ -94,8 +120,7 @@ func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error elem, err := rs.db.Get(key) rs.buf.Reset() rs.buf.Write(elem.Bytes()) - err = rs.decDest.Decode(&dest) - log.Print(err) + rs.decDest.Decode(&dest) return } @@ -104,10 +129,9 @@ func (rs *RedisStorage) SetTariffPlan(tp *TariffPlan) error { rs.mux.Lock() defer rs.mux.Unlock() - var writeBuf bytes.Buffer - encoder := gob.NewEncoder(&writeBuf) - encoder.Encode(tp) - return rs.db.Set(tp.Id, writeBuf.Bytes()) + rs.buf.Reset() + rs.encTP.Encode(tp) + return rs.db.Set(tp.Id, rs.buf.Bytes()) } func (rs *RedisStorage) GetTariffPlan(key string) (tp *TariffPlan, err error) { @@ -127,10 +151,9 @@ func (rs *RedisStorage) SetUserBudget(ub *UserBudget) error { rs.mux.Lock() defer rs.mux.Unlock() - var writeBuf bytes.Buffer - encoder := gob.NewEncoder(&writeBuf) - encoder.Encode(ub) - return rs.db.Set(ub.Id, writeBuf.Bytes()) + rs.buf.Reset() + rs.encUB.Encode(ub) + return rs.db.Set(ub.Id, rs.buf.Bytes()) } func (rs *RedisStorage) GetUserBudget(key string) (ub *UserBudget, err error) { diff --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