destination storage refactoring started

This commit is contained in:
Radu Ioan Fericean
2016-07-18 21:51:42 +03:00
parent 8cc273b2f1
commit db50767a99
5 changed files with 38 additions and 54 deletions

View File

@@ -115,15 +115,15 @@ func CacheGet(key string) (v interface{}, err error) {
}
// Appends to an existing slice in the cache key
func CachePush(key string, value string) {
func CachePush(key string, values ...string) {
if !transactionLock {
mux.Lock()
defer mux.Unlock()
}
if !transactionON {
cache.Append(key, value)
cache.Append(key, values...)
} else {
transactionBuffer = append(transactionBuffer, &transactionItem{key: key, value: value, kind: KIND_ADP})
transactionBuffer = append(transactionBuffer, &transactionItem{key: key, value: values, kind: KIND_ADP})
}
}

View File

@@ -16,7 +16,7 @@ import (
type cacheStore interface {
Put(string, interface{})
Get(string) (interface{}, error)
Append(string, string)
Append(string, ...string)
Pop(string, string)
Delete(string)
DeletePrefix(string)
@@ -56,14 +56,16 @@ func (cs cacheDoubleStore) Get(key string) (interface{}, error) {
return nil, utils.ErrNotFound
}
func (cs cacheDoubleStore) Append(key string, value string) {
func (cs cacheDoubleStore) Append(key string, values ...string) {
var elements map[string]struct{} // using map for faster check if element is present
if v, err := cs.Get(key); err == nil {
elements = v.(map[string]struct{})
} else {
elements = make(map[string]struct{})
}
elements[value] = struct{}{}
for _, value := range values {
elements[value] = struct{}{}
}
cache.Put(key, elements)
}
@@ -205,14 +207,16 @@ func (cs cacheLRUTTL) Get(key string) (interface{}, error) {
return nil, utils.ErrNotFound
}
func (cs cacheLRUTTL) Append(key string, value string) {
func (cs cacheLRUTTL) Append(key string, values ...string) {
var elements map[string]struct{} // using map for faster check if element is present
if v, err := cs.Get(key); err == nil {
elements = v.(map[string]struct{})
} else {
elements = make(map[string]struct{})
}
elements[value] = struct{}{}
for _, value := range values {
elements[value] = struct{}{}
}
cache.Put(key, elements)
}
@@ -288,14 +292,16 @@ func (cs cacheSimpleStore) Put(key string, value interface{}) {
cs.cache[key] = value
}
func (cs cacheSimpleStore) Append(key string, value string) {
func (cs cacheSimpleStore) Append(key string, values ...string) {
var elements map[string]struct{}
if v, err := cs.Get(key); err == nil {
elements = v.(map[string]struct{})
} else {
elements = make(map[string]struct{})
}
elements[value] = struct{}{}
for _, value := range values {
elements[value] = struct{}{}
}
cache.Put(key, elements)
}

View File

@@ -47,8 +47,8 @@ type RatingStorage interface {
GetRatingProfile(string, bool) (*RatingProfile, error)
SetRatingProfile(*RatingProfile) error
RemoveRatingProfile(string) error
GetDestination(string) (*Destination, error)
SetDestination(*Destination) error
GetDestinationIDs(string) ([]string, error)
SetDestinationIDs(*Destination) error
RemoveDestination(string) error
GetLCR(string, bool) (*LCR, error)
SetLCR(*LCR) error

View File

@@ -979,20 +979,15 @@ func (ms *MongoStorage) GetDestination(key string) (result *Destination, err err
}
func (ms *MongoStorage) SetDestination(dest *Destination) (err error) {
result, err := ms.ms.Marshal(dest)
if err != nil {
return err
for _, p := range dest.Prefixes {
session, col := ms.conn(colDst)
if _, err = col.Upsert(bson.M{"key": p}, &struct {
Key string
Value []string
}, bson.M{"key": p}, bson.M{"$addToSet": bson.M{Value: dest.Id}}); err != nil {
break
}
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(result)
w.Close()
session, col := ms.conn(colDst)
defer session.Close()
_, err = col.Upsert(bson.M{"key": dest.Id}, &struct {
Key string
Value []byte
}{Key: dest.Id, Value: b.Bytes()})
if err == nil && historyScribe != nil {
var response int
historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(false), &response)

View File

@@ -582,42 +582,25 @@ func (rs *RedisStorage) SetLCR(lcr *LCR) (err error) {
return
}
func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) {
key = utils.DESTINATION_PREFIX + key
var values []byte
if values, err = rs.db.Cmd("GET", key).Bytes(); len(values) > 0 && err == nil {
b := bytes.NewBuffer(values)
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
out, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
r.Close()
dest = new(Destination)
err = rs.ms.Unmarshal(out, dest)
// create optimized structure
for _, p := range dest.Prefixes {
CachePush(utils.DESTINATION_PREFIX+p, dest.Id)
}
func (rs *RedisStorage) GetDestinationIDs(prefix string) (ids []string, err error) {
prefix = utils.DESTINATION_PREFIX + prefix
var values []string
if values, err = rs.db.Cmd("SMEMBERS", prefix).List(); len(values) > 0 && err == nil {
CachePush(utils.DESTINATION_PREFIX+prefix, values...)
} else {
return nil, utils.ErrNotFound
}
return
}
func (rs *RedisStorage) SetDestination(dest *Destination) (err error) {
result, err := rs.ms.Marshal(dest)
if err != nil {
return err
func (rs *RedisStorage) SetDestinationIDs(dest *Destination) (err error) {
for _, p := range dest.Prefixes {
err = rs.db.Cmd("SADD", utils.DESTINATION_PREFIX+p, dest.Id).Err
if err != nil {
break
}
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(result)
w.Close()
err = rs.db.Cmd("SET", utils.DESTINATION_PREFIX+dest.Id, b.Bytes()).Err
if err == nil && historyScribe != nil {
response := 0
go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(false), &response)