/src/lib/storage/repository.e

http://github.com/tybor/Liberty · Specman e · 219 lines · 146 code · 25 blank · 48 comment · 0 complexity · c8cd914d72d295e0ea66f839b8a53916 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class REPOSITORY[O_ -> STORABLE]
  5. --
  6. -- A REPOSITORY for objects of type O_ can be viewed as a DICTIONARY[O_, STRING] (i.e. objects of type
  7. -- O_ are stored using key names which are actually ordinary STRINGs).
  8. -- Also note that stored objects of type O_ are always non-Void objects.
  9. --
  10. -- A repository is meant to be stored on a physical store (say, a stream, a file, a database and so on).
  11. --
  12. -- See also XML_FILE_REPOSITORY, XML_STREAM_REPOSITORY.
  13. --
  14. feature {ANY} -- Getting and setting objects in the repository:
  15. frozen has (object_name: STRING): BOOLEAN
  16. -- Is `object_name' the name of some stored object.
  17. require
  18. not object_name.is_empty
  19. do
  20. Result := repository.has(object_name)
  21. end
  22. frozen at (object_name: STRING): O_
  23. -- Return the object currently associated to `object_name'.
  24. require
  25. has(object_name)
  26. do
  27. Result := repository.at(object_name)
  28. ensure
  29. Result /= Void
  30. end
  31. frozen add (object: O_; object_name: STRING)
  32. -- Add a new `object' in the `Current' repository.
  33. require
  34. object /= Void
  35. new_reference: not has(object_name)
  36. do
  37. repository.add(object, object_name)
  38. ensure
  39. reference_stored: object = at(object_name)
  40. end
  41. frozen put (object: O_; object_name: STRING) assign at
  42. -- Update or add a new `object' in the `Current' repository.
  43. require
  44. object /= Void
  45. do
  46. repository.put(object, object_name)
  47. ensure
  48. reference_stored: object = at(object_name)
  49. end
  50. remove (object_name: STRING)
  51. -- Remove entry `object_name' from the `Current' repository.
  52. require
  53. has(object_name)
  54. do
  55. repository.remove(object_name)
  56. ensure
  57. not has(object_name)
  58. end
  59. feature {ANY} -- Counting:
  60. count: INTEGER
  61. -- Actual `count' of stored elements.
  62. do
  63. Result := repository.count
  64. end
  65. is_empty: BOOLEAN
  66. -- Is it empty ?
  67. do
  68. Result := count = 0
  69. ensure
  70. Result = (count = 0)
  71. end
  72. feature {ANY} -- Iterating facilities:
  73. lower: INTEGER 1
  74. upper: INTEGER
  75. do
  76. Result := count
  77. ensure
  78. Result = count
  79. end
  80. valid_index (index: INTEGER): BOOLEAN
  81. do
  82. Result := 1 <= index and then index <= count
  83. ensure
  84. Result = index.in_range(lower, upper)
  85. end
  86. item (index: INTEGER): O_
  87. require
  88. valid_index(index)
  89. do
  90. Result := repository.item(index)
  91. ensure
  92. Result = at(key(index))
  93. end
  94. key (index: INTEGER): STRING
  95. require
  96. valid_index(index)
  97. do
  98. Result := repository.key(index)
  99. ensure
  100. at(Result) = item(index)
  101. end
  102. new_iterator_on_items: ITERATOR[O_]
  103. do
  104. Result := repository.new_iterator_on_items
  105. ensure
  106. Result /= Void
  107. end
  108. new_iterator_on_keys: ITERATOR[STRING]
  109. do
  110. Result := repository.new_iterator_on_keys
  111. ensure
  112. Result /= Void
  113. end
  114. key_map_in (buffer: COLLECTION[STRING])
  115. -- Append in `buffer', all available keys (this may be useful to
  116. -- speed up the traversal).
  117. require
  118. buffer /= Void
  119. do
  120. repository.key_map_in(buffer)
  121. ensure
  122. buffer.count = count + old buffer.count
  123. end
  124. item_map_in (buffer: COLLECTION[O_])
  125. -- Append in `buffer', all available items (this may be useful to
  126. -- speed up the traversal).
  127. require
  128. buffer /= Void
  129. do
  130. repository.item_map_in(buffer)
  131. ensure
  132. buffer.count = count + old buffer.count
  133. end
  134. feature {ANY} -- Really storing data:
  135. update
  136. -- Update the repository objects. Get all objects from the physical store.
  137. require
  138. is_updateable
  139. deferred
  140. end
  141. commit
  142. -- Commit all the repository objects to the physical store.
  143. require
  144. is_commitable
  145. deferred
  146. end
  147. is_connected: BOOLEAN
  148. -- True if the repository is connected to a physical store.
  149. deferred
  150. end
  151. is_updateable: BOOLEAN
  152. -- True if the repository can be updated from data in the physical store.
  153. deferred
  154. ensure
  155. Result implies is_connected
  156. end
  157. is_commitable: BOOLEAN
  158. -- True if the repository can be committed to the underlying physical store.
  159. deferred
  160. ensure
  161. Result implies is_connected
  162. end
  163. feature {} -- Implementation
  164. repository: DICTIONARY[O_, STRING]
  165. objects_are_expanded: BOOLEAN
  166. local
  167. o: O_
  168. do
  169. Result := o /= Void
  170. end
  171. invariant
  172. repository /= Void
  173. reference_storables: not objects_are_expanded
  174. end -- class REPOSITORY
  175. --
  176. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  177. --
  178. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  179. -- of this software and associated documentation files (the "Software"), to deal
  180. -- in the Software without restriction, including without limitation the rights
  181. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  182. -- copies of the Software, and to permit persons to whom the Software is
  183. -- furnished to do so, subject to the following conditions:
  184. --
  185. -- The above copyright notice and this permission notice shall be included in
  186. -- all copies or substantial portions of the Software.
  187. --
  188. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  189. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  190. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  191. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  192. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  193. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  194. -- THE SOFTWARE.