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,56 @@
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",
"mysql_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/archive/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mysql.go",
"redis.go",
],
importpath = "go-common/app/service/main/archive/dao/videoshot",
tags = ["automanaged"],
deps = [
"//app/service/main/archive/conf:go_default_library",
"//app/service/main/archive/model/videoshot:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/stat/prom: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,106 @@
package videoshot
import (
"context"
"go-common/library/cache/redis"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/stat/prom"
"go-common/app/service/main/archive/conf"
"go-common/app/service/main/archive/model/videoshot"
)
// Dao is videoshot dao.
type Dao struct {
// mysql
db *sql.DB
dbRead *sql.DB
getStmt *sql.Stmt
inStmt *sql.Stmt
// redis
rds *redis.Pool
// prom
infoProm *prom.Prom
// chan
cacheCh chan func()
}
// New new a videoshot dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: sql.NewMySQL(c.DB.Arc),
dbRead: sql.NewMySQL(c.DB.ArcRead),
rds: redis.NewPool(c.Redis.Archive.Config),
cacheCh: make(chan func(), 1024),
}
d.getStmt = d.dbRead.Prepared(_getSQL)
d.inStmt = d.db.Prepared(_inSQL)
d.infoProm = prom.BusinessInfoCount
go d.cacheproc()
return d
}
// Videoshot get videoshot.
func (d *Dao) Videoshot(c context.Context, cid int64) (v *videoshot.Videoshot, err error) {
var count, ver int
if count, ver, err = d.cache(c, cid); err != nil {
log.Error("d.cache(%d) error(%v)", cid, err)
err = nil // NOTE: ignore error use db
}
if count != 0 {
v = &videoshot.Videoshot{Cid: cid, Count: count}
v.SetVersion(ver)
return
}
if v, err = d.videoshot(c, cid); err != nil || v == nil {
log.Warn("d.videoshot(%d) error(%v) or v==nil", cid, err)
return
}
d.cacheCh <- func() {
d.addCache(context.TODO(), v.Cid, v.Version(), v.Count)
}
return
}
// AddVideoshot add videoshot.
func (d *Dao) AddVideoshot(c context.Context, v *videoshot.Videoshot) (err error) {
if _, err = d.addVideoshot(c, v); err != nil {
log.Error("d.addVideoshot(%v) error(%v)", v, err)
return
}
d.cacheCh <- func() {
d.addCache(context.TODO(), v.Cid, v.Version(), v.Count)
}
return
}
// Close close resource.
func (d *Dao) Close() {
if d.rds != nil {
d.rds.Close()
}
if d.db != nil {
d.db.Close()
}
close(d.cacheCh)
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
conn := d.rds.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
func (d *Dao) cacheproc() {
for {
f, ok := <-d.cacheCh
if !ok {
return
}
f()
}
}

View File

@@ -0,0 +1,33 @@
package videoshot
import (
"flag"
"go-common/app/service/main/archive/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.archive-service")
flag.Set("conf_token", "Y2LJhIsHx87nJaOBSxuG5TeZoLdBFlrE")
flag.Set("tree_id", "2302")
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,41 @@
package videoshot
import (
"context"
"database/sql"
"go-common/app/service/main/archive/model/videoshot"
"go-common/library/log"
)
const (
_inSQL = "INSERT INTO archive_video_shot (id,count,ctime,mtime) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE count=?,mtime=? "
_getSQL = "SELECT id,count,ctime,mtime FROM archive_video_shot WHERE id=?"
)
// videoshot get a videoshot by id.
func (d *Dao) videoshot(c context.Context, cid int64) (shot *videoshot.Videoshot, err error) {
d.infoProm.Incr("videoshot")
row := d.getStmt.QueryRow(c, cid)
shot = &videoshot.Videoshot{}
if err = row.Scan(&shot.Cid, &shot.Count, &shot.CTime, &shot.MTime); err != nil {
if err == sql.ErrNoRows {
shot = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
}
return
}
// addVideoshot add a videoshot into mysql.
func (d *Dao) addVideoshot(c context.Context, shot *videoshot.Videoshot) (cid int64, err error) {
res, err := d.inStmt.Exec(c, shot.Cid, shot.Count, shot.CTime, shot.MTime, shot.Count, shot.MTime)
if err != nil {
log.Error("inStmt.Exec error(%v)", err)
return
}
cid, err = res.LastInsertId()
return
}

View File

@@ -0,0 +1,21 @@
package videoshot
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestVideoshotvideoshot(t *testing.T) {
var (
c = context.TODO()
cid = int64(10097272)
)
convey.Convey("videoshot", t, func(ctx convey.C) {
_, err := d.videoshot(c, cid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,55 @@
package videoshot
import (
"context"
"fmt"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
// _hashKeyNum the max count of hash key
_hashKeyNum = int64(100000)
_keyPrefix = "vs_"
)
func hashKey(cid int64) string {
return fmt.Sprintf("%s%d", _keyPrefix, cid%_hashKeyNum)
}
// cache get videoshot's count by id.
func (d *Dao) cache(c context.Context, cid int64) (count, ver int, err error) {
var (
key = hashKey(cid)
conn = d.rds.Get(c)
out int64
)
defer conn.Close()
if out, err = redis.Int64(conn.Do("HGET", key, cid)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("HGET(%s, %d) error(%v)", key, cid, err)
}
return
}
ver = int(out >> 32)
count = int(int32(out))
return
}
// addCache set videoshot info into redis.
func (d *Dao) addCache(c context.Context, cid int64, ver, count int) (err error) {
var (
key = hashKey(cid)
conn = d.rds.Get(c)
in int64
)
in = int64(ver)<<32 | int64(count)
defer conn.Close()
if _, err = conn.Do("HSET", key, cid, in); err != nil {
log.Error("HSET(%s, %d, %d)", key, cid, count, err)
}
return
}

View File

@@ -0,0 +1,50 @@
package videoshot
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestVideoshothashKey(t *testing.T) {
var (
cid = int64(1)
)
convey.Convey("hashKey", t, func(ctx convey.C) {
p1 := hashKey(cid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestVideoshotcache(t *testing.T) {
var (
c = context.TODO()
cid = int64(1)
)
convey.Convey("cache", t, func(ctx convey.C) {
count, ver, err := d.cache(c, cid)
ctx.Convey("Then err should be nil.count,ver should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ver, convey.ShouldNotBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestVideoshotaddCache(t *testing.T) {
var (
c = context.TODO()
cid = int64(1)
ver = int(0)
count = int(0)
)
convey.Convey("addCache", t, func(ctx convey.C) {
err := d.addCache(c, cid, ver, count)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}