2024-09-01 16:17:48 -04:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
context2 "context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/fatih/structtag"
|
|
|
|
"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"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reference stores a typed document reference
|
|
|
|
type Reference struct {
|
|
|
|
// owning model name
|
|
|
|
Model string
|
|
|
|
// the name of the struct field
|
|
|
|
FieldName string
|
|
|
|
// index of field in owning struct
|
|
|
|
Idx int
|
|
|
|
// the type of the referenced object
|
|
|
|
HydratedType reflect.Type
|
|
|
|
|
|
|
|
// field kind (struct, slice, ...)
|
|
|
|
Kind reflect.Kind
|
|
|
|
|
2024-09-14 15:28:34 -04:00
|
|
|
exists bool
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
|
2024-09-14 15:28:34 -04:00
|
|
|
type gridFSReference struct {
|
2024-09-12 18:10:03 -04:00
|
|
|
BucketName string
|
|
|
|
FilenameFmt string
|
|
|
|
LoadType reflect.Type
|
|
|
|
Idx int
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
type TModelRegistry map[string]*Model
|
2024-09-01 16:17:48 -04:00
|
|
|
|
|
|
|
// ModelRegistry - the ModelRegistry stores a map containing
|
2024-09-14 02:09:38 -04:00
|
|
|
// pointers to Model instances, keyed by an associated
|
2024-09-01 16:17:48 -04:00
|
|
|
// model name
|
2024-09-14 15:34:49 -04:00
|
|
|
var ModelRegistry = make(TModelRegistry)
|
2024-09-01 16:17:48 -04:00
|
|
|
|
|
|
|
// DB - The mongodb database handle
|
|
|
|
var DB *mongo.Database
|
|
|
|
|
|
|
|
// DBClient - The mongodb client
|
|
|
|
var DBClient *mongo.Client
|
|
|
|
|
|
|
|
// NextStringID - Override this function with your own
|
|
|
|
// string ID generator!
|
|
|
|
var NextStringID func() string
|
|
|
|
|
|
|
|
var mutex sync.Mutex
|
|
|
|
|
2024-09-14 15:28:34 -04:00
|
|
|
func makeGfsRef(tag *structtag.Tag, idx int) gridFSReference {
|
2024-09-12 18:10:03 -04:00
|
|
|
opts := tag.Options
|
|
|
|
var ffmt string
|
|
|
|
if len(opts) < 1 {
|
|
|
|
ffmt = "%s"
|
|
|
|
} else {
|
|
|
|
ffmt = opts[0]
|
|
|
|
}
|
|
|
|
var typ reflect.Type
|
|
|
|
if len(opts) < 2 {
|
|
|
|
typ = reflect.TypeOf("")
|
|
|
|
} else {
|
|
|
|
switch opts[1] {
|
|
|
|
case "bytes":
|
|
|
|
typ = reflect.TypeOf([]byte{})
|
|
|
|
case "string":
|
|
|
|
typ = reflect.TypeOf("")
|
|
|
|
default:
|
|
|
|
typ = reflect.TypeOf("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-14 15:28:34 -04:00
|
|
|
return gridFSReference{
|
2024-09-12 18:10:03 -04:00
|
|
|
FilenameFmt: ffmt,
|
|
|
|
BucketName: tag.Name,
|
|
|
|
LoadType: typ,
|
|
|
|
Idx: idx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:17:48 -04:00
|
|
|
func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Reference {
|
|
|
|
if modelName != "" {
|
|
|
|
if ModelRegistry.Index(modelName) != -1 {
|
|
|
|
return Reference{
|
|
|
|
Idx: idx,
|
|
|
|
Model: modelName,
|
|
|
|
HydratedType: ht,
|
|
|
|
Kind: ht.Kind(),
|
2024-09-14 15:28:34 -04:00
|
|
|
exists: true,
|
2024-09-01 16:17:48 -04:00
|
|
|
FieldName: fieldName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Reference{
|
|
|
|
Idx: idx,
|
|
|
|
Model: modelName,
|
|
|
|
FieldName: fieldName,
|
|
|
|
HydratedType: ht,
|
|
|
|
Kind: ht.Kind(),
|
2024-09-14 15:28:34 -04:00
|
|
|
exists: true,
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("model name was empty")
|
|
|
|
}
|
|
|
|
|
2024-09-14 15:28:34 -04:00
|
|
|
func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) {
|
2024-09-01 16:17:48 -04:00
|
|
|
coll := ""
|
2024-09-12 18:10:03 -04:00
|
|
|
refs := make(map[string]Reference)
|
|
|
|
idcs := make(map[string][]InternalIndex)
|
2024-09-14 15:28:34 -04:00
|
|
|
gfsRefs := make(map[string]gridFSReference)
|
2024-09-01 16:17:48 -04:00
|
|
|
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
sft := t.Field(i)
|
|
|
|
ft := sft.Type
|
|
|
|
tags, err := structtag.Parse(string(sft.Tag))
|
|
|
|
panik(err)
|
|
|
|
shouldContinue := true
|
|
|
|
for {
|
|
|
|
if !shouldContinue {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch ft.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
ft = ft.Elem()
|
|
|
|
if _, ok := tags.Get("ref"); ok != nil {
|
|
|
|
if ft.Kind() == reflect.Struct {
|
2024-09-12 18:10:03 -04:00
|
|
|
ii2, rr2, gg2, _ := parseTags(ft, reflect.New(ft).Elem())
|
|
|
|
for k, vv := range ii2 {
|
|
|
|
idcs[sft.Name+"."+k] = vv
|
|
|
|
}
|
|
|
|
for k, vv := range rr2 {
|
|
|
|
refs[sft.Name+"."+k] = vv
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
for k, vv := range gg2 {
|
|
|
|
gfsRefs[sft.Name+"."+k] = vv
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case reflect.Pointer:
|
|
|
|
ft = ft.Elem()
|
|
|
|
fallthrough
|
|
|
|
case reflect.Struct:
|
2024-09-14 02:09:38 -04:00
|
|
|
if ft.ConvertibleTo(reflect.TypeOf(Document{})) {
|
2024-09-01 16:17:48 -04:00
|
|
|
collTag, err := tags.Get("coll")
|
|
|
|
panik(err)
|
|
|
|
coll = collTag.Name
|
|
|
|
idxTag, err := tags.Get("idx")
|
|
|
|
if err == nil {
|
|
|
|
idcs[sft.Type.Name()] = scanIndex(idxTag.Value())
|
|
|
|
}
|
|
|
|
shouldContinue = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if refTag, ok := tags.Get("ref"); ok == nil {
|
|
|
|
sname := sft.Name + "@" + refTag.Name
|
|
|
|
refs[sname] = makeRef(i, refTag.Name, sft.Name, sft.Type)
|
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
if gtag, ok := tags.Get("gridfs"); ok == nil {
|
|
|
|
sname := sft.Name + "@" + gtag.Name
|
|
|
|
gfsRefs[sname] = makeGfsRef(gtag, i)
|
|
|
|
}
|
2024-09-01 16:17:48 -04:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
idxTag, err := tags.Get("idx")
|
|
|
|
if err == nil {
|
|
|
|
idcs[sft.Name] = scanIndex(idxTag.Value())
|
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
if gtag, ok := tags.Get("gridfs"); ok == nil {
|
|
|
|
sname := sft.Name + "@" + gtag.Name
|
|
|
|
gfsRefs[sname] = makeGfsRef(gtag, i)
|
|
|
|
}
|
2024-09-01 16:17:48 -04:00
|
|
|
shouldContinue = false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
return idcs, refs, gfsRefs, coll
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
// Has returns the model typename and Model instance corresponding
|
2024-09-01 16:17:48 -04:00
|
|
|
// to the argument passed, as well as a boolean indicating whether it
|
|
|
|
// was found. otherwise returns `"", nil, false`
|
2024-09-14 02:09:38 -04:00
|
|
|
func (r TModelRegistry) Has(i interface{}) (string, *Model, bool) {
|
2024-09-01 16:17:48 -04:00
|
|
|
t := reflect.TypeOf(i)
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
n := t.Name()
|
|
|
|
if rT, ok := ModelRegistry[n]; ok {
|
|
|
|
return n, rT, true
|
|
|
|
}
|
|
|
|
return "", nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasByName functions almost identically to Has,
|
|
|
|
// except that it takes a string as its argument.
|
2024-09-14 02:09:38 -04:00
|
|
|
func (r TModelRegistry) HasByName(n string) (string, *Model, bool) {
|
2024-09-01 16:17:48 -04:00
|
|
|
if t, ok := ModelRegistry[n]; ok {
|
|
|
|
return n, t, true
|
|
|
|
}
|
|
|
|
return "", nil, false
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
// Index returns the index at which the Document struct is embedded
|
2024-09-01 16:17:48 -04:00
|
|
|
func (r TModelRegistry) Index(n string) int {
|
|
|
|
if v, ok := ModelRegistry[n]; ok {
|
2024-09-14 15:28:34 -04:00
|
|
|
return v.idx
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2024-09-12 18:10:03 -04:00
|
|
|
func (r TModelRegistry) new_(n string) interface{} {
|
2024-09-05 17:53:04 -04:00
|
|
|
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
2024-09-01 16:17:48 -04:00
|
|
|
v := reflect.New(m.Type)
|
2024-09-14 15:28:34 -04:00
|
|
|
df := v.Elem().Field(m.idx)
|
2024-09-14 02:09:38 -04:00
|
|
|
do := reflect.New(df.Type())
|
|
|
|
d := do.Interface().(IDocument)
|
|
|
|
//d := df.Interface().(IDocument)
|
|
|
|
d.setModel(*m)
|
|
|
|
d.getModel().typeName = name
|
|
|
|
df.Set(reflect.ValueOf(d).Elem())
|
2024-09-01 16:17:48 -04:00
|
|
|
return v.Interface()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Model registers models in the ModelRegistry, where
|
|
|
|
// they can be accessed via a model's struct name
|
|
|
|
func (r TModelRegistry) Model(mdl ...any) {
|
|
|
|
defer mutex.Unlock()
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
|
|
for _, m := range mdl {
|
|
|
|
t := reflect.TypeOf(m)
|
|
|
|
v := reflect.ValueOf(m)
|
|
|
|
vp := v
|
|
|
|
if vp.Kind() != reflect.Ptr {
|
|
|
|
vp = reflect.New(v.Type())
|
|
|
|
vp.Elem().Set(v)
|
|
|
|
}
|
|
|
|
id, ok := vp.Interface().(HasID)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("you MUST implement the HasID interface!!! skipping...\n"))
|
|
|
|
}
|
|
|
|
switch (id).Id().(type) {
|
|
|
|
case int, int64, int32, string, primitive.ObjectID, uint, uint32, uint64:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Printf("invalid ID type specified!!! skipping...\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = reflect.Indirect(reflect.ValueOf(m)).Type()
|
|
|
|
v = reflect.ValueOf(m).Elem()
|
|
|
|
}
|
|
|
|
n := t.Name()
|
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
panic(fmt.Sprintf("Only structs can be passed to this function, silly! (passed type: %s)", n))
|
|
|
|
}
|
|
|
|
idx := -1
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
ft := t.Field(i)
|
2024-09-14 02:09:38 -04:00
|
|
|
if (ft.Type.ConvertibleTo(reflect.TypeOf(Document{}))) {
|
2024-09-01 16:17:48 -04:00
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx < 0 {
|
2024-09-14 02:09:38 -04:00
|
|
|
panic("A model must embed the Document struct!")
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
inds, refs, gfs, coll := parseTags(t, v)
|
2024-09-01 16:17:48 -04:00
|
|
|
if coll == "" {
|
2024-09-14 15:20:17 -04:00
|
|
|
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
2024-09-14 02:09:38 -04:00
|
|
|
ModelRegistry[n] = &Model{
|
2024-09-14 15:28:34 -04:00
|
|
|
idx: idx,
|
2024-09-12 18:10:03 -04:00
|
|
|
Type: t,
|
2024-09-14 15:28:34 -04:00
|
|
|
collection: coll,
|
2024-09-12 18:10:03 -04:00
|
|
|
Indexes: inds,
|
2024-09-14 15:28:34 -04:00
|
|
|
references: refs,
|
|
|
|
gridFSReferences: gfs,
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range ModelRegistry {
|
2024-09-14 15:28:34 -04:00
|
|
|
for k2, v2 := range v.references {
|
|
|
|
if !v2.exists {
|
2024-09-01 16:17:48 -04:00
|
|
|
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
2024-09-14 15:28:34 -04:00
|
|
|
tmp := ModelRegistry[k].references[k2]
|
|
|
|
ModelRegistry[k].references[k2] = Reference{
|
2024-09-01 16:17:48 -04:00
|
|
|
Model: k,
|
|
|
|
Idx: tmp.Idx,
|
|
|
|
FieldName: tmp.FieldName,
|
|
|
|
Kind: tmp.Kind,
|
|
|
|
HydratedType: tmp.HydratedType,
|
2024-09-14 15:28:34 -04:00
|
|
|
exists: true,
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func innerWatch(coll *mongo.Collection) {
|
|
|
|
sspipeline := mongo.Pipeline{
|
|
|
|
bson.D{{"$match", bson.D{{"$or",
|
|
|
|
bson.A{
|
|
|
|
bson.D{{
|
|
|
|
"operationType", "insert",
|
|
|
|
}},
|
|
|
|
bson.D{{"operationType", "update"}},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := coll.Watch(context.TODO(), sspipeline, options.ChangeStream().SetFullDocument(options.UpdateLookup).SetFullDocumentBeforeChange(options.WhenAvailable))
|
|
|
|
panik(err)
|
|
|
|
defer func(stream *mongo.ChangeStream, ctx context2.Context) {
|
|
|
|
err := stream.Close(ctx)
|
|
|
|
panik(err)
|
|
|
|
}(stream, context.TODO())
|
|
|
|
for stream.Next(context.TODO()) {
|
|
|
|
var data bson.M
|
|
|
|
if err := stream.Decode(&data); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var uid = data["documentKey"].(bson.M)["_id"]
|
|
|
|
|
|
|
|
if data["operationType"] == "insert" {
|
|
|
|
counterColl := DB.Collection(COUNTER_COL)
|
|
|
|
counterColl.UpdateOne(context.TODO(), bson.M{"collection": coll.Name()}, bson.M{"$set": bson.M{
|
|
|
|
"current": uid,
|
|
|
|
}}, options.Update().SetUpsert(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%v\n", data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(uri string, dbName string) {
|
|
|
|
cli, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("failed to open database")
|
|
|
|
}
|
|
|
|
panik(err)
|
|
|
|
DB = cli.Database(dbName)
|
|
|
|
colls, err := DB.ListCollectionNames(context.TODO(), bson.M{"name": bson.M{"$ne": COUNTER_COL}}, options.ListCollections().SetNameOnly(true))
|
|
|
|
|
|
|
|
for _, c := range colls {
|
|
|
|
if c == COUNTER_COL {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go innerWatch(DB.Collection(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
DBClient = cli
|
|
|
|
}
|