-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_archive.go
96 lines (84 loc) · 1.88 KB
/
mock_archive.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
// Copyright 2016 Hcnet Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package historyarchive
import (
"bytes"
"errors"
"io"
"io/ioutil"
"strings"
"sync"
"github.com/shantanu-hashcash/go/support/storage"
)
type MockArchiveBackend struct {
mutex sync.Mutex
files map[string][]byte
}
func (b *MockArchiveBackend) Exists(pth string) (bool, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
_, ok := b.files[pth]
return ok, nil
}
func (b *MockArchiveBackend) Size(pth string) (int64, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
f, ok := b.files[pth]
sz := int64(0)
if ok {
sz = int64(len(f))
}
return sz, nil
}
func (b *MockArchiveBackend) GetFile(pth string) (io.ReadCloser, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
var buf []byte
buf, ok := b.files[pth]
if !ok {
return nil, errors.New("no such file: " + pth)
}
return ioutil.NopCloser(bytes.NewReader(buf)), nil
}
func (b *MockArchiveBackend) PutFile(pth string, in io.ReadCloser) error {
b.mutex.Lock()
defer b.mutex.Unlock()
buf, e := ioutil.ReadAll(in)
if e != nil {
return e
}
b.files[pth] = buf
return nil
}
func (b *MockArchiveBackend) ListFiles(pth string) (chan string, chan error) {
b.mutex.Lock()
defer b.mutex.Unlock()
ch := make(chan string)
errs := make(chan error)
files := make([]string, 0, len(b.files))
for k := range b.files {
files = append(files, k)
}
go func() {
for _, f := range files {
if strings.HasPrefix(f, pth) {
ch <- f
}
}
close(ch)
close(errs)
}()
return ch, errs
}
func (b *MockArchiveBackend) CanListFiles() bool {
return true
}
func (b *MockArchiveBackend) Close() error {
return nil
}
func makeMockBackend() storage.Storage {
b := new(MockArchiveBackend)
b.files = make(map[string][]byte)
return b
}