Skip to content

Commit 5d8a7ef

Browse files
committed
manifest/internal: adjust VirtualBackings map to track pointers
To prepare the VirtualBackings struct to support maintaining a table backing heap, convert the backingWithMetadata map to store pointers.
1 parent 342d2e8 commit 5d8a7ef

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

internal/manifest/virtual_backings.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import (
6767
// - ingestion fails, calls Unprotect(B1). B1 is now Unused() and the next
6868
// version (applied by whatever next operation is) will remove B1.
6969
type VirtualBackings struct {
70-
m map[base.DiskFileNum]backingWithMetadata
70+
m map[base.DiskFileNum]*backingWithMetadata
7171

7272
// unused are all the backings in m that are not inUse(). Used for
7373
// implementing Unused() efficiently.
@@ -79,7 +79,7 @@ type VirtualBackings struct {
7979
// MakeVirtualBackings returns empty initialized VirtualBackings.
8080
func MakeVirtualBackings() VirtualBackings {
8181
return VirtualBackings{
82-
m: make(map[base.DiskFileNum]backingWithMetadata),
82+
m: make(map[base.DiskFileNum]*backingWithMetadata),
8383
unused: make(map[*TableBacking]struct{}),
8484
}
8585
}
@@ -108,7 +108,7 @@ func (bv *VirtualBackings) AddAndRef(backing *TableBacking) {
108108
// We take a reference on the backing because in case of protected backings
109109
// (see Protect), we might be the only ones holding on to a backing.
110110
backing.Ref()
111-
bv.mustAdd(backingWithMetadata{
111+
bv.mustAdd(&backingWithMetadata{
112112
backing: backing,
113113
virtualTables: make(map[base.TableNum]*TableMetadata),
114114
})
@@ -147,8 +147,10 @@ func (bv *VirtualBackings) AddTable(m *TableMetadata) {
147147
delete(bv.unused, v.backing)
148148
}
149149
v.virtualizedSize += m.Size
150+
if _, ok := v.virtualTables[m.TableNum]; ok {
151+
panic(errors.AssertionFailedf("table %s already uses backing %s", m.TableNum, v.backing.DiskFileNum))
152+
}
150153
v.virtualTables[m.TableNum] = m
151-
bv.m[m.TableBacking.DiskFileNum] = v
152154
}
153155

154156
// RemoveTable is used when a table using a backing is removed. The backing is
@@ -161,7 +163,6 @@ func (bv *VirtualBackings) RemoveTable(backing base.DiskFileNum, table base.Tabl
161163
}
162164
delete(v.virtualTables, table)
163165
v.virtualizedSize -= t.Size
164-
bv.m[backing] = v
165166
if !v.inUse() {
166167
bv.unused[v.backing] = struct{}{}
167168
}
@@ -178,7 +179,6 @@ func (bv *VirtualBackings) Protect(n base.DiskFileNum) {
178179
delete(bv.unused, v.backing)
179180
}
180181
v.protectionCount++
181-
bv.m[n] = v
182182
}
183183

184184
// Unprotect reverses a Protect call.
@@ -189,7 +189,6 @@ func (bv *VirtualBackings) Unprotect(n base.DiskFileNum) {
189189
panic(errors.AssertionFailedf("invalid protectionCount"))
190190
}
191191
v.protectionCount--
192-
bv.m[n] = v
193192
if !v.inUse() {
194193
bv.unused[v.backing] = struct{}{}
195194
}
@@ -292,15 +291,15 @@ func (bv *VirtualBackings) String() string {
292291
return buf.String()
293292
}
294293

295-
func (bv *VirtualBackings) mustAdd(v backingWithMetadata) {
294+
func (bv *VirtualBackings) mustAdd(v *backingWithMetadata) {
296295
_, ok := bv.m[v.backing.DiskFileNum]
297296
if ok {
298297
panic("pebble: trying to add an existing file backing")
299298
}
300299
bv.m[v.backing.DiskFileNum] = v
301300
}
302301

303-
func (bv *VirtualBackings) mustGet(n base.DiskFileNum) backingWithMetadata {
302+
func (bv *VirtualBackings) mustGet(n base.DiskFileNum) *backingWithMetadata {
304303
v, ok := bv.m[n]
305304
if !ok {
306305
panic(fmt.Sprintf("unknown backing %s", n))

0 commit comments

Comments
 (0)