Optimize marshalling for jsonb types (#19898)

We check for the presence of binary_parameters
in the DSN and add the 0x01 byte accordingly.

This helps us avoid casting to string
and efficiently use the database.

```release-note
NONE
```

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Agniva De Sarker
2022-04-06 15:01:32 +05:30
коммит произвёл GitHub
родитель ec91ca46ff
Коммит 2e027ae927
8 изменённых файлов: 136 добавлений и 11 удалений

Просмотреть файл

@@ -9,6 +9,7 @@ import (
dbsql "database/sql"
"fmt"
"log"
"net/url"
"path/filepath"
"strconv"
"strings"
@@ -128,6 +129,8 @@ type SqlStore struct {
license *model.License
licenseMutex sync.RWMutex
metrics einterfaces.MetricsInterface
isBinaryParam bool
}
func New(settings model.SqlSettings, metrics einterfaces.MetricsInterface) *SqlStore {
@@ -160,6 +163,11 @@ func New(settings model.SqlSettings, metrics einterfaces.MetricsInterface) *SqlS
mlog.Fatal("Failed to apply database migrations.", mlog.Err(err))
}
store.isBinaryParam, err = store.computeBinaryParam()
if err != nil {
mlog.Fatal("Failed to compute binary param", mlog.Err(err))
}
store.stores.team = newSqlTeamStore(store)
store.stores.channel = newSqlChannelStore(store, metrics)
store.stores.post = newSqlPostStore(store, metrics)
@@ -323,6 +331,29 @@ func (ss *SqlStore) DriverName() string {
return *ss.settings.DriverName
}
// computeBinaryParam returns whether the data source uses binary_parameters
// when using Postgres
func (ss *SqlStore) computeBinaryParam() (bool, error) {
if ss.DriverName() != model.DatabaseDriverPostgres {
return false, nil
}
url, err := url.Parse(*ss.settings.DataSource)
if err != nil {
return false, err
}
return url.Query().Get("binary_parameters") == "yes", nil
}
func (ss *SqlStore) IsBinaryParamEnabled() bool {
return ss.isBinaryParam
}
// AppendBinaryFlag updates the byte slice to work using binary_parameters=yes.
func (ss *SqlStore) AppendBinaryFlag(buf []byte) []byte {
return append([]byte{0x01}, buf...)
}
func (ss *SqlStore) getCurrentSchemaVersion() (string, error) {
var version string
err := ss.GetMasterX().Get(&version, "SELECT Value FROM Systems WHERE Name='Version'")