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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_wroot_basket
  5 #define tools_wroot_basket
  6 
  7 #include "ibo"
  8 #include "key"
  9 #include "buffer"
 10 
 11 namespace tools {
 12 namespace wroot {
 13 
 14 class basket : public virtual ibo, public key {
 15   typedef key parent;
 16 public:
 17   static const std::string& s_class() {
 18     static const std::string s_v("tools::wroot::basket");
 19     return s_v;
 20   }
 21 public: //ibo
 22   virtual const std::string& store_cls() const {
 23     static const std::string s_v("TBasket");
 24     return s_v;
 25   }
 26   virtual bool stream(buffer& a_buffer) const {
 27     // in principle we pass here only for the last basket
 28     // of a branch when it is streamed from branch::stream().
 29 
 30     // We pass also here in the case of filling "tree in a tree" (MEMPHYS sim).
 31 
 32     // some consitency checks :
 33     //G.Barrand : the below test is "too much". Someone
 34     //            may have to write a tree (and then a basket)
 35     //            which had been never filled.
 36     //            Moreover with the today branch code, a
 37     //              basket.write_on_file()
 38     //            happens only in a branch.fill() that deletes
 39     //            the basket just after the call.
 40   //if(!m_data.length()) {
 41   //  // to be sure to not work on a basket already written
 42   //  // with write_on_file()
 43   //  m_out << "tools::wroot::basket::stream :"
 44   //        << " m_data.length() is null."
 45   //        << std::endl;
 46   //  return false;
 47   //}
 48     if(m_seek_key) {
 49       m_out << "tools::wroot::basket::stream :"
 50             << " m_seek_key is not null (" << m_seek_key << ")."
 51             << std::endl;
 52       return false;
 53     }
 54     if(m_last) {
 55       m_out << "tools::wroot::basket::stream :"
 56             << " m_last is not null."
 57             << std::endl;
 58       return false;
 59     }
 60     if(!m_entry_offset) {
 61       m_out << "tools::wroot::basket::stream :"
 62             << " m_entry_offset is null."
 63             << std::endl;
 64       return false;
 65     }
 66 
 67    {uint32 _last = m_data.length()+m_key_length;
 68     if(_last>m_last) {
 69       const_cast<basket&>(*this).m_last = _last;
 70     }}
 71     if(m_last>m_buf_size) {
 72       const_cast<basket&>(*this).m_buf_size = m_last;
 73     }
 74 
 75     char flag = 11;
 76     if(m_displacement)  flag += 40;
 77     if(!_stream_header(a_buffer,m_verbose,flag)) return false;
 78 
 79     if(m_entry_offset && m_nev) {
 80       if(!a_buffer.write_array(m_entry_offset,m_nev)) return false;
 81       if(m_displacement) {
 82         if(!a_buffer.write_array(m_displacement,m_nev)) return false;
 83       }
 84     }
 85 
 86     if(m_data.to_displace()) {
 87       if(!const_cast<basket&>(*this).m_data.displace_mapped(m_key_length)) {
 88         m_out << "tools::wroot::basket::stream :"
 89               << " m_data.displace_mapped() failed."
 90               << std::endl;
 91         return false;
 92       }
 93     }
 94 
 95     buffer bref(m_out,a_buffer.byte_swap(),256);
 96     if(!_stream_header(bref,m_verbose)) return false; //then header stored twice !
 97     //if(bref.length()!=m_key_length) {}
 98     if(!bref.write_fast_array(m_data.buf(),m_data.length())) return false;
 99     if(!a_buffer.write_fast_array(bref.buf(),bref.length())) return false;
100 
101     return true;
102   }
103 public:
104   basket(std::ostream& a_out,
105          bool a_byte_swap,
106          seek a_seek_directory,
107          const std::string& a_object_name,
108          const std::string& a_object_title,
109          const std::string& a_object_class,
110          uint32 a_basket_size,bool a_verbose)
111   :parent(a_out,a_seek_directory,a_object_name,a_object_title,a_object_class)
112   ,m_verbose(a_verbose)
113   ,m_data(a_out,a_byte_swap,a_basket_size)
114   ,m_nev_buf_size(1000)
115   ,m_nev(0)
116   ,m_last(0)
117   ,m_entry_offset(0)
118   ,m_displacement(0)
119   {
120 #ifdef TOOLS_MEM
121     mem::increment(s_class().c_str());
122 #endif
123 
124     if(m_version>big_file_version_tag()) {
125     } else {
126       // G.Barrand : April 2016 : WARNING : this is a change in the ROOT/IO format compared to CERN-ROOT/4.x :
127       // We follow the logic found on CERN-ROOT/5.x that enforces "+1000" on all baskets.
128       m_version += big_file_version_tag();
129     }
130 
131     m_key_length = header_record_size(m_version);
132     initialize_zero();
133 
134     if(m_nev_buf_size) {
135       m_entry_offset = new int[m_nev_buf_size];
136      {for(uint32 i=0;i<m_nev_buf_size;i++) m_entry_offset[i] = 0;}
137     }
138   }
139   virtual ~basket(){
140     delete [] m_entry_offset;
141     delete [] m_displacement;
142     m_entry_offset = 0;
143     m_displacement = 0;
144 #ifdef TOOLS_MEM
145     mem::decrement(s_class().c_str());
146 #endif
147   }
148 protected:
149   basket(const basket& a_from)
150   :ibo(a_from)
151   ,parent(a_from)
152   ,m_verbose(a_from.m_verbose)
153   ,m_data(m_out,a_from.m_data.byte_swap(),256)
154   ,m_nev_buf_size(a_from.m_nev_buf_size)
155   ,m_nev(a_from.m_nev)
156   ,m_last(a_from.m_last)
157   ,m_entry_offset(0)
158   ,m_displacement(0)
159   {
160 #ifdef TOOLS_MEM
161     mem::increment(s_class().c_str());
162 #endif
163   }
164   basket& operator=(const basket& a_from){
165     parent::operator=(a_from);
166     m_nev_buf_size = a_from.m_nev_buf_size;
167     m_nev = a_from.m_nev;
168     m_last = a_from.m_last;
169     return *this;
170   }
171 public:
172   const buffer& datbuf() const {return m_data;}
173   buffer& datbuf() {return m_data;}
174 
175   const int* entry_offset() const {return m_entry_offset;}
176   int* entry_offset() {return m_entry_offset;}
177 
178   const int* displacement() const {return m_displacement;}
179   int* displacement() {return m_displacement;}
180 
181   uint32 nev_buf_size() const {return m_nev_buf_size;}
182   uint32 nev() const {return m_nev;}
183   uint32 last() const {return m_last;}
184 
185   void set_nev(uint32 a_last,uint32 a_nev_buf_size,uint32 a_nev,const int* a_entry_offset,const int* a_displacement) {
186     //used in mpi_create_basket.
187     m_last = a_last;
188     m_nev_buf_size = a_nev_buf_size;
189     m_nev = a_nev;
190     delete [] m_entry_offset;
191     m_entry_offset = 0;
192     delete [] m_displacement;
193     m_displacement = 0;
194 
195     if(a_entry_offset && m_nev_buf_size) {
196       m_entry_offset = new int[m_nev_buf_size];
197       for(uint32 i=0;i<m_nev;i++) m_entry_offset[i] = a_entry_offset[i];
198     }
199 
200     if(a_displacement && m_nev_buf_size) {
201       m_displacement = new int[m_nev_buf_size];
202       for(uint32 i=0;i<m_nev;i++) m_displacement[i] = a_displacement[i];
203     }
204   }
205 
206   bool update(uint32 a_offset) {
207     if(m_entry_offset) {
208       if((m_nev+1)>=m_nev_buf_size) { // for the +1, we follow CERN-ROOT/TBasket logic that wants to store
209                                       // nev+1 elements for m_entry_offset in the write_on_file() method.
210         uint32 newsize = mx<uint32>(10,2*m_nev_buf_size);
211         if(!realloc<int>(m_entry_offset,newsize,m_nev_buf_size,true)){
212           m_out << "tools::wroot::basket::update : realloc failed." << std::endl;
213           return false;
214         }
215         if(m_displacement) {
216           if(!realloc<int>(m_displacement,newsize,m_nev_buf_size,true)){
217             m_out << "tools::wroot::basket::update : realloc failed." << std::endl;
218             return false;
219           }
220         }
221         m_nev_buf_size = newsize;
222       }
223       m_entry_offset[m_nev] = (int)a_offset;
224     }
225     m_nev++;
226     return true;
227   }
228 
229   bool write_on_file(ifile& a_file,uint16 a_cycle,uint32& a_nbytes) {
230     // write m_data buffer into file.
231     //NOTE : m_data does not contain the key at its head.
232     //       At this point m_seek_key should be 0.
233 
234     a_nbytes = 0;
235 
236     if(m_seek_key) {
237       m_out << "tools::wroot::basket::write_on_file :"
238             << " m_seek_key should be 0 (" << m_seek_key << ")."
239             << std::endl;
240       return false;
241     }
242 
243     if(m_version>big_file_version_tag()) {
244     } else {
245       m_out << "tools::wroot::basket::write_on_file : "
246             << " we should not pass here (1)."
247             << std::endl;
248       return false;
249 /*
250       if(a_file.END()>START_BIG_FILE()) {
251         //GB : enforce m_version>big_file_version_tag() if m_version is still 2 but
252         //     seek_key>START_BIG_FILE. If not doing that we shall
253         //     write a big m_seek_key on a seek32 and then have
254         //     a problem when reading.
255 
256         //m_out << "tools::wroot::basket::write_on_file : "
257         //      << " WARNING : pos>START_BIG_FILE."
258         //      << std::endl;
259 
260         m_version += big_file_version_tag();
261         m_key_length += 8;
262 
263         if(m_entry_offset) {
264           for(uint32 i=0;i<m_nev;i++) m_entry_offset[i] += 8;
265           if(m_displacement) {
266             //??? Do we have to shift them ?
267             m_out << "tools::wroot::basket::write_on_file : "
268                   << " displace logic : m_displacement not null."
269                   << std::endl;
270           }
271         }
272 
273       }
274 */
275     }
276 
277     // Transfer m_entry_offset table at the end of fBuffer. Offsets to fBuffer
278     // are transformed in entry length to optimize compression algorithm.
279     m_last = m_key_length+m_data.length();
280     if(m_entry_offset) {
281       if(!m_data.write_array<int>(m_entry_offset,m_nev+1)) { // for the +1 we follow CERN-ROOT/TBasket logic.
282         delete [] m_entry_offset;
283         m_entry_offset = 0;
284         return false;
285       }
286       delete [] m_entry_offset;
287       m_entry_offset = 0;
288       if(m_displacement) {
289         if(!m_data.write_array<int>(m_displacement,m_nev+1)) {
290           delete [] m_displacement;
291           m_displacement = 0;
292           return false;
293         }
294         delete [] m_displacement;
295         m_displacement = 0;
296       }
297     }
298 
299     m_object_size = m_data.length(); //uncompressed size.
300 
301     m_cycle = a_cycle;
302 
303     if(!m_data.displace_mapped(m_key_length)) return false;
304 
305     char* kbuf = 0;
306     uint32 klen = 0;
307     bool kdelete = false;
308     a_file.compress_buffer(m_data,kbuf,klen,kdelete);
309 
310     if(!initialize(a_file,klen)) { //it will do a m_seek_key = a_file.END() and then a_file.set_END(...)
311       m_out << "tools::wroot::basket::write_on_file :"
312             << " initialize() failed."
313             << std::endl;
314       if(kdelete) delete [] kbuf;
315       return false;
316     }
317 
318     //write header of the key :
319    {buffer bref(m_out,a_file.byte_swap(),256);
320     if(!_stream_header(bref,a_file.verbose())) return false;
321     if(bref.length()!=m_key_length) {
322       m_out << "tools::wroot::basket::write_on_file :"
323             << " key len anomaly " << bref.length()
324             << " m_key_length " << m_key_length
325             << std::endl;
326       if(kdelete) delete [] kbuf;
327       return false;
328     }
329     ::memcpy(m_buffer,bref.buf(),m_key_length);}
330 
331     ::memcpy(m_buffer+m_key_length,kbuf,klen);
332     if(kdelete) delete [] kbuf;
333 
334     uint32 nbytes;
335     if(!parent::write_file(a_file,nbytes)) return false;
336 
337     m_data.pos() = m_data.buf(); //empty m_data.
338 
339     a_nbytes = m_key_length + klen;
340     return true;
341   }
342 protected:
343   uint32 header_record_size(uint32 a_version) const {
344     // header only.
345     uint32 nbytes = parent::record_size(a_version);
346 
347     nbytes += sizeof(short);   //version
348     nbytes += sizeof(uint32); //m_buf_size
349     nbytes += sizeof(uint32); //m_nev_buf_size
350     nbytes += sizeof(uint32); //m_nev
351     nbytes += sizeof(uint32); //m_last
352     nbytes += sizeof(char);   //flag
353 
354     return nbytes;
355   }
356   bool _stream_header(buffer& a_buffer,bool a_verbose,char a_flag = 0) const {
357    {uint32 l = parent::record_size(m_version);
358     if((a_buffer.length()+l)>a_buffer.size()) {
359       if(!a_buffer.expand(a_buffer.size()+l)) return false;
360     }
361     wbuf wb(m_out,a_buffer.byte_swap(),a_buffer.max_pos(),a_buffer.pos());
362     if(!parent::to_buffer(wb,a_verbose)) return false;}
363 
364     if(!a_buffer.write_version(2)) return false;
365     if(!a_buffer.write(m_buf_size)) return false;
366     if(!a_buffer.write(m_nev_buf_size)) return false;
367     if(!a_buffer.write(m_nev)) return false;
368     if(!a_buffer.write(m_last)) return false;
369     if(!a_buffer.write(a_flag)) return false;
370     return true;
371   }
372 protected:
373   bool m_verbose;
374   buffer m_data;
375 protected:
376   uint32 m_nev_buf_size;  //Length in Int_t of m_entry_offset
377   uint32 m_nev;           //Number of entries in basket
378   uint32 m_last;          //Pointer to last used byte in basket
379   int* m_entry_offset;    //[m_nev] Offset of entries in fBuffer(TKey)
380   int* m_displacement;    //![m_nev] Displacement of entries in fBuffer(TKey)
381 };
382 
383 }}
384 
385 #endif