forked from perkeep/perkeep
/
fetcher.go
194 lines (174 loc) · 4.39 KB
/
fetcher.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
"sync"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/context"
"camlistore.org/pkg/types"
)
// Fetcher is an in-memory implementation of the blobserver Storage
// interface. It started as just a fetcher and grew. It also includes
// other convenience methods for testing.
type Fetcher struct {
mu sync.RWMutex
m map[string]*Blob // keyed by blobref string
sorted []string // blobrefs sorted
// ReceiveErr optionally returns the error to return on receive.
ReceiveErr error
// FetchErr, if non-nil, specifies the error to return on the next fetch call.
// If it returns nil, fetches proceed as normal.
FetchErr func() error
}
var _ blobserver.Storage = (*Fetcher)(nil)
func (tf *Fetcher) AddBlob(b *Blob) {
tf.mu.Lock()
defer tf.mu.Unlock()
if tf.m == nil {
tf.m = make(map[string]*Blob)
}
key := b.BlobRef().String()
_, had := tf.m[key]
tf.m[key] = b
if !had {
tf.sorted = append(tf.sorted, key)
sort.Strings(tf.sorted)
}
}
func (tf *Fetcher) FetchStreaming(ref blob.Ref) (file io.ReadCloser, size uint32, err error) {
return tf.Fetch(ref)
}
func (tf *Fetcher) Fetch(ref blob.Ref) (file types.ReadSeekCloser, size uint32, err error) {
if tf.FetchErr != nil {
if err = tf.FetchErr(); err != nil {
return
}
}
tf.mu.RLock()
defer tf.mu.RUnlock()
if tf.m == nil {
err = os.ErrNotExist
return
}
tb, ok := tf.m[ref.String()]
if !ok {
err = os.ErrNotExist
return
}
size = uint32(len(tb.Contents))
return struct {
*io.SectionReader
io.Closer
}{
io.NewSectionReader(strings.NewReader(tb.Contents), 0, int64(size)),
types.NopCloser,
}, size, nil
}
func (tf *Fetcher) BlobContents(br blob.Ref) (contents string, ok bool) {
tf.mu.RLock()
defer tf.mu.RUnlock()
b, ok := tf.m[br.String()]
if !ok {
return
}
return b.Contents, true
}
func (tf *Fetcher) ReceiveBlob(br blob.Ref, source io.Reader) (blob.SizedRef, error) {
sb := blob.SizedRef{}
h := br.Hash()
if h == nil {
return sb, fmt.Errorf("Unsupported blobref hash for %s", br)
}
all, err := ioutil.ReadAll(io.TeeReader(source, h))
if err != nil {
return sb, err
}
if !br.HashMatches(h) {
// This is a somewhat redundant check, since
// blobserver.Receive now does it. But for testing code,
// it's worth the cost.
return sb, fmt.Errorf("Hash mismatch receiving blob %s", br)
}
if err := tf.ReceiveErr; err != nil {
return sb, err
}
b := &Blob{Contents: string(all)}
tf.AddBlob(b)
return blob.SizedRef{br, uint32(len(all))}, nil
}
func (tf *Fetcher) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
for _, br := range blobs {
tf.mu.RLock()
b, ok := tf.m[br.String()]
tf.mu.RUnlock()
if ok {
dest <- blob.SizedRef{br, uint32(len(b.Contents))}
}
}
return nil
}
func (tf *Fetcher) NumBlobs() int {
tf.mu.RLock()
defer tf.mu.RUnlock()
return len(tf.m)
}
// BlobrefStrings returns the sorted stringified blobrefs stored in this fetcher.
func (tf *Fetcher) BlobrefStrings() []string {
tf.mu.RLock()
defer tf.mu.RUnlock()
s := make([]string, len(tf.sorted))
copy(s, tf.sorted)
return s
}
func (tf *Fetcher) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) error {
defer close(dest)
tf.mu.RLock()
defer tf.mu.RUnlock()
n := 0
for _, k := range tf.sorted {
if k <= after {
continue
}
b := tf.m[k]
select {
case dest <- blob.SizedRef{b.BlobRef(), uint32(b.Size())}:
case <-ctx.Done():
return context.ErrCanceled
}
n++
if limit > 0 && n == limit {
break
}
}
return nil
}
func (tf *Fetcher) RemoveBlobs(blobs []blob.Ref) error {
tf.mu.Lock()
defer tf.mu.Unlock()
for _, br := range blobs {
delete(tf.m, br.String())
}
tf.sorted = tf.sorted[:0]
for k := range tf.m {
tf.sorted = append(tf.sorted, k)
}
sort.Strings(tf.sorted)
return nil
}