]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
interval_map: Add interfaces for getting start/end and lower ranges.
authorAlex Ainscow <aainscow@uk.ibm.com>
Wed, 8 Jan 2025 22:04:56 +0000 (22:04 +0000)
committerAlex Ainscow <aainscow@uk.ibm.com>
Mon, 27 Jan 2025 11:20:01 +0000 (11:20 +0000)
 These new utilities add the ability to:
   get_lower_range: Return the lowest interval iterator which covers the specified range
   get_start_off(): return the first offset in the interval.
   get_end_off(): Return the end offset of the last interval
   contains(): Return true if specified is entirely contained within the interval map.

Signed-off-by: Alex Ainscow <aainscow@uk.ibm.com>
src/common/interval_map.h

index 65a89e211f2b1d37f4dd22bc5b409452e04493d9..e57fac539a9d8226cd968c1003e6002851443bd1 100644 (file)
@@ -61,6 +61,16 @@ class interval_map {
     auto lst = m.lower_bound(off + len);
     return std::make_pair(fst, lst);
   }
+  cmapiter get_range_fst(K off) const {
+    // fst is first iterator with end after off (may be end)
+    auto fst = m.upper_bound(off);
+    if (fst != m.begin())
+      --fst;
+    if (fst != m.end() && off >= (fst->first + fst->second.first))
+      ++fst;
+
+    return fst;
+  }
   void try_merge(mapiter niter) {
     if (niter != m.begin()) {
       auto prev = niter;
@@ -258,6 +268,32 @@ public:
     auto rng = get_range(off, len);
     return std::make_pair(const_iterator(rng.first), const_iterator(rng.second));
   }
+
+  const_iterator get_lower_range(
+      K off,
+      K len) const {
+    return const_iterator(get_range_fst(off, len));
+  }
+  K get_start_off() const
+  {
+    auto i = m.begin();
+    ceph_assert(i != m.end());
+    return i->first;
+  }
+  K get_end_off() const
+  {
+    auto i = m.rbegin();
+    ceph_assert(i != m.rend());
+    return i->first + i->second.first;
+  }
+  bool contains(K off, K len) const {
+    auto it = get_range_fst(off, len);
+    if (it == m.end()) return false;
+
+    K _off = it->first;
+    K _len = it->second.first;
+    return _off <= off && _off + _len >= off + len;
+  }
   unsigned ext_count() const {
     return m.size();
   }