Index: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java =================================================================== --- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java (revision 1599475) +++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java (working copy) @@ -45,6 +45,7 @@ import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,6 +52,7 @@ import org.apache.jackrabbit.oak.api.Blob; import org.apache.jackrabbit.oak.plugins.blob.BlobStoreBlob; +import org.apache.jackrabbit.oak.plugins.segment.Compactor; import org.apache.jackrabbit.oak.plugins.segment.RecordId; import org.apache.jackrabbit.oak.plugins.segment.Segment; import org.apache.jackrabbit.oak.plugins.segment.SegmentId; @@ -125,6 +127,17 @@ private final AtomicBoolean cleanupNeeded = new AtomicBoolean(false); /** + * Flag to request segment compaction during the next flush. + */ + private final AtomicBoolean compactNeeded = new AtomicBoolean(false); + + /** + * The number of new files created at which a compaction should be + * triggered. + */ + private int compactThreshold = 10; + + /** * List of old tar file generations that are waiting to be removed. */ private final LinkedList toBeRemoved = newLinkedList(); @@ -233,6 +246,7 @@ timeToClose.await(1, SECONDS); while (timeToClose.getCount() > 0) { long start = System.nanoTime(); + compact(); try { flush(); } catch (IOException e) { @@ -394,6 +408,16 @@ } } + public void compact() { + boolean compact = compactNeeded.getAndSet(false); + if (compact) { + long start = System.nanoTime(); + Compactor.compact(this); + log.debug("TarMK Compaction: Completed in {}ms", MILLISECONDS + .convert(System.nanoTime() - start, NANOSECONDS)); + } + } + public synchronized Iterable getSegmentIds() { List ids = newArrayList(); for (UUID uuid : writer.getUUIDs()) { @@ -567,6 +591,11 @@ directory, String.format(FILE_NAME_FORMAT, writeNumber, "a")); writer = new TarWriter(writeFile); + + if (writeNumber % compactThreshold == 0) { + compactNeeded.set(true); + } + } } catch (IOException e) { throw new RuntimeException(e); @@ -591,6 +620,7 @@ public void gc() { System.gc(); cleanupNeeded.set(true); + compactNeeded.set(true); } public Map> getTarReaderIndex() {