1
0
mirror of https://github.com/nxshock/zkv.git synced 2024-11-27 11:21:02 +05:00

Add compression level option

This commit is contained in:
nxshock 2022-12-03 12:55:42 +05:00
parent 06a429ae4c
commit 8ab9e96ef6
3 changed files with 15 additions and 6 deletions

View File

@ -1,5 +1,8 @@
package zkv package zkv
import "github.com/klauspost/compress/zstd"
var defaultOptions = Options{ var defaultOptions = Options{
MaxParallelReads: 64, MaxParallelReads: 64,
CompressionLevel: zstd.SpeedDefault,
} }

View File

@ -1,13 +1,21 @@
package zkv package zkv
import "github.com/klauspost/compress/zstd"
type Options struct { type Options struct {
// Maximum number of concurrent reads
MaxParallelReads uint MaxParallelReads uint
// Compression level
CompressionLevel zstd.EncoderLevel
} }
func (o *Options) Validate() error { func (o *Options) setDefaults() {
if o.MaxParallelReads == 0 { if o.MaxParallelReads == 0 {
o.MaxParallelReads = defaultOptions.MaxParallelReads o.MaxParallelReads = defaultOptions.MaxParallelReads
} }
return nil if o.CompressionLevel == 0 {
o.CompressionLevel = defaultOptions.CompressionLevel
}
} }

6
zkv.go
View File

@ -110,16 +110,14 @@ func (db *Database) Get(key, value interface{}) error {
} }
func OpenWithOptions(filePath string, options Options) (*Database, error) { func OpenWithOptions(filePath string, options Options) (*Database, error) {
options.setDefaults()
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
f.Close() f.Close()
return nil, fmt.Errorf("ошибка при открытии файла для записи: %v", err) return nil, fmt.Errorf("ошибка при открытии файла для записи: %v", err)
} }
if options.Validate() != nil {
return nil, err
}
compressor, err := zstd.NewWriter(f) compressor, err := zstd.NewWriter(f)
if err != nil { if err != nil {
f.Close() f.Close()