/src/lib/storage/repository/repository_transient.e

http://github.com/tybor/Liberty · Specman e · 136 lines · 86 code · 11 blank · 39 comment · 1 complexity · e3e8f43a62327048a2469e37ccc22d11 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class REPOSITORY_TRANSIENT
  5. --
  6. -- Allows one to register transient objects (ones not to be saved by a REPOSITORY). Those objects are not
  7. -- committed (they are replaced by a tag called "transient reference" in the saved stream); nor are they
  8. -- restored (if a "transient reference" is found it is replaced with the registered object).
  9. --
  10. -- Usual usage:
  11. -- (create {REPOSITORY_TRANSIENT}).register(to_internals)
  12. --
  13. -- '''Caveat''': if you have cycles involving both transient and normal objects, those cycles won't be
  14. -- detected (at commit) and won't be restored (at update). In a nutshell, ''don't do that''.
  15. --
  16. insert
  17. INTERNALS_HANDLER
  18. feature {ANY}
  19. register (transient_object: INTERNALS; transient_reference: STRING)
  20. -- Register a `transient_object' with the given `transient_reference'.
  21. require
  22. not transient_object.type_is_expanded
  23. transient_object.object_as_pointer /= default_pointer
  24. not transient_reference.is_empty
  25. not has_object(transient_reference)
  26. local
  27. ref: STRING; t: REPOSITORY_TRANSIENT_OBJECT
  28. do
  29. ref := strings.new_twin(transient_reference)
  30. if transient_objects_pool.is_empty then
  31. create t.set(transient_object, ref)
  32. else
  33. t := transient_objects_pool.item
  34. t.set(transient_object, ref)
  35. end
  36. transient.put(ref, t)
  37. ensure
  38. has_object(transient_reference)
  39. end
  40. unregister (transient_reference: STRING)
  41. -- Unregister the `transient_reference'.
  42. require
  43. has_object(transient_reference)
  44. local
  45. t: REPOSITORY_TRANSIENT_OBJECT
  46. do
  47. t := transient.key_at(transient_reference)
  48. check
  49. t /= Void
  50. end
  51. transient.remove(t)
  52. strings.recycle(t.key)
  53. transient_objects_pool.recycle(t)
  54. ensure
  55. not has_object(transient_reference)
  56. end
  57. has_object (a_transient_reference: STRING): BOOLEAN
  58. -- True if the `transient_reference'" is registered.
  59. do
  60. Result := transient.has_value(a_transient_reference)
  61. end
  62. feature {REPOSITORY_IMPL}
  63. reference (a_object: INTERNALS): STRING
  64. require
  65. not a_object.type_is_expanded
  66. a_object.object_as_pointer /= default_pointer
  67. local
  68. dummy: like dummy_transient_object
  69. do
  70. dummy := dummy_transient_object
  71. dummy.set(a_object, once "")
  72. Result := transient.reference_at(dummy)
  73. dummy.reset
  74. ensure
  75. Result /= Void implies not Result.is_empty
  76. Result /= Void implies a_object.object_as_pointer = object(Result).object_as_pointer
  77. end
  78. feature {REPOSITORY_LAYOUT}
  79. object (a_reference: STRING): INTERNALS
  80. require
  81. not a_reference.is_empty
  82. has_object(a_reference)
  83. do
  84. Result := transient.key_at(a_reference).internals
  85. end
  86. feature {}
  87. transient: HASHED_BIJECTIVE_DICTIONARY[STRING, REPOSITORY_TRANSIENT_OBJECT]
  88. -- The key is the transient object, to speed up the most frequent operations which are `reference'
  89. -- and `object'.
  90. once
  91. create Result.with_capacity(2)
  92. end
  93. transient_objects_pool: RECYCLING_POOL[REPOSITORY_TRANSIENT_OBJECT]
  94. once
  95. create Result.make
  96. end
  97. dummy_transient_object: REPOSITORY_TRANSIENT_OBJECT
  98. once
  99. create Result.make
  100. end
  101. strings: STRING_RECYCLING_POOL
  102. once
  103. create Result.make
  104. end
  105. end -- class REPOSITORY_TRANSIENT
  106. --
  107. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  108. --
  109. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  110. -- of this software and associated documentation files (the "Software"), to deal
  111. -- in the Software without restriction, including without limitation the rights
  112. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  113. -- copies of the Software, and to permit persons to whom the Software is
  114. -- furnished to do so, subject to the following conditions:
  115. --
  116. -- The above copyright notice and this permission notice shall be included in
  117. -- all copies or substantial portions of the Software.
  118. --
  119. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  120. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  121. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  122. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  123. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  124. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  125. -- THE SOFTWARE.