DoHierarchy.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from direct.directnotify.DirectNotifyGlobal import directNotify
  2. class DoHierarchy:
  3. """
  4. This table has been a source of memory leaks, with DoIds getting left in the table indefinitely.
  5. DoHierarchy guards access to the table and ensures correctness.
  6. """
  7. notify = directNotify.newCategory("DoHierarchy")
  8. def __init__(self):
  9. # parentId->zoneId->set(child DoIds)
  10. self._table = {}
  11. self._allDoIds = set()
  12. def isEmpty(self):
  13. assert ((len(self._table) == 0) == (len(self._allDoIds) == 0))
  14. return len(self._table) == 0 and len(self._allDoIds) == 0
  15. def __len__(self):
  16. return len(self._allDoIds)
  17. def clear(self):
  18. assert self.notify.debugCall()
  19. self._table = {}
  20. self._allDoIds = set()
  21. def getDoIds(self, parentId, zoneId=None, classType=None):
  22. """
  23. Moved from DoCollectionManager
  24. ==============================
  25. parentId is any distributed object id.
  26. zoneId is a uint32, defaults to None (all zones). Try zone 2 if
  27. you're not sure which zone to use (0 is a bad/null zone and
  28. 1 has had reserved use in the past as a no messages zone, while
  29. 2 has traditionally been a global, uber, misc stuff zone).
  30. dclassType is a distributed class type filter, defaults
  31. to None (no filter).
  32. If dclassName is None then all objects in the zone are returned;
  33. otherwise the list is filtered to only include objects of that type.
  34. """
  35. parent=self._table.get(parentId)
  36. if parent is None:
  37. return []
  38. if zoneId is None:
  39. r = []
  40. for zone in parent.values():
  41. for obj in zone:
  42. r.append(obj)
  43. else:
  44. r = parent.get(zoneId, [])
  45. if classType is not None:
  46. a = []
  47. for doId in r:
  48. obj = self.getDo(doId)
  49. if isinstance(obj, classType):
  50. a.append(doId)
  51. r = a
  52. return r
  53. def storeObjectLocation(self, doId, parentId, zoneId):
  54. assert self.notify.debugCall()
  55. assert doId not in self._allDoIds
  56. parentZoneDict = self._table.setdefault(parentId, {})
  57. zoneDoSet = parentZoneDict.setdefault(zoneId, set())
  58. zoneDoSet.add(doId)
  59. self._allDoIds.add(doId)
  60. def deleteObjectLocation(self, doId, parentId, zoneId):
  61. assert self.notify.debugCall()
  62. assert doId in self._allDoIds
  63. parentZoneDict = self._table.get(parentId)
  64. if parentZoneDict is not None:
  65. zoneDoSet = parentZoneDict.get(zoneId)
  66. if zoneDoSet is not None:
  67. if doId in zoneDoSet:
  68. zoneDoSet.remove(doId)
  69. self._allDoIds.remove(doId)
  70. if len(zoneDoSet) == 0:
  71. del parentZoneDict[zoneId]
  72. if len(parentZoneDict) == 0:
  73. del self._table[parentId]
  74. else:
  75. self.notify.error(
  76. "deleteObjectLocation: objId: %s not found" % doId)
  77. else:
  78. self.notify.error(
  79. "deleteObjectLocation: zoneId: %s not found" % zoneId)
  80. else:
  81. self.notify.error(
  82. "deleteObjectLocation: parentId: %s not found" % parentId)