/*
 * Copyright 2014-2018 Chronicle Software
 *
 * http://chronicle.software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.openhft.chronicle.queue.impl.single;

import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.threads.Threads;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutorService;

public enum QueueFileShrinkManager {
    ;
    private static final Boolean DISABLE_QUEUE_FILE_SHRINKING = Boolean.getBoolean("chronicle.queue.disableFileShrinking");
    public static final String THREAD_NAME = "queue-file-shrink-daemon";
    private static final ExecutorService executor = Threads.acquireExecutorService(THREAD_NAME, 1, true);

    public static void scheduleShrinking(File queueFile, long writePos) {
        if (DISABLE_QUEUE_FILE_SHRINKING)
            return;
        executor.submit(() -> {
            try {
                Jvm.debug().on(QueueFileShrinkManager.class, "Shrinking " + queueFile + " to " + writePos);
                RandomAccessFile raf = new RandomAccessFile(queueFile, "rw");

                raf.setLength(writePos);
                raf.close();
            } catch (IOException ex) {
                Jvm.warn().on(QueueFileShrinkManager.class, "Failed to shrink file " + queueFile, ex);
            }
        });
    }
}

@@ -1407,16 +1407,47 @@
 () -%3E %7B%0A
+            while (true) %7B%0A    
         
@@ -1468,24 +1468,28 @@
             
+   
+ 
 Jvm.debug().
@@ -1584,16 +1584,20 @@
         
+    
 RandomAc
@@ -1663,24 +1663,28 @@
             
+ 
+   
 raf.setLengt
@@ -1708,24 +1708,28 @@
             
+  
+  
 raf.close();
@@ -1729,16 +1729,20 @@
 lose();%0A
+    
         
@@ -1768,24 +1768,286 @@
 ption ex) %7B%0A
+                    // on macrosux windows, keep retrying until the file is unmapped%0A                    if (ex.getMessage().contains(%22The requested operation cannot be performed on a file with a user-mapped section open%22))%0A                        continue;%0A    
             
@@ -2137,16 +2137,57 @@
 e, ex);%0A
+                %7D%0A                break;%0A
         

Fix for macrosuxx OS