CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chunk_detector.cc
Go to the documentation of this file.
1 
6 #include "chunk_detector.h"
7 
8 #include <algorithm>
9 #include <cassert>
10 #include <limits>
11 
12 #include "ingestion/item.h"
13 
14 
16  uint64_t result = DoFindNextCutMark(block);
17  if (result == 0)
18  offset_ += block->size();
19  return result;
20 }
21 
22 
23 //------------------------------------------------------------------------------
24 
25 
27  assert(buffer->type() == BlockItem::kBlockData);
28 
29  const uint64_t beginning = offset();
30  const uint64_t end = offset() + buffer->size();
31 
32  const uint64_t next_cut = last_cut() + chunk_size_;
33  if (next_cut >= beginning && next_cut < end) {
34  return DoCut(next_cut);
35  }
36 
37  return NoCut(next_cut);
38 }
39 
40 
41 //------------------------------------------------------------------------------
42 
43 
44 // This defines the center of the interval where the xor32 rolling checksum is
45 // queried. You should never change this number, since it affects the definition
46 // of cut marks.
47 const int32_t Xor32Detector::kMagicNumber = std::numeric_limits<uint32_t>::max()
48  / 2;
49 
50 
51 Xor32Detector::Xor32Detector(const uint64_t minimal_chunk_size,
52  const uint64_t average_chunk_size,
53  const uint64_t maximal_chunk_size)
54  : minimal_chunk_size_(minimal_chunk_size)
55  , average_chunk_size_(average_chunk_size)
56  , maximal_chunk_size_(maximal_chunk_size)
57  , threshold_(
58  (average_chunk_size > 0)
59  ? (std::numeric_limits<uint32_t>::max() / average_chunk_size)
60  : 0)
61  , xor32_ptr_(0)
62  , xor32_(0) {
64  if (minimal_chunk_size_ > 0) {
68  }
69 }
70 
71 
74  const unsigned char *data = buffer->data();
75 
76  // Get the offset where the next xor32 computation needs to be continued
77  // Note: this could be after collecting at least kMinChunkSize bytes in the
78  // current chunk, or directly at the beginning of the buffer, when a
79  // cut mark is currently searched
80  const uint64_t global_offset = std::max(
81  last_cut() + static_cast<uint64_t>(minimal_chunk_size_ - kXor32Window),
82  xor32_ptr_);
83 
84  // Check if the next xor32 computation is taking place in the current buffer
85  if (global_offset >= offset() + static_cast<uint64_t>(buffer->size())) {
86  return NoCut(global_offset);
87  }
88 
89  // get the byte offset in the current buffer
90  uint64_t internal_offset = global_offset - offset();
91  assert(internal_offset < static_cast<uint64_t>(buffer->size()));
92 
93  // Precompute the xor32 rolling checksum for finding the next cut mark
94  // Note: this might be skipped, if the precomputation was already performed
95  // for the current rolling checksum
96  // (internal_precompute_end will be negative --> loop is not entered)
97  const uint64_t precompute_end = last_cut() + minimal_chunk_size_;
98  const int64_t internal_precompute_end = std::min(
99  static_cast<int64_t>(precompute_end - offset()),
100  static_cast<int64_t>(buffer->size()));
101  assert(internal_precompute_end - static_cast<int64_t>(internal_offset)
102  <= static_cast<int64_t>(kXor32Window));
103  for (; static_cast<int64_t>(internal_offset) < internal_precompute_end;
104  ++internal_offset) {
105  xor32(data[internal_offset]);
106  }
107 
108  // Do the actual computation and try to find a xor32 based cut mark
109  // Note: this loop is bound either by kMaxChunkSize or by the size of the
110  // current buffer, thus the computation would continue later
111  const uint64_t internal_max_chunk_size_end = last_cut() + maximal_chunk_size_
112  - offset();
113  const uint64_t internal_compute_end = std::min(
114  internal_max_chunk_size_end, static_cast<uint64_t>(buffer->size()));
115  for (; internal_offset < internal_compute_end; ++internal_offset) {
116  xor32(data[internal_offset]);
117 
118  // check if we found a cut mark
119  if (CheckThreshold()) {
120  return DoCut(internal_offset + offset());
121  }
122  }
123 
124  // Check if the loop was exited because we reached kMaxChunkSize and do a
125  // hard cut in this case. If not, it exited because we ran out of data in this
126  // buffer --> continue computation with the next buffer
127  if (internal_offset == internal_max_chunk_size_end) {
128  return DoCut(internal_offset + offset());
129  } else {
130  return NoCut(internal_offset + offset());
131  }
132 }
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)
const uint64_t maximal_chunk_size_
uint64_t offset_
static const int32_t kMagicNumber
virtual uint64_t NoCut(const uint64_t offset)
assert((mem||(size==0))&&"Out Of Memory")
unsigned char * data()
Definition: item.h:211
static const unsigned kXor32Window
virtual uint64_t DoFindNextCutMark(BlockItem *buffer)
uint64_t xor32_ptr_
const uint64_t minimal_chunk_size_
uint32_t size()
Definition: item.h:213
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
BlockType type()
Definition: item.h:219
virtual uint64_t DoCut(uint64_t offset)
bool CheckThreshold()
uint64_t last_cut() const
const uint64_t average_chunk_size_
virtual uint64_t DoFindNextCutMark(BlockItem *block)=0