proxygen
folly::RecordIOWriter Class Reference

#include <RecordIO.h>

Public Member Functions

 RecordIOWriter (File file, uint32_t fileId=1)
 
void write (std::unique_ptr< IOBuf > buf)
 
off_t filePos () const
 

Private Attributes

File file_
 
uint32_t fileId_
 
std::unique_lock< FilewriteLock_
 
std::atomic< off_t > filePos_
 

Detailed Description

Class to write a stream of RecordIO records to a file.

RecordIOWriter is thread-safe

Definition at line 45 of file RecordIO.h.

Constructor & Destructor Documentation

folly::RecordIOWriter::RecordIOWriter ( File  file,
uint32_t  fileId = 1 
)
explicit

Create a RecordIOWriter around a file; will append to the end of file if it exists.

Each file must have a non-zero file id, which is embedded in all record headers. Readers will only return records with the requested file id (or, if the reader is created with fileId=0 in the constructor, the reader will return all records). File ids are only used to allow resynchronization if you store RecordIO records (with headers) inside other RecordIO records (for example, if a record consists of a fragment from another RecordIO file). If you're not planning to do that, the defaults are fine.

Definition at line 33 of file RecordIO.cpp.

References folly::checkUnixError(), folly::File::fd(), file_, filePos_, and writeLock_.

34  : file_(std::move(file)),
35  fileId_(fileId),
36  writeLock_(file_, std::defer_lock),
37  filePos_(0) {
38  if (!writeLock_.try_lock()) {
39  throw std::runtime_error("RecordIOWriter: file locked by another process");
40  }
41 
42  struct stat st;
43  checkUnixError(fstat(file_.fd(), &st), "fstat() failed");
44 
45  filePos_ = st.st_size;
46 }
std::atomic< off_t > filePos_
Definition: RecordIO.h:80
std::unique_lock< File > writeLock_
Definition: RecordIO.h:79
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
int fd() const
Definition: File.h:85
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101

Member Function Documentation

off_t folly::RecordIOWriter::filePos ( ) const
inline

Return the position in the file where the next byte will be written. Conservative, as stuff can be written at any time from another thread.

Definition at line 72 of file RecordIO.h.

References filePos_.

Referenced by folly::test::TEST().

72  {
73  return filePos_;
74  }
std::atomic< off_t > filePos_
Definition: RecordIO.h:80
void folly::RecordIOWriter::write ( std::unique_ptr< IOBuf buf)

Write a record. We will use at most headerSize() bytes of headroom, you might want to arrange that before copying your data into it.

Definition at line 48 of file RecordIO.cpp.

References folly::checkUnixError(), folly::File::fd(), file_, fileId_, filePos_, folly::recordio_helpers::prependHeader(), folly::pwriteFull(), and folly::pwritevFull().

Referenced by folly::test::TEST().

48  {
49  size_t totalLength = prependHeader(buf, fileId_);
50  if (totalLength == 0) {
51  return; // nothing to do
52  }
53 
54  DCHECK_EQ(buf->computeChainDataLength(), totalLength);
55 
56  // We're going to write. Reserve space for ourselves.
57  off_t pos = filePos_.fetch_add(off_t(totalLength));
58 
59 #if FOLLY_HAVE_PWRITEV
60  auto iov = buf->getIov();
61  ssize_t bytes = pwritevFull(file_.fd(), iov.data(), iov.size(), pos);
62 #else
63  buf->unshare();
64  buf->coalesce();
65  ssize_t bytes = pwriteFull(file_.fd(), buf->data(), buf->length(), pos);
66 #endif
67 
68  checkUnixError(bytes, "pwrite() failed");
69  DCHECK_EQ(size_t(bytes), totalLength);
70 }
std::atomic< off_t > filePos_
Definition: RecordIO.h:80
size_t prependHeader(std::unique_ptr< IOBuf > &buf, uint32_t fileId)
Definition: RecordIO.cpp:139
ssize_t pwriteFull(int fd, const void *buf, size_t count, off_t offset)
Definition: FileUtil.cpp:138
int fd() const
Definition: File.h:85
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
ssize_t pwritevFull(int fd, iovec *iov, int count, off_t offset)
Definition: FileUtil.cpp:154

Member Data Documentation

File folly::RecordIOWriter::file_
private

Definition at line 77 of file RecordIO.h.

Referenced by RecordIOWriter(), and write().

uint32_t folly::RecordIOWriter::fileId_
private

Definition at line 78 of file RecordIO.h.

Referenced by write().

std::atomic<off_t> folly::RecordIOWriter::filePos_
private

Definition at line 80 of file RecordIO.h.

Referenced by filePos(), RecordIOWriter(), and write().

std::unique_lock<File> folly::RecordIOWriter::writeLock_
private

Definition at line 79 of file RecordIO.h.

Referenced by RecordIOWriter().


The documentation for this class was generated from the following files: