-
Notifications
You must be signed in to change notification settings - Fork 44
/
fileblockstore.go
60 lines (47 loc) · 1.48 KB
/
fileblockstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package fileblockstore
import (
"context"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonfile/fileproto/fileprotoerr"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)
var log = logger.NewNamed(CName)
var (
ErrCIDNotFound = fileprotoerr.ErrCIDNotFound
ErrCIDUnexpected = fileprotoerr.ErrUnexpected
)
const CName = "common.commonfile.fileblockstore"
type ctxKey uint
const (
ctxKeySpaceId ctxKey = iota
ctxKeyFileId
)
type BlockStore interface {
Get(ctx context.Context, k cid.Cid) (blocks.Block, error)
GetMany(ctx context.Context, ks []cid.Cid) <-chan blocks.Block
Add(ctx context.Context, b []blocks.Block) error
Delete(ctx context.Context, c cid.Cid) error
}
type BlockStoreLocal interface {
BlockStore
ExistsCids(ctx context.Context, ks []cid.Cid) (exists []cid.Cid, err error)
NotExistsBlocks(ctx context.Context, bs []blocks.Block) (notExists []blocks.Block, err error)
}
type BlockStoreSpaceIds interface {
SpaceIds() []string
}
func CtxWithSpaceId(ctx context.Context, spaceId string) context.Context {
return context.WithValue(ctx, ctxKeySpaceId, spaceId)
}
func CtxGetSpaceId(ctx context.Context) (spaceId string) {
spaceId, _ = ctx.Value(ctxKeySpaceId).(string)
return
}
func CtxWithFileId(ctx context.Context, spaceId string) context.Context {
return context.WithValue(ctx, ctxKeyFileId, spaceId)
}
func CtxGetFileId(ctx context.Context) (spaceId string) {
spaceId, _ = ctx.Value(ctxKeyFileId).(string)
return
}