Index: xapian-compact.cc
===================================================================
--- xapian-compact.cc	(revision 7953)
+++ xapian-compact.cc	(working copy)
@@ -323,10 +323,36 @@
 	    Xapian::Database db(srcdir);
 	    // No point trying to merge empty databases!
 	    if (db.get_doccount() != 0) {
-		Xapian::docid last = db.get_lastdocid();
+		FlintTable in(string(srcdir) + "/record.", true);
+		in.open();
+
+		FlintCursor cur(&in);
+
+		// Prune any unused docids off the start of this source database.
+		cur.find_entry("");
+		if (cur.next()) {
+		    Xapian::docid did;
+		    const char * d = cur.current_key.data();
+		    const char * e = d + cur.current_key.size();
+		    if (unpack_uint_preserving_sort(&d, e, &did)) {
+			// tot_off could wrap here, but it's unsigned, so that's OK.
+			tot_off -= (did - 1);
+		    }
+		    cout << did << " - ";
+		}
+
+		// Prune unused docids off the end of this source database.
+		cur.find_entry("\xff\xff\xff\xff\xff\xff\xff\xff\xff");
+		Xapian::docid last;
+		const char * d = cur.current_key.data();
+		const char * e = d + cur.current_key.size();
+		if (!unpack_uint_preserving_sort(&d, e, &last)) {
+		    last = db.get_lastdocid();
+		}
+		cout << last << endl;
+
 		offset.push_back(tot_off);
 		tot_off += last;
-		// FIXME: prune unused docids off the start and end of each range...
 		sources.push_back(string(srcdir) + '/');
 	    }
 	}

