2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2020-10-10 18:53:59 +02:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-10-10 18:53:59 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2020-10-10 18:53:59 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-10-10 18:53:59 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-10 18:53:59 +02:00
|
|
|
|
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
2020-10-10 18:53:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage is the memory implementation of a storage backend
|
|
|
|
type Storage struct {
|
|
|
|
store map[string]interface{}
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorage creates a new memory storage
|
|
|
|
func NewStorage() *Storage {
|
|
|
|
s := &Storage{}
|
|
|
|
s.store = make(map[string]interface{})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts a value into the memory storage
|
|
|
|
func (s *Storage) Put(key string, value interface{}) (err error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
s.store[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a saved value from memory storage
|
2021-01-31 12:32:46 +01:00
|
|
|
func (s *Storage) Get(key string) (value interface{}, exists bool, err error) {
|
2020-10-10 18:53:59 +02:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
value, exists = s.store[key]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del removes a saved value from a memory storage
|
|
|
|
func (s *Storage) Del(key string) (err error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
delete(s.store, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrBy increases the value saved at key by the amount provided through update
|
|
|
|
// It assumes the value saved for the key either does not exist or has a type of int64
|
|
|
|
func (s *Storage) IncrBy(key string, update int64) (err error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2020-10-20 20:28:44 +02:00
|
|
|
val, is := s.store[key].(int64)
|
2020-10-10 18:53:59 +02:00
|
|
|
if !is {
|
|
|
|
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
|
|
|
}
|
|
|
|
s.store[key] = val + update
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecrBy decreases the value saved at key by the amount provided through update
|
|
|
|
// It assumes the value saved for the key either does not exist or has a type of int64
|
|
|
|
func (s *Storage) DecrBy(key string, update int64) (err error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2020-10-20 20:28:44 +02:00
|
|
|
val, is := s.store[key].(int64)
|
2020-10-10 18:53:59 +02:00
|
|
|
if !is {
|
|
|
|
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
|
|
|
}
|
|
|
|
s.store[key] = val - update
|
|
|
|
return nil
|
|
|
|
}
|