-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_doc.go
281 lines (258 loc) · 6.25 KB
/
handle_doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package updater
import (
"fmt"
"github.com/hwcer/logger"
"github.com/hwcer/schema"
"github.com/hwcer/updater/dataset"
"github.com/hwcer/updater/operator"
"strings"
)
/*
切记不要直接修改dataset
建议使用dataset中实现以下接口提高性能
Get(k string) any //获取k的值
Set(k string, v any) error //设置k值的为v
*/
type documentModel interface {
New(update *Updater) any //初始化对象
Field(update *Updater, iid int32) (string, error) //使用IID映射字段名
Getter(update *Updater, data *dataset.Document, keys []string) error //获取数据接口,需要对data进行赋值,keys==nil 获取所有
Setter(update *Updater, dirty dataset.Update) error //保存数据接口
}
// Document 文档存储
type Document struct {
statement
dirty dataset.Update //数据缓存
model documentModel //handle model
dataset *dataset.Document //数据
}
func NewDocument(u *Updater, model any, ram RAMType) Handle {
r := &Document{}
r.model = model.(documentModel)
r.statement = *newStatement(u, ram, r.operator, r.has)
return r
}
func (this *Document) val(k string) (r int64, ok bool) {
if v := this.dataset.Val(k); v != nil {
r, ok = dataset.TryParseInt64(v)
}
return
}
func (this *Document) save() (err error) {
if this.Updater.Async {
return
}
dirty := this.Dirty()
if err = this.dataset.Save(dirty); err != nil {
return
}
if len(dirty) == 0 {
return nil
}
if err = this.model.Setter(this.statement.Updater, dirty); err == nil {
this.dirty = nil
}
return
}
// reset 运行时开始时
func (this *Document) reset() {
this.statement.reset()
if this.dataset == nil {
this.dataset = dataset.NewDoc(nil)
}
}
// release 运行时释放
func (this *Document) release() {
this.statement.release()
if !this.Updater.Async {
if this.statement.ram == RAMTypeNone {
this.dataset = nil
} else {
this.dataset.Release()
}
}
}
func (this *Document) init() (err error) {
if this.dataset == nil {
this.dataset = dataset.NewDoc(nil)
}
if this.statement.ram == RAMTypeMaybe {
this.statement.ram = RAMTypeNone
logger.Alert("updater Document模型不支持RAMTypeMaybe已经自动转换为RAMTypeNone")
}
if this.statement.ram == RAMTypeAlways && !this.dataset.Loader() {
err = this.model.Getter(this.Updater, this.dataset, nil)
}
return
}
// 关闭时执行,玩家下线
func (this *Document) destroy() (err error) {
return this.save()
}
func (this *Document) Has(k any) bool {
return true
}
// Get 对象中的特定值
// 不建议使用GET获取特定字段值
func (this *Document) Get(k any) (r any) {
if key, err := this.ObjectId(k); err == nil {
r = this.dataset.Val(key)
}
return
}
// Val 不建议使用Val获取特定字段值的int64值
func (this *Document) Val(k any) (r int64) {
if key, err := this.ObjectId(k); err == nil {
r, _ = this.val(key)
}
return
}
// Set 设置
// Set(k string,v any)
func (this *Document) Set(k any, v ...any) {
switch len(v) {
case 1:
this.operator(operator.TypesSet, k, 0, v[0])
default:
this.Updater.Error = ErrArgsIllegal(k, v)
}
}
func (this *Document) Select(keys ...any) {
for _, k := range keys {
if key, err := this.ObjectId(k); err == nil {
this.statement.Select(key)
} else {
logger.Alert(err)
}
}
}
func (this *Document) Data() (err error) {
if this.Updater.Error != nil {
return this.Updater.Error
}
if len(this.keys) == 0 {
return nil
}
keys := this.keys.ToString()
if err = this.model.Getter(this.Updater, this.dataset, keys); err == nil {
this.statement.date()
}
return
}
func (this *Document) Verify() (err error) {
if this.Updater.Error != nil {
return this.Updater.Error
}
for _, act := range this.statement.operator {
if err = this.Parse(act); err != nil {
return
}
}
this.statement.verify()
return
}
// Table 表名
func (this *Document) Table() (r string) {
if sch := this.Schema(); sch != nil {
r = sch.Table
}
return
}
func (this *Document) Schema() *schema.Schema {
sch, err := this.dataset.Schema()
if err != nil {
this.Updater.Error = err
}
return sch
}
func (this *Document) Parser() Parser {
return ParserTypeDocument
}
func (this *Document) submit() (err error) {
if err = this.Updater.Error; err != nil {
return
}
this.statement.submit()
if err = this.save(); err != nil && this.ram != RAMTypeNone {
logger.Alert("数据库[%v]同步数据错误,等待下次同步:%v", this.Table(), err)
err = nil
}
return
}
// Dirty 设置脏数据,手动修改内存后置脏同步到数据库
func (this *Document) Dirty() dataset.Update {
if this.dirty == nil {
this.dirty = dataset.Update{}
}
return this.dirty
}
func (this *Document) Range(f func(k string, v any) bool) {
this.dataset.Range(f)
}
func (this *Document) Interface() any {
return this.dataset.Any()
}
func (this *Document) Any() any {
return this.dataset.Any()
}
func (this *Document) ObjectId(k any) (key string, err error) {
switch v := k.(type) {
case string:
key = v
default:
iid := dataset.ParseInt32(k)
key, err = this.model.Field(this.Updater, iid)
}
if err != nil {
return
}
if strings.Index(key, ".") > 0 {
return
}
if sch := this.Schema(); sch != nil {
if field := sch.LookUpField(key); field != nil {
key = field.DBName
} else {
err = fmt.Errorf("document field not exist")
}
}
return
}
func (this *Document) IType(iid int32) IType {
if h, ok := this.model.(modelIType); ok {
v := h.IType(iid)
return itypesDict[v]
} else {
return this.Updater.IType(iid)
}
}
func (this *Document) operator(t operator.Types, k any, v int64, r any) {
if t == operator.TypesDel {
logger.Debug("updater document del is disabled")
return
}
op := operator.New(t, v, r)
switch s := k.(type) {
case string:
op.Key = s
default:
if op.IID = dataset.ParseInt32(k); op.IID > 0 {
op.Key, this.Updater.Error = this.model.Field(this.Updater, op.IID)
}
}
if this.Updater.Error == nil && op.Key == "" {
this.Updater.Error = fmt.Errorf("document operator key empty:%+v", op)
}
if this.Updater.Error != nil {
return
}
this.statement.Select(op.Key)
it := this.IType(op.IID)
if it != nil {
op.Bag = it.Id()
if listen, ok := it.(ITypeListener); ok {
listen.Listener(this.Updater, op)
}
}
this.statement.Operator(op)
}