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

Compress file content with zstd #46

Merged
merged 11 commits into from
Jun 7, 2023
Merged

Compress file content with zstd #46

merged 11 commits into from
Jun 7, 2023

Conversation

kshuta
Copy link
Contributor

@kshuta kshuta commented May 22, 2023

@robherley
This is a draft pull request that implements file content setter/getter as discussed in #6. I was hoping to get feedback on the core compression and decompression implementations to make sure they align with what you had in mind!

If the implementations are something you had in mind, I'll go ahead and swap file.Content statements with getters and setters.

Copy link
Owner

@robherley robherley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 overall looks good, just a few small comments!

thanks again for working on this 💪

internal/snips/file.go Outdated Show resolved Hide resolved
internal/snips/file.go Outdated Show resolved Hide resolved
internal/snips/file.go Outdated Show resolved Hide resolved
kshuta and others added 4 commits May 21, 2023 22:48
Co-authored-by: Rob Herley <robherley@github.com>
Co-authored-by: Rob Herley <robherley@github.com>
@kshuta
Copy link
Contributor Author

kshuta commented May 25, 2023

@robherley

Was hoping if you could take a look at the additional setters and getters: SetRawContent() and GetRawContent(). They are exclusively used in db_sqlite.go to pass the raw content to and from the database.

When storing the file content to the db we need access to the compressed file content, but File.GetContent() would return the uncompressed file content. Similarly, we need to pass the content from the database to the File struct, but SetRawContent() may compress the already compressed file. We can deal with this by always setting the compress argument to false in the db file, but I thought since we already have GetRawContent() from the problem before, might as well add SetRawContent().

I feel like there is a better way to handle this, and I would appreciate it if you could give me any advice!

Copy link
Owner

@robherley robherley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just making a public RawContent field is fine, makes usage in DB pkg a bit easier, left some comments!

Lemme know if you need anything clarified 👍

docs/self-hosting.md Outdated Show resolved Hide resolved
internal/config/config.go Outdated Show resolved Hide resolved
docs/self-hosting.md Outdated Show resolved Hide resolved
internal/snips/file.go Outdated Show resolved Hide resolved
internal/ssh/handler.go Outdated Show resolved Hide resolved
@kshuta kshuta marked this pull request as ready for review May 26, 2023 04:06
Copy link
Owner

@robherley robherley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a few minor things then we can ship it 🚀

internal/snips/file_test.go Outdated Show resolved Hide resolved
internal/ssh/handler.go Outdated Show resolved Hide resolved
internal/ssh/handler.go Show resolved Hide resolved
in: []byte("Hello World"),
want: map[string]any{
"getContent": []byte("Hello World"),
"rawContent": []byte{40, 181, 47, 253, 4, 0, 89, 0, 0, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 194, 91, 36, 25},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raw bytes were acquired from the following script:

package main

import (
	"fmt"

	"github.com/klauspost/compress/zstd"
)

func main() {
	encoder, err := zstd.NewWriter(nil)
	if err != nil {
		panic(err)
	}

	target := []byte("Hello World")

	rawContent := encoder.EncodeAll(target, nil)
	for _, val := range rawContent {
		fmt.Printf("%d, ", val)
	}

}

@kshuta
Copy link
Contributor Author

kshuta commented Jun 6, 2023

@robherley Ready for review!

Copy link
Owner

@robherley robherley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one last super tiny fix and then we can merge

if err != nil {
sesh.Error(err, "Unable to download file", "There was an error downloading the file: %q", file.ID)
} else {
wish.Print(sesh, content)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
wish.Print(sesh, content)
wish.Print(sesh, string(content))

found this when testing locally 😉 it was printing out the byte slice haha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry! was caught up in the unit tests :(

@robherley robherley merged commit 140b28f into robherley:main Jun 7, 2023
@kshuta kshuta deleted the 6_zstd branch June 8, 2023 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants