CernVM-FS  2.10.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/export.h"
14 #include "util/mmap_file.h"
15 #include "util/single_copy.h"
16 
38  FRIEND_TEST(T_FileBackedBuffer, SimpleWriteAndRead);
39  FRIEND_TEST(T_FileBackedBuffer, EmptyBuffer);
40  FRIEND_TEST(T_FileBackedBuffer, EmptyWrite);
41  FRIEND_TEST(T_FileBackedBuffer, StraightToFile);
42  FRIEND_TEST(T_FileBackedBuffer, OnlyInMemory);
43 
44  public:
45  static FileBackedBuffer *Create(uint64_t in_memory_threshold,
46  const std::string &tmp_dir = "/tmp/");
47 
49  void Append(const void *source, uint64_t len);
50  void Commit();
51  int64_t Data(void** ptr, int64_t len, uint64_t pos);
52  int64_t Read(void* ptr, int64_t len);
53  int64_t ReadP(void* ptr, int64_t len, uint64_t pos);
54  void Rewind();
55 
56  uint64_t GetSize() const;
57 
58  private:
59  FileBackedBuffer(uint64_t in_memory_threshold, const std::string &tmp_dir);
60  void SaveToFile();
61 
62  const uint64_t in_memory_threshold_;
63  const std::string tmp_dir_;
64 
65  enum {
66  kWriteState = 0,
67  kReadState
68  } state_;
69 
70  enum {
71  kMemoryMode = 0,
72  kFileMode
73  } mode_;
74 
75  uint64_t size_;
76  // used for writing and reading in kMemoryMode
77  unsigned char *buf_;
78  // used for reading and writing in both modes
79  uint64_t pos_;
80  // used for writing in kFileMode
81  FILE *fp_;
82  // used for writing and reading (opened mmapped_) in kFileMode
83  std::string file_path_;
84  // used for reading in kFileMode
86 };
87 
88 #endif // CVMFS_UTIL_FILE_BACKED_BUFFER_H_
std::string file_path_
static Publisher * Create(const SettingsPublisher &settings)
#define CVMFS_EXPORT
Definition: export.h:11
const uint64_t in_memory_threshold_
static void Read(void *buf, size_t nbyte)
Definition: helper_util.cc:94
MemoryMappedFile * mmapped_
unsigned char * buf_
const std::string tmp_dir_