forked from nlnwa/gowarcserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nlnwa#3: Expose compression type in CLI/conf
This allows the user to set the compression type so save memory footprint at the cost of cpu cycles
- Loading branch information
Showing
8 changed files
with
219 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package compressiontype_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dgraph-io/badger/v2/options" | ||
ct "github.com/nlnwa/gowarcserver/pkg/compressiontype" | ||
) | ||
|
||
func TestCompressionType_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cType options.CompressionType | ||
expected string | ||
errorState bool | ||
}{ | ||
{ | ||
"options.None to string none", | ||
options.None, | ||
"none", | ||
false, | ||
}, | ||
{ | ||
"options.Snappy to string snappy", | ||
options.Snappy, | ||
"snappy", | ||
false, | ||
}, | ||
{ | ||
"options.ZSTD to string zstd", | ||
options.ZSTD, | ||
"zstd", | ||
false, | ||
}, | ||
{ | ||
"Illegal to error", | ||
999, | ||
"", | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ct.CompressionType(tt.cType).String() | ||
if err != nil && !tt.errorState { | ||
t.Errorf("Unexpected failure: %v", err) | ||
} else if err == nil && tt.errorState { | ||
t.Errorf("Expected failure with %v, but got '%v'", tt.cType, got) | ||
} | ||
|
||
if tt.expected != got { | ||
t.Errorf("Exptected %v got %v", tt.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCompressionType_FromString(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
stringValue string | ||
expected ct.CompressionType | ||
errorState bool | ||
}{ | ||
{ | ||
"string none to options.None", | ||
"none", | ||
ct.CompressionType(options.None), | ||
false, | ||
}, | ||
{ | ||
"string snappy to options.Snappy", | ||
"snappy", | ||
ct.CompressionType(options.Snappy), | ||
false, | ||
}, | ||
{ | ||
"string zstd to options.ZSTD", | ||
"zstd", | ||
ct.CompressionType(options.ZSTD), | ||
false, | ||
}, | ||
{ | ||
"string ZstD to options.ZSTD", | ||
"ZstD", | ||
ct.CompressionType(options.ZSTD), | ||
false, | ||
}, | ||
{ | ||
"string garbage to error", | ||
"garbage", | ||
0, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ct.FromString(tt.stringValue) | ||
if err != nil && !tt.errorState { | ||
t.Errorf("Unexpected failure: %v", err) | ||
} else if err == nil && tt.errorState { | ||
t.Errorf("Expected failure with %v, but got %v", tt.stringValue, got) | ||
} | ||
|
||
if tt.expected != got { | ||
t.Errorf("Exptected %v got %v", tt.expected, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package compressiontype | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/dgraph-io/badger/v2/options" | ||
) | ||
|
||
type CompressionType options.CompressionType | ||
|
||
// should be const, since it is internal to this file it should be ok as var... | ||
var stringRepresentation = [...]string{"none", "snappy", "zstd"} | ||
|
||
func (c CompressionType) String() (string, error) { | ||
if c != CompressionType(options.None) && c != CompressionType(options.Snappy) && c != CompressionType(options.ZSTD) { | ||
return "", fmt.Errorf("CompressionType can only have value %v, %v or %v", options.None, options.Snappy, options.ZSTD) | ||
} | ||
return stringRepresentation[c], nil | ||
} | ||
|
||
func FromString(value string) (CompressionType, error) { | ||
lowered := strings.ToLower(value) | ||
|
||
// for now we manually check for each type and return if we find it | ||
if lowered == "none" { | ||
return CompressionType(options.None), nil | ||
} else if lowered == "snappy" { | ||
return CompressionType(options.Snappy), nil | ||
} else if lowered == "zstd" { | ||
return CompressionType(options.ZSTD), nil | ||
} | ||
|
||
return CompressionType(0), fmt.Errorf("unexpected value '%v', expected any of listed: '%v'", lowered, strings.Join(stringRepresentation[:], ", ")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dbfromviper | ||
|
||
/* | ||
Usually it should be avoided to reference viper in pkg, but this functionality is used by | ||
cmd index and server, so it's an exception. | ||
*/ | ||
|
||
import ( | ||
"github.com/dgraph-io/badger/v2/options" | ||
"github.com/nlnwa/gowarcserver/pkg/compressiontype" | ||
"github.com/nlnwa/gowarcserver/pkg/index" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// TODO: test somehow? | ||
// Create a database based on the viper settings set by the user | ||
func DbFromViper() (*index.Db, error) { | ||
compressionString := viper.GetString("compression") | ||
compression, cErr := compressiontype.FromString(compressionString) | ||
if cErr != nil { | ||
return nil, cErr | ||
} | ||
|
||
dbDir := viper.GetString("indexdir") | ||
db, dbErr := index.NewIndexDb(dbDir, options.CompressionType(compression)) | ||
if dbErr != nil { | ||
return nil, dbErr | ||
} | ||
|
||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.