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,45 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"addit.go",
"relation.go",
],
importpath = "go-common/app/job/main/relation/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//app/service/main/relation/model:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/job/main/relation/model/i64b:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["addit_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)

View File

@@ -0,0 +1,58 @@
package model
// AchieveFlag is
type AchieveFlag uint64
// const
var (
EmptyAchieve = AchieveFlag(0)
FollowerAchieve1k = AchieveFlag(1 << 0)
FollowerAchieve5k = AchieveFlag(1 << 1)
FollowerAchieve10k = AchieveFlag(1 << 2)
FollowerAchieve100k = AchieveFlag(1 << 3)
FollowerAchieve1000k = AchieveFlag(1 << 12)
)
// AchieveFromFollower is
func AchieveFromFollower(count int64) AchieveFlag {
if count <= 0 {
return EmptyAchieve
}
if count >= 100000 {
return AchieveFlag(1 << uint64(2+count/100000))
}
if count >= 10000 && count < 100000 {
return FollowerAchieve10k
}
if count >= 5000 && count < 10000 {
return FollowerAchieve5k
}
if count >= 1000 && count < 5000 {
return FollowerAchieve1k
}
return EmptyAchieve
}
// AllAchieveFromFollower is
func AllAchieveFromFollower(count int64) []AchieveFlag {
flags := []AchieveFlag{}
if count <= 0 {
return flags
}
if count >= 1000 {
flags = append(flags, FollowerAchieve1k)
}
if count >= 5000 {
flags = append(flags, FollowerAchieve5k)
}
if count >= 10000 {
flags = append(flags, FollowerAchieve10k)
}
if count >= 100000 {
remain := count / 100000
for i := int64(1); i <= remain; i++ {
flags = append(flags, AchieveFlag(1<<uint64(2+i)))
}
}
return flags
}

View File

@@ -0,0 +1,69 @@
package model
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDao_AllAchieveFromFollower(t *testing.T) {
Convey("AllAchieveFromFollower", t, func() {
flags := AllAchieveFromFollower(500)
So(flags, ShouldBeEmpty)
flags = AllAchieveFromFollower(1000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k})
flags = AllAchieveFromFollower(2000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k})
flags = AllAchieveFromFollower(5000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k})
flags = AllAchieveFromFollower(5001)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k})
flags = AllAchieveFromFollower(10000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k, FollowerAchieve10k})
flags = AllAchieveFromFollower(100000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k, FollowerAchieve10k, FollowerAchieve10k << 1})
flags = AllAchieveFromFollower(200000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k, FollowerAchieve10k, FollowerAchieve10k << 1, FollowerAchieve10k << 2})
flags = AllAchieveFromFollower(300000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k, FollowerAchieve10k, FollowerAchieve10k << 1, FollowerAchieve10k << 2, FollowerAchieve10k << 3})
flags = AllAchieveFromFollower(305000)
So(flags, ShouldResemble, []AchieveFlag{FollowerAchieve1k, FollowerAchieve5k, FollowerAchieve10k, FollowerAchieve10k << 1, FollowerAchieve10k << 2, FollowerAchieve10k << 3})
})
}
func TestDao_AchieveFromFollower(t *testing.T) {
Convey("AchieveFromFollower", t, func() {
flag := AchieveFromFollower(500)
So(flag, ShouldBeZeroValue)
flag = AchieveFromFollower(1000)
So(flag, ShouldEqual, FollowerAchieve1k)
flag = AchieveFromFollower(2000)
So(flag, ShouldEqual, FollowerAchieve1k)
flag = AchieveFromFollower(5000)
So(flag, ShouldEqual, FollowerAchieve5k)
flag = AchieveFromFollower(10000)
So(flag, ShouldEqual, FollowerAchieve10k)
flag = AchieveFromFollower(100000)
So(flag, ShouldEqual, FollowerAchieve10k<<1)
flag = AchieveFromFollower(200000)
So(flag, ShouldEqual, FollowerAchieve10k<<2)
flag = AchieveFromFollower(305000)
So(flag, ShouldEqual, FollowerAchieve10k<<3)
})
}

View File

@@ -0,0 +1,35 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["xints.go"],
importpath = "go-common/app/job/main/relation/model/i64b",
tags = ["automanaged"],
)
go_test(
name = "go_default_test",
srcs = ["xints_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)

View File

@@ -0,0 +1,91 @@
package i64b
import (
"database/sql/driver"
"encoding/binary"
)
//Int64Bytes be used to MySql\Protobuf varbinary converting.
type Int64Bytes []int64
// MarshalTo marshal int64 slice to bytes,each int64 will occupy Fixed 8 bytes.
//if the argument data not supplied with the full size,it will return the actual written size
func (is Int64Bytes) MarshalTo(data []byte) (int, error) {
for i, n := range is {
start := i * 8
end := (i + 1) * 8
if len(data) < end {
return start, nil
}
bs := data[start:end]
binary.BigEndian.PutUint64(bs, uint64(n))
}
return 8 * len(is), nil
}
// Size return the total size it will occupy in bytes
func (is Int64Bytes) Size() int {
return len(is) * 8
}
// Unmarshal parse the data into int64 slice
func (is *Int64Bytes) Unmarshal(data []byte) error {
return is.Scan(data)
}
// Scan parse the data into int64 slice
func (is *Int64Bytes) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case []byte:
var res []int64
for i := 0; i < len(sc) && i+8 <= len(sc); i += 8 {
ui := binary.BigEndian.Uint64(sc[i : i+8])
res = append(res, int64(ui))
}
*is = res
}
return
}
// Value marshal int64 slice to driver.Value,each int64 will occupy Fixed 8 bytes
func (is Int64Bytes) Value() (driver.Value, error) {
return is.Bytes(), nil
}
// Bytes marshal int64 slice to bytes,each int64 will occupy Fixed 8 bytes
func (is Int64Bytes) Bytes() []byte {
res := make([]byte, 0, 8*len(is))
for _, i := range is {
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(i))
res = append(res, bs...)
}
return res
}
// Evict get rid of the sepcified num from the slice
func (is *Int64Bytes) Evict(e int64) (ok bool) {
res := make([]int64, len(*is)-1)
for _, v := range *is {
if v != e {
res = append(res, v)
} else {
ok = true
}
}
*is = res
return
}
// Exist judge the sepcified num is in the slice or not
func (is Int64Bytes) Exist(i int64) (e bool) {
for _, v := range is {
if v == i {
e = true
return
}
}
return
}

View File

@@ -0,0 +1,74 @@
package i64b
import (
"testing"
)
func TestMarshalAndUnmarshal(t *testing.T) {
var a = Int64Bytes{1, 2, 3}
data := make([]byte, a.Size())
n, err := a.MarshalTo(data)
if n != 24 {
t.Logf("marshal size must be 24")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Int64Bytes
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b[0] != 1 || b[1] != 2 || b[2] != 3 {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}
func TestUncompleteMarshal(t *testing.T) {
var a = Int64Bytes{1, 2, 3}
data := make([]byte, a.Size()-7)
n, err := a.MarshalTo(data)
if n != 16 {
t.Logf("marshal size must be 16")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Int64Bytes
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b[0] != 1 || b[1] != 2 {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}
func TestNilMarshal(t *testing.T) {
var a = Int64Bytes{1, 2, 3}
var data []byte
n, err := a.MarshalTo(data)
if n != 0 {
t.Logf("marshal size must be 0")
t.FailNow()
}
if err != nil {
t.Fatalf("err:%v", err)
}
var b Int64Bytes
err = b.Unmarshal(data)
if err != nil {
t.Fatalf("err:%v", err)
}
if b != nil {
t.Logf("unmarshal failed!b:%v", b)
t.FailNow()
}
}

View File

@@ -0,0 +1,58 @@
package model
import (
"encoding/json"
"time"
sml "go-common/app/service/main/relation/model"
)
// Message define binlog databus message.
type Message struct {
Action string `json:"action"`
Table string `json:"table"`
New json.RawMessage `json:"new"`
Old json.RawMessage `json:"old"`
}
// Relation user_relation_fid_0~user_relation_fid_49,user_relation_mid_0~user_relation_mid_49
type Relation struct {
Mid int64 `json:"mid,omitempty"`
Fid int64 `json:"fid,omitempty"`
Attribute uint32 `json:"attribute"`
Status int `json:"status"`
MTime string `json:"mtime"`
CTime string `json:"ctime"`
}
// Stat user_relation_stat
type Stat struct {
Mid int64 `json:"mid,omitempty"`
Following int64 `json:"following"`
Whisper int64 `json:"whisper"`
Black int64 `json:"black"`
Follower int64 `json:"follower"`
}
// LastChangeAt is.
func (r *Relation) LastChangeAt() (at time.Time, err error) {
// FIXME(zhoujiahui): ctime and mtime should not be used here
return time.ParseInLocation("2006-01-02 15:04:05", r.MTime, time.Local)
}
// Attr is.
func (r *Relation) Attr() uint32 {
return sml.Attr(r.Attribute)
}
// IsRecent is.
func (r *Relation) IsRecent(at time.Time, trange time.Duration) bool {
lastAt, err := r.LastChangeAt()
if err != nil {
return false
}
if lastAt.Sub(at) > trange {
return true
}
return false
}