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,54 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["document_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/kvo/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"document.go",
"document_cache.go",
"user_conf.go",
"user_conf_cache.go",
],
importpath = "go-common/app/interface/main/kvo/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/kvo/conf:go_default_library",
"//app/interface/main/kvo/model:go_default_library",
"//library/cache/memcache: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,50 @@
package dao
import (
"context"
"time"
"go-common/app/interface/main/kvo/conf"
"go-common/library/cache/memcache"
"go-common/library/database/sql"
)
// Dao kvo data access obj with bfs
type Dao struct {
cache *memcache.Pool
mcExpire int32
// http client for bfs req
db *sql.DB
// sql stmt
getUserConf *sql.Stmt
getDocument *sql.Stmt
}
// New new data access
func New(c *conf.Config) (d *Dao) {
d = &Dao{
cache: memcache.NewPool(c.Memcache.Kvo),
mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
db: sql.NewMySQL(c.Mysql),
}
d.getUserConf = d.db.Prepared(_getUserConf)
d.getDocument = d.db.Prepared(_getDocument)
return
}
// Ping check if health
func (d *Dao) Ping(ctx context.Context) (err error) {
if err = d.pingMemcache(ctx); err != nil {
return
}
if err = d.db.Ping(ctx); err != nil {
return
}
return
}
// BeginTx begin trans
func (d *Dao) BeginTx(c context.Context) (*sql.Tx, error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"time"
"go-common/app/interface/main/kvo/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_upDocument = "INSERT INTO document(check_sum,doc,ctime,mtime) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE doc=?"
_getDocument = "SELECT check_sum,doc FROM document WHERE check_sum=?"
)
// Document get docuemtn
func (d *Dao) Document(ctx context.Context, checkSum int64) (doc *model.Document, err error) {
row := d.getDocument.QueryRow(ctx, checkSum)
doc = &model.Document{}
err = row.Scan(&doc.CheckSum, &doc.Doc)
if err != nil {
if err == sql.ErrNoRows {
doc = nil
err = nil
return
}
log.Error("row.scan err:%v", err)
}
return
}
// TxUpDocuement add a document
func (d *Dao) TxUpDocuement(ctx context.Context, tx *sql.Tx, checkSum int64, data string, now time.Time) (err error) {
_, err = tx.Exec(_upDocument, checkSum, data, now, now, data)
if err != nil {
log.Error("db.exec err:%v", err)
}
return
}

View File

@@ -0,0 +1,69 @@
package dao
import (
"context"
"encoding/json"
"fmt"
mc "go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_docKeyPrefix = "d"
)
// pingMemcache check memcache health
func (d *Dao) pingMemcache(ctx context.Context) (err error) {
conn := d.cache.Get(ctx)
item := mc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}
err = conn.Set(&item)
conn.Close()
return
}
// DocumentCache memcache get document(hash)
func (d *Dao) DocumentCache(ctx context.Context, checkSum int64) (data json.RawMessage, err error) {
var result *mc.Item
conn := d.cache.Get(ctx)
defer conn.Close()
if result, err = conn.Get(cacheDocKey(checkSum)); err != nil {
if err == mc.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", cacheDocKey(checkSum), err)
return
}
data = json.RawMessage(result.Value)
return
}
// DelDocumentCache remove memcache document detail
func (d *Dao) DelDocumentCache(ctx context.Context, checkSum int64) (err error) {
conn := d.cache.Get(ctx)
defer conn.Close()
if err = conn.Delete(cacheDocKey(checkSum)); err == mc.ErrNotFound {
err = nil
}
return
}
// SetDocumentCache add document cache
func (d *Dao) SetDocumentCache(ctx context.Context, checkSum int64, data json.RawMessage) (err error) {
conn := d.cache.Get(ctx)
defer conn.Close()
if err = conn.Set(&mc.Item{
Key: cacheDocKey(checkSum),
Value: data,
Expiration: d.mcExpire,
}); err != nil {
log.Error("dao.SetDocumentCache(%v,%s) err:%v", cacheDocKey(checkSum), data, err)
return
}
return
}
func cacheDocKey(checkSum int64) string {
return fmt.Sprintf("%v_%v", _docKeyPrefix, checkSum)
}

View File

@@ -0,0 +1,45 @@
package dao
import (
"context"
"flag"
"path/filepath"
"testing"
"go-common/app/interface/main/kvo/conf"
. "github.com/smartystreets/goconvey/convey"
)
var d *Dao
func WithDao(f func(d *Dao)) func() {
return func() {
dir, _ := filepath.Abs("../cmd/web-interface-test.toml")
flag.Set("conf", dir)
conf.Init()
if d == nil {
d = New(conf.Conf)
}
f(d)
}
}
func TestDao_Document(t *testing.T) {
Convey("test document", t, WithDao(func(d *Dao) {
checkSum := int64(11111)
data, err := d.Document(context.TODO(), checkSum)
So(err, ShouldBeNil)
Printf("%+v", data)
}))
}
func TestDao_UserConf(t *testing.T) {
Convey("test document", t, WithDao(func(d *Dao) {
mid := int64(11111)
moduleKey := 1
data, err := d.UserConf(context.TODO(), mid, moduleKey)
So(err, ShouldBeNil)
Printf("%+v", data)
}))
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"time"
"go-common/app/interface/main/kvo/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_getUserConf = "SELECT mid,module_key,check_sum,timestamp FROM user_conf WHERE mid=? AND module_key=?"
_upUserConf = "INSERT INTO user_conf(mid,module_key,check_sum,timestamp,ctime,mtime) VALUES(?,?,?,?,?,?) ON DUPLICATE KEY UPDATE check_sum=?, timestamp=?"
)
// UserConf get userconf
func (d *Dao) UserConf(ctx context.Context, mid int64, moduleKey int) (userConf *model.UserConf, err error) {
row := d.getUserConf.QueryRow(ctx, mid, moduleKey)
userConf = &model.UserConf{}
err = row.Scan(&userConf.Mid, &userConf.ModuleKey, &userConf.CheckSum, &userConf.Timestamp)
if err != nil {
if err == sql.ErrNoRows {
userConf = nil
err = nil
return
}
log.Error("row.Scan err:%v", err)
}
return
}
// TxUpUserConf add or update user conf
func (d *Dao) TxUpUserConf(ctx context.Context, tx *sql.Tx, mid int64, moduleKey int, checkSum int64, now time.Time) (err error) {
_, err = tx.Exec(_upUserConf, mid, moduleKey, checkSum, now.Unix(), now, now, checkSum, now.Unix())
if err != nil {
log.Error("db.exec err:%v", err)
}
return
}

View File

@@ -0,0 +1,70 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/kvo/model"
mc "go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_userConfKeyPrefix = "u"
)
func cacheUserConfKey(mid int64, moduleKey int) string {
return fmt.Sprintf("%v_%v_%v", _userConfKeyPrefix, mid, moduleKey)
}
// UserConfCache user config cache
func (d *Dao) UserConfCache(ctx context.Context, mid int64, moduleKey int) (uc *model.UserConf, err error) {
var r *mc.Item
conn := d.cache.Get(ctx)
defer conn.Close()
if r, err = conn.Get(cacheUserConfKey(mid, moduleKey)); err != nil {
if err == mc.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", cacheUserConfKey(mid, moduleKey), err)
return
}
if err = json.Unmarshal(r.Value, &uc); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
uc = nil
}
return
}
// SetUserConfCache set user config cache
func (d *Dao) SetUserConfCache(ctx context.Context, uc *model.UserConf) (err error) {
bs, err := json.Marshal(uc)
if err != nil {
log.Error("SetUserConfCache.Marshal err:%v", err)
return
}
conn := d.cache.Get(ctx)
defer conn.Close()
if err = conn.Set(&mc.Item{
Key: cacheUserConfKey(uc.Mid, uc.ModuleKey),
Value: bs,
Expiration: d.mcExpire,
}); err != nil {
log.Error("dao.SetUserConfCache(%v,%v) err:%v", cacheUserConfKey(uc.Mid, uc.ModuleKey), bs, err)
return
}
return
}
// DelUserConfCache del user config cache
func (d *Dao) DelUserConfCache(ctx context.Context, mid int64, moduleKey int) (err error) {
conn := d.cache.Get(ctx)
defer conn.Close()
if err = conn.Delete(cacheUserConfKey(mid, moduleKey)); err == mc.ErrNotFound {
err = nil
}
return
}