Blame view

publish/skynet/lualib/skynet/sharetable.lua 10.7 KB
4d6f285d   zhouhaihai   增加发布功能
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  local skynet = require "skynet"
  local service = require "skynet.service"
  local core = require "skynet.sharetable.core"
  local is_sharedtable = core.is_sharedtable
  local stackvalues = core.stackvalues
  
  local function sharetable_service()
  	local skynet = require "skynet"
  	local core = require "skynet.sharetable.core"
  
  	local matrix = {}	-- all the matrix
  	local files = {}	-- filename : matrix
  	local clients = {}
  
  	local sharetable = {}
  
  	local function close_matrix(m)
  		if m == nil then
  			return
  		end
  		local ptr = m:getptr()
  		local ref = matrix[ptr]
  		if ref == nil or ref.count == 0 then
  			matrix[ptr] = nil
  			m:close()
  		end
  	end
  
  	function sharetable.loadfile(source, filename, ...)
  		close_matrix(files[filename])
  		local m = core.matrix("@" .. filename, ...)
  		files[filename] = m
  		skynet.ret()
  	end
  
  	function sharetable.loadstring(source, filename, datasource, ...)
  		close_matrix(files[filename])
  		local m = core.matrix(datasource, ...)
  		files[filename] = m
  		skynet.ret()
  	end
  
  	local function loadtable(filename, ptr, len)
  		close_matrix(files[filename])
  		local m = core.matrix([[
  			local unpack, ptr, len = ...
  			return unpack(ptr, len)
  		]], skynet.unpack, ptr, len)
  		files[filename] = m
  	end
  
  	function sharetable.loadtable(source, filename, ptr, len)
  		local ok, err = pcall(loadtable, filename, ptr, len)
  		skynet.trash(ptr, len)
  		assert(ok, err)
  		skynet.ret()
  	end
  
  	local function query_file(source, filename)
  		local m = files[filename]
  		local ptr = m:getptr()
  		local ref = matrix[ptr]
  		if ref == nil then
  			ref = {
  				filename = filename,
  				count = 0,
  				matrix = m,
  				refs = {},
  			}
  			matrix[ptr] = ref
  		end
  		if ref.refs[source] == nil then
  			ref.refs[source] = true
  			local list = clients[source]
  			if not list then
  				clients[source] = { ptr }
  			else
  				table.insert(list, ptr)
  			end
  			ref.count = ref.count + 1
  		end
  		return ptr
  	end
  
  	function sharetable.query(source, filename)
  		local m = files[filename]
  		if m == nil then
  			skynet.ret()
  			return
  		end
  		local ptr = query_file(source, filename)
  		skynet.ret(skynet.pack(ptr))
  	end
  
  	function sharetable.close(source)
  		local list = clients[source]
  		if list then
  			for _, ptr in ipairs(list) do
  				local ref = matrix[ptr]
  				if ref and ref.refs[source] then
  					ref.refs[source] = nil
  					ref.count = ref.count - 1
  					if ref.count == 0 then
  						if files[ref.filename] ~= ref.matrix then
  							-- It's a history version
  							skynet.error(string.format("Delete a version (%s) of %s", ptr, ref.filename))
  							ref.matrix:close()
  							matrix[ptr] = nil
  						end
  					end
  				end
  			end
  			clients[source] = nil
  		end
  		-- no return
  	end
  
  	skynet.dispatch("lua", function(_,source,cmd,...)
  		sharetable[cmd](source,...)
  	end)
  
  	skynet.info_func(function()
  		local info = {}
  
  		for filename, m in pairs(files) do
  			info[filename] = {
  				current = m:getptr(),
  				size = m:size(),
  			}
  		end
  
  		local function address(refs)
  			local keys = {}
  			for addr in pairs(refs) do
  				table.insert(keys, skynet.address(addr))
  			end
  			table.sort(keys)
  			return table.concat(keys, ",")
  		end
  
  		for ptr, copy in pairs(matrix) do
  			local v = info[copy.filename]
  			local h = v.history
  			if h == nil then
  				h = {}
  				v.history = h
  			end
  			table.insert(h, string.format("%s [%d]: (%s)", copy.matrix:getptr(), copy.matrix:size(), address(copy.refs)))
  		end
  		for _, v in pairs(info) do
  			if v.history then
  				v.history = table.concat(v.history, "\n\t")
  			end
  		end
  
  		return info
  	end)
  
  end
  
  local function load_service(t, key)
  	if key == "address" then
  		t.address = service.new("sharetable", sharetable_service)
  		return t.address
  	else
  		return nil
  	end
  end
  
  local function report_close(t)
  	local addr = rawget(t, "address")
  	if addr then
  		skynet.send(addr, "lua", "close")
  	end
  end
  
  local sharetable = setmetatable ( {} , {
  	__index = load_service,
  	__gc = report_close,
  })
  
  function sharetable.loadfile(filename, ...)
  	skynet.call(sharetable.address, "lua", "loadfile", filename, ...)
  end
  
  function sharetable.loadstring(filename, source, ...)
  	skynet.call(sharetable.address, "lua", "loadstring", filename, source, ...)
  end
  
  function sharetable.loadtable(filename, tbl)
  	assert(type(tbl) == "table")
  	skynet.call(sharetable.address, "lua", "loadtable", filename, skynet.pack(tbl))
  end
  
  
  local RECORD = {}
  function sharetable.query(filename)
  	local newptr = skynet.call(sharetable.address, "lua", "query", filename)
  	if newptr then
  		local t = core.clone(newptr)
  		local map = RECORD[filename]
  		if not map then
  			map = {}
  			RECORD[filename] = map
  		end
  		map[t] = true
  		return t
  	end
  end
  
  
  local pairs = pairs
  local type = type
  local assert = assert
  local next = next
  local rawset = rawset
  local getuservalue = debug.getuservalue
  local setuservalue = debug.setuservalue
  local getupvalue = debug.getupvalue
  local setupvalue = debug.setupvalue
  local getlocal = debug.getlocal
  local setlocal = debug.setlocal
  local getinfo = debug.getinfo
  
  local NILOBJ = {}
  local function insert_replace(old_t, new_t, replace_map)
      for k, ov in pairs(old_t) do
          if type(ov) == "table" then
              local nv = new_t[k]
              if nv == nil then
                  nv = NILOBJ
              end
              assert(replace_map[ov] == nil)
              replace_map[ov] = nv
              nv = type(nv) == "table" and nv or NILOBJ
              insert_replace(ov, nv, replace_map)
          end
      end
      replace_map[old_t] = new_t
      return replace_map
  end
  
  
  local function resolve_replace(replace_map)
      local match = {}
      local record_map = {}
  
      local function getnv(v)
          local nv = replace_map[v]
          if nv then
              if nv == NILOBJ then
                  return nil
              end
              return nv
          end
          assert(false)
      end
  
      local function match_value(v)
          assert(v ~= nil)
          if v == RECORD then
              return
          end
  
          local tv = type(v)
          local f = match[tv]
          if record_map[v] or is_sharedtable(v) then
              return
          end
  
          if f then
              record_map[v] = true
              f(v)
          end
      end
  
      local function match_mt(v)
          local mt = debug.getmetatable(v)
          if mt then
              local nv = replace_map[mt]
              if nv then
                  nv = getnv(mt)
                  debug.setmetatable(v, nv)
              else
                  match_value(mt)
              end
          end
      end
  
      local function match_internmt()
          local internal_types = {
              pointer = debug.upvalueid(getnv, 1),
              boolean = false,
              str = "",
              number = 42,
              thread = coroutine.running(),
              func = getnv,
          }
          for _,v in pairs(internal_types) do
              match_mt(v)
          end
          match_mt(nil)
      end
  
  
      local function match_table(t)
          local keys = false
          for k,v in next, t do
              local tk = type(k)
              if match[tk] then
                  keys = keys or {}
                  keys[#keys+1] = k
              end
  
              local nv = replace_map[v]
              if nv then
                  nv = getnv(v)
                  rawset(t, k, nv)
              else
                  match_value(v)
              end
          end
  
          if keys then
              for _, old_k in ipairs(keys) do
                  local new_k = replace_map[old_k]
                  if new_k then
                      local value = rawget(t, old_k)
                      new_k = getnv(old_k)
                      rawset(t, old_k, nil)
                      if new_k then
                          rawset(t, new_k, value)
                      end
                  else
                      match_value(old_k)
                  end
              end
          end
          match_mt(t)
      end
  
      local function match_userdata(u)
          local uv = getuservalue(u)
          local nv = replace_map[uv]
          if nv then
              nv = getnv(uv)
              setuservalue(u, nv)
          end
          match_mt(u)
      end
  
      local function match_funcinfo(info)
          local func = info.func
          local nups = info.nups
          for i=1,nups do
              local name, upv = getupvalue(func, i)
              local nv = replace_map[upv]
              if nv then
                  nv = getnv(upv)
                  setupvalue(func, i, nv)
              elseif upv then
                  match_value(upv)
              end
          end
  
          local level = info.level
          local curco = info.curco or coroutine.running()
          if not level then
              return
          end
          local i = 1
          while true do
              local name, v = getlocal(curco, level, i)
              if name == nil then
                  break
              end
              if replace_map[v] then
                  local nv = getnv(v)
                  setlocal(curco, level, i, nv)
              elseif v then
                  match_value(v)
              end
              i = i + 1
          end
      end
  
      local function match_function(f)
          local info = getinfo(f, "uf")
          match_funcinfo(info)
      end
  
      local stack_values_tmp = {}
      local function match_thread(co)
          -- match stackvalues
          local n = stackvalues(co, stack_values_tmp)
          for i=1,n do
              local v = stack_values_tmp[i]
              stack_values_tmp[i] = nil
              match_value(v)
          end
  
          -- match callinfo
          local level = 1
          -- jump the fucntion from sharetable.update to top
          local is_self = coroutine.running() == co
          if is_self then
              while true do
                  local info = getinfo(co, level, "uf")
                  level = level + 1
                  if not info then
                      level = 1
                      break
                  elseif info.func == sharetable.update then
                      break
                  end
              end
          end
  
          while true do
              local info = getinfo(co, level, "uf")
              if not info then
                  break
              end
              info.level = is_self and level + 1 or level
              info.curco = co
              match_funcinfo(info)
              level = level + 1
          end
      end
  
      match["table"] = match_table
      match["function"] = match_function
      match["userdata"] = match_userdata
      match["thread"] = match_thread
  
      match_internmt()
  
      local root = debug.getregistry()
      assert(replace_map[root] == nil)
      match_table(root)
  end
  
  
  function sharetable.update(...)
  	local names = {...}
  	local replace_map = {}
  	for _, name in ipairs(names) do
  		local map = RECORD[name]
  		if map then
  			local new_t = sharetable.query(name)
  			for old_t,_ in pairs(map) do
  				if old_t ~= new_t then
  					insert_replace(old_t, new_t, replace_map)
                      map[old_t] = nil
  				end
  			end
  		end
  	end
  
      if next(replace_map) then
  	   resolve_replace(replace_map)
      end
  end
  
  return sharetable