Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fsBTree.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001-2012 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE
  12. #
  13. ##############################################################################
  14. # fsBTrees are data structures used for ZODB FileStorage. They are not
  15. # expected to be "public" excpect to FileStorage.
  16. # Each item in an fsBTree maps a two-byte key to a six-byte value.
  17. __all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
  18. 'fsBucket', 'fsSet', 'fsBTree', 'fsTreeSet',
  19. 'union', 'intersection', 'difference',
  20. )
  21. from zope.interface import moduleProvides
  22. from .Interfaces import IIntegerObjectBTreeModule
  23. from ._base import Bucket
  24. from ._base import Set
  25. from ._base import Tree as BTree
  26. from ._base import TreeSet
  27. from ._base import difference as _difference
  28. from ._base import intersection as _intersection
  29. from ._base import set_operation as _set_operation
  30. from ._base import to_bytes as _to_bytes
  31. from ._base import union as _union
  32. from ._base import _fix_pickle
  33. from ._compat import import_c_extension
  34. _BUCKET_SIZE = 500
  35. _TREE_SIZE = 500
  36. using64bits = False
  37. _to_key = _to_bytes(2)
  38. _to_value = _to_bytes(6)
  39. class fsBucketPy(Bucket):
  40. _to_key = _to_key
  41. _to_value = _to_value
  42. def toString(self):
  43. return b''.join(self._keys) + b''.join(self._values)
  44. def fromString(self, v):
  45. length = len(v)
  46. if length % 8 != 0:
  47. raise ValueError()
  48. count = length // 8
  49. keys, values = v[:count*2], v[count*2:]
  50. self.clear()
  51. while keys and values:
  52. key, keys = keys[:2], keys[2:]
  53. value, values = values[:6], values[6:]
  54. self._keys.append(key)
  55. self._values.append(value)
  56. return self
  57. class fsSetPy(Set):
  58. _to_key = _to_key
  59. class fsBTreePy(BTree):
  60. max_leaf_size = _BUCKET_SIZE
  61. max_internal_size = _TREE_SIZE
  62. _to_key = _to_key
  63. _to_value = _to_value
  64. class fsTreeSetPy(TreeSet):
  65. max_leaf_size = _BUCKET_SIZE
  66. max_internal_size = _TREE_SIZE
  67. _to_key = _to_key
  68. # Can't declare forward refs, so fix up afterwards:
  69. fsBucketPy._mapping_type = fsBucketPy._bucket_type = fsBucketPy
  70. fsBucketPy._set_type = fsSetPy
  71. fsSetPy._mapping_type = fsBucketPy
  72. fsSetPy._set_type = fsSetPy._bucket_type = fsSetPy
  73. fsBTreePy._mapping_type = fsBTreePy._bucket_type = fsBucketPy
  74. fsBTreePy._set_type = fsSetPy
  75. fsTreeSetPy._mapping_type = fsBucketPy
  76. fsTreeSetPy._set_type = fsTreeSetPy._bucket_type = fsSetPy
  77. differencePy = _set_operation(_difference, fsSetPy)
  78. unionPy = _set_operation(_union, fsSetPy)
  79. intersectionPy = _set_operation(_intersection, fsSetPy)
  80. import_c_extension(globals())
  81. _fix_pickle(globals(), __name__)
  82. moduleProvides(IIntegerObjectBTreeModule)