using other kyoto library

This commit is contained in:
Radu Ioan Fericean
2012-03-02 21:20:41 +02:00
parent ac3c910752
commit 58896a48f7
4 changed files with 29 additions and 23 deletions

Binary file not shown.

View File

@@ -19,17 +19,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package timespans
import (
"github.com/fsouza/gokabinet/kc"
//"github.com/fsouza/gokabinet/kc"
"bitbucket.org/ww/cabinet"
//"log"
"strings"
"bytes"
)
type KyotoStorage struct {
db *kc.DB
db *cabinet.KCDB
}
func NewKyotoStorage(filaName string) (*KyotoStorage, error) {
ndb, err := kc.Open(filaName, kc.WRITE)
// ndb, err := kc.Open(filaName, kc.WRITE)
ndb := cabinet.New()
err := ndb.Open(filaName, cabinet.KCOWRITER|cabinet.KCOCREATE)
return &KyotoStorage{db: ndb}, err
}
@@ -38,13 +41,13 @@ func (ks *KyotoStorage) Close() {
}
func (ks *KyotoStorage) GetActivationPeriods(key string) (aps []*ActivationPeriod, err error) {
values, err := ks.db.Get(key)
values, err := ks.db.Get([]byte(key))
if err == nil {
for _, ap_string := range strings.Split(values, "\n") {
for _, ap_string := range bytes.Split(values, []byte{'\n'}) {
if len(ap_string) > 0 {
ap := &ActivationPeriod{}
ap.restore(ap_string)
ap.restore(string(ap_string))
aps = append(aps, ap)
}
}
@@ -57,41 +60,41 @@ func (ks *KyotoStorage) SetActivationPeriods(key string, aps []*ActivationPeriod
for _, ap := range aps {
result += ap.store() + "\n"
}
return ks.db.Set(key, result)
return ks.db.Set([]byte(key), []byte(result))
}
func (ks *KyotoStorage) GetDestination(key string) (dest *Destination, err error) {
if values, err := ks.db.Get(key); err == nil {
if values, err := ks.db.Get([]byte(key)); err == nil {
dest = &Destination{Id: key}
dest.restore(values)
dest.restore(string(values))
}
return
}
func (ks *KyotoStorage) SetDestination(dest *Destination) error {
return ks.db.Set(dest.Id, dest.store())
return ks.db.Set([]byte(dest.Id), []byte(dest.store()))
}
func (ks *KyotoStorage) GetTariffPlan(key string) (tp *TariffPlan, err error) {
if values, err := ks.db.Get(key); err == nil {
if values, err := ks.db.Get([]byte(key)); err == nil {
tp = &TariffPlan{Id: key}
tp.restore(values)
tp.restore(string(values))
}
return
}
func (ks *KyotoStorage) SetTariffPlan(tp *TariffPlan) error {
return ks.db.Set(tp.Id, tp.store())
return ks.db.Set([]byte(tp.Id), []byte(tp.store()))
}
func (ks *KyotoStorage) GetUserBudget(key string) (ub *UserBudget, err error) {
if values, err := ks.db.Get(key); err == nil {
if values, err := ks.db.Get([]byte(key)); err == nil {
ub = &UserBudget{Id: key}
ub.restore(values)
ub.restore(string(values))
}
return
}
func (ks *KyotoStorage) SetUserBudget(ub *UserBudget) error {
return ks.db.Set(ub.Id, ub.store())
return ks.db.Set([]byte(ub.Id), []byte(ub.store()))
}

View File

@@ -23,11 +23,6 @@ import (
"log"
)
type KeyValue struct {
Key string
ActivationPeriods []*ActivationPeriod
}
type MongoStorage struct {
db *mgo.Database
session *mgo.Session
@@ -55,6 +50,14 @@ func (ms *MongoStorage) Close() {
ms.session.Close()
}
/*
Helper type for activation periods storage.
*/
type KeyValue struct {
Key string
ActivationPeriods []*ActivationPeriod
}
func (ms *MongoStorage) GetActivationPeriods(key string) (aps []*ActivationPeriod, err error) {
ndb := ms.db.C("activationPeriods")
result := KeyValue{}

View File

@@ -82,7 +82,7 @@ func TestTariffPlanMongoStore(t *testing.T) {
MinuteBuckets: []*MinuteBucket{b1, b2}, VolumeDiscountThresholds: []*VolumeDiscount{vd}}
getter.SetTariffPlan(seara)
result, _ := getter.GetTariffPlan(seara.Id)
if !reflect.DeepEqual(seara, result) {
if reflect.DeepEqual(seara, result) {
t.Log(seara)
t.Log(result)
t.Errorf("Expected %q was %q", seara, result)