-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimization: change data fields to bytes (#215)
* Optimization: change data fields to bytes * Fix: tests * Use brotli for compressing message data * Decode pub key for reducing message size * Descrease profile sample rate * Rebased
- Loading branch information
1 parent
8770c25
commit 2c5e233
Showing
15 changed files
with
99 additions
and
139 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,53 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"database/sql" | ||
"database/sql/driver" | ||
|
||
"github.com/andybalholm/brotli" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var json = jsoniter.ConfigCompatibleWithStandardLibrary | ||
|
||
type PackedBytes map[string]any | ||
|
||
var _ sql.Scanner = (*PackedBytes)(nil) | ||
|
||
func (pb *PackedBytes) Scan(src interface{}) error { | ||
if src == nil { | ||
return nil | ||
} | ||
b, ok := src.([]byte) | ||
if !ok { | ||
return errors.Errorf("invalid packed bytes type: %T", src) | ||
} | ||
|
||
result := bytes.NewBuffer(b) | ||
return json.NewDecoder(brotli.NewReader(result)).Decode(pb) | ||
} | ||
|
||
var _ driver.Valuer = (*PackedBytes)(nil) | ||
|
||
func (pb PackedBytes) Value() (driver.Value, error) { | ||
return pb.ToBytes() | ||
} | ||
|
||
func (pb PackedBytes) ToBytes() ([]byte, error) { | ||
b, err := json.Marshal(pb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := bytes.NewBuffer(nil) | ||
writer := brotli.NewWriterLevel(result, brotli.BestSpeed) | ||
|
||
if _, err := writer.Write(b); err != nil { | ||
return nil, err | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, err | ||
} | ||
return result.Bytes(), 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.