-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_coll.go
337 lines (304 loc) · 7.65 KB
/
handle_coll.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package updater
import (
"fmt"
"github.com/hwcer/logger"
"github.com/hwcer/updater/dataset"
"github.com/hwcer/updater/operator"
)
type collectionModel interface {
Upsert(update *Updater, op *operator.Operator) bool
Getter(update *Updater, data *dataset.Collection, keys []string) error //keys==nil 初始化所有
Setter(update *Updater, bulkWrite dataset.BulkWrite) error
BulkWrite(update *Updater) dataset.BulkWrite
}
// collectionUpsert set时如果不存在,是否自动转换为new
//type collectionUpsert interface {
// Upsert(update *Updater, op *operator.Operator) bool
//}
type Collection struct {
statement
model collectionModel
remove []string //需要移除内存的数据,仅仅RAMMaybe有效
dataset *dataset.Collection
bulkWrite dataset.BulkWrite
}
func NewCollection(u *Updater, model any, ram RAMType) Handle {
r := &Collection{}
r.model = model.(collectionModel)
r.statement = *newStatement(u, ram, r.operator, r.Has)
return r
}
func (this *Collection) Parser() Parser {
return ParserTypeCollection
}
//func (this *Collection) get(k string) (r *dataset.Document) {
// return this.dataset.Get(k)
//}
func (this *Collection) val(id string) (r int64, ok bool) {
var i *dataset.Document
if i, ok = this.dataset.Get(id); ok {
r = i.GetInt64(dataset.ItemNameVAL)
}
return
}
func (this *Collection) save() (err error) {
if this.Updater.Async || this.dataset == nil {
return
}
bulkWrite := this.BulkWrite()
if err = this.dataset.Save(bulkWrite); err != nil {
return
}
if err = this.model.Setter(this.statement.Updater, bulkWrite); err == nil {
this.bulkWrite = nil
}
return
}
func (this *Collection) reset() {
this.statement.reset()
if this.dataset == nil {
this.dataset = dataset.NewColl()
}
}
func (this *Collection) release() {
this.statement.release()
if !this.Updater.Async {
if this.statement.ram == RAMTypeNone {
this.dataset = nil
} else {
this.dataset.Release()
}
}
}
func (this *Collection) init() error {
this.dataset = dataset.NewColl()
if this.statement.ram == RAMTypeMaybe || this.statement.ram == RAMTypeAlways {
this.Updater.Error = this.model.Getter(this.Updater, this.dataset, nil)
}
return this.Updater.Error
}
// 关闭时执行,玩家下线
func (this *Collection) destroy() (err error) {
return this.save()
}
func (this *Collection) Len() int {
return this.dataset.Len()
}
func (this *Collection) Has(id any) (r bool) {
if oid, err := this.ObjectId(id); err == nil {
r = this.dataset.Has(oid)
} else {
logger.Debug(err)
}
return
}
// Get 返回item,不可叠加道具只能使用oid获取
func (this *Collection) Get(key any) (r any) {
if oid, err := this.ObjectId(key); err == nil {
if i := this.dataset.Val(oid); i != nil {
r = i.Any()
}
} else {
logger.Debug(err)
}
return
}
// Val 直接获取 item中的val值,不可叠加道具只能使用oid获取
func (this *Collection) Val(key any) (r int64) {
if oid, err := this.ObjectId(key); err == nil {
r, _ = this.val(oid)
}
return
}
// Set 设置 k= oid||iid
// Set(oid||iid,map[string]any)
// Set(oid||iid,key string,val any)
func (this *Collection) Set(k any, v ...any) {
switch len(v) {
case 1:
if update := dataset.ParseUpdate(v[0]); update != nil {
this.operator(operator.TypesSet, k, 0, update)
} else {
this.Updater.Error = ErrArgsIllegal(k, v)
}
case 2:
if field, ok := v[0].(string); ok {
this.operator(operator.TypesSet, k, 0, dataset.NewUpdate(field, v[1]))
} else {
this.Updater.Error = ErrArgsIllegal(k, v)
}
default:
this.Updater.Error = ErrArgsIllegal(k, v)
}
}
// Remove 从内存中移除,用于清理不常用数据,不会改变数据库
func (this *Collection) Remove(id ...string) {
this.remove = append(this.remove, id...)
}
// New 使用全新的模型插入
func (this *Collection) New(v dataset.Model) (err error) {
op := &operator.Operator{OID: v.GetOID(), IID: v.GetIID(), Type: operator.TypesNew, Result: []any{v}}
op.Value = 1
if err = this.mayChange(op); err != nil {
return this.Updater.Errorf(err)
}
this.statement.Operator(op)
return
}
func (this *Collection) Select(keys ...any) {
for _, k := range keys {
if oid, err := this.ObjectId(k); err == nil {
this.statement.Select(oid)
} else {
logger.Alert(err)
}
}
}
func (this *Collection) 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 *Collection) 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
}
func (this *Collection) submit() (err error) {
//defer this.statement.done()
if err = this.Updater.Error; err != nil {
return
}
//r = this.statement.operator
//同步到内存
//for _, op := range this.statement.cache {
// if op.Type.IsValid() {
// if err = this.dataset.Update(op); err == nil {
// this.dirty.Update(op)
// } else {
// logger.Alert("数据保存失败已经丢弃,Error:%v,Operator:%+v", err, op)
// err = nil
// }
// }
//}
this.statement.submit()
if err = this.save(); err != nil && this.ram != RAMTypeNone {
logger.Alert("同步数据失败,等待下次同步:%v", err)
err = nil
}
if len(this.remove) > 0 {
this.dataset.Remove(this.remove...)
this.remove = nil
}
return
}
// Len 总记录数
//func (this *Collection) Len() int {
// return len(this.dataset)
//}
func (this *Collection) Range(h func(id string, doc *dataset.Document) bool) {
this.dataset.Range(h)
}
func (this *Collection) 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 *Collection) ITypeCollection(iid int32) (r ITypeCollection) {
if it := this.IType(iid); it != nil {
r, _ = it.(ITypeCollection)
}
return
}
func (this *Collection) BulkWrite() dataset.BulkWrite {
if this.bulkWrite == nil {
this.bulkWrite = this.model.BulkWrite(this.Updater)
}
return this.bulkWrite
}
func (this *Collection) mayChange(op *operator.Operator) (err error) {
it := this.ITypeCollection(op.IID)
if it == nil {
return ErrITypeNotExist(op.IID)
}
op.Bag = it.Id()
if listen, ok := it.(ITypeListener); ok {
listen.Listener(this.Updater, op)
}
if op.Type == operator.TypesDrop || op.Type == operator.TypesResolve {
return nil
}
//可以堆叠道具
if op.OID == "" && it.Stacked() {
op.OID, err = it.ObjectId(this.Updater, op.IID)
}
if err != nil {
return
}
if op.OID != "" {
this.statement.Select(op.OID)
}
return
}
func (this *Collection) operator(t operator.Types, k any, v int64, r any) {
if this.Updater.Error != nil {
return
}
op := operator.New(t, v, r)
switch d := k.(type) {
case string:
op.OID = d
default:
op.IID = dataset.ParseInt32(k)
}
if op.IID == 0 {
op.IID, this.Updater.Error = Config.ParseId(this.Updater, op.OID)
}
if this.Updater.Error != nil {
return
}
if err := this.mayChange(op); err != nil {
this.Updater.Error = err
return
}
this.statement.Operator(op)
}
func (this *Collection) ObjectId(key any) (oid string, err error) {
if v, ok := key.(string); ok {
return v, nil
}
iid := dataset.ParseInt32(key)
if iid <= 0 {
return "", fmt.Errorf("iid empty:%v", iid)
}
it := this.ITypeCollection(iid)
if it == nil {
return "", fmt.Errorf("IType unknown:%v", iid)
}
if !it.Stacked() {
return "", ErrObjectIdEmpty(iid)
}
oid, err = it.ObjectId(this.Updater, iid)
if err == nil && oid == "" {
err = ErrUnableUseIIDOperation
}
return
}