161 lines
3.9 KiB
Go
161 lines
3.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
r "go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/pb"
|
|
"math/rand"
|
|
"sync"
|
|
"testing"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func Test_Id(t *testing.T) {
|
|
wg := new(sync.WaitGroup)
|
|
for i := 0; i < 3; i++ {
|
|
wg.Add(1)
|
|
go func(_wg *sync.WaitGroup) {
|
|
for i := 0; i < 10; i++ {
|
|
id := primitive.NewObjectID().Hex()
|
|
fmt.Printf(id + "\n")
|
|
}
|
|
_wg.Done()
|
|
}(wg)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func Test_2D(t *testing.T) {
|
|
if sys, err := mgo.NewSys(
|
|
mgo.SetMongodbUrl("mongodb://10.0.0.9:10013"),
|
|
mgo.SetMongodbDatabase("dreamfactory2"),
|
|
); err != nil {
|
|
fmt.Printf("err:%v", err)
|
|
return
|
|
} else {
|
|
//创建uid索引
|
|
_index, err := sys.CreateIndex(core.SqlTable(comm.TableArena), mongo.IndexModel{
|
|
Keys: bson.M{"loc": "2dsphere"},
|
|
})
|
|
fmt.Printf("_index:%v err:%v \n", _index, err)
|
|
port := []float64{2, float64(rand.Int31n(100)) / 1000.0}
|
|
fmt.Printf("port:%v \n", port)
|
|
if cursor, err := sys.Find(comm.TableArena, bson.M{
|
|
"dan": 2,
|
|
// "loc": bson.M{
|
|
// "$geoWithin": bson.M{
|
|
// "$center": bson.A{port, 100},
|
|
// },
|
|
"loc": bson.M{
|
|
"$near": bson.M{
|
|
"$geometry": bson.M{"type": "Point", "coordinates": port},
|
|
"$maxDistance": 100000,
|
|
},
|
|
},
|
|
// bson.A{[]int32{2 * 1000, rand.Int31n(100)}, 100}
|
|
// "loc": bson.M{
|
|
// "$near": bson.M{
|
|
// "$geometry": bson.M{"type": "Point", "coordinates": []int32{dan * 1000, rand.Int31n(100)}},
|
|
// "$maxDistance": 100,
|
|
// },
|
|
// },
|
|
}, options.Find().SetSkip(0).SetLimit(int64(5))); err != nil {
|
|
fmt.Printf("err:%v", err)
|
|
return
|
|
} else {
|
|
for cursor.Next(context.Background()) {
|
|
temp := &pb.DBArenaUser{}
|
|
if err = cursor.Decode(temp); err != nil {
|
|
fmt.Printf("err:%v", err)
|
|
return
|
|
}
|
|
fmt.Printf("temp:%v\n", temp.Uinfo.Uid)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//测试api_getlist
|
|
func Test_Json_RTask(t *testing.T) {
|
|
if sys, err := r.NewSys(
|
|
r.SetRedisType(r.Redis_Single),
|
|
r.SetRedis_Single_Addr("10.0.0.9:10011"),
|
|
r.SetRedis_Single_Password("li13451234"),
|
|
r.SetRedis_Single_DB(3),
|
|
); err != nil {
|
|
fmt.Printf("err:%v", err)
|
|
return
|
|
} else {
|
|
// ret, err := json.MarshalMap(&pb.DBRtaskRecord{
|
|
// Id: "ASDASD",
|
|
// Uid: "ASDASDASD",
|
|
// Vals: map[int32]*pb.RtaskData{
|
|
// 1: {Data: map[int32]int32{1: 2}, Rtype: 2, Timestamp: 12312},
|
|
// },
|
|
// })
|
|
err = sys.HMSet("test001", &pb.DBRtaskRecord{
|
|
Id: "ASDASD",
|
|
Uid: "ASDASDASD",
|
|
Vals: map[int32]*pb.RtaskData{
|
|
1: {Data: map[int32]int32{1: 2}, Rtype: 2, Timestamp: 12312},
|
|
},
|
|
})
|
|
fmt.Printf("err:%v", err)
|
|
}
|
|
|
|
// sys.
|
|
// fmt.Printf("ret:%v err:%v", ret, err)
|
|
// ret, err = json.MarshalMap(&TestData{
|
|
// Name: "asdasd",
|
|
// Age: 10,
|
|
// List: []string{"12asd3", "45sdaa6", "asdasd"},
|
|
// Map: map[string]interface{}{
|
|
// "asd": 586,
|
|
// "asdasd": "asd1231",
|
|
// },
|
|
// })
|
|
// fmt.Printf("ret:%v err:%v", ret, err)
|
|
}
|
|
|
|
func TestRa(t *testing.T) {
|
|
if sys, err := r.NewSys(
|
|
r.SetRedisType(r.Redis_Single),
|
|
r.SetRedis_Single_Addr("10.0.0.9:10011"),
|
|
r.SetRedis_Single_Password("li13451234"),
|
|
r.SetRedis_Single_DB(15),
|
|
); err != nil {
|
|
fmt.Printf("err:%v", err)
|
|
return
|
|
} else {
|
|
s, err2 := sys.ZRevRange("sociatyboss:personalrank", 0, 10).Result()
|
|
if err2 != nil {
|
|
t.Fatal(err2)
|
|
}
|
|
// m := &redis.Z{Score: float64(1), Member: "dd"}
|
|
// sys.ZAdd("sociatyrank", m)
|
|
|
|
// sys.ZIncrBy("sociatyrank", 10, "a100aa")
|
|
// sys.ZIncrBy("sociatyrank", 10, "a100ab")
|
|
|
|
for _, v := range s {
|
|
// fmt.Println(v)
|
|
score, _ := sys.ZScore("sociatyboss:personalrank", v)
|
|
ranking, _ := sys.ZRevRank("sociatyboss:personalrank", v)
|
|
fmt.Printf("%d %v - %d \n", (ranking + 1), v, int64(score))
|
|
|
|
result, _ := sys.ZRevRank("sociatyboss:personalrank", v)
|
|
fmt.Println(result)
|
|
}
|
|
|
|
}
|
|
}
|