parent
58c4c13cf1
commit
caebdf32ca
@ -0,0 +1,39 @@
|
|||||||
|
package mgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Result[V any](val V, err error) V {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func Check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
cli := Result(mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.48:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second)))
|
||||||
|
coll := cli.Database("openim_v3").Collection("version_test")
|
||||||
|
tmp, err := NewVersionLog(coll)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
vl := tmp.(*VersionLogMgo)
|
||||||
|
res, err := vl.writeLogBatch2(context.Background(), "100", []string{"1000", "1001", "1003"}, model.VersionStateInsert, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("%+v", res)
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
const (
|
||||||
|
BlackName = "black"
|
||||||
|
ConversationName = "conversation"
|
||||||
|
FriendName = "friend"
|
||||||
|
FriendVersionName = "friend_version"
|
||||||
|
FriendRequestName = "friend_request"
|
||||||
|
GroupName = "group"
|
||||||
|
GroupMemberName = "group_member"
|
||||||
|
GroupMemberVersionName = "group_member_version"
|
||||||
|
GroupJoinVersionName = "group_join_version"
|
||||||
|
GroupRequestName = "group_request"
|
||||||
|
LogName = "log"
|
||||||
|
ObjectName = "s3"
|
||||||
|
UserName = "user"
|
||||||
|
)
|
@ -0,0 +1,14 @@
|
|||||||
|
package versionctx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EnableVersionCtx() grpc.ServerOption {
|
||||||
|
return grpc.ChainUnaryInterceptor(enableVersionCtxInterceptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func enableVersionCtxInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
||||||
|
return handler(WithVersionLog(ctx), req)
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package versionctx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
tablerelation "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Collection struct {
|
||||||
|
Name string
|
||||||
|
Doc *tablerelation.VersionLog
|
||||||
|
}
|
||||||
|
|
||||||
|
type versionKey struct{}
|
||||||
|
|
||||||
|
func WithVersionLog(ctx context.Context) context.Context {
|
||||||
|
return context.WithValue(ctx, versionKey{}, &VersionLog{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetVersionLog(ctx context.Context) *VersionLog {
|
||||||
|
if v, ok := ctx.Value(versionKey{}).(*VersionLog); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type VersionLog struct {
|
||||||
|
lock sync.Mutex
|
||||||
|
data []Collection
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VersionLog) Append(data ...Collection) {
|
||||||
|
if v == nil || len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
v.lock.Lock()
|
||||||
|
defer v.lock.Unlock()
|
||||||
|
v.data = append(v.data, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VersionLog) Get() []Collection {
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
v.lock.Lock()
|
||||||
|
defer v.lock.Unlock()
|
||||||
|
return v.data
|
||||||
|
}
|
Loading…
Reference in new issue