Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build flags to make CGO optional #1094

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
jarifibrahim marked this conversation as resolved.
Show resolved Hide resolved
// Use snappy as default compression algorithm if badger is built without CGO.
if !y.CgoEnabled {
defaultCompression = options.Snappy
}
return Options{
Dir: path,
ValueDir: path,
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions table/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
3 changes: 1 addition & 2 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
36 changes: 36 additions & 0 deletions y/zstd_cgo.go
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 38 additions & 0 deletions y/zstd_nocgo.go
Original file line number Diff line number Diff line change
@@ -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
}