cmd/swarm, swarm: LocalStore storage integration
This commit is contained in:
committed by
Anton Evangelatov
parent
c94d582aa7
commit
996755c4a8
@ -1,6 +1,7 @@
|
||||
package chunk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@ -28,7 +29,7 @@ type chunk struct {
|
||||
sdata []byte
|
||||
}
|
||||
|
||||
func NewChunk(addr Address, data []byte) *chunk {
|
||||
func NewChunk(addr Address, data []byte) Chunk {
|
||||
return &chunk{
|
||||
addr: addr,
|
||||
sdata: data,
|
||||
@ -107,3 +108,105 @@ func Proximity(one, other []byte) (ret int) {
|
||||
}
|
||||
return MaxPO
|
||||
}
|
||||
|
||||
// ModeGet enumerates different Getter modes.
|
||||
type ModeGet int
|
||||
|
||||
// Getter modes.
|
||||
const (
|
||||
// ModeGetRequest: when accessed for retrieval
|
||||
ModeGetRequest ModeGet = iota
|
||||
// ModeGetSync: when accessed for syncing or proof of custody request
|
||||
ModeGetSync
|
||||
// ModeGetLookup: when accessed to lookup a a chunk in feeds or other places
|
||||
ModeGetLookup
|
||||
)
|
||||
|
||||
// ModePut enumerates different Putter modes.
|
||||
type ModePut int
|
||||
|
||||
// Putter modes.
|
||||
const (
|
||||
// ModePutRequest: when a chunk is received as a result of retrieve request and delivery
|
||||
ModePutRequest ModePut = iota
|
||||
// ModePutSync: when a chunk is received via syncing
|
||||
ModePutSync
|
||||
// ModePutUpload: when a chunk is created by local upload
|
||||
ModePutUpload
|
||||
)
|
||||
|
||||
// ModeSet enumerates different Setter modes.
|
||||
type ModeSet int
|
||||
|
||||
// Setter modes.
|
||||
const (
|
||||
// ModeSetAccess: when an update request is received for a chunk or chunk is retrieved for delivery
|
||||
ModeSetAccess ModeSet = iota
|
||||
// ModeSetSync: when push sync receipt is received
|
||||
ModeSetSync
|
||||
// ModeSetRemove: when a chunk is removed
|
||||
ModeSetRemove
|
||||
)
|
||||
|
||||
// Descriptor holds information required for Pull syncing. This struct
|
||||
// is provided by subscribing to pull index.
|
||||
type Descriptor struct {
|
||||
Address Address
|
||||
BinID uint64
|
||||
}
|
||||
|
||||
func (d *Descriptor) String() string {
|
||||
if d == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%s bin id %v", d.Address.Hex(), d.BinID)
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
Get(ctx context.Context, mode ModeGet, addr Address) (ch Chunk, err error)
|
||||
Put(ctx context.Context, mode ModePut, ch Chunk) (exists bool, err error)
|
||||
Has(ctx context.Context, addr Address) (yes bool, err error)
|
||||
Set(ctx context.Context, mode ModeSet, addr Address) (err error)
|
||||
LastPullSubscriptionBinID(bin uint8) (id uint64, err error)
|
||||
SubscribePull(ctx context.Context, bin uint8, since, until uint64) (c <-chan Descriptor, stop func())
|
||||
Close() (err error)
|
||||
}
|
||||
|
||||
// FetchStore is a Store which supports syncing
|
||||
type FetchStore interface {
|
||||
Store
|
||||
FetchFunc(ctx context.Context, addr Address) func(context.Context) error
|
||||
}
|
||||
|
||||
// Validator validates a chunk.
|
||||
type Validator interface {
|
||||
Validate(ch Chunk) bool
|
||||
}
|
||||
|
||||
// ValidatorStore encapsulates Store by decorting the Put method
|
||||
// with validators check.
|
||||
type ValidatorStore struct {
|
||||
Store
|
||||
validators []Validator
|
||||
}
|
||||
|
||||
// NewValidatorStore returns a new ValidatorStore which uses
|
||||
// provided validators to validate chunks on Put.
|
||||
func NewValidatorStore(store Store, validators ...Validator) (s *ValidatorStore) {
|
||||
return &ValidatorStore{
|
||||
Store: store,
|
||||
validators: validators,
|
||||
}
|
||||
}
|
||||
|
||||
// Put overrides Store put method with validators check. If one of the validators
|
||||
// return true, the chunk is considered valid and Store Put method is called.
|
||||
// If all validators return false, ErrChunkInvalid is returned.
|
||||
func (s *ValidatorStore) Put(ctx context.Context, mode ModePut, ch Chunk) (exists bool, err error) {
|
||||
for _, v := range s.validators {
|
||||
if v.Validate(ch) {
|
||||
return s.Store.Put(ctx, mode, ch)
|
||||
}
|
||||
}
|
||||
return false, ErrChunkInvalid
|
||||
}
|
||||
|
Reference in New Issue
Block a user