Skip to content

Commit 278a861

Browse files
committed
block: move CompressionStats code
1 parent 687a1b7 commit 278a861

File tree

2 files changed

+87
-73
lines changed

2 files changed

+87
-73
lines changed

sstable/block/compression_stats.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2+
// of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
package block
6+
7+
import (
8+
"fmt"
9+
"iter"
10+
"strings"
11+
12+
"github.com/cockroachdb/pebble/internal/compression"
13+
)
14+
15+
// CompressionStats collects compression statistics for a single file - the
16+
// total compressed and uncompressed sizes for each distinct compression.Setting
17+
// used.
18+
type CompressionStats struct {
19+
n int
20+
// Compression profiles have three settings (data, value, other) and
21+
// NoCompression can also be used for data that didn't compress.
22+
buf [4]CompressionStatsForSetting
23+
}
24+
25+
type CompressionStatsForSetting struct {
26+
Setting compression.Setting
27+
UncompressedBytes uint64
28+
CompressedBytes uint64
29+
}
30+
31+
// add updates the stats to reflect a block that was compressed with the given setting.
32+
func (c *CompressionStats) add(
33+
setting compression.Setting, sizeUncompressed, sizeCompressed uint64,
34+
) {
35+
for i := 0; i < c.n; i++ {
36+
if c.buf[i].Setting == setting {
37+
c.buf[i].UncompressedBytes += sizeUncompressed
38+
c.buf[i].CompressedBytes += sizeCompressed
39+
return
40+
}
41+
}
42+
if c.n >= len(c.buf)-1 {
43+
panic("too many compression settings")
44+
}
45+
c.buf[c.n] = CompressionStatsForSetting{
46+
Setting: setting,
47+
UncompressedBytes: sizeUncompressed,
48+
CompressedBytes: sizeCompressed,
49+
}
50+
c.n++
51+
}
52+
53+
// MergeWith updates the receiver stats to include the other stats.
54+
func (c *CompressionStats) MergeWith(other *CompressionStats) {
55+
for i := 0; i < other.n; i++ {
56+
c.add(other.buf[i].Setting, other.buf[i].UncompressedBytes, other.buf[i].CompressedBytes)
57+
}
58+
}
59+
60+
// All returns an iterator over the collected stats, in arbitrary order.
61+
func (c CompressionStats) All() iter.Seq[CompressionStatsForSetting] {
62+
return func(yield func(cs CompressionStatsForSetting) bool) {
63+
for i := 0; i < c.n; i++ {
64+
if !yield(c.buf[i]) {
65+
return
66+
}
67+
}
68+
}
69+
}
70+
71+
// String returns a string representation of the stats, in the format:
72+
// "<setting1>:<compressed1>/<uncompressed1>,<setting2>:<compressed2>/<uncompressed2>,..."
73+
func (c CompressionStats) String() string {
74+
var buf strings.Builder
75+
buf.Grow(c.n * 64)
76+
for i := 0; i < c.n; i++ {
77+
if i > 0 {
78+
buf.WriteString(",")
79+
}
80+
fmt.Fprintf(&buf, "%s:%d/%d", c.buf[i].Setting.String(), c.buf[i].CompressedBytes, c.buf[i].UncompressedBytes)
81+
}
82+
return buf.String()
83+
}

sstable/block/compressor.go

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2+
// of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
15
package block
26

37
import (
4-
"fmt"
5-
"iter"
68
"math/rand"
7-
"strings"
89

910
"github.com/cockroachdb/pebble/internal/compression"
1011
"github.com/cockroachdb/pebble/sstable/block/blockkind"
@@ -108,76 +109,6 @@ func (c *Compressor) Stats() *CompressionStats {
108109
return &c.stats
109110
}
110111

111-
// CompressionStats collects compression statistics for a single file - the
112-
// total compressed and uncompressed sizes for each distinct compression.Setting
113-
// used.
114-
type CompressionStats struct {
115-
n int
116-
// Compression profiles have three settings (data, value, other) and
117-
// NoCompression can also be used for data that didn't compress.
118-
buf [4]CompressionStatsForSetting
119-
}
120-
121-
type CompressionStatsForSetting struct {
122-
Setting compression.Setting
123-
UncompressedBytes uint64
124-
CompressedBytes uint64
125-
}
126-
127-
// add updates the stats to reflect a block that was compressed with the given setting.
128-
func (c *CompressionStats) add(
129-
setting compression.Setting, sizeUncompressed, sizeCompressed uint64,
130-
) {
131-
for i := 0; i < c.n; i++ {
132-
if c.buf[i].Setting == setting {
133-
c.buf[i].UncompressedBytes += sizeUncompressed
134-
c.buf[i].CompressedBytes += sizeCompressed
135-
return
136-
}
137-
}
138-
if c.n >= len(c.buf)-1 {
139-
panic("too many compression settings")
140-
}
141-
c.buf[c.n] = CompressionStatsForSetting{
142-
Setting: setting,
143-
UncompressedBytes: sizeUncompressed,
144-
CompressedBytes: sizeCompressed,
145-
}
146-
c.n++
147-
}
148-
149-
// MergeWith updates the receiver stats to include the other stats.
150-
func (c *CompressionStats) MergeWith(other *CompressionStats) {
151-
for i := 0; i < other.n; i++ {
152-
c.add(other.buf[i].Setting, other.buf[i].UncompressedBytes, other.buf[i].CompressedBytes)
153-
}
154-
}
155-
156-
// All returns an iterator over the collected stats, in arbitrary order.
157-
func (c CompressionStats) All() iter.Seq[CompressionStatsForSetting] {
158-
return func(yield func(cs CompressionStatsForSetting) bool) {
159-
for i := 0; i < c.n; i++ {
160-
if !yield(c.buf[i]) {
161-
return
162-
}
163-
}
164-
}
165-
}
166-
167-
// String returns a string representation of the stats, in the format:
168-
// "<setting1>:<compressed1>/<uncompressed1>,<setting2>:<compressed2>/<uncompressed2>,..."
169-
func (c CompressionStats) String() string {
170-
var buf strings.Builder
171-
buf.Grow(c.n * 64)
172-
for i := 0; i < c.n; i++ {
173-
if i > 0 {
174-
buf.WriteString(",")
175-
}
176-
fmt.Fprintf(&buf, "%s:%d/%d", c.buf[i].Setting.String(), c.buf[i].CompressedBytes, c.buf[i].UncompressedBytes)
177-
}
178-
return buf.String()
179-
}
180-
181112
type Decompressor = compression.Decompressor
182113

183114
func GetDecompressor(c CompressionIndicator) Decompressor {

0 commit comments

Comments
 (0)