CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chunk_detector.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_INGESTION_CHUNK_DETECTOR_H_
6 #define CVMFS_INGESTION_CHUNK_DETECTOR_H_
7 
8 #include <gtest/gtest_prod.h>
9 #include <stdint.h>
10 
11 #include <cstdlib>
12 
13 #include <algorithm>
14 
15 class BlockItem;
16 
22  public:
24  virtual ~ChunkDetector() { }
25  uint64_t FindNextCutMark(BlockItem *block);
26 
27  virtual bool MightFindChunks(uint64_t size) const = 0;
28 
29  protected:
30  virtual uint64_t DoFindNextCutMark(BlockItem *block) = 0;
31 
37  virtual uint64_t DoCut(uint64_t offset) {
38  last_cut_ = offset;
39  return offset;
40  }
41 
46  virtual uint64_t NoCut(uint64_t /* offset */) { return 0; }
47 
48  uint64_t last_cut() const { return last_cut_; }
49  uint64_t offset() const { return offset_; }
50 
51  private:
52  uint64_t last_cut_;
53  uint64_t offset_;
54 };
55 
56 
62  public:
63  explicit StaticOffsetDetector(uint64_t s) : chunk_size_(s) { }
64  bool MightFindChunks(uint64_t size) const { return size > chunk_size_; }
65 
66  protected:
67  virtual uint64_t DoFindNextCutMark(BlockItem *buffer);
68 
69  private:
70  const uint64_t chunk_size_;
71 };
72 
73 
90 class Xor32Detector : public ChunkDetector {
91  FRIEND_TEST(T_ChunkDetectors, Xor32);
92 
93  public:
94  Xor32Detector(const uint64_t minimal_chunk_size,
95  const uint64_t average_chunk_size,
96  const uint64_t maximal_chunk_size);
97 
98  bool MightFindChunks(const uint64_t size) const {
99  return size > minimal_chunk_size_;
100  }
101 
102  protected:
103  virtual uint64_t DoFindNextCutMark(BlockItem *buffer);
104 
105  virtual uint64_t DoCut(const uint64_t offset) {
106  xor32_ = 0;
107  xor32_ptr_ = offset;
108  return ChunkDetector::DoCut(offset);
109  }
110 
111  virtual uint64_t NoCut(const uint64_t offset) {
112  xor32_ptr_ = offset;
113  return ChunkDetector::NoCut(offset);
114  }
115 
116  inline void xor32(const unsigned char byte) {
117  xor32_ = (xor32_ << 1) ^ byte;
118  }
119 
120  inline bool CheckThreshold() {
121  return abs(static_cast<int32_t>(xor32_) - kMagicNumber) < threshold_;
122  }
123 
124  private:
125  // xor32 only depends on a window of the last 32 bytes in the data stream
126  static const unsigned kXor32Window = 32;
127  static const int32_t kMagicNumber;
128 
129  const uint64_t minimal_chunk_size_;
130  const uint64_t average_chunk_size_;
131  const uint64_t maximal_chunk_size_;
132  const int32_t threshold_;
133 
134  uint64_t xor32_ptr_;
135  uint32_t xor32_;
136 };
137 
138 #endif // CVMFS_INGESTION_CHUNK_DETECTOR_H_
virtual uint64_t DoFindNextCutMark(BlockItem *buffer)
void xor32(const unsigned char byte)
const uint64_t chunk_size_
virtual uint64_t DoCut(const uint64_t offset)
bool MightFindChunks(const uint64_t size) const
const uint64_t maximal_chunk_size_
uint64_t offset_
static const int32_t kMagicNumber
virtual uint64_t NoCut(const uint64_t offset)
uint64_t last_cut_
static const unsigned kXor32Window
FRIEND_TEST(T_ChunkDetectors, Xor32)
virtual uint64_t DoFindNextCutMark(BlockItem *buffer)
uint64_t xor32_ptr_
const uint64_t minimal_chunk_size_
const int32_t threshold_
bool MightFindChunks(uint64_t size) const
uint64_t FindNextCutMark(BlockItem *block)
virtual uint64_t NoCut(uint64_t)
Xor32Detector(const uint64_t minimal_chunk_size, const uint64_t average_chunk_size, const uint64_t maximal_chunk_size)
uint64_t offset() const
virtual ~ChunkDetector()
virtual uint64_t DoCut(uint64_t offset)
static void size_t size
Definition: smalloc.h:47
StaticOffsetDetector(uint64_t s)
virtual bool MightFindChunks(uint64_t size) const =0
bool CheckThreshold()
uint64_t last_cut() const
const uint64_t average_chunk_size_
virtual uint64_t DoFindNextCutMark(BlockItem *block)=0