go-gun/gun/storage.go

42 lines
1.1 KiB
Go
Raw Normal View History

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)
2019-02-25 04:23:15 +00:00
// If bool is false, it's deferred
2019-02-22 09:23:14 +00:00
Put(ctx context.Context, parentSoul, field string, val *ValueWithState) (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
}
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) {
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) {
s.values.Store(parentSoulAndField{parentSoul, field}, val)
// 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
}