Add a utility to make unique files/dirs easier, closes https://github.com/OpenHFT/Chronicle-Core/issues/149

This commit is contained in:
Peter Lawrey
2020-11-02 08:56:07 +00:00
parent 7dedb9cf89
commit 184dd87e57
23 changed files with 56 additions and 54 deletions
+12 -10
View File
@@ -53,24 +53,26 @@ ChronicleMap<CharSequence, PostalCodeRange> cityPostalCodes = ChronicleMap
== Persisted Chronicle Map
You can amend this code to create a persisted Chronicle Map by replacing `.create()` calls with `.createPersistedTo(cityPostalCodesFile)`. Use a persisted Chronicle Map if you want it to, either:
You can amend this code to create a persisted Chronicle Map by replacing `.create()` calls with `.createPersistedTo(cityPostalCodesFile)`.
Use a persisted Chronicle Map if you want it to, either:
- outlive the process it was created within; for example, to support hot application redeployment.
- be accessible from multiple concurrent processes on the same server.
- persist the data to disk.
- outlive the process it was created within; for example, to support hot application redeployment.
- be accessible from multiple concurrent processes on the same server.
- persist the data to disk.
The `cityPostalCodesFile` has to represent the same location on your server for all the Java
processes that wish to access this Chronicle Map instance. For example, `System.getProperty("java.io.tmpdir") + "/cityPostalCodes.dat"`.
The `cityPostalCodesFile` has to represent the same location on your server for all the Java processes that wish to access this Chronicle Map instance.
For example, `OS.getTarget() + "/cityPostalCodes.dat"`.
The name and location of the file is entirely up to you.
NOTE: When you create a `ChronicleMap` instance with `.createPersistedTo(file)`, and the specified
file already exists in the system, you open a view to the existing Chronicle Map data store from
this JVM process, rather than creating a new Chronicle Map data store. That means that the data store may already contain some entries. No special action with the data is performed during such an operation. If you want to clean up corrupted entries, and ensure that the data store is in correct state, see the <<Recovery>> section.
NOTE: When you create a `ChronicleMap` instance with `.createPersistedTo(file)`, and the specified file already exists in the system, you open a view to the existing Chronicle Map data store from this JVM process, rather than creating a new Chronicle Map data store.
That means that the data store may already contain some entries.
No special action with the data is performed during such an operation.
If you want to clean up corrupted entries, and ensure that the data store is in correct state, see the <<Recovery>> section.
== ChronicleMap instance vs Chronicle Map data store
In this tutorial, the term `ChronicleMap` instance (or simply `ChronicleMap`) is used to refer to an on-heap object providing access to a Chronicle Map data store (or Chronicle Map key-value store, or Chronicle Map store, or simply Chronicle Map, with space between two words in contrast to `ChronicleMap`), which could be purely in-memory, or persisted to disk.
In this tutorial, the term `ChronicleMap` instance (or simply `ChronicleMap`) is used to refer to an on-heap object providing access to a Chronicle Map data store (or Chronicle Map key-value store, or Chronicle Map store, or simply Chronicle Map, with space between two words in contrast to `ChronicleMap`), which could be purely in-memory, or persisted to disk.
Currently the Java implementation doesn't allow creation of multiple accessor ChronicleMap` objects for a single *in-memory Chronicle Map store*; there is always a one-to-one relationship.
+1 -1
View File
@@ -288,7 +288,7 @@ Map to either
The `cityPostalCodesFile` has to represent the same location on your server among all Java
processes, wishing to access this Chronicle Map instance, e. g.
`System.getProperty("java.io.tmpdir") + "/cityPostalCodes.dat"`.
`OS.getTarget() + "/cityPostalCodes.dat"`.
The name and location of the file is entirely up to you.
@@ -27,7 +27,7 @@ import static org.junit.Assert.fail;
public class AcquireGetUsingMain {
public static void main(String[] args) throws IOException {
File file = new File(System.getProperty("java.io.tmpdir") + "/test1");
File file = new File(OS.getTarget() + "/test1");
ChronicleMap<String, Data> theSharedMap =
ChronicleMapBuilder.of(String.class, Data.class)
.createPersistedTo(file);
@@ -26,7 +26,7 @@ public class ArrayTest {
// don't use Double[] as it uses ~3.5x the memory of a double[]
@Test
public void testDoubleArray() throws IOException {
File file = new File(OS.getTarget() + "/testDoubleArray-" + System.nanoTime());
File file = new File(OS.getTarget() + "/testDoubleArray-" + Time.uniqueId());
ChronicleMap<Long, double[]> writeMap = ChronicleMapBuilder
.of(Long.class, double[].class)
.entries(1_000)
@@ -46,7 +46,7 @@ public class ArrayTest {
// don't use Long[] as it uses ~3.5x the memory of a long[]
@Test
public void testLongArray() throws IOException {
File file = new File(OS.getTarget() + "/testLongArray-" + System.nanoTime());
File file = new File(OS.getTarget() + "/testLongArray-" + Time.uniqueId());
ChronicleMap<Long, long[]> writeMap = ChronicleMapBuilder
.of(Long.class, long[].class)
.entries(1_000)
@@ -38,12 +38,12 @@ import static org.junit.Assert.assertEquals;
*/
public class ChronicleMapImportExportTest {
public static final String TMP = System.getProperty("java.io.tmpdir");
public static final String TMP = OS.getTarget();
@Test
public void test() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
ChronicleMapBuilder<String, String> builder = ChronicleMapBuilder
@@ -68,7 +68,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testWithMapValue() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
System.out.println(file.getAbsoluteFile());
@@ -97,7 +97,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testWithMapOfMapValue() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
System.out.println(file.getAbsoluteFile());
@@ -127,7 +127,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testWithIntegerAndDouble() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
try (ChronicleMap<Integer, Double> expected = ChronicleMap.of(Integer.class, Double.class)
@@ -150,7 +150,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testWithCharSeq() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
System.out.println(file.getAbsolutePath());
@@ -178,10 +178,10 @@ public class ChronicleMapImportExportTest {
@Test
public void testFromHashMap() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
System.out.println(file.getCanonicalFile());
File file2 = new File(TMP + "/chronicle-map-2" + System.nanoTime() + ".json");
File file2 = new File(TMP + "/chronicle-map-2" + Time.uniqueId() + ".json");
System.out.println(file2.getCanonicalFile());
HashMap<Integer, String> map = new HashMap<Integer, String>();
@@ -216,7 +216,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testWithLongValue() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
//file.deleteOnExit();
System.out.println(file.getAbsolutePath());
@@ -250,7 +250,7 @@ public class ChronicleMapImportExportTest {
@Test
public void testBondVOInterface() throws IOException, InterruptedException {
File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
File file = new File(TMP + "/chronicle-map-" + Time.uniqueId() + ".json");
file.deleteOnExit();
System.out.println(file.getAbsolutePath());
@@ -38,9 +38,9 @@ public class ChronicleMapSanityCheckTest {
@Test
public void testSanity1() throws IOException, InterruptedException {
String tmp = System.getProperty("java.io.tmpdir");
String tmp = OS.getTarget();
String pathname = tmp + "/testSanity1-" + UUID.randomUUID().toString() + ".dat";
String pathname = tmp + "/testSanity1-" + Time.uniqueId() + ".dat";
File file = new File(pathname);
@@ -107,8 +107,8 @@ public class ChronicleMapTest {
}
static File getPersistenceFile() {
String TMP = System.getProperty("java.io.tmpdir");
File file = new File(TMP + "/chm-test" + System.nanoTime() + count++);
String TMP = OS.getTarget();
File file = new File(TMP + "/chm-test" + Time.uniqueId() + count++);
file.deleteOnExit();
return file;
}
@@ -46,7 +46,7 @@ public class ConcurrentMap8Test extends JSR166TestCase {
assertEquals(8, builder.minSegments());
//// end of test
String chmPath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "CHMTest" + System.nanoTime();
String chmPath = OS.getTarget() + System.getProperty("file.separator") + "CHMTest" + Time.uniqueId();
try {
return builder.create(new File(chmPath));
} catch (IOException e) {
@@ -64,8 +64,8 @@ public class ConcurrentMap8Test extends JSR166TestCase {
assertEquals(8, builder.minSegments());
//// end of test
String chmPath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
"CHMTest" + System.nanoTime();
String chmPath = OS.getTarget() + System.getProperty("file.separator") +
"CHMTest" + Time.uniqueId();
try {
return builder.create(new File(chmPath));
} catch (IOException e) {
@@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class EntryCountMapTest {
static final String TMP = System.getProperty("java.io.tmpdir");
static final String TMP = OS.getTarget();
static final int ecmTests = Integer.getInteger("ecm.tests", 5);
double score = 0;
int scoreCount = 0;
@@ -19,7 +19,7 @@ public class ForEachSegmentTest {
ChronicleMapBuilder<Integer, MyDto> builder = ChronicleMapBuilder.simpleMapOf(Integer.class, MyDto.class)
.entries(256)
.actualSegments(1);
File tmp = new File(OS.TMP, "stressTest-" + System.nanoTime());
File tmp = new File(OS.TMP, "stressTest-" + Time.uniqueId());
tmp.deleteOnExit();
try (ChronicleMap<Integer, MyDto> map = builder.createOrRecoverPersistedTo(tmp)) {
map.put(1, new MyDto());
@@ -38,7 +38,7 @@ public class ForEachSegmentTest {
ChronicleMapBuilder<Integer, MyDto> builder = ChronicleMapBuilder.simpleMapOf(Integer.class, MyDto.class)
.entries(256)
.actualSegments(1);
File tmp = new File(OS.TMP, "stressTest-" + System.nanoTime());
File tmp = new File(OS.TMP, "stressTest-" + Time.uniqueId());
Thread t = null;
try (ChronicleMap<Integer, MyDto> map = builder.createOrRecoverPersistedTo(tmp)) {
try {
@@ -28,7 +28,7 @@ import java.io.IOException;
public class Issue125Test {
@Test
public void test() throws IOException {
final File cacheRoot = new File(OS.getTarget() + "/test.cm3");
final File cacheRoot = new File(OS.getTarget() + "/test-" + Time.unqiueId+".cm3");
ChronicleMapBuilder<byte[], byte[]> shaToNodeBuilder =
ChronicleMapBuilder.of(byte[].class, byte[].class)
// .name("bytes-to-bytes")
@@ -30,10 +30,10 @@ public class Issue62ChronicleServer {
//"This is just a long string, which causes sink to fail for some reason.";
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
final static File MAP_FILE_B =
Paths.get(System.getProperty("java.io.tmpdir"), "map.b").toFile();
Paths.get(OS.getTarget(), Time.uniqueId()+"map.b").toFile();
private final static Logger LOGGER = LoggerFactory.getLogger(Issue62ChronicleServer.class);
private final static File MAP_FILE_A =
Paths.get(System.getProperty("java.io.tmpdir"), "map.a").toFile();
Paths.get(OS.getTarget(), Time.uniqueId()+"map.a").toFile();
static void prepare(File file) {
if (file.exists())
@@ -87,7 +87,7 @@ public class Issue63Test {
@Test
public void issue63test() throws IOException {
Path path = Paths.get(System.getProperty("java.io.tmpdir") + "/test-vectors1.dat");
Path path = Paths.get(OS.getTarget() + "/test-vectors1-"+Time.uniqueId()+".dat");
if (Files.exists(path)) Files.delete(path);
File mapFile = path.toFile();
mapFile.deleteOnExit();
@@ -185,7 +185,7 @@ public class Issue63Test {
void testChronicleMap() throws IOException {
int num = 1_000_000;
testChronicleMap(System.getProperty("java.io.tmpdir"), num, num);
testChronicleMap(OS.getTarget(), num, num);
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < num; i++) {
String id = UUID.randomUUID().toString().substring(0, 32);
@@ -313,7 +313,7 @@ public class Issue63Test {
averageKnownItems.add("average sized known item");
}
Path knownItemsPath = Paths.get(
System.getProperty("java.io.tmpdir") + "/test-vectors2.dat");
OS.getTarget() + "/test-vectors2.dat");
Files.deleteIfExists(knownItemsPath);
ChronicleMap<CharSequence, List<CharSequence>> knownItems;
ChronicleMapBuilder<CharSequence, List<CharSequence>> knownItemsBuilder = ChronicleMap
@@ -63,7 +63,7 @@ public class MarkTest {
@Test(timeout = 25000)
public void persistedTest() {
int rnd = new Random().nextInt();
final File db = Paths.get(System.getProperty("java.io.tmpdir"), "mark" + rnd).toFile();
final File db = Paths.get(OS.getTarget(), "mark" + rnd).toFile();
if (db.exists())
db.delete();
try {
@@ -41,7 +41,7 @@ public class ProcessInstanceLimiterMain implements Runnable {
ChronicleMapBuilder.of(String.class, Data.class);
builder.entries(10000);
builder.minSegments(2);
File file = new File(System.getProperty("java.io.tmpdir") + "/" + sharedMapName);
File file = new File(OS.getTarget() + "/" + sharedMapName);
this.theSharedMap = builder.create();
Thread t = new Thread(this, "ProcessInstanceLimiterMain updater");
t.setDaemon(true);
@@ -24,11 +24,11 @@ import java.util.Map;
import static org.junit.Assert.assertSame;
public class RecursiveRefereneChMapTest {
public static final String TMP = System.getProperty("java.io.tmpdir");
public static final String TMP = OS.getTarget();
@Test
public void testRecursive() throws IOException {
File file = new File(TMP + "/test." + System.nanoTime() + ".tmp");
File file = new File(TMP + "/test." + Time.uniqueId() + ".tmp");
file.deleteOnExit();
Map<String, StupidCycle> map = ChronicleMapBuilder.of(String.class, StupidCycle.class)
.averageKey("Test").averageValue(new StupidCycle())
@@ -44,7 +44,7 @@ public class WriteThroughputTest {
doTest(count, size, map);
}
File file = new File("bandwidthTest" + System.nanoTime() + ".deleteme");
File file = new File("bandwidthTest" + Time.uniqueId() + ".deleteme");
file.deleteOnExit();
try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
.of(CharSequence.class, CharSequence.class)
@@ -57,7 +57,7 @@ public class WriteThroughputTest {
doTest(count, size, map);
}
File file2 = new File("bandwidthTest" + System.nanoTime() + ".snappy.deleteme");
File file2 = new File("bandwidthTest" + Time.uniqueId() + ".snappy.deleteme");
file2.deleteOnExit();
try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
.of(CharSequence.class, CharSequence.class)
@@ -78,7 +78,7 @@ public class WriteThroughputTest {
int count = 2 << 10;
int size = 50 << 10;
File file3 = new File("bandwidthTest" + System.nanoTime() + ".Z.deleteme");
File file3 = new File("bandwidthTest" + Time.uniqueId() + ".Z.deleteme");
file3.deleteOnExit();
try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
.of(CharSequence.class, CharSequence.class)
@@ -52,7 +52,7 @@ public class LotsOfEntriesMain {
private static void workEntries(final boolean add)
throws IOException, ExecutionException, InterruptedException {
final long entries = 100_000_000;
File file = new File("/tmp/lotsOfEntries.dat");
File file = new File(OS.getTarget(), "lotsOfEntries.dat");
final ChronicleMap<CharSequence, MyFloats> map = ChronicleMapBuilder
.of(CharSequence.class, MyFloats.class)
.entries(entries)
@@ -29,7 +29,7 @@ import static org.junit.Assert.assertEquals;
public class ExternalizableTest {
@Test
public void externalizable() throws IOException {
String path = OS.getTarget() + "/test-" + System.nanoTime() + ".map";
String path = OS.getTarget() + "/test-" + Time.uniqueId() + ".map";
new File(path).deleteOnExit();
try (ChronicleMap<Long, SomeClass> storage = ChronicleMapBuilder
.of(Long.class, SomeClass.class)
@@ -37,7 +37,7 @@ import static org.junit.Assert.assertEquals;
public class OpenJDKAndHashMapExamplesTest {
private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
private static final String TMP = System.getProperty("java.io.tmpdir");
private static final String TMP = OS.getTarget();
public static long parseYYYYMMDD(String s) {
try {
@@ -50,7 +50,7 @@ public class OpenJDKAndHashMapExamplesTest {
@Test
public void bondExample() throws IOException, InterruptedException {
File file = new File(TMP + "/chm-myBondPortfolioCHM-" + System.nanoTime());
File file = new File(TMP + "/chm-myBondPortfolioCHM-" + Time.uniqueId());
file.deleteOnExit();
ChronicleMap<String, BondVOInterface> chm = ChronicleMapBuilder
@@ -37,7 +37,7 @@ public class StateMachineTutorial {
ChronicleMap<Integer, StateMachineData> map = null;
try {
File dataFile = new File(System.getProperty("java.io.tmpdir"), "hft-state-machine");
File dataFile = new File(OS.getTarget(), "hft-state-machine");
map = ChronicleMapBuilder.of(Integer.class, StateMachineData.class)
.entries(8).create();
@@ -87,7 +87,7 @@ import static net.openhft.chronicle.algo.bytes.Access.checkedBytesStoreAccess;
public class ProcessInstanceLimiter implements Runnable {
private static final long DEFAULT_TIME_UPDATE_INTERVAL_MS = 100L;
private static final String DEFAULT_SHARED_MAP_NAME = "ProcessInstanceLimiter_DEFAULT_SHARED_MAP_";
private static final String DEFAULT_SHARED_MAP_DIRECTORY = System.getProperty("java.io.tmpdir");
private static final String DEFAULT_SHARED_MAP_DIRECTORY = OS.getTarget();
private static final String DEFAULT_PROCESS_NAME = "_DEFAULT_";
static {
@@ -36,7 +36,7 @@ public class ArrayTest {
public void test0() throws IOException, JSONException{
ClassAliasPool.CLASS_ALIASES.addAlias(MovingAverageArray.class);
File file = new File(OS.getTarget() + "/pf-PosistionsAndClose-" + System.nanoTime());
File file = new File(OS.getTarget() + "/pf-PosistionsAndClose-" + Time.uniqueId());
ChronicleMap<Long, MovingAverageArray> mapWrite = ChronicleMap
.of(Long.class, MovingAverageArray.class)