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:
parent
06a429ae4c
commit
8ab9e96ef6
@ -1,5 +1,8 @@
|
||||
package zkv
|
||||
|
||||
import "github.com/klauspost/compress/zstd"
|
||||
|
||||
var defaultOptions = Options{
|
||||
MaxParallelReads: 64,
|
||||
CompressionLevel: zstd.SpeedDefault,
|
||||
}
|
||||
|
12
options.go
12
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
|
||||
}
|
||||
}
|
||||
|
6
zkv.go
6
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()
|
||||
|
Loading…
Reference in New Issue
Block a user