XRootD
BufferUtils.cc
Go to the documentation of this file.
1 
2 #include "BufferUtils.hh"
3 #include <algorithm> // std::max
4 
5 using namespace XrdCephBuffer;
6 
7 #ifdef CEPHBUFDEBUG
8 // to synchronise logging statements
9  std::mutex cephbuf_iolock;
10 #endif
11 
12 // ------------------------------------------------------ //
13 // Extent //
14 
15 bool Extent::in_extent(off_t pos) const
16 {
17  return ((pos > begin()) && (pos < end()));
18 }
19 
20 bool Extent::isContiguous(const Extent &rhs) const
21 {
22  // does the rhs connect directly to the end of the first
23  if (end() != rhs.begin())
24  return false;
25  return true;
26 }
27 
28 bool Extent::allInExtent(off_t pos, size_t len) const
29 {
30  // is all the range in this extent
31  if ((pos < begin()) || (pos >= end()))
32  return false;
33 
34  if (off_t(pos + len) > end())
35  return false;
36  return true;
37 }
38 bool Extent::someInExtent(off_t pos, size_t len) const
39 { // is some of the range in this extent
40  if ((off_t(pos + len) < begin()) || (pos >= end()))
41  return false;
42  return true;
43 }
44 
45 Extent Extent::containedExtent(off_t pos, size_t len) const
46 {
47  // return the subset of input range that is in this extent
48  off_t subbeg = std::max(begin(), pos);
49  off_t subend = std::min(end(), off_t(pos + len));
50 
51  return Extent(subbeg, subend - subbeg);
52 }
54 {
55  return containedExtent(rhs.begin(), rhs.len());
56 }
57 
58 bool Extent::operator<(const Extent &rhs) const
59 {
60  // comparison primarily on begin values
61  // use end values if begin values are equal.
62 
63  if (begin() > rhs.begin()) return false;
64  if (begin() < rhs.begin()) return true;
65  if (end() < rhs.end() ) return true;
66  return false;
67 }
68 bool Extent::operator==(const Extent &rhs) const
69 {
70  // equivalence based only on start and end
71  if (begin() != rhs.begin())
72  return false;
73  if (end() != rhs.end())
74  return false;
75  return true;
76 }
77 
78 // ------------------------------------------------------ //
79 // ExtentHolder //
80 
82 
84 {
85  m_extents.reserve(elements);
86 }
87 
89 {
90  m_extents.reserve(extents.size());
91  for (ExtentContainer::const_iterator vit = m_extents.cbegin(); vit != m_extents.cend(); ++vit) {
92  push_back(*vit);
93  }
94 
95 }
97 {
98  m_extents.clear();
99 }
100 
101 void ExtentHolder::push_back(const Extent & in) {
102  if (size()) {
103  m_begin = std::min(m_begin, in.begin());
104  m_end = std::max(m_end, in.end());
105  } else {
106  m_begin = in.begin();
107  m_end = in.end();
108  }
109  return m_extents.push_back(in);
110 }
111 
112 
113 
115  // if (!size()) return Extent(0,0);
116  // ExtentContainer se = getSortedExtents();
117  // off_t b = se.front().begin();
118  // off_t e = se.back().end();
119 
120  return Extent(m_begin, m_end-m_begin);
121 
122 }
123 
125  size_t nbytes{0};
126  for (ExtentContainer::const_iterator vit = m_extents.cbegin(); vit != m_extents.cend(); ++vit) {
127  nbytes += vit->len();
128  }
129  return nbytes;
130 }
131 
133  size_t bytesUsed = bytesContained();
134  size_t totalRange = asExtent().len(); //might be expensive to call
135  return totalRange - bytesUsed;
136 }
137 
138 
140  std::sort(m_extents.begin(), m_extents.end());
141 }
142 
143 
145  ExtentContainer v;
146  v.assign(m_extents.begin(), m_extents.end() );
147  std::sort(v.begin(), v.end());
148  return v;
149 }
150 
152  ExtentContainer v;
153  v.assign(m_extents.begin(), m_extents.end() );
154  return v;
155 }
156 
157 // ------------------------------------------------------ //
158 // Timer ns //
159 
160 Timer_ns::Timer_ns(long &output) : m_output_val(output)
161 {
162  m_start = std::chrono::steady_clock::now();
163 }
164 
166 {
167  auto end = std::chrono::steady_clock::now();
168  m_output_val = std::chrono::duration_cast<std::chrono::nanoseconds>(end - m_start).count();
169 }
std::mutex cephbuf_iolock
Definition: BufferUtils.cc:9
size_t bytesContained() const
Definition: BufferUtils.cc:124
ExtentContainer getExtents() const
Definition: BufferUtils.cc:151
size_t size() const
number of extent elements
Definition: BufferUtils.hh:122
ExtentContainer getSortedExtents() const
Definition: BufferUtils.cc:144
void push_back(const Extent &in)
Definition: BufferUtils.cc:101
void sort()
inplace sort by offset of contained extents
Definition: BufferUtils.cc:139
const ExtentContainer & extents() const
Definition: BufferUtils.hh:133
off_t end() const
similar to stl vector end.
Definition: BufferUtils.hh:68
Extent containedExtent(off_t pos, size_t len) const
return the subset of range that is in this extent
Definition: BufferUtils.cc:45
off_t begin() const
Same as offset, but a bit more stl container like.
Definition: BufferUtils.hh:67
bool isContiguous(const Extent &rhs) const
Definition: BufferUtils.cc:20
bool operator==(const Extent &rhs) const
Definition: BufferUtils.cc:68
size_t len() const
Definition: BufferUtils.hh:66
bool operator<(const Extent &rhs) const
Definition: BufferUtils.cc:58
bool allInExtent(off_t pos, size_t len) const
is all the range in this extent
Definition: BufferUtils.cc:28
bool in_extent(off_t pos) const
is this position within the range of this extent
Definition: BufferUtils.cc:15
Extent(off_t offset, size_t len)
Ecapsulates an offsets and length, with added functionaliyu Class that represents an offset possition...
Definition: BufferUtils.hh:64
bool someInExtent(off_t pos, size_t len) const
is some of the range in this extent
Definition: BufferUtils.cc:38
Timer_ns(long &output_ns)
RAII based timer information outputing a long value of ns Almost trivial class to time something and ...
Definition: BufferUtils.cc:160
is a simple implementation of IXrdCephBufferData using std::vector<char> representation for the buffe...
Definition: BufferUtils.hh:29
std::vector< Extent > ExtentContainer
Container defintion for Extents Typedef to provide a container of extents as a simple stl vector cont...
Definition: BufferUtils.hh:99