-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti.go
137 lines (94 loc) · 2.78 KB
/
multi.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
package reader
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"io"
_ "log"
"sync"
)
// MultiReader is a struct that implements the `Reader` interface for reading documents from one or more `Reader` instances.
type MultiReader struct {
Reader
readers []Reader
lookup map[string]int
mu *sync.RWMutex
}
// NewMultiReaderFromURIs returns a new `Reader` instance for reading documents from one or more `Reader` instances.
// 'uris' is assumed to be a list of URIs each of which will be used to invoke the `NewReader` method.
func NewMultiReaderFromURIs(ctx context.Context, uris ...string) (Reader, error) {
readers := make([]Reader, 0)
for _, uri := range uris {
r, err := NewReader(ctx, uri)
if err != nil {
return nil, fmt.Errorf("Failed to create reader for %s, %w", uri, err)
}
readers = append(readers, r)
}
return NewMultiReader(ctx, readers...)
}
// NewMultiReaderFromURIs returns a new `Reader` instance for reading documents from one or more `Reader` instances.
func NewMultiReader(ctx context.Context, readers ...Reader) (Reader, error) {
lookup := make(map[string]int)
mu := new(sync.RWMutex)
mr := MultiReader{
readers: readers,
lookup: lookup,
mu: mu,
}
return &mr, nil
}
// Read will open an `io.ReadSeekCloser` for a file matching 'path'. In the case of multiple underlying
// `Reader` instances the first instance to successfully load 'path' will be returned.
func (mr *MultiReader) Read(ctx context.Context, path string) (io.ReadSeekCloser, error) {
missing := errors.New("Unable to read URI")
mr.mu.RLock()
idx, ok := mr.lookup[path]
mr.mu.RUnlock()
if ok {
// log.Printf("READ MULTIREADER LOOKUP INDEX FOR %s AS %d\n", path, idx)
if idx == -1 {
return nil, missing
}
r := mr.readers[idx]
return r.Read(ctx, path)
}
var fh io.ReadSeekCloser
idx = -1
var errors error
for i, r := range mr.readers {
rsp, err := r.Read(ctx, path)
if err != nil {
errors = multierror.Append(fmt.Errorf("Failed to read %s with %T, %w", path, r, err))
} else {
fh = rsp
idx = i
break
}
}
// log.Printf("SET MULTIREADER LOOKUP INDEX FOR %s AS %d\n", path, idx)
mr.mu.Lock()
mr.lookup[path] = idx
mr.mu.Unlock()
if fh == nil {
return nil, errors
}
return fh, nil
}
// ReaderURI returns the absolute URL for 'path'. In the case of multiple underlying
// `Reader` instances the first instance to successfully load 'path' will be returned.
func (mr *MultiReader) ReaderURI(ctx context.Context, path string) string {
mr.mu.RLock()
idx, ok := mr.lookup[path]
mr.mu.RUnlock()
if ok {
return mr.readers[idx].ReaderURI(ctx, path)
}
r, err := mr.Read(ctx, path)
if err != nil {
return fmt.Sprintf("x-urn:go-reader:multi#%s", path)
}
defer r.Close()
return mr.ReaderURI(ctx, path)
}