diff --git a/options.go b/options.go index c88e298bc..098343a19 100644 --- a/options.go +++ b/options.go @@ -21,6 +21,7 @@ import ( "github.com/dgraph-io/badger/options" "github.com/dgraph-io/badger/table" + "github.com/dgraph-io/badger/y" ) // Note: If you add a new option X make sure you also add a WithX method on Options. @@ -98,6 +99,11 @@ type Options struct { // DefaultOptions sets a list of recommended options for good performance. // Feel free to modify these to suit your needs with the WithX methods. func DefaultOptions(path string) Options { + defaultCompression := options.ZSTD + // Use snappy as default compression algorithm if badger is built without CGO. + if !y.CgoEnabled { + defaultCompression = options.Snappy + } return Options{ Dir: path, ValueDir: path, @@ -120,7 +126,7 @@ func DefaultOptions(path string) Options { CompactL0OnClose: true, KeepL0InMemory: true, VerifyValueChecksum: false, - Compression: options.ZSTD, + Compression: defaultCompression, MaxCacheSize: 1 << 30, // 1 GB // Nothing to read/write value log using standard File I/O // MemoryMap to mmap() the value log files diff --git a/table/builder.go b/table/builder.go index bd22e4318..134d58f3a 100644 --- a/table/builder.go +++ b/table/builder.go @@ -22,7 +22,6 @@ import ( "math" "unsafe" - "github.com/DataDog/zstd" "github.com/dgryski/go-farm" "github.com/golang/protobuf/proto" "github.com/golang/snappy" @@ -347,7 +346,7 @@ func (b *Builder) compressData(data []byte) ([]byte, error) { case options.Snappy: return snappy.Encode(nil, data), nil case options.ZSTD: - return zstd.Compress(nil, data) + return y.ZSTDCompress(nil, data) } return nil, errors.New("Unsupported compression type") } diff --git a/table/table.go b/table/table.go index bb1861905..691332e46 100644 --- a/table/table.go +++ b/table/table.go @@ -30,7 +30,6 @@ import ( "sync/atomic" "unsafe" - "github.com/DataDog/zstd" "github.com/golang/protobuf/proto" "github.com/golang/snappy" "github.com/pkg/errors" @@ -539,7 +538,7 @@ func (t *Table) decompressData(data []byte) ([]byte, error) { case options.Snappy: return snappy.Decode(nil, data) case options.ZSTD: - return zstd.Decompress(nil, data) + return y.ZSTDDecompress(nil, data) } return nil, errors.New("Unsupported compression type") } diff --git a/y/zstd_cgo.go b/y/zstd_cgo.go new file mode 100644 index 000000000..d0ec119ca --- /dev/null +++ b/y/zstd_cgo.go @@ -0,0 +1,36 @@ +// +build cgo + +/* + * Copyright 2019 Dgraph Labs, Inc. and Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package y + +import ( + "github.com/DataDog/zstd" +) + +// CgoEnabled is used to check if CGO is enabled while building badger. +const CgoEnabled = true + +// ZSTDDecompress decompresses a block using ZSTD algorithm. +func ZSTDDecompress(dst, src []byte) ([]byte, error) { + return zstd.Decompress(dst, src) +} + +// ZSTDCompress compresses a block using ZSTD algorithm. +func ZSTDCompress(dst, src []byte) ([]byte, error) { + return zstd.Compress(dst, src) +} diff --git a/y/zstd_nocgo.go b/y/zstd_nocgo.go new file mode 100644 index 000000000..e79d51d55 --- /dev/null +++ b/y/zstd_nocgo.go @@ -0,0 +1,38 @@ +// +build !cgo + +/* + * Copyright 2019 Dgraph Labs, Inc. and Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package y + +import ( + "errors" +) + +var errZstdCgo = errors.New("zstd compression requires building badger with cgo enabled") + +// CgoEnabled is used to check if CGO is enabled while building badger. +const CgoEnabled = false + +// ZSTDDecompress decompresses a block using ZSTD algorithm. +func ZSTDDecompress(dst, src []byte) ([]byte, error) { + return nil, errZstdCgo +} + +// ZSTDCompress compresses a block using ZSTD algorithm. +func ZSTDCompress(dst, src []byte) ([]byte, error) { + return nil, errZstdCgo +}