Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"seq_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/seq-server/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"seq.go",
],
importpath = "go-common/app/service/main/seq-server/dao",
tags = ["automanaged"],
deps = [
"//app/service/main/seq-server/conf:go_default_library",
"//app/service/main/seq-server/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,39 @@
package dao
import (
"context"
"go-common/app/service/main/seq-server/conf"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao is seq-server dao.
type Dao struct {
c *conf.Config
// db
db *sql.DB
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// db
db: sql.NewMySQL(c.DB.Number),
}
return
}
// Ping ping db
func (d *Dao) Ping() (err error) {
if err = d.db.Ping(context.TODO()); err != nil {
log.Error("d.db.Ping error(%v)", err)
}
return
}
// Close close resource.
func (d *Dao) Close() {
d.db.Close()
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/service/main/seq-server/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.common-arch.seq-server")
flag.Set("conf_token", "3d6e1df5016b46baef2db5dd70add25e")
flag.Set("tree_id", "5986")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,72 @@
package dao
import (
"context"
"database/sql"
"time"
"go-common/app/service/main/seq-server/model"
"go-common/library/log"
)
const (
_allSQL = "SELECT id,max_seq,step,token,perch,ctime,mtime FROM business"
_maxSeqSQL = "SELECT max_seq FROM business WHERE id=?"
_upMaxSeqSQL = "UPDATE business SET max_seq=? WHERE id=? AND max_seq=?"
_upMaxSeqTokenSQL = "UPDATE business SET max_seq=?,step=? WHERE id=? AND token=?"
)
// All get all seq
func (d *Dao) All(c context.Context) (bs map[int64]*model.Business, err error) {
rows, err := d.db.Query(c, _allSQL)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _allSQL, err)
return
}
bs = make(map[int64]*model.Business)
defer rows.Close()
for rows.Next() {
b := new(model.Business)
if err = rows.Scan(&b.ID, &b.MaxSeq, &b.Step, &b.Token, &b.Perch, &b.CTime, &b.MTime); err != nil {
log.Error("rows.Scan error(%v)")
return
}
b.BenchTime = b.CTime.Time().UnixNano() / int64(time.Millisecond)
b.LastTimestamp = time.Now().UnixNano() / int64(time.Millisecond)
bs[b.ID] = b
}
return
}
// MaxSeq return current max seq.
func (d *Dao) MaxSeq(c context.Context, businessID int64) (maxSeq int64, err error) {
row := d.db.QueryRow(c, _maxSeqSQL, businessID)
if err = row.Scan(&maxSeq); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// UpMaxSeq update max seq by businessID.
func (d *Dao) UpMaxSeq(c context.Context, businessID, maxSeq, lastSeq int64) (rows int64, err error) {
res, err := d.db.Exec(c, _upMaxSeqSQL, maxSeq, businessID, lastSeq)
if err != nil {
log.Error("d.db.Exec(%s, %d, %d) error(%v)", _upMaxSeqSQL, maxSeq, businessID)
return
}
return res.RowsAffected()
}
// UpMaxSeqToken update max seq by businessID and token.
func (d *Dao) UpMaxSeqToken(c context.Context, businessID, maxSeq, step int64, token string) (rows int64, err error) {
res, err := d.db.Exec(c, _upMaxSeqTokenSQL, maxSeq, step, businessID, token)
if err != nil {
log.Error("d.db.Exec(%s, %d, %d) error(%v)", _upMaxSeqSQL, maxSeq, businessID)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,68 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAll(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("All", t, func(ctx convey.C) {
bs, err := d.All(c)
ctx.Convey("Then err should be nil.bs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bs, convey.ShouldNotBeNil)
})
})
}
func TestDaoMaxSeq(t *testing.T) {
var (
c = context.TODO()
businessID = int64(2)
)
convey.Convey("MaxSeq", t, func(ctx convey.C) {
maxSeq, err := d.MaxSeq(c, businessID)
ctx.Convey("Then err should be nil.maxSeq should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(maxSeq, convey.ShouldNotBeNil)
})
})
}
func TestDaoUpMaxSeq(t *testing.T) {
var (
c = context.TODO()
businessID = int64(1)
maxSeq = int64(10)
lastSeq = int64(2)
)
convey.Convey("UpMaxSeq", t, func(ctx convey.C) {
rows, err := d.UpMaxSeq(c, businessID, maxSeq, lastSeq)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestDaoUpMaxSeqToken(t *testing.T) {
var (
c = context.TODO()
businessID = int64(1)
maxSeq = int64(10)
step = int64(2)
token = ""
)
convey.Convey("UpMaxSeqToken", t, func(ctx convey.C) {
rows, err := d.UpMaxSeqToken(c, businessID, maxSeq, step, token)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}