SheafSystem  0.0.0.0
map_record.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class map_record
19 
20 #include "SheafSystem/map_record.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/error_message.h"
24 #include "SheafSystem/member_record_set.h"
25 #include "SheafSystem/interval_index_map_iterator.h"
26 
27 //#define DIAGNOSTIC_OUTPUT
28 // #undef DIAGNOSTIC_OUTPUT
29 
30 // ===========================================================
31 // MAP_RECORD FACET
32 // ===========================================================
33 
37  : variable_length_record(xhost.scaffold())
38 {
39 
40  // Preconditions:
41 
42  // Body:
43 
44  // Neither buffer is initialized.
45 
46  put_is_internal(false);
47  put_is_external(false);
48 
49  // Postconditions:
50 
51  ensure(invariant());
52  ensure(buf() == 0);
53  ensure(buf_ub() == 0);
54  ensure(!is_internal());
55  ensure(!is_external());
56 
57  // Exit:
58 
59  return;
60 }
61 
64 map_record(const map_record& xother)
65  : variable_length_record(xother)
66 {
67 
68  // Preconditions:
69 
70  // Body:
71 
72  not_implemented();
73 
74  // Postconditions:
75 
76  ensure(invariant());
77 }
78 
82 {
83 
84  // Preconditions:
85 
86  // Body:
87 
88  // Nothing specific to do.
90 
91  // Postconditions:
92 
93  // Exit:
94 
95  return;
96 }
97 
98 // ===========================================================
99 // WRITE FACET
100 // ===========================================================
101 
103 void
105 externalize(hvl_t* xhdf_buffer, const interval_index_map& xmap)
106 {
107 
108  // Preconditions:
109 
110  require(scaffold().structure().state_is_read_accessible());
111 
112  // Body:
113 
114 #ifdef DIAGNOSTIC_OUTPUT
115  cout << "map_record::externalize:" << endl;
116 #endif
117 
118  // Initialize the internal buffer from the map.
119 
120  transfer_map_to_internal_buffer();
121 
122  // Convert the internal buffer to external form
123  // and place the result in the external buffer.
124 
125  convert_internal_buffer_to_external_buffer(xmap);
126 
127  // Transfer the external buffer to HDF
128 
129  transfer_external_buffer_to_HDF(xhdf_buffer);
130 
131  // Postconditions:
132 
133  // Exit
134 
135  return;
136 }
137 
139 void
140 sheaf::map_record::
141 transfer_map_to_internal_buffer()
142 {
143  // Preconditions:
144 
145  require(scaffold().structure().state_is_read_accessible());
146 
147  // Body:
148 
149  // Nothing to do; internal buffer is the map itself.
150 
151  // Now the internal buffer is initialized;
152 
153  put_is_internal(true);
154 
155  // Postconditions:
156 
157  ensure(is_internal());
158 
159  // Exit
160 
161  return;
162 }
163 
164 void
165 sheaf::map_record::
166 convert_internal_buffer_to_external_buffer(const interval_index_map& xmap)
167 {
168  // Preconditions:
169 
170  // Body:
171 
172  // Initialize the external buffer from the internal buffer.
173 
174  initialize_external_buffer(xmap);
175 
176  // Externalize all the data fields.
177 
178  size_t lnext_field_offset = 0;
179 
180  externalize_map_data(lnext_field_offset, xmap);
181 
182  // Now the external buffer is valid.
183 
184  put_is_external(true);
185 
186  // Postconditions:
187 
188  ensure(is_external());
189 
190  // Exit
191 
192  return;
193 }
194 
196 void
197 sheaf::map_record::
198 initialize_external_buffer(const interval_index_map& xmap)
199 {
200  // Preconditions:
201 
202 
203  // Body:
204 
205  // Determine the size of the external buffer.
206 
207  size_type lend_offset = 0;
208 
209  interval_index_map_iterator* litr =
210  dynamic_cast<interval_index_map_iterator*>(xmap.iterator());
211  while(!litr->is_done())
212  {
213  lend_offset += 4;
214  litr->next_interval();
215  }
216  delete litr;
217 
218  // Allocate the external buffer.
219  // Buffer will be deleted in member_record_set::delete_external_buffers.
220 
221  void* lbuf = lend_offset > 0 ? new pod_index_type[lend_offset] : 0;
222 
223  put_buf(lbuf, lend_offset);
224 
225  // Buffer will not be deleted in destructor for this.
226 
227  put_delete_buffer(false);
228 
229  // The external buffer is allocated but not initialized.
230 
231  put_is_external(false);
232 
233  // Postconditions:
234 
235  ensure(!is_external());
236 
237  // Exit
238 
239  return;
240 }
241 
243 void
244 sheaf::map_record::
245 externalize_map_data(size_t& xnext_field_offset, const interval_index_map& xmap)
246 {
247 
248  // Preconditions:
249 
250  require(is_internal());
251 
252  // Body:
253 
254  define_old_variable(size_t old_xnext_field_offset = xnext_field_offset);
255 
256 #ifdef DIAGNOSTIC_OUTPUT
257  cout << scaffold().structure().name()
258  << " file_id_map: " << endl;
259  xmap.print_map_rep(cout);
260  cout << xmap << endl;
261 #endif
262 
263  interval_index_map_iterator* litr =
264  dynamic_cast<interval_index_map_iterator*>(xmap.iterator(false));
265  while(!litr->is_done())
266  {
267  int_buf()[xnext_field_offset++] = litr->domain_interval_begin();
268  int_buf()[xnext_field_offset++] = litr->domain_interval_end();
269  int_buf()[xnext_field_offset++] = litr->range_interval_begin();
270  int_buf()[xnext_field_offset++] = litr->range_interval_end();
271  litr->next_interval();
272 
273 #ifdef DIAGNOSTIC_OUTPUT
274  pod_index_type* lbuf = int_buf() + old_xnext_field_offset;
275  cout << setw(12) << lbuf[0]
276  << setw(12) << lbuf[1]
277  << setw(12) << lbuf[2]
278  << setw(12) << lbuf[3]
279  << endl;
280 #endif
281 
282  }
283  delete litr;
284 
285  // Postconditions:
286 
287  ensure(xnext_field_offset == buf_ub());
288 
289  // Exit
290 
291  return;
292 }
293 
295 void
296 sheaf::map_record::
297 transfer_external_buffer_to_HDF(hvl_t* xbuf)
298 {
299  // Preconditions:
300 
301  require(xbuf != 0);
302  require(is_external());
303 
304  // Body:
305 
306  xbuf->p = buf();
307  xbuf->len = buf_ub();
308 
309  // Postconditions:
310 
311  ensure(xbuf->p == buf());
312  ensure(xbuf->len == buf_ub());
313 
314  // Exit
315 
316  return;
317 }
318 
319 // ===========================================================
320 // READ FACET
321 // ===========================================================
322 
324 void
326 internalize(hvl_t* xhdf_buffer, interval_index_map& xmap)
327 {
328 
329  // Preconditions:
330 
331  require(scaffold().structure().state_is_read_write_accessible());
332 
333  // Body:
334 
335 #ifdef DIAGNOSTIC_OUTPUT
336  cout << "map_record::internalize:" << endl;
337 #endif
338 
339  // Initialize the external buffer from the HDF buffer.
340 
341  transfer_HDF_to_external_buffer(xhdf_buffer);
342 
343  // Convert the external buffer to internal format
344  // and place the result in the internal buffer
345 
346  convert_external_buffer_to_internal_buffer(xmap);
347 
348  // Transfer the internal buffer to the member.
349 
350  transfer_internal_buffer_to_map();
351 
352  // Postconditions:
353 
354  // Exit
355 
356  return;
357 }
358 
360 void
361 sheaf::map_record::
362 transfer_HDF_to_external_buffer(hvl_t* xbuf)
363 {
364  // Preconditions:
365 
366  require(xbuf != 0);
367 
368  // Body:
369 
370  put_buf(xbuf->p, xbuf->len);
371 
372  put_is_external(true);
373  put_is_internal(false);
374 
375  // Postconditions:
376 
377  ensure(buf() == xbuf->p);
378  ensure(buf_ub() == xbuf->len);
379  ensure(is_external());
380  ensure(!is_internal());
381 
382  // Exit
383 
384  return;
385 }
386 
388 void
389 sheaf::map_record::
390 convert_external_buffer_to_internal_buffer(interval_index_map& xmap)
391 {
392  // Preconditions:
393 
394  // Body:
395 
396  size_t lnext_field_offset = 0;
397 
398  // Internalize all the data fields.
399 
400  internalize_map_data(lnext_field_offset, xmap);
401 
402  // Now the internal buffer is valid.
403 
404  put_is_internal(true);
405 
406  // Postconditions:
407 
408  ensure(is_internal());
409 
410  // Exit
411 
412  return;
413 }
414 
416 void
417 sheaf::map_record::
418 internalize_map_data(size_t& xnext_field_offset, interval_index_map& xmap)
419 {
420 
421  // Preconditions:
422 
423  require(is_external());
424 
425  // Body:
426 
427  define_old_variable(size_t old_xnext_field_offset = xnext_field_offset);
428 
429  pod_index_type* lbuf = int_buf();
430  while(xnext_field_offset < buf_ub())
431  {
432 
433 #ifdef DIAGNOSTIC_OUTPUT
434  cout << setw(12) << lbuf[0]
435  << setw(12) << lbuf[1]
436  << setw(12) << lbuf[2]
437  << setw(12) << lbuf[3]
438  << endl;
439 #endif
440 
441  xmap.insert_interval(lbuf[0], lbuf[1], lbuf[2], lbuf[3]);
442  xnext_field_offset += 4;
443  lbuf += 4;
444  }
445 
446 
447 #ifdef DIAGNOSTIC_OUTPUT
448  cout << scaffold().structure().name()
449  << " file_id_map: " << endl;
450  xmap.print_map_rep(cout);
451  cout << xmap << endl;
452 #endif
453 
454  // Postconditions:
455 
456  ensure(xnext_field_offset == buf_ub());
457 
458  // Exit
459 
460  return;
461 }
462 
464 void
465 sheaf::map_record::
466 transfer_internal_buffer_to_map()
467 {
468  // Preconditions:
469 
470  require(scaffold().structure().state_is_read_write_accessible());
471 
472  // Body:
473 
474  // Nothing to do; the internal buffer is the map itself;
475 
476  // Postconditions:
477 
478  // Exit
479 
480  return;
481 }
482 
483 // ===========================================================
484 // ANY FACET
485 // ===========================================================
486 
490 clone() const
491 {
492  map_record* result;
493 
494  // Preconditions:
495 
496  // Body:
497 
498  result = new map_record(*this);
499 
500  // Postconditions:
501 
502  ensure(result != 0);
503  ensure(is_same_type(result));
504 
505  // Exit:
506 
507  return result;
508 }
509 
511 bool
513 invariant() const
514 {
515  bool result = true;
516 
517  // Preconditions:
518 
519  // Body:
520 
521  // Must satisfy base class invariant
522 
523  result = result && variable_length_record::invariant();
524 
525  if(invariant_check())
526  {
527  // Prevent recursive calls to invariant
528 
530 
531  // Finished, turn invariant checking back on.
532 
534  }
535 
536  // Postconditions:
537 
538  // Exit
539 
540  return result;
541 }
542 
544 bool
546 is_ancestor_of(const any* other) const
547 {
548 
549  // Preconditions:
550 
551  require(other != 0);
552 
553  // Body:
554 
555  // True if other conforms to this
556 
557  bool result = dynamic_cast<const map_record*>(other) != 0;
558 
559  // Postconditions:
560 
561  return result;
562 
563 }
564 
565 
virtual bool invariant() const
Class invariant.
poset_scaffold & scaffold()
The scaffold for the poset associated with this record (mutable version).
Definition: record.h:112
void * buf() const
The buffer.
The general variable length record wrapper/adapter for transferring data between the kernel and the i...
void externalize(hvl_t *xhdf_buffer, const interval_index_map &xmap)
Converts the record from internal to external form.
Definition: map_record.cc:105
void put_delete_buffer(bool xval)
Sets delete_buffer to value xval.
virtual ~map_record()
Destructor.
Definition: map_record.cc:81
void put_buf(const void *xbuf, size_t xub)
Sets the buffer to xbuf.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
Definition: map_record.cc:546
Abstract base class with useful features for all objects.
Definition: any.h:39
A record_set containing records of type member_record.
void internalize(hvl_t *xhdf_buffer, interval_index_map &xmap)
Converts the record from external to internal form.
Definition: map_record.cc:326
A record buffer for transferring file id map data for the member record data set between the kernel a...
Definition: map_record.h:43
size_t buf_ub() const
The size of the buffer, in bytes.
bool is_internal() const
True if the internal buffer has been initialized.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
void put_is_internal(bool xis_internal)
Sets is_internal to xis_internal.
virtual std::string name() const
The name of this poset.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
poset_state_handle & structure()
The handle for the poset being transferred. (Name chosen to void name conflict with class poset...
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
map_record(member_record_set &xhost)
Creates an instance in record set xhost for reading or writing member data.
Definition: map_record.cc:36
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual map_record * clone() const
Virtual constructor; makes a new instance of the same type as this.
Definition: map_record.cc:490
virtual bool invariant() const
Class invariant.
Definition: map_record.cc:513
bool is_external() const
True if the external buffer has been initialized.
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
void put_is_external(bool xis_external)
Sets is_external to xis_external.