2019-02-20 20:54:46 +00:00
|
|
|
package gun
|
|
|
|
|
2019-02-22 06:46:19 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrStorageNotFound = errors.New("Not found")
|
|
|
|
|
2019-02-20 20:54:46 +00:00
|
|
|
type Storage interface {
|
2019-02-25 05:14:26 +00:00
|
|
|
Get(ctx context.Context, parentSoul, field string) (Value, State, error)
|
2019-02-25 04:23:15 +00:00
|
|
|
// If bool is false, it's deferred
|
2019-02-25 05:14:26 +00:00
|
|
|
Put(ctx context.Context, parentSoul, field string, val Value, state State) (bool, error)
|
2019-02-22 21:40:55 +00:00
|
|
|
Tracking(ctx context.Context, parentSoul, field string) (bool, error)
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StorageInMem struct {
|
2019-02-22 06:46:19 +00:00
|
|
|
values sync.Map
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:51:50 +00:00
|
|
|
type parentSoulAndField struct{ parentSoul, field string }
|
|
|
|
|
2019-02-25 05:14:26 +00:00
|
|
|
type valueWithState struct {
|
|
|
|
val Value
|
|
|
|
state State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageInMem) Get(ctx context.Context, parentSoul, field string) (Value, State, error) {
|
2019-02-22 19:51:50 +00:00
|
|
|
v, ok := s.values.Load(parentSoulAndField{parentSoul, field})
|
|
|
|
if !ok {
|
2019-02-25 05:14:26 +00:00
|
|
|
return nil, 0, ErrStorageNotFound
|
2019-02-22 19:51:50 +00:00
|
|
|
}
|
2019-02-25 05:14:26 +00:00
|
|
|
vs := v.(*valueWithState)
|
|
|
|
return vs.val, vs.state, nil
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 05:14:26 +00:00
|
|
|
func (s *StorageInMem) Put(ctx context.Context, parentSoul, field string, val Value, state State) (bool, error) {
|
|
|
|
s.values.Store(parentSoulAndField{parentSoul, field}, &valueWithState{val, state})
|
2019-02-22 19:51:50 +00:00
|
|
|
// TODO: conflict resolution state check?
|
|
|
|
return true, nil
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|
2019-02-22 21:40:55 +00:00
|
|
|
|
|
|
|
func (s *StorageInMem) Tracking(ctx context.Context, parentSoul, field string) (bool, error) {
|
|
|
|
_, ok := s.values.Load(parentSoulAndField{parentSoul, field})
|
|
|
|
return ok, nil
|
|
|
|
}
|