/* * Copyright (C) 2015 higherfrequencytrading.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ package net.openhft.chronicle.map; import com.google.common.collect.ImmutableSet; import org.jetbrains.annotations.NotNull; import org.junit.Test; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ThreadLocalRandom; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class NestedContextsTest { @Test public void nestedContextsTest() throws ExecutionException, InterruptedException { HashSet averageValue = new HashSet<>(); for (int i = 0; i < 5; i++) { averageValue.add(i); } ChronicleMap> graph = ChronicleMapBuilder .of(Integer.class, (Class>) (Class) Set.class) .averageValue(averageValue) .actualSegments(2) .create(); addEdge(graph, 1, 2); addEdge(graph, 2, 3); addEdge(graph, 1, 3); assertEquals(ImmutableSet.of(2, 3), graph.get(1)); assertEquals(ImmutableSet.of(1, 3), graph.get(2)); assertEquals(ImmutableSet.of(1, 2), graph.get(3)); verifyGraphConsistent(graph); ForkJoinPool pool = new ForkJoinPool(8); try { pool.submit(() -> { ThreadLocalRandom.current().ints().limit(10_000).parallel().forEach(i -> { int sourceNode = Math.abs(i % 10); int targetNode; do { targetNode = ThreadLocalRandom.current().nextInt(10); } while (targetNode == sourceNode); if (i % 2 == 0) { addEdge(graph, sourceNode, targetNode); } else { removeEdge(graph, sourceNode, targetNode); } }); }).get(); verifyGraphConsistent(graph); } finally { pool.shutdownNow(); } } private static void verifyGraphConsistent(ChronicleMap> graph) { graph.forEach((node, neighbours) -> neighbours.forEach(neighbour -> assertTrue(graph.get(neighbour).contains(node)))); } public static boolean addEdge( ChronicleMap> graph, int source, int target) { if (source == target) throw new IllegalArgumentException("loops are forbidden"); ExternalMapQueryContext, ?> sourceC = graph.queryContext(source); ExternalMapQueryContext, ?> targetC = graph.queryContext(target); // order for consistent lock acquisition => avoid dead lock if (sourceC.segmentIndex() <= targetC.segmentIndex()) { return innerAddEdge(source, sourceC, target, targetC); } else { return innerAddEdge(target, targetC, source, sourceC); } } private static boolean innerAddEdge( int source, ExternalMapQueryContext, ?> sourceContext, int target, ExternalMapQueryContext, ?> targetContext) { try (ExternalMapQueryContext, ?> sc = sourceContext) { try (ExternalMapQueryContext, ?> tc = targetContext) { sc.updateLock().lock(); tc.updateLock().lock(); MapEntry> sEntry = sc.entry(); if (sEntry != null) { MapEntry> tEntry = tc.entry(); if (tEntry != null) { return addEdgeBothPresent(sc, sEntry, source, tc, tEntry, target); } else { addEdgePresentAbsent(sc, sEntry, source, tc, target); return true; } } else { MapEntry> tEntry = tc.entry(); if (tEntry != null) { addEdgePresentAbsent(tc, tEntry, target, sc, source); } else { addEdgeBothAbsent(sc, source, tc, target); } return true; } } } } private static boolean addEdgeBothPresent( MapQueryContext, ?> sc, @NotNull MapEntry> sEntry, int source, MapQueryContext, ?> tc, @NotNull MapEntry> tEntry, int target) { Set sNeighbours = sEntry.value().get(); if (sNeighbours.add(target)) { Set tNeighbours = tEntry.value().get(); boolean added = tNeighbours.add(source); assert added; sEntry.doReplaceValue(sc.wrapValueAsData(sNeighbours)); tEntry.doReplaceValue(tc.wrapValueAsData(tNeighbours)); return true; } else { return false; } } private static void addEdgePresentAbsent( MapQueryContext, ?> sc, @NotNull MapEntry> sEntry, int source, MapQueryContext, ?> tc, int target) { Set sNeighbours = sEntry.value().get(); boolean added = sNeighbours.add(target); assert added; sEntry.doReplaceValue(sc.wrapValueAsData(sNeighbours)); addEdgeOneSide(tc, source); } private static void addEdgeBothAbsent(MapQueryContext, ?> sc, int source, MapQueryContext, ?> tc, int target) { addEdgeOneSide(sc, target); addEdgeOneSide(tc, source); } private static void addEdgeOneSide(MapQueryContext, ?> tc, int source) { Set tNeighbours = new HashSet<>(); tNeighbours.add(source); MapAbsentEntry> tAbsentEntry = tc.absentEntry(); assert tAbsentEntry != null; tAbsentEntry.doInsert(tc.wrapValueAsData(tNeighbours)); } public static boolean removeEdge( ChronicleMap> graph, int source, int target) { ExternalMapQueryContext, ?> sourceC = graph.queryContext(source); ExternalMapQueryContext, ?> targetC = graph.queryContext(target); // order for consistent lock acquisition => avoid dead lock if (sourceC.segmentIndex() <= targetC.segmentIndex()) { return innerRemoveEdge(source, sourceC, target, targetC); } else { return innerRemoveEdge(target, targetC, source, sourceC); } } private static boolean innerRemoveEdge( int source, ExternalMapQueryContext, ?> sourceContext, int target, ExternalMapQueryContext, ?> targetContext) { try (ExternalMapQueryContext, ?> sc = sourceContext) { try (ExternalMapQueryContext, ?> tc = targetContext) { sc.updateLock().lock(); MapEntry> sEntry = sc.entry(); if (sEntry == null) return false; Set sNeighbours = sEntry.value().get(); if (!sNeighbours.remove(target)) return false; tc.updateLock().lock(); MapEntry> tEntry = tc.entry(); if (tEntry == null) throw new IllegalStateException("target node should be present in the graph"); Set tNeighbours = tEntry.value().get(); if (!tNeighbours.remove(source)) throw new IllegalStateException("the target node have an edge to the source"); sEntry.doReplaceValue(sc.wrapValueAsData(sNeighbours)); tEntry.doReplaceValue(tc.wrapValueAsData(tNeighbours)); return true; } } } }