forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_chunk_source.go
94 lines (77 loc) · 2.51 KB
/
aws_chunk_source.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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package nbs
import (
"sync"
"time"
"github.com/attic-labs/noms/go/d"
)
func newAWSChunkSource(ddb *ddbTableStore, s3 *s3ObjectReader, al awsLimits, name addr, chunkCount uint32, indexCache *indexCache, stats *Stats) chunkSource {
if indexCache != nil {
indexCache.lockEntry(name)
defer indexCache.unlockEntry(name)
if index, found := indexCache.get(name); found {
tra := &awsTableReaderAt{al: al, ddb: ddb, s3: s3, name: name, chunkCount: chunkCount}
return &awsChunkSource{newTableReader(index, tra, s3BlockSize), name}
}
}
t1 := time.Now()
indexBytes, tra := func() ([]byte, tableReaderAt) {
if al.tableMayBeInDynamo(chunkCount) {
data, err := ddb.ReadTable(name, stats)
if data != nil {
return data, &dynamoTableReaderAt{ddb: ddb, h: name}
}
d.PanicIfTrue(err == nil) // There MUST be either data or an error
d.PanicIfNotType(err, tableNotInDynamoErr{})
}
size := indexSize(chunkCount) + footerSize
buff := make([]byte, size)
n, err := s3.ReadFromEnd(name, buff, stats)
d.PanicIfError(err)
d.PanicIfFalse(size == uint64(n))
return buff, &s3TableReaderAt{s3: s3, h: name}
}()
stats.IndexBytesPerRead.Sample(uint64(len(indexBytes)))
stats.IndexReadLatency.SampleTimeSince(t1)
index := parseTableIndex(indexBytes)
if indexCache != nil {
indexCache.put(name, index)
}
return &awsChunkSource{newTableReader(index, tra, s3BlockSize), name}
}
type awsChunkSource struct {
tableReader
name addr
}
func (acs *awsChunkSource) hash() addr {
return acs.name
}
type awsTableReaderAt struct {
once sync.Once
tra tableReaderAt
al awsLimits
ddb *ddbTableStore
s3 *s3ObjectReader
name addr
chunkCount uint32
}
func (atra *awsTableReaderAt) hash() addr {
return atra.name
}
func (atra *awsTableReaderAt) ReadAtWithStats(p []byte, off int64, stats *Stats) (n int, err error) {
atra.once.Do(func() { atra.tra = atra.getTableReaderAt(stats) })
return atra.tra.ReadAtWithStats(p, off, stats)
}
func (atra *awsTableReaderAt) getTableReaderAt(stats *Stats) tableReaderAt {
if atra.al.tableMayBeInDynamo(atra.chunkCount) {
data, err := atra.ddb.ReadTable(atra.name, stats)
if data != nil {
return &dynamoTableReaderAt{ddb: atra.ddb, h: atra.name}
}
d.PanicIfTrue(err == nil) // There MUST be either data or an error
d.PanicIfNotType(err, tableNotInDynamoErr{})
}
return &s3TableReaderAt{s3: atra.s3, h: atra.name}
}