CMS 3D CMS Logo

Storage.cc
Go to the documentation of this file.
3 #include <cassert>
4 
5 using namespace edm::storage;
6 
8 
10 
12 IOSize Storage::read(IOBuffer into, IOOffset pos) { return read(into.data(), into.size(), pos); }
13 
15  // FIXME: this is not thread safe! split into separate interface
16  // that a particular storage can choose to support or not? make
17  // sure that throw semantics are correct here!
18  // FIXME: use saveposition object in case exceptions are thrown?
19  IOOffset here = position();
20  position(pos);
21  n = read(into, n);
22  position(here);
23  return n;
24 }
25 
27  IOOffset here = position();
28  IOSize total = 0;
29  for (IOSize i = 0; i < n; ++i) {
30  try {
31  position(into[i].offset());
32  total += read(into[i].data(), into[i].size());
33  } catch (cms::Exception &) {
34  if (!total)
35  throw;
36  break;
37  }
38  }
39  position(here);
40  return total;
41 }
42 
46 
88  unsigned char byte;
89  IOSize n = read(&byte, 1);
90  return n == 0 ? -1 : byte;
91 }
92 
115 IOSize Storage::read(IOBuffer into) { return read(into.data(), into.size()); }
116 
147  assert(!buffers || into);
148 
149  // Keep reading as long as possible; ignore errors if we have read
150  // something, otherwise pass it on.
151  IOSize status;
152  IOSize done = 0;
153  try {
154  for (IOSize i = 0; i < buffers; done += status, ++i)
155  if ((status = read(into[i])) == 0)
156  break;
157  } catch (cms::Exception &) {
158  if (!done)
159  throw;
160  }
161 
162  return done;
163 }
164 
168 
190 IOSize Storage::xread(IOBuffer into) { return xread(into.data(), into.size()); }
191 
214  assert(into);
215 
216  // Keep reading as long as possible. Let system errors fly over
217  // us, they are a hard error.
218  IOSize x;
219  IOSize done = 0;
220  while (done < n && (x = read((char *)into + done, n - done)))
221  done += x;
222 
223  return done;
224 }
225 
250  // FIXME: Use read(into, buffers) and then sort out in case of
251  // failure, the readv probably succeed directly with much less
252  // overhead.
253 
254  assert(!buffers || into);
255 
256  // Keep reading as long as possible. Let system errors fly
257  // over us, they are a hard error.
258  IOSize x;
259  IOSize done = 0;
260  for (IOSize i = 0; i < buffers; ++i) {
261  done += (x = xread(into[i]));
262  if (x < into[i].size())
263  break;
264  }
265  return done;
266 }
267 
269 IOSize Storage::write(IOBuffer from, IOOffset pos) { return write(from.data(), from.size(), pos); }
270 
271 IOSize Storage::write(const void *from, IOSize n, IOOffset pos) {
272  // FIXME: this is not thread safe! split into separate interface
273  // that a particular storage can choose to support or not? make
274  // sure that throw semantics are correct here!
275 
276  // FIXME: use saveposition object in case exceptions are thrown?
277  IOOffset here = position();
278  position(pos);
279  n = write(from, n);
280  position(here);
281  return n;
282 }
283 
285  IOSize total = 0;
286  for (IOSize i = 0; i < n; ++i) {
287  try {
288  total += write(from[i].data(), from[i].size(), from[i].offset());
289  } catch (cms::Exception &) {
290  if (!total)
291  throw;
292  break;
293  }
294  }
295  return total;
296 }
297 
301 
329 IOSize Storage::write(unsigned char byte) { return write(&byte, 1); }
330 
349 IOSize Storage::write(IOBuffer from) { return write(from.data(), from.size()); }
350 
374 IOSize Storage::writev(const IOBuffer *from, IOSize buffers) {
375  assert(!buffers || from);
376 
377  // Keep writing as long as possible; ignore errors if we have
378  // written something, otherwise pass it on.
379  IOSize x;
380  IOSize done = 0;
381  try {
382  for (IOSize i = 0; i < buffers; ++i) {
383  done += (x = write(from[i].data(), from[i].size()));
384  if (x < from[i].size())
385  break;
386  }
387  } catch (cms::Exception &) {
388  if (!done)
389  throw;
390  }
391 
392  return done;
393 }
394 
398 
424 IOSize Storage::xwrite(IOBuffer from) { return xwrite(from.data(), from.size()); }
425 
452 IOSize Storage::xwrite(const void *from, IOSize n) {
453  // Keep writing until we've written it all. Let errors fly over.
454  IOSize done = 0;
455  while (done < n)
456  done += write((const char *)from + done, n - done);
457 
458  return done;
459 }
460 
489 IOSize Storage::xwritev(const IOBuffer *from, IOSize buffers) {
490  // Keep writing until we've written it all. Let errors fly over.
491  // FIXME: Use writev(from, buffers) and then sort out in case of
492  // failure, the writev probably succeed directly with much less
493  // overhead.
494  assert(!buffers || from);
495 
496  IOSize done = 0;
497  for (IOSize i = 0; i < buffers; ++i)
498  done += xwrite(from[i].data(), from[i].size());
499 
500  return done;
501 }
502 
505  Storage *self = const_cast<Storage *>(this);
506  return self->position(0, CURRENT);
507 }
508 
510  // FIXME: use saveposition object in case exceptions are thrown?
511  Storage *self = const_cast<Storage *>(this);
512  IOOffset here = position();
513  self->position(0, END);
514  IOOffset size = position();
515  self->position(here); // FIXME: VERIFY()?
516  return size;
517 }
518 
519 void Storage::rewind() { position(0); }
520 
522 bool Storage::prefetch(const IOPosBuffer * /* what */, IOSize /* n */) { return false; }
523 
525 void Storage::flush() {}
526 
527 void Storage::close() {}
528 
530 bool Storage::eof() const { return position() == size(); }
int64_t IOOffset
Definition: IOTypes.h:20
virtual IOSize readv(IOBuffer *into, IOSize buffers)
Definition: Storage.cc:146
virtual bool eof() const
Definition: Storage.cc:530
IOSize xreadv(IOBuffer *into, IOSize buffers)
Definition: Storage.cc:249
IOSize xwritev(const IOBuffer *from, IOSize buffers)
Definition: Storage.cc:489
virtual void rewind()
Definition: Storage.cc:519
assert(be >=bs)
virtual IOOffset size() const
Definition: Storage.cc:509
void * data() const
Definition: IOBuffer.h:33
IOSize size() const
Definition: IOBuffer.h:36
virtual void flush()
Definition: Storage.cc:525
size_t IOSize
Definition: IOTypes.h:15
virtual IOSize writev(const IOBuffer *from, IOSize buffers)
Definition: Storage.cc:374
virtual ~Storage()
Definition: Storage.cc:9
IOSize write(unsigned char byte)
Definition: Storage.cc:329
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
IOSize xread(IOBuffer into)
Definition: Storage.cc:190
virtual IOOffset position() const
Definition: Storage.cc:504
IOSize xwrite(const void *from, IOSize n)
Definition: Storage.cc:452
virtual void close()
Definition: Storage.cc:527
virtual bool prefetch(const IOPosBuffer *what, IOSize n)
Definition: Storage.cc:522