forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersisting_chunk_source.go
165 lines (132 loc) · 3.45 KB
/
persisting_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
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
// 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 (
"bytes"
"io"
"sync"
"time"
"github.com/attic-labs/noms/go/chunks"
"github.com/attic-labs/noms/go/d"
)
func newPersistingChunkSource(mt *memTable, haver chunkReader, p tablePersister, rl chan struct{}, stats *Stats) *persistingChunkSource {
t1 := time.Now()
ccs := &persistingChunkSource{mt: mt}
ccs.wg.Add(1)
rl <- struct{}{}
go func() {
defer ccs.wg.Done()
cs := p.Persist(mt, haver, stats)
ccs.mu.Lock()
defer ccs.mu.Unlock()
ccs.cs = cs
ccs.mt = nil
<-rl
if cs.count() > 0 {
stats.PersistLatency.SampleTimeSince(t1)
}
}()
return ccs
}
type persistingChunkSource struct {
mu sync.RWMutex
mt *memTable
wg sync.WaitGroup
cs chunkSource
}
func (ccs *persistingChunkSource) getReader() chunkReader {
ccs.mu.RLock()
defer ccs.mu.RUnlock()
if ccs.mt != nil {
return ccs.mt
}
return ccs.cs
}
func (ccs *persistingChunkSource) has(h addr) bool {
cr := ccs.getReader()
d.Chk.True(cr != nil)
return cr.has(h)
}
func (ccs *persistingChunkSource) hasMany(addrs []hasRecord) bool {
cr := ccs.getReader()
d.Chk.True(cr != nil)
return cr.hasMany(addrs)
}
func (ccs *persistingChunkSource) get(h addr, stats *Stats) []byte {
cr := ccs.getReader()
d.Chk.True(cr != nil)
return cr.get(h, stats)
}
func (ccs *persistingChunkSource) getMany(reqs []getRecord, foundChunks chan *chunks.Chunk, wg *sync.WaitGroup, stats *Stats) bool {
cr := ccs.getReader()
d.Chk.True(cr != nil)
return cr.getMany(reqs, foundChunks, wg, stats)
}
func (ccs *persistingChunkSource) count() uint32 {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.count()
}
func (ccs *persistingChunkSource) uncompressedLen() uint64 {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.uncompressedLen()
}
func (ccs *persistingChunkSource) hash() addr {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.hash()
}
func (ccs *persistingChunkSource) index() tableIndex {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.index()
}
func (ccs *persistingChunkSource) reader() io.Reader {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.reader()
}
func (ccs *persistingChunkSource) calcReads(reqs []getRecord, blockSize uint64) (reads int, remaining bool) {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
return ccs.cs.calcReads(reqs, blockSize)
}
func (ccs *persistingChunkSource) extract(chunks chan<- extractRecord) {
ccs.wg.Wait()
d.Chk.True(ccs.cs != nil)
ccs.cs.extract(chunks)
}
type emptyChunkSource struct{}
func (ecs emptyChunkSource) has(h addr) bool {
return false
}
func (ecs emptyChunkSource) hasMany(addrs []hasRecord) bool {
return true
}
func (ecs emptyChunkSource) get(h addr, stats *Stats) []byte {
return nil
}
func (ecs emptyChunkSource) getMany(reqs []getRecord, foundChunks chan *chunks.Chunk, wg *sync.WaitGroup, stats *Stats) bool {
return true
}
func (ecs emptyChunkSource) count() uint32 {
return 0
}
func (ecs emptyChunkSource) uncompressedLen() uint64 {
return 0
}
func (ecs emptyChunkSource) hash() addr {
return addr{}
}
func (ecs emptyChunkSource) index() tableIndex {
return tableIndex{}
}
func (ecs emptyChunkSource) reader() io.Reader {
return &bytes.Buffer{}
}
func (ecs emptyChunkSource) calcReads(reqs []getRecord, blockSize uint64) (reads int, remaining bool) {
return 0, true
}
func (ecs emptyChunkSource) extract(chunks chan<- extractRecord) {}