PageRenderTime 11ms CodeModel.GetById 8ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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