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-22 09:23:14 +00:00
|
|
|
Get(ctx context.Context, parentSoul, field string) (*ValueWithState, error)
|
|
|
|
Put(ctx context.Context, parentSoul, field string, val *ValueWithState) (bool, error)
|
2019-02-22 06:46:19 +00:00
|
|
|
// Tracking(ctx context.Context, id 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-22 09:23:14 +00:00
|
|
|
func (s *StorageInMem) Get(ctx context.Context, parentSoul, field string) (*ValueWithState, error) {
|
2019-02-22 19:51:50 +00:00
|
|
|
v, ok := s.values.Load(parentSoulAndField{parentSoul, field})
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrStorageNotFound
|
|
|
|
}
|
|
|
|
return v.(*ValueWithState), nil
|
2019-02-22 06:46:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 09:23:14 +00:00
|
|
|
func (s *StorageInMem) Put(ctx context.Context, parentSoul, field string, val *ValueWithState) (bool, error) {
|
2019-02-22 19:51:50 +00:00
|
|
|
s.values.Store(parentSoulAndField{parentSoul, field}, val)
|
|
|
|
// TODO: conflict resolution state check?
|
|
|
|
return true, nil
|
2019-02-20 20:54:46 +00:00
|
|
|
}
|