From faf7d4cc6b677d717a2422abe89fa321d2f46f43 Mon Sep 17 00:00:00 2001 From: Edwardro22 Date: Sun, 22 Jan 2017 16:35:12 +0200 Subject: [PATCH] Populated MapStorage --- engine/storage_map.go | 64 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/engine/storage_map.go b/engine/storage_map.go index 6e67b6362..f4441be7b 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -1024,30 +1024,70 @@ func (ms *MapStorage) GetAccountActionPlans(acntID string, skipCache bool, trans key := utils.AccountActionPlansPrefix + acntID var values []byte if !skipCache { - if x, ok := cache.Get(key); ok { + if ap, ok := cache.Get(key); !ok { return nil, utils.ErrNotFound } else { - return x.([]string), nil + return ap.([]string), nil } } - if _, ok := ms.dict[key]; ok { + if _, ok := ms.dict[key]; !ok { + cache.Set(key, nil, cacheCommit(transactionID), transactionID) + return nil, utils.ErrNotFound + } + + if err = ms.ms.Unmarshal(values, &apIDs); err != nil { return nil, err - } else { - if err = ms.ms.Unmarshal(values, &apIDs); err != nil { - return nil, err - } else { - cache.Set(key, nil, cacheCommit(transactionID), transactionID) - return nil, utils.ErrNotFound - } - cache.Set(key, apIDs, cacheCommit(transactionID), transactionID) - return apIDs, nil } + + cache.Set(key, apIDs, cacheCommit(transactionID), transactionID) + return //apIDs, nil } func (ms *MapStorage) SetAccountActionPlans(acntID string, apIDs []string, overwrite bool) (err error) { + if !overwrite { + oldaPlIDs, err := ms.GetAccountActionPlans(acntID, true, utils.NonTransactional) + if err != nil { + return err + } else { + for _, oldAPid := range oldaPlIDs { + if !utils.IsSliceMember(apIDs, oldAPid) { + apIDs = append(apIDs, oldAPid) + } + } + } + } + + ms.mu.Lock() + defer ms.mu.Unlock() + if _, err = ms.ms.Marshal(&apIDs); err != nil { + return err + } return } + func (ms *MapStorage) RemAccountActionPlans(acntID string, apIDs []string) (err error) { + ms.mu.Lock() + defer ms.mu.Unlock() + key := utils.AccountActionPlansPrefix + acntID + if len(apIDs) == 0 { + delete(ms.dict, key) + return + } + oldaPlIDs, err := ms.GetAccountActionPlans(acntID, true, utils.NonTransactional) + if err != nil { + return err + } + for i := 0; i < len(oldaPlIDs); { + if utils.IsSliceMember(apIDs, oldaPlIDs[i]) { + oldaPlIDs = append(oldaPlIDs[:i], oldaPlIDs[i+1:]...) + continue + } + i++ + } + if len(oldaPlIDs) == 0 { + delete(ms.dict, key) + return + } return }