-
Notifications
You must be signed in to change notification settings - Fork 23
/
blobstore.go
118 lines (89 loc) · 2.83 KB
/
blobstore.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-FileCopyrightText: 2021 The Go-SSB Authors
//
// SPDX-License-Identifier: MIT
package ssb
import (
"context"
"fmt"
"io"
"github.com/ssbc/go-luigi"
"github.com/ssbc/go-muxrpc/v2"
refs "github.com/ssbc/go-ssb-refs"
)
const (
// BlobStoreOpPut is used in put notifications
BlobStoreOpPut BlobStoreOp = "put"
// BlobStoreOpRm is used in remove notifications
BlobStoreOpRm BlobStoreOp = "rm"
)
// BlobStore is the interface of our blob store
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mock/blobstore.go . BlobStore
type BlobStore interface {
// Get returns a reader of the blob with given ref.
Get(ref refs.BlobRef) (io.ReadCloser, error)
// Put stores the data in the reader in the blob store and returns the address.
Put(blob io.Reader) (refs.BlobRef, error)
// PutExpected makes sure the added blob really is the passedBlobref
// helpful for want/get operations which don't want to waste resources
// PutExpected(io.Reader, *refs.BlobRef) error
// Delete deletes a blob from the blob store.
Delete(ref refs.BlobRef) error
// List returns a source of the refs of all stored blobs.
List() luigi.Source
// Size returns the size of the blob with given ref.
Size(ref refs.BlobRef) (int64, error)
// Register allows to get notified when the store changes
Register(sink BlobStoreEmitter) CancelFunc
}
type BlobStoreEmitter interface {
EmitBlob(BlobStoreNotification) error
io.Closer
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mock/wantmanager.go . WantManager
type WantManager interface {
io.Closer
BlobWantsBroadcaster
Want(ref refs.BlobRef) error
Wants(ref refs.BlobRef) bool
WantWithDist(ref refs.BlobRef, dist int64) error
//Unwant(ref refs.BlobRef) error
CreateWants(context.Context, *muxrpc.ByteSink, muxrpc.Endpoint) luigi.Sink
AllWants() []BlobWant
}
type CancelFunc func()
type BlobWantsEmitter interface {
EmitWant(BlobWant) error
io.Closer
}
type BlobWantsBroadcaster interface {
Register(sink BlobWantsEmitter) CancelFunc
}
type BlobWant struct {
Ref refs.BlobRef
// if Dist is negative, it is the hop count to the original wanter.
// if it is positive, it is the size of the blob.
Dist int64
}
func (w BlobWant) String() string {
return fmt.Sprintf("%s:%d", w.Ref.ShortSigil(), w.Dist)
}
// BlobStoreNotification contains info on a single change of the blob store.
// Op is either "rm" or "put".
type BlobStoreNotification struct {
Op BlobStoreOp
Ref refs.BlobRef
Size int64
}
func (bn BlobStoreNotification) String() string {
s := bn.Op.String() + ": " + bn.Ref.Sigil()
if bn.Size > 0 {
s += fmt.Sprintf(" (size: %d)", bn.Size)
}
return s
}
// BlobStoreOp specifies the operation in a blob store notification.
type BlobStoreOp string
// String returns the string representation of the operation.
func (op BlobStoreOp) String() string {
return string(op)
}