Skip to content

Commit

Permalink
Merge pull request #927 from oom-ai/refactor/remove-cgo
Browse files Browse the repository at this point in the history
Refactor/remove cgo
  • Loading branch information
lianxmfor authored Jan 7, 2022
2 parents 1900471 + 9f7e1a3 commit c008737
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 28 deletions.
13 changes: 6 additions & 7 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ builds:
id: "oomcli"
binary: oomcli
env:
- CGO_ENABLED=1
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64
ldflags:
- >
-s -w
Expand All @@ -23,22 +25,19 @@ builds:
id: "oomagent"
binary: oomagent
env:
- CGO_ENABLED=1
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64
ldflags:
- >
-s -w
-X github.com/oom-ai/oomstore/version.Version={{.Version}}
-X github.com/oom-ai/oomstore/version.Commit={{.Commit}}
-X github.com/oom-ai/oomstore/version.Built={{.CommitDate}}
archives:
- replacements:
darwin: Darwin
linux: Linux
amd64: x86_64
checksum:
name_template: "checksums.txt"
snapshot:
Expand Down
20 changes: 17 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ require (
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.4
github.com/mattn/go-sqlite3 v1.14.9
github.com/mitchellh/mapstructure v1.4.3
github.com/olekukonko/tablewriter v0.0.5
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee
github.com/pkg/errors v0.9.1
github.com/snowflakedb/gosnowflake v1.6.5
github.com/spf13/cast v1.4.1
github.com/spf13/cobra v1.3.0
Expand All @@ -33,6 +34,7 @@ require (
google.golang.org/grpc v1.43.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
modernc.org/sqlite v1.14.3
)

require (
Expand Down Expand Up @@ -82,23 +84,24 @@ require (
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pierrec/lz4/v4 v4.1.12 // indirect
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3 // indirect
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00 // indirect
github.com/pingcap/kvproto v0.0.0-20211229082925-7a8280c36daf // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -108,13 +111,24 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
lukechampine.com/uint128 v1.1.1 // indirect
modernc.org/cc/v3 v3.35.18 // indirect
modernc.org/ccgo/v3 v3.12.95 // indirect
modernc.org/libc v1.11.104 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.0.5 // indirect
modernc.org/opt v0.1.1 // indirect
modernc.org/strutil v1.1.1 // indirect
modernc.org/token v1.0.0 // indirect
)
131 changes: 131 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions internal/database/dbutil/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"github.com/jackc/pgerrcode"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/mattn/go-sqlite3"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
"github.com/pkg/errors"
"github.com/snowflakedb/gosnowflake"
"google.golang.org/api/googleapi"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"
)

func OpenSQLite(dbFile string) (*sqlx.DB, error) {
db, err := sqlx.Open("sqlite3", dbFile)
db, err := sqlx.Open("sqlite", dbFile)
return db, errors.WithStack(err)
}

Expand Down Expand Up @@ -59,10 +60,9 @@ func DeserializeString(i interface{}, backend types.BackendType) string {
func IsTableNotFoundError(err error, backend types.BackendType) bool {
err = errors.Cause(err)
switch backend {
// https://github.com/mattn/go-sqlite3/issues/244
case types.BackendSQLite:
if sqliteErr, ok := err.(sqlite3.Error); ok {
return sqliteErr.Code == sqlite3.ErrError
if sqliteErr, ok := err.(*sqlite.Error); ok {
return sqliteErr.Code() == sqlite3.SQLITE_CORE
}

// https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_no_such_table
Expand Down
8 changes: 5 additions & 3 deletions internal/database/metadata/sqlite/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sqlite
import (
"context"

"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"

"github.com/oom-ai/oomstore/internal/database/metadata"
)
Expand All @@ -13,11 +14,12 @@ func createEntity(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadat
query := "INSERT INTO entity(name, description) VALUES(?, ?)"
res, err := sqlxCtx.ExecContext(ctx, query, opt.EntityName, opt.Description)
if err != nil {
if er, ok := err.(sqlite3.Error); ok {
if er.ExtendedCode == sqlite3.ErrConstraintUnique {
if er, ok := err.(*sqlite.Error); ok {
if er.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
return 0, errors.Errorf("entity %s already exists", opt.EntityName)
}
}

return 0, err
}

Expand Down
7 changes: 4 additions & 3 deletions internal/database/metadata/sqlite/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sqlite
import (
"context"

"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"

"github.com/oom-ai/oomstore/internal/database/metadata"
)
Expand All @@ -16,8 +17,8 @@ func createFeature(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metada
query := "INSERT INTO feature(name, full_name, group_id, value_type, description) VALUES (?, ?, ?, ?, ?)"
res, err := sqlxCtx.ExecContext(ctx, sqlxCtx.Rebind(query), opt.FeatureName, opt.FullName, opt.GroupID, opt.ValueType, opt.Description)
if err != nil {
if sqliteErr, ok := err.(sqlite3.Error); ok {
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
if sqliteErr, ok := err.(*sqlite.Error); ok {
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
return 0, errors.Errorf("feature %s already exists", opt.FeatureName)
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/database/metadata/sqlite/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"

"github.com/pkg/errors"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"

"github.com/mattn/go-sqlite3"
"github.com/oom-ai/oomstore/internal/database/metadata"
"github.com/oom-ai/oomstore/pkg/errdefs"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
Expand All @@ -19,8 +20,8 @@ func createGroup(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata
query := "INSERT INTO feature_group(name, entity_id, category, description) VALUES(?, ?, ?, ?)"
res, err := sqlxCtx.ExecContext(ctx, query, opt.GroupName, opt.EntityID, opt.Category, opt.Description)
if err != nil {
if sqliteErr, ok := err.(sqlite3.Error); ok {
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
if sqliteErr, ok := err.(*sqlite.Error); ok {
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
return 0, errors.Errorf("feature group %s already exists", opt.GroupName)
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/database/metadata/sqlite/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package sqlite
import (
"context"

"github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
"modernc.org/sqlite"
sqlite3 "modernc.org/sqlite/lib"

"github.com/oom-ai/oomstore/internal/database/dbutil"
"github.com/oom-ai/oomstore/internal/database/metadata"
"github.com/pkg/errors"
)

func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, string, error) {
Expand All @@ -21,8 +23,8 @@ func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
insertQuery := "INSERT INTO feature_group_revision(group_id, revision, snapshot_table, cdc_table, anchored, description) VALUES (?, ?, ?, ?, ?, ?)"
res, err := sqlxCtx.ExecContext(ctx, sqlxCtx.Rebind(insertQuery), opt.GroupID, opt.Revision, snapshotTable, cdcTable, opt.Anchored, opt.Description)
if err != nil {
if sqliteErr, ok := err.(sqlite3.Error); ok {
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
if sqliteErr, ok := err.(*sqlite.Error); ok {
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
return 0, "", errors.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
}
}
Expand Down

0 comments on commit c008737

Please sign in to comment.