From 3871b4fd73a6f42d19b25ad46745fea8c03f3876 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Wed, 20 May 2015 18:05:07 +0100 Subject: [PATCH] Use Java 7 Files to delete and rename. --- .../openhft/chronicle/map/FilePerKeyMap.java | 20 ++++++++++++++++++- .../chronicle/map/FilePerKeyMapTest.java | 20 +++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java index e172da819..a575b9959 100755 --- a/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java +++ b/src/main/java/net/openhft/chronicle/map/FilePerKeyMap.java @@ -8,6 +8,7 @@ import java.io.*; import java.nio.file.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -136,7 +137,24 @@ public class FilePerKeyMap implements Map, Closeable { @Override public void clear() { - getFiles().forEach(this::deleteFile); + AtomicInteger count = new AtomicInteger(); + Stream files = getFiles(); + files.forEach((path) -> { + try { + deleteFile(path); + } catch (Exception e) { + count.incrementAndGet(); + // ignored at afirst. + } + }); + if (count.intValue() > 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().isInterrupted(); + } + getFiles().forEach(this::deleteFile); + } } @NotNull diff --git a/src/test/java/net/openhft/chronicle/map/FilePerKeyMapTest.java b/src/test/java/net/openhft/chronicle/map/FilePerKeyMapTest.java index 42ed147d8..1446cb8c0 100755 --- a/src/test/java/net/openhft/chronicle/map/FilePerKeyMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/FilePerKeyMapTest.java @@ -125,18 +125,16 @@ public class FilePerKeyMapTest { String value = sb.toString(); //small warm-up - for (int i = 0; i < 10; i++) { - map.put("big file", value); + for (int j = -1; j < 3; j++) { + + long time = System.currentTimeMillis(); + int iterations = 50; + for (int i = 0; i < iterations; i++) { + map.put("big file-" + i, value); + } + if (j >= 0) + System.out.println("Time to update " + iterations + " puts " + (System.currentTimeMillis() - time) + " ms."); } - - - long time = System.currentTimeMillis(); - int iterations = 50; - for (int i = 0; i < iterations; i++) { - map.put("big file", value); - } - System.out.println("Time to update " + iterations + " iterations " + (System.currentTimeMillis() - time)); - map.clear(); }