CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file_backed_buffer.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_FILE_BACKED_BUFFER_H_
6 #define CVMFS_UTIL_FILE_BACKED_BUFFER_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "gtest/gtest_prod.h"
13 #include "util/mmap_file.h"
14 #include "util/single_copy.h"
15 
37  FRIEND_TEST(T_FileBackedBuffer, SimpleWriteAndRead);
38  FRIEND_TEST(T_FileBackedBuffer, EmptyBuffer);
39  FRIEND_TEST(T_FileBackedBuffer, EmptyWrite);
40  FRIEND_TEST(T_FileBackedBuffer, StraightToFile);
41  FRIEND_TEST(T_FileBackedBuffer, OnlyInMemory);
42 
43  public:
44  static FileBackedBuffer *Create(uint64_t in_memory_threshold,
45  const std::string &tmp_dir = "/tmp/");
46 
48  void Append(const void *source, uint64_t len);
49  void Commit();
50  int64_t Data(void** ptr, int64_t len, uint64_t pos);
51  int64_t Read(void* ptr, int64_t len);
52  int64_t ReadP(void* ptr, int64_t len, uint64_t pos);
53  void Rewind();
54 
55  uint64_t GetSize() const;
56 
57  private:
58  FileBackedBuffer(uint64_t in_memory_threshold, const std::string &tmp_dir);
59  void SaveToFile();
60 
61  const uint64_t in_memory_threshold_;
62  const std::string tmp_dir_;
63 
64  enum {
67  } state_;
68 
69  enum {
72  } mode_;
73 
74  uint64_t size_;
75  // used for writing and reading in kMemoryMode
76  unsigned char *buf_;
77  // used for reading and writing in both modes
78  uint64_t pos_;
79  // used for writing in kFileMode
80  FILE *fp_;
81  // used for writing and reading (opened mmapped_) in kFileMode
82  std::string file_path_;
83  // used for reading in kFileMode
85 };
86 
87 #endif // CVMFS_UTIL_FILE_BACKED_BUFFER_H_
FRIEND_TEST(T_FileBackedBuffer, SimpleWriteAndRead)
enum FileBackedBuffer::@5 state_
std::string file_path_
static FileBackedBuffer * Create(uint64_t in_memory_threshold, const std::string &tmp_dir="/tmp/")
const uint64_t in_memory_threshold_
MemoryMappedFile * mmapped_
int64_t ReadP(void *ptr, int64_t len, uint64_t pos)
unsigned char * buf_
int64_t Data(void **ptr, int64_t len, uint64_t pos)
int64_t Read(void *ptr, int64_t len)
enum FileBackedBuffer::@6 mode_
void Append(const void *source, uint64_t len)
FileBackedBuffer(uint64_t in_memory_threshold, const std::string &tmp_dir)
uint64_t GetSize() const
const std::string tmp_dir_