forked from huin/chunkymonkey
/
multistore.go
62 lines (51 loc) · 1.56 KB
/
multistore.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
package chunkstore
import (
"errors"
. "chunkymonkey/types"
)
// MultiStore provides the ability to load a chunk from one or more potential
// sources of chunk data. The primary purpose of this is to read from a
// persistant store first, then fall back to generating a chunk if the
// persistant store does not have it. MultiStore implements IChunkStore.
type MultiStore struct {
readStores []IChunkStore
writeStore IChunkStore
}
func NewMultiStore(readStores []IChunkStore, writeStore IChunkStore) *MultiStore {
s := &MultiStore{
readStores: readStores,
writeStore: writeStore,
}
return s
}
func (s *MultiStore) ReadChunk(chunkLoc ChunkXz) (reader IChunkReader, err error) {
for _, store := range s.readStores {
result := <-store.ReadChunk(chunkLoc)
if result.Err == nil {
return result.Reader, result.Err
} else {
if _, ok := result.Err.(NoSuchChunkError); ok {
// Fall through to next chunk store.
continue
}
return nil, result.Err
}
}
return nil, NoSuchChunkError(false)
}
func (s *MultiStore) SupportsWrite() bool {
return s.writeStore != nil && s.writeStore.SupportsWrite()
}
func (s *MultiStore) Writer() IChunkWriter {
if s.writeStore != nil {
return s.writeStore.Writer()
}
return nil
}
func (s *MultiStore) WriteChunk(writer IChunkWriter) error {
if s.writeStore == nil {
return errors.New("writes not supported")
}
s.writeStore.WriteChunk(writer)
return nil
}