From 8ab9e96ef662addb87522840fb1d2481bac3a754 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sat, 3 Dec 2022 12:55:42 +0500 Subject: [PATCH] Add compression level option --- defaults.go | 3 +++ options.go | 12 ++++++++++-- zkv.go | 6 ++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/defaults.go b/defaults.go index 2e3437f..959736c 100644 --- a/defaults.go +++ b/defaults.go @@ -1,5 +1,8 @@ package zkv +import "github.com/klauspost/compress/zstd" + var defaultOptions = Options{ MaxParallelReads: 64, + CompressionLevel: zstd.SpeedDefault, } diff --git a/options.go b/options.go index 7105683..dbbd27c 100644 --- a/options.go +++ b/options.go @@ -1,13 +1,21 @@ package zkv +import "github.com/klauspost/compress/zstd" + type Options struct { + // Maximum number of concurrent reads MaxParallelReads uint + + // Compression level + CompressionLevel zstd.EncoderLevel } -func (o *Options) Validate() error { +func (o *Options) setDefaults() { if o.MaxParallelReads == 0 { o.MaxParallelReads = defaultOptions.MaxParallelReads } - return nil + if o.CompressionLevel == 0 { + o.CompressionLevel = defaultOptions.CompressionLevel + } } diff --git a/zkv.go b/zkv.go index f12364c..1dbc52b 100644 --- a/zkv.go +++ b/zkv.go @@ -110,16 +110,14 @@ func (db *Database) Get(key, value interface{}) 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) if err != nil { f.Close() return nil, fmt.Errorf("ошибка при открытии файла для записи: %v", err) } - if options.Validate() != nil { - return nil, err - } - compressor, err := zstd.NewWriter(f) if err != nil { f.Close()