Blame view

src/shared/ModelBase.lua 9.47 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
  local ModelBase = class("ModelBase")
  ModelBase.key = "key"
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
3
  ModelBase.schema = {}
314bc5df   zhengshouren   提交服务器初始代码
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
  local string_format = string.format
  local table_insert = table.insert
  local table_unpack = table.unpack
  local assert = assert
  local next = next
  local ipairs = ipairs
  local pairs = pairs
  local tostring = tostring
  local tonumber = tonumber
  local redisproxy = redisproxy
  
  local function filterProperties(properties, filter)
      for i, field in ipairs(filter) do
          properties[field] = nil
      end
  end
  
  function ModelBase:ctor(properties)
      self.isModelBase_ = true
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
24
25
26
27
28
29
30
      self.cacheFields = {} --缓存字段 不更新数据库的字段
  
      self[self.class.key .. "_"] = properties[self.class.key] --数据库key
      properties[self.class.key] = nil
  
      if not self:isValidKey() then
          print(string_format("%s [%s:key] should be give in new(ctor)", tostring(self), self.class.__cname))
1a1a6375   gaofengduan   fix lua warning
31
          return
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
32
      end
314bc5df   zhengshouren   提交服务器初始代码
33
34
  
      if type(properties) ~= "table" then properties = {} end
fa565e0c   zhouhaihai   优化结构
35
      self:loadProperties(properties)  --缺少的域将设置默认值
314bc5df   zhengshouren   提交服务器初始代码
36
37
  end
  
46fac6f1   zhouahaihai   酱料
38
  -- startCache 和 endCache 在恰当的时候*配对使用*  嵌套使用多次增加引用计数 直到引用计数为0 写入
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
39
40
41
  function ModelBase:startCache( ... )
      for _, field in ipairs({ ... }) do
          if self.class.schema[field] then
46fac6f1   zhouahaihai   酱料
42
              self.cacheFields[field] = (self.cacheFields[field] or 0) + 1
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
43
44
45
46
          end
      end
  end
  
46fac6f1   zhouahaihai   酱料
47
  --减少缓存引用计数 为时写入, 无参数 强制刷新所有缓存
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
48
  function ModelBase:endCache( ... )
1a1a6375   gaofengduan   fix lua warning
49
50
      local args = { ... }
      local params = {}
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
51
  
1a1a6375   gaofengduan   fix lua warning
52
      local function doOneCache(field)
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
53
54
          local propname = field .. "_"
          table_insert(params, field)
1a1a6375   gaofengduan   fix lua warning
55
          if self.class.schema[field][1] == "table" then
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
              table_insert(params, MsgPack.pack(self[propname]))
          else
              table_insert(params, self[propname])
          end
      end
  
      if not next(args) then
          for field, _ in pairs(self.cacheFields) do
              doOneCache(field)
          end
          self.cacheFields = {}
      else
          for _, field in ipairs(args) do
              if self.cacheFields[field] then
46fac6f1   zhouahaihai   酱料
70
71
72
73
74
                  self.cacheFields[field] = self.cacheFields[field] - 1
                  if self.cacheFields[field] <= 0 then
                      self.cacheFields[field] = nil
                      doOneCache(field)
                  end
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
75
76
77
78
79
80
81
82
              end
          end
      end
  
      if next(params) then
          redisproxy:hmset(self:getKey(), table_unpack(params))
      end
  end
314bc5df   zhengshouren   提交服务器初始代码
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  --[[--
  
  返回对象的 ID 值。
  
  **Returns:**
  
  -   ID 
  
  ]]
  function ModelBase:getKey()
      local id = self[self.class.key .. "_"]
      assert(id ~= nil, string_format("%s [%s:getKey()] Invalid key", tostring(self), self.class.__cname))
      return id
  end
  
  function ModelBase:load(properties)
      if not self:isValidKey() then
          print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname))
          return false
      end
  
      if not properties then
          properties = redisproxy:hgetall(self:getKey())
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
106
          properties = table.arrayToMap(properties)
314bc5df   zhengshouren   提交服务器初始代码
107
      end
314bc5df   zhengshouren   提交服务器初始代码
108
109
      if not next(properties) then return false end
  
fa565e0c   zhouhaihai   优化结构
110
      self:loadProperties(properties)
314bc5df   zhengshouren   提交服务器初始代码
111
112
113
114
115
116
117
118
119
120
121
122
123
  
      self:onLoad()
  
      return true
  end
  
  --创建model对应的redis数据, 必须已经设置了ID
  function ModelBase:create()
      if not self:isValidKey() then
          print(string_format("%s [%s:key] should be set before create", tostring(self), self.class.__cname))
          return nil
      end
  
314bc5df   zhengshouren   提交服务器初始代码
124
125
126
127
128
      self:save()
      self:onCreate()
  
      return self
  end
fa565e0c   zhouhaihai   优化结构
129
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
130
  -- save 忽略 缓存配置
314bc5df   zhengshouren   提交服务器初始代码
131
132
133
134
135
  function ModelBase:save()
      local redisProperties = self:getProperties()
  
      local params = {}
      for fieldName, value in pairs(redisProperties) do
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
136
137
138
139
140
          local propname = fieldName .. "_"
          table_insert(params, fieldName)
          if self.class.schema[fieldName][1] == "table" then
              table_insert(params, MsgPack.pack(self[propname]))
          else
314bc5df   zhengshouren   提交服务器初始代码
141
              table_insert(params, self[propname])
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
142
          end
314bc5df   zhengshouren   提交服务器初始代码
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      end
      if next(params) then
          redisproxy:hmset(self:getKey(), table_unpack(params))
      end
  end
  
  --[[--
  
  确定对象是否设置了有效的 key
  
  ]]
  function ModelBase:isValidKey()
      local propname = self.class.key .. "_"
      local key = self[propname]
      return type(key) == "string" and key ~= ""
  end
  
  --[[--
  
fa565e0c   zhouhaihai   优化结构
162
  加载对象的属性进内存。
314bc5df   zhengshouren   提交服务器初始代码
163
164
165
166
167
168
169
  NOTE: 如果properties缺少schema中的域, 将用默认值来填充
  
  **Parameters:**
  
  -   properties: 包含属性值的数组
  
  ]]
fa565e0c   zhouhaihai   优化结构
170
  function ModelBase:loadProperties(properties)
314bc5df   zhengshouren   提交服务器初始代码
171
      assert(type(properties) == "table", "Invalid properties")
314bc5df   zhengshouren   提交服务器初始代码
172
  
314bc5df   zhengshouren   提交服务器初始代码
173
174
175
176
      for field, schema in pairs(self.class.schema) do
          local typ, def = table_unpack(schema)
          local propname = field .. "_"
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
177
178
179
180
          if typ == "table" and type(properties[field]) == "string" then
              properties[field] = MsgPack.unpack(properties[field])
          end
  
3dbe0d5d   zhouhaihai   #fxtr_10186 修复两个bug
181
          local val = properties[field] or clone(def)
314bc5df   zhengshouren   提交服务器初始代码
182
183
184
185
186
187
          if val ~= nil then
              if typ == "number" then val = tonumber(val) end
              assert(type(val) == typ,
                 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
                                   tostring(self), self.class.__cname, field, typ, type(val)))
              self[propname] = val
314bc5df   zhengshouren   提交服务器初始代码
188
189
          end
      end
314bc5df   zhengshouren   提交服务器初始代码
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  end
  
  --[[--
  
  取得对象的属性值。
  
  **Parameters:**
  
  -   fields: 要取得哪些属性的值,如果未指定该参数,则返回 fields 中设定的属性
  -   filter: 要从结果中过滤掉哪些属性,如果未指定则不过滤
  
  **Returns:**
  
  -   包含属性值的数组
  
  ]]
  function ModelBase:getProperties(fields, filter)
      local schema = self.class.schema
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
208
      if type(fields) ~= "table" then fields = table.keys(self.class.schema) end
314bc5df   zhengshouren   提交服务器初始代码
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
  
      local properties = {}
      for i, field in ipairs(fields) do
          local propname = field .. "_"
          local typ = schema[field][1]
          local val = self[propname]
          assert(type(val) == typ,
                 string_format("%s [%s:getProperties()] Type mismatch, %s expected %s, actual is %s",
                                   tostring(self), self.class.__cname, field, typ, type(val)))
          properties[field] = val
      end
  
      if type(filter) == "table" then
          filterProperties(properties, filter)
      end
  
      return properties
  end
  
  function ModelBase:getProperty(property)
      if type(property) ~= "string" then return nil end
      if not self.class.schema[property] then return nil end
      return self:getProperties({property})[property]
  end
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
234
  function ModelBase:setProperty(property, value)
314bc5df   zhengshouren   提交服务器初始代码
235
236
237
238
239
240
241
242
243
244
      if not self.class.schema[property] then
          print(string_format("%s [%s:setProperty()] Invalid property : %s",
              tostring(self), self.class.__cname, property))
          return
      end
  
      local typ, def = table_unpack(self.class.schema[property])
      local propname = property .. "_"
  
      if typ == "number" then value = tonumber(value) end
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
245
246
247
      if typ == "table" and not value then
          value = self[propname] -- table 可以用自己的缓冲
      end
314bc5df   zhengshouren   提交服务器初始代码
248
249
250
251
252
      assert(type(value) == typ,
         string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
           tostring(self), self.class.__cname, property, typ, type(value)))
      self[propname] = value
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
253
254
255
256
257
      if not self.cacheFields[property] then
          -- table 使用msgpack
          if typ == "table" then
              value = MsgPack.pack(value)
          end
314bc5df   zhengshouren   提交服务器初始代码
258
          redisproxy:hset(self:getKey(), property, value)
314bc5df   zhengshouren   提交服务器初始代码
259
260
261
      end
  end
  
fa565e0c   zhouhaihai   优化结构
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
  function ModelBase:setProperties(fields)
      local result = {}
      for property, value in pairs(fields) do
           if not self.class.schema[property] then
              print(string_format("%s [%s:setProperty()] Invalid property : %s",
                  tostring(self), self.class.__cname, property))
          else
              local typ, def = table_unpack(self.class.schema[property])
              local propname = property .. "_"
              if typ == "number" then value = tonumber(value) end
              if typ == "table" and not value then
                  value = self[propname] -- table 可以用自己的缓冲
              end
              assert(type(value) == typ,
                 string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
                   tostring(self), self.class.__cname, property, typ, type(value)))
              self[propname] = value
  
              if not self.cacheFields[property] then
                  table_insert(result, property)
                  if typ == "table" then
                      table_insert(result, MsgPack.pack(self[propname]))
                  else
                      table_insert(result, self[propname])
                  end
              end
          end
      end
      if next(result) then
          redisproxy:hmset(self:getKey(), table_unpack(result))
      end
  end
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
295
  function ModelBase:incrProperty(property, value)
314bc5df   zhengshouren   提交服务器初始代码
296
297
298
299
300
301
302
303
304
      if not self.class.schema[property] then
          print(string_format("%s [%s:setProperty()] Invalid property : %s",
              tostring(self), self.class.__cname, property))
          return
      end
  
      local typ, def = table_unpack(self.class.schema[property])
      local propname = property .. "_"
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
305
      if typ == "table" then return end
314bc5df   zhengshouren   提交服务器初始代码
306
      if typ == "number" then value = tonumber(value) end
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
307
  
314bc5df   zhengshouren   提交服务器初始代码
308
309
310
311
312
      assert(type(value) == typ,
         string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s",
           tostring(self), self.class.__cname, property, typ, type(value)))
      self[propname] = self[propname] + value
  
9ad697c8   zhouahaihai   删除 fields ,增加数据库...
313
      if not self.cacheFields[property] then
314bc5df   zhengshouren   提交服务器初始代码
314
          return redisproxy:hincrby(self:getKey(), property, value)
314bc5df   zhengshouren   提交服务器初始代码
315
316
317
318
319
320
321
322
323
324
      end
  end
  
  function ModelBase:onLoad()
  end
  
  function ModelBase:onCreate()
  end
  
  return ModelBase