Blame view

models/schema.go 2.63 KB
765431a4   zhangqijia   增加schema接口, 抽象 mo...
1
  package models
98b0736d   zhangqijia   添加定时器, 检查心跳
2
3
  
  import (
765431a4   zhangqijia   增加schema接口, 抽象 mo...
4
5
6
  	"pro2d/common/components"
  	"pro2d/common/db"
  	"pro2d/common/logger"
98b0736d   zhangqijia   添加定时器, 检查心跳
7
8
9
10
  	"reflect"
  	"strings"
  )
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
11
12
13
14
15
16
17
18
  type SchemaOption func(schema *Schema)
  
  func WithSchemaDB(idb components.IDB) SchemaOption {
  	return func(schema *Schema) {
  		schema.db = idb
  	}
  }
  
98b0736d   zhangqijia   添加定时器, 检查心跳
19
  type Schema struct {
765431a4   zhangqijia   增加schema接口, 抽象 mo...
20
  	db           components.IDB
33ea26ab   zhangqijia   使用schema封装mongo
21
22
23
  	reflectValue *reflect.Value
  	reflectType  reflect.Type
  
38dd96b4   zhangqijia   定时器+网络数据 peer 在一条...
24
  	cacheFields map[string]interface{}
33ea26ab   zhangqijia   使用schema封装mongo
25
  
98b0736d   zhangqijia   添加定时器, 检查心跳
26
27
28
29
  	pri interface{}
  	schema interface{}
  }
  
33ea26ab   zhangqijia   使用schema封装mongo
30
31
  func NewSchema(key string, schema interface{}) *Schema {
  	s := reflect.ValueOf(schema)
98b0736d   zhangqijia   添加定时器, 检查心跳
32
  	if s.Kind() == reflect.Ptr {
33ea26ab   zhangqijia   使用schema封装mongo
33
  		s = reflect.ValueOf(schema).Elem()
98b0736d   zhangqijia   添加定时器, 检查心跳
34
  	}
33ea26ab   zhangqijia   使用schema封装mongo
35
36
37
  	sch := &Schema{
  		reflectValue: &s,
  		reflectType: s.Type(),
38dd96b4   zhangqijia   定时器+网络数据 peer 在一条...
38
  		cacheFields: make(map[string]interface{}),
98b0736d   zhangqijia   添加定时器, 检查心跳
39
40
  		schema: schema,
  	}
765431a4   zhangqijia   增加schema接口, 抽象 mo...
41
42
43
  
  	sch.db = db.NewMongoColl(sch.GetSchemaName(), sch)
  	sch.pri =  db.GetBsonD(sch.getPriTag(), key)
33ea26ab   zhangqijia   使用schema封装mongo
44
  	return sch
98b0736d   zhangqijia   添加定时器, 检查心跳
45
46
  }
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
47
48
49
50
51
52
53
54
55
56
57
58
  func (s *Schema) FindIndex() (string, []string) {
  	var index []string
  	for i := 0; i < s.reflectType.NumField(); i++ {
  		if s.reflectType.Field(i).Tag.Get("index") != "" {
  			js := strings.Split(s.reflectType.Field(i).Tag.Get("json"), ",")
  			if len(js) == 0 {
  				continue
  			}
  			index = append(index, js[0])
  		}
  	}
  	return strings.ToLower(s.reflectType.Name()), index
98b0736d   zhangqijia   添加定时器, 检查心跳
59
60
  }
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
61
62
63
64
  func (s *Schema) Init()  {
  	coll, keys := s.FindIndex()
  	for  _, index := range keys {
  		s.db.CreateTable()
98b0736d   zhangqijia   添加定时器, 检查心跳
65
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
66
67
68
69
70
  		logger.Debug("InitDoc collect: %v, createIndex: %s", coll, index)
  		res, err := s.db.SetUnique(index)
  		if err != nil {
  			logger.Error("InitDoc unique: %s, err: %v", res, err)
  			continue
98b0736d   zhangqijia   添加定时器, 检查心跳
71
72
  		}
  	}
765431a4   zhangqijia   增加schema接口, 抽象 mo...
73
74
75
76
  }
  
  func (s *Schema) GetDB() components.IDB {
  	return s.db
98b0736d   zhangqijia   添加定时器, 检查心跳
77
  }
33ea26ab   zhangqijia   使用schema封装mongo
78
79
80
81
82
83
84
85
86
  
  func (s *Schema) GetPri() interface{} {
  	return s.pri
  }
  
  func (s *Schema) GetSchema() interface{} {
  	return s.schema
  }
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
87
88
89
90
  func (s *Schema) GetSchemaName() string {
  	return strings.ToLower(s.reflectType.Name())
  }
  
33ea26ab   zhangqijia   使用schema封装mongo
91
  func (s *Schema) Load() error {
765431a4   zhangqijia   增加schema接口, 抽象 mo...
92
  	return s.db.Load()
33ea26ab   zhangqijia   使用schema封装mongo
93
94
95
  }
  
  func (s *Schema) Create() error {
765431a4   zhangqijia   增加schema接口, 抽象 mo...
96
  	_, err := s.db.Create()
33ea26ab   zhangqijia   使用schema封装mongo
97
98
99
  	return err
  }
  
38dd96b4   zhangqijia   定时器+网络数据 peer 在一条...
100
101
  func (s *Schema) Update() {
  	if s.cacheFields != nil {
765431a4   zhangqijia   增加schema接口, 抽象 mo...
102
  		s.db.UpdateProperties(s.cacheFields)
38dd96b4   zhangqijia   定时器+网络数据 peer 在一条...
103
104
105
106
  		s.cacheFields = make(map[string]interface{})
  	}
  }
  
7f269318   zhangqijia   add pb.go; 添加关闭连接...
107
  func (s *Schema) SetProperty(key string, val interface{}) {
33ea26ab   zhangqijia   使用schema封装mongo
108
  	s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val))
7f269318   zhangqijia   add pb.go; 添加关闭连接...
109
  	s.cacheFields[strings.ToLower(key)] = val
33ea26ab   zhangqijia   使用schema封装mongo
110
111
  }
  
7f269318   zhangqijia   add pb.go; 添加关闭连接...
112
  func (s *Schema) SetProperties(properties map[string]interface{}) {
33ea26ab   zhangqijia   使用schema封装mongo
113
114
  	for key, val := range properties {
  		s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val))
7f269318   zhangqijia   add pb.go; 添加关闭连接...
115
  		s.cacheFields[strings.ToLower(key)] = val
33ea26ab   zhangqijia   使用schema封装mongo
116
  	}
765431a4   zhangqijia   增加schema接口, 抽象 mo...
117
118
119
120
121
122
123
124
125
126
127
128
  }
  
  func (s *Schema) getPriTag() string {
  	var pri string
  	for i := 0; i < s.reflectType.NumField(); i++ {
  		if s.reflectType.Field(i).Tag.Get("pri") == "1" {
  			pri = strings.ToLower(s.reflectType.Field(i).Name)
  			break
  		}
  	}
  	return pri
  }