different db for redis

This commit is contained in:
Radu Ioan Fericean
2012-02-20 18:19:13 +02:00
parent b6265dfdc4
commit 3463dce9c8
4 changed files with 19 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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