Skip to content

Commit

Permalink
Add new compression code
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Aug 15, 2024
1 parent 9b771cb commit a7d5f60
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 43 additions & 2 deletions db/savedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"encoding/gob"

"github.com/klauspost/compress/zstd"

Check failure on line 24 in db/savedata.go

View workflow job for this annotation

GitHub Actions / Build (linux)

no required module provides package github.com/klauspost/compress/zstd; to add it:

Check failure on line 24 in db/savedata.go

View workflow job for this annotation

GitHub Actions / Build (windows)

no required module provides package github.com/klauspost/compress/zstd; to add it:
"github.com/pagefaultgames/rogueserver/defs"
)

Expand Down Expand Up @@ -60,6 +61,19 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
return system, err
}

dec, err := zstd.NewReader(nil)
if err != nil {
return system, err
}

defer dec.Close()

decompressed, err := dec.DecodeAll(data, nil)
if err == nil {
// replace if it worked, otherwise use the original data
data = decompressed
}

err = gob.NewDecoder(bytes.NewReader(data)).Decode(&system)
if err != nil {
return system, err
Expand All @@ -75,7 +89,14 @@ func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
return err
}

_, err = handle.Exec("INSERT INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, buf.Bytes(), buf.Bytes())
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if err != nil {
return err
}

defer enc.Close()

_, err = handle.Exec("REPLACE INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP())", uuid, enc.EncodeAll(buf.Bytes(), nil))
if err != nil {
return err
}
Expand All @@ -101,6 +122,19 @@ func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) {
return session, err
}

dec, err := zstd.NewReader(nil)
if err != nil {
return session, err
}

defer dec.Close()

decompressed, err := dec.DecodeAll(data, nil)
if err == nil {
// replace if it worked, otherwise use the original data
data = decompressed
}

err = gob.NewDecoder(bytes.NewReader(data)).Decode(&session)
if err != nil {
return session, err
Expand All @@ -126,7 +160,14 @@ func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) erro
return err
}

_, err = handle.Exec("INSERT INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, slot, buf.Bytes(), buf.Bytes())
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if err != nil {
return err
}

defer enc.Close()

_, err = handle.Exec("REPLACE INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, slot, enc.EncodeAll(buf.Bytes(), nil))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
Expand Down

0 comments on commit a7d5f60

Please # to comment.