Skip to content

Commit 08f47e5

Browse files
authored
Merge pull request #399 from canopy-network/custom-compression
feat: custom compression algorithm
2 parents 1c2c748 + f4fd4c0 commit 08f47e5

5 files changed

Lines changed: 49 additions & 2 deletions

File tree

lib/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ type StoreConfig struct {
279279
LSSCompactionInterval uint64 `json:"lssCompactionInterval"` // interval for compacting latest store data
280280
BackupDirectory string `json:"backupDirectory"` // directory where backups of the database are stored
281281
BackupInterval uint64 `json:"backupInterval"` // interval in blocks for creating backups of the database (0 to disable automatic backups)
282+
CompressionProfile string `json:"compressionProfile"` // the pebbledb compression profile to use.
282283
}
283284

284285
// DefaultDataDirPath() is $USERHOME/.canopy
@@ -306,6 +307,7 @@ func DefaultStoreConfig() StoreConfig {
306307
LSSCompactionInterval: uint64(rand.Int32N(101) + 500), // clean every 500-600 blocks (random)
307308
BackupDirectory: path.Join(DefaultDataDirPath(), "backup"), // backup directory name
308309
BackupInterval: 0, // backups disabled by default
310+
CompressionProfile: "zstd",
309311
}
310312
}
311313

lib/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ type ProveStoreI interface {
103103
// IteratorI defines an interface for iterating over key-value pairs in a data store
104104
type IteratorI interface {
105105
Valid() bool // if the item the iterator is pointing at is valid
106-
Next() // move to next item
106+
Next() // move to next itema
107107
Key() (key []byte) // retrieve key
108108
Value() (value []byte) // retrieve value
109109
Close() // close the iterator when done, ensuring proper resource management

store/store.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ func NewStore(config lib.Config, path string, metrics *lib.Metrics, log lib.Logg
9797
BlockSize: 64 << 10, // 64 KB data blocks
9898
IndexBlockSize: 32 << 10, // 32 KB index blocks
9999
Compression: func() *sstable.CompressionProfile {
100-
return sstable.ZstdCompression // biggest compression at the expense of more CPU resources
100+
profile := getCompressionProfile(config.CompressionProfile)
101+
log.Infof("Using %s compression for sstables", profile.Name)
102+
return profile
101103
},
102104
}
103105
db, err := pebble.Open(path, &pebble.Options{

store/versioned_store.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66
"fmt"
77
"math"
8+
"strings"
89

910
"github.com/canopy-network/canopy/lib"
1011
"github.com/cockroachdb/pebble/v2"
@@ -641,3 +642,23 @@ func newTargetWindowFilter(low, high uint64) sstable.BlockPropertyFilter {
641642
nil,
642643
)
643644
}
645+
646+
// getCompressionProfile returns the compression profile (algorithm) to use for the versioned store
647+
func getCompressionProfile(profile string) *sstable.CompressionProfile {
648+
switch strings.ToLower(profile) {
649+
case "zstd":
650+
return sstable.ZstdCompression
651+
case "snappy":
652+
return sstable.SnappyCompression
653+
case "nocompression":
654+
return sstable.NoCompression
655+
case "fastest":
656+
return sstable.FastestCompression
657+
case "balanced":
658+
return sstable.BalancedCompression
659+
case "good":
660+
return sstable.GoodCompression
661+
default:
662+
return sstable.ZstdCompression
663+
}
664+
}

store/versioned_store_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/canopy-network/canopy/lib"
10+
"github.com/cockroachdb/pebble/v2/sstable"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -65,3 +66,24 @@ func TestMakeVersionedKeyConcurrent(t *testing.T) {
6566
require.False(t, failed.Load(), "concurrent makeVersionedKey produced malformed keys")
6667
}
6768

69+
func TestGetCompressionProfile(t *testing.T) {
70+
tests := []struct {
71+
input string
72+
expected *sstable.CompressionProfile
73+
}{
74+
{"zstd", sstable.ZstdCompression},
75+
{"ZSTD", sstable.ZstdCompression},
76+
{"snappy", sstable.SnappyCompression},
77+
{"fastest", sstable.FastestCompression},
78+
{"balanced", sstable.BalancedCompression},
79+
{"good", sstable.GoodCompression},
80+
{"noCompression", sstable.NoCompression},
81+
{"unknown", sstable.ZstdCompression},
82+
{"", sstable.ZstdCompression},
83+
}
84+
for _, tt := range tests {
85+
t.Run(tt.input, func(t *testing.T) {
86+
require.Equal(t, tt.expected, getCompressionProfile(tt.input))
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)