From 3463dce9c81e29ffd7be1308a7580d4682f3a757 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 20 Feb 2012 18:19:13 +0200 Subject: [PATCH] different db for redis --- timespans/destinations_test.go | 14 +++++++------- timespans/kyoto_storage.go | 2 +- timespans/mongo_storage.go | 6 +++--- timespans/redis_storage.go | 11 ++++++++--- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/timespans/destinations_test.go b/timespans/destinations_test.go index 6893cc0dd..c90bcab2c 100644 --- a/timespans/destinations_test.go +++ b/timespans/destinations_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestDestinationStoreRestore(t *testing.T){ +func TestDestinationStoreRestore(t *testing.T) { nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} s := nationale.store() d1 := &Destination{Id: "nationale"} @@ -14,7 +14,7 @@ func TestDestinationStoreRestore(t *testing.T){ } } -func TestKyotoStore(t *testing.T){ +func TestKyotoStore(t *testing.T) { getter, _ := NewKyotoStorage("test.kch") defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} @@ -25,7 +25,7 @@ func TestKyotoStore(t *testing.T){ } } -func TestRedisStore(t *testing.T){ +func TestRedisStore(t *testing.T) { getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} @@ -36,7 +36,7 @@ func TestRedisStore(t *testing.T){ } } -func TestMongoStore(t *testing.T){ +func TestMongoStore(t *testing.T) { getter, _ := NewMongoStorage("127.0.0.1", "test") defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} @@ -49,7 +49,7 @@ func TestMongoStore(t *testing.T){ /********************************* Benchmarks **********************************/ -func BenchmarkDestinationKyotoStoreRestore(b *testing.B){ +func BenchmarkDestinationKyotoStoreRestore(b *testing.B) { getter, _ := NewKyotoStorage("test.kch") defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} @@ -59,7 +59,7 @@ func BenchmarkDestinationKyotoStoreRestore(b *testing.B){ } } -func BenchmarkDestinationRedisStoreRestore(b *testing.B){ +func BenchmarkDestinationRedisStoreRestore(b *testing.B) { getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10) defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} @@ -69,7 +69,7 @@ func BenchmarkDestinationRedisStoreRestore(b *testing.B){ } } -func BenchmarkDestinationMongoStoreRestore(b *testing.B){ +func BenchmarkDestinationMongoStoreRestore(b *testing.B) { getter, _ := NewMongoStorage("127.0.0.1", "test") defer getter.Close() nationale = &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} diff --git a/timespans/kyoto_storage.go b/timespans/kyoto_storage.go index e4f87e3c1..c15e272be 100644 --- a/timespans/kyoto_storage.go +++ b/timespans/kyoto_storage.go @@ -43,7 +43,7 @@ func (ks *KyotoStorage) SetActivationPeriods(key string, aps []*ActivationPeriod func (ks *KyotoStorage) GetDestination(key string) (dest *Destination, err error) { values, err := ks.db.Get(key) - dest = &Destination{Id:key} + dest = &Destination{Id: key} dest.restore(values) return } diff --git a/timespans/mongo_storage.go b/timespans/mongo_storage.go index 212a24837..4206c54ac 100644 --- a/timespans/mongo_storage.go +++ b/timespans/mongo_storage.go @@ -11,7 +11,7 @@ type KeyValue struct { } type MongoStorage struct { - db *mgo.Database + db *mgo.Database session *mgo.Session } @@ -22,9 +22,9 @@ func NewMongoStorage(address, db string) (*MongoStorage, error) { } session.SetMode(mgo.Monotonic, true) - index := mgo.Index{Key: []string{"key"},Unique: true,DropDups: true,Background: true} + index := mgo.Index{Key: []string{"key"}, Unique: true, DropDups: true, Background: true} err = session.DB(db).C("ap").EnsureIndex(index) - index = mgo.Index{Key: []string{"id"},Unique: true,DropDups: true,Background: true} + index = mgo.Index{Key: []string{"id"}, Unique: true, DropDups: true, Background: true} err = session.DB(db).C("dest").EnsureIndex(index) return &MongoStorage{db: session.DB(db), session: session}, nil diff --git a/timespans/redis_storage.go b/timespans/redis_storage.go index 6ad10261e..d1d7ce5e2 100644 --- a/timespans/redis_storage.go +++ b/timespans/redis_storage.go @@ -6,12 +6,13 @@ import ( ) type RedisStorage struct { - db *godis.Client + dbNb int + db *godis.Client } func NewRedisStorage(address string, db int) (*RedisStorage, error) { ndb := godis.New(address, db, "") - return &RedisStorage{db: ndb}, nil + return &RedisStorage{db: ndb, dbNb: db}, nil } func (rs *RedisStorage) Close() { @@ -19,6 +20,7 @@ func (rs *RedisStorage) Close() { } func (rs *RedisStorage) GetActivationPeriods(key string) (aps []*ActivationPeriod, err error) { + rs.db.Select(rs.dbNb) elem, err := rs.db.Get(key) values := elem.String() if err == nil { @@ -34,6 +36,7 @@ func (rs *RedisStorage) GetActivationPeriods(key string) (aps []*ActivationPerio } func (rs *RedisStorage) SetActivationPeriods(key string, aps []*ActivationPeriod) { + rs.db.Select(rs.dbNb) result := "" for _, ap := range aps { result += ap.store() + "\n" @@ -42,12 +45,14 @@ func (rs *RedisStorage) SetActivationPeriods(key string, aps []*ActivationPeriod } func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) { + rs.db.Select(rs.dbNb + 1) values, err := rs.db.Get(key) - dest = &Destination{Id:key} + dest = &Destination{Id: key} dest.restore(values.String()) return } func (rs *RedisStorage) SetDestination(dest *Destination) { + rs.db.Select(rs.dbNb + 1) rs.db.Set(dest.Id, dest.store()) }