]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
objectcacher: user helper to get starting point in buffer map
authorSage Weil <sage@newdream.net>
Sat, 5 May 2012 23:31:57 +0000 (16:31 -0700)
committerSage Weil <sage@newdream.net>
Sat, 5 May 2012 23:31:57 +0000 (16:31 -0700)
A common pattern is to search for the first buffer intersecting or
following an object offset.  Use a helper for that.

Signed-off-by: Sage Weil <sage@newdream.net>
src/osdc/ObjectCacher.cc
src/osdc/ObjectCacher.h

index bab8fcb3db25b59a66d67db8b45186d0bd85ab65..483d329dbbf9c4e9c3d58dbb7b507559c0adccfa 100644 (file)
@@ -131,15 +131,7 @@ void ObjectCacher::Object::try_merge_bh(BufferHead *bh)
  */
 bool ObjectCacher::Object::is_cached(loff_t cur, loff_t left)
 {
-  map<loff_t, BufferHead*>::iterator p = data.lower_bound(cur);
-  
-  if (p != data.begin() && 
-      (p == data.end() || p->first > cur)) {
-    p--;     // might overlap!
-    if (p->first + p->second->length() <= cur) 
-      p++;   // doesn't overlap.
-  }
-  
+  map<loff_t, BufferHead*>::iterator p = data_lower_bound(cur);
   while (left > 0) {
     if (p == data.end())
       return false;
@@ -179,19 +171,10 @@ int ObjectCacher::Object::map_read(OSDRead *rd,
     ldout(oc->cct, 10) << "map_read " << ex_it->oid 
              << " " << ex_it->offset << "~" << ex_it->length << dendl;
     
-    map<loff_t, BufferHead*>::iterator p = data.lower_bound(ex_it->offset);
-    // p->first >= start
-    
     loff_t cur = ex_it->offset;
     loff_t left = ex_it->length;
-    
-    if (p != data.begin() && 
-        (p == data.end() || p->first > cur)) {
-      p--;     // might overlap!
-      if (p->first + p->second->length() <= cur) 
-        p++;   // doesn't overlap.
-    }
-    
+
+    map<loff_t, BufferHead*>::iterator p = data_lower_bound(ex_it->offset);
     while (left > 0) {
       // at end?
       if (p == data.end()) {
@@ -264,33 +247,16 @@ ObjectCacher::BufferHead *ObjectCacher::Object::map_write(OSDWrite *wr)
   for (vector<ObjectExtent>::iterator ex_it = wr->extents.begin();
        ex_it != wr->extents.end();
        ex_it++) {
-    
+
     if (ex_it->oid != oid.oid) continue;
-    
+
     ldout(oc->cct, 10) << "map_write oex " << ex_it->oid
              << " " << ex_it->offset << "~" << ex_it->length << dendl;
-    
-    map<loff_t, BufferHead*>::iterator p = data.lower_bound(ex_it->offset);
-    // p->first >= start
-    
+
     loff_t cur = ex_it->offset;
     loff_t left = ex_it->length;
-    
-    if (p != data.begin() && 
-        (p == data.end() || p->first > cur)) {
-      p--;     // might overlap or butt up!
-
-      /*// dirty and butts up?
-      if (p->first + p->second->length() == cur &&
-          p->second->is_dirty()) {
-        ldout(oc->cct, 10) << "map_write will append to tail of " << *p->second << dendl;
-        final = p->second;
-      }
-      */
-      if (p->first + p->second->length() <= cur) 
-        p++;   // doesn't overlap.
-    }    
-    
+
+    map<loff_t, BufferHead*>::iterator p = data_lower_bound(ex_it->offset);
     while (left > 0) {
       loff_t max = left;
 
@@ -412,14 +378,7 @@ void ObjectCacher::Object::discard(loff_t off, loff_t len)
 {
   ldout(oc->cct, 10) << "discard " << *this << " " << off << "~" << len << dendl;
 
-  map<loff_t, BufferHead*>::iterator p = data.lower_bound(off);
-  if (p != data.begin() &&
-      (p == data.end() || p->first > off)) {
-    p--;     // might overlap!
-    if (p->first + p->second->length() <= off)
-      p++;   // doesn't overlap.
-  }
-
+  map<loff_t, BufferHead*>::iterator p = data_lower_bound(off);
   while (p != data.end()) {
     BufferHead *bh = p->second;
     if (bh->start() >= off + len)
@@ -1430,14 +1389,7 @@ bool ObjectCacher::flush(Object *ob, loff_t offset, loff_t length)
 {
   bool clean = true;
   ldout(cct, 10) << "flush " << *ob << " " << offset << "~" << length << dendl;
-  map<loff_t,BufferHead*>::iterator p = ob->data.lower_bound(offset);
-  if (p != ob->data.begin() && 
-      (p == ob->data.end() || p->first > offset)) {
-    p--;     // might overlap!
-    if (p->first + p->second->length() <= offset) 
-      p++;   // doesn't overlap.
-  }
-  for ( ; p != ob->data.end(); p++) {
+  for (map<loff_t,BufferHead*>::iterator p = ob->data_lower_bound(offset); p != ob->data.end(); p++) {
     BufferHead *bh = p->second;
     ldout(cct, 20) << "flush  " << *bh << dendl;
     if (length && bh->start() > offset+length) {
index db02727c6d268ed874a8dd6ca423db8ab8c55ce5..83738a2ec16d5e20fc06f5820d90085729517d0c 100644 (file)
@@ -224,6 +224,23 @@ class ObjectCacher {
        dirty_or_tx == 0;
     }
 
+    /**
+     * find first buffer that includes or follows an offset
+     *
+     * @param offset object byte offset
+     * @return iterator pointing to buffer, or data.end()
+     */
+    map<loff_t,BufferHead*>::iterator data_lower_bound(loff_t offset) {
+      map<loff_t,BufferHead*>::iterator p = data.lower_bound(offset);
+      if (p != data.begin() &&
+         (p == data.end() || p->first > offset)) {
+       p--;     // might overlap!
+       if (p->first + p->second->length() <= offset)
+         p++;   // doesn't overlap.
+      }
+      return p;
+    }
+
     // bh
     // add to my map
     void add_bh(BufferHead *bh) {