Skip to content

Commit

Permalink
Merge pull request #36 from GGP1/fix_file_rename
Browse files Browse the repository at this point in the history
Use compression if the content is not empty
  • Loading branch information
GGP1 authored Jul 28, 2024
2 parents e222fa3 + 4240bd1 commit 76c5411
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
5 changes: 0 additions & 5 deletions commands/file/mv/mv_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mv

import (
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -75,10 +74,6 @@ func TestMvFileIntoDir(t *testing.T) {
}

func TestMvErrors(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("For some strange reason this test fails in unix systems because the database file throws \"bad file descriptor\"")
}

db := cmdutil.SetContext(t)
createFile(t, db, "exists")
dir := "dir/"
Expand Down
8 changes: 8 additions & 0 deletions db/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func Rename(db *bolt.DB, oldName, newName string) error {
}

func compress(content []byte) ([]byte, error) {
if len(content) == 0 {
return nil, nil
}

var gzipBuf bytes.Buffer
gw := gzip.NewWriter(&gzipBuf)

Expand All @@ -145,6 +149,10 @@ func compress(content []byte) ([]byte, error) {
}

func decompress(content []byte) ([]byte, error) {
if len(content) == 0 {
return nil, nil
}

compressed := bytes.NewBuffer(content)
gr, err := gzip.NewReader(compressed)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions db/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,38 @@ func TestKeyError(t *testing.T) {
assert.Error(t, err)
}

func TestCompression(t *testing.T) {
content := []byte{1, 2, 3}

compressed, err := compress(content)
assert.NoError(t, err)

assert.NotEqual(t, content, compressed)

decompressed, err := decompress(compressed)
assert.NoError(t, err)

assert.Equal(t, content, decompressed)
}

func TestCompressNil(t *testing.T) {
var content []byte

compressed, err := compress(content)
assert.NoError(t, err)

assert.Equal(t, content, compressed)
}

func TestDecompressNil(t *testing.T) {
var content []byte

decompressed, err := decompress(content)
assert.NoError(t, err)

assert.Equal(t, content, decompressed)
}

func setContext(t testing.TB) *bolt.DB {
return dbutil.SetContext(t, bucket.File.GetName())
}

0 comments on commit 76c5411

Please sign in to comment.