Great refactoring

This commit is contained in:
leventov
2015-01-17 03:19:35 +07:00
parent 8a4ad9ade2
commit d897891bfb
72 changed files with 5612 additions and 6382 deletions
@@ -18,7 +18,7 @@
package net.openhft.chronicle.map;
import net.openhft.lang.io.Bytes;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
@@ -27,16 +27,19 @@ import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.assertEquals;
@Ignore("event listener API is not yet determined")
public class CHMMetaDataTest {
private static final String TMP = System.getProperty("java.io.tmpdir");
@Test
public void testAccessTimes() throws IOException {
File file = new File(TMP, "testAccessTimes");
BytesMapEventListener listener =
new StringStringMapEventListener(new AtomicLong(1));
// MapBytesEventListener listener =
// new StringStringMapEventListener(new AtomicLong(1));
ChronicleMap<String, String> map = ChronicleMapBuilder.of(String.class, String.class)
.metaDataBytes(8).bytesEventListener(listener).create();
.metaDataBytes(8)
// .bytesEventListener(listener)
.create();
try {
map.put("a", "aye");
@@ -60,31 +63,31 @@ public class CHMMetaDataTest {
}
}
private static class StringStringMapEventListener
extends BytesMapEventListener {
private static final long serialVersionUID = 0L;
private final AtomicLong timeStamps;
public StringStringMapEventListener(AtomicLong timeStamps) {
this.timeStamps = timeStamps;
}
@Override
public void onGetFound(Bytes entry, long metaDataPos, long keyPos, long valuePos) {
entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
}
@Override
public void onPut(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean added) {
if (added)
assertEquals(0, entry.readLong(metaDataPos));
entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
}
@Override
public void onRemove(Bytes entry, long metaDataPos, long keyPos, long valuePos) {
System.out.println("Removed entry with ts of " + entry.readLong(metaDataPos));;
}
}
// private static class StringStringMapEventListener
// extends MapBytesEventListener {
// private static final long serialVersionUID = 0L;
//
// private final AtomicLong timeStamps;
//
// public StringStringMapEventListener(AtomicLong timeStamps) {
// this.timeStamps = timeStamps;
// }
//
// @Override
// public void onGetFound(MapBytesEvent event) {
// entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
// }
//
// @Override
// public void onPut(MapPutBytesEvent event) {
// if (added)
// assertEquals(0, entry.readLong(metaDataPos));
// entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
// }
//
// @Override
// public void onRemove(MapBytesEvent event) {
// System.out.println("Removed entry with ts of " + entry.readLong(metaDataPos));;
// }
// }
}
File diff suppressed because it is too large Load Diff
@@ -11,7 +11,6 @@ import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
@@ -256,10 +255,10 @@ public class ChronicleMapImportExportTest {
LongValue value = expected.newValueInstance();
// this will add the entry
try (WriteContext<?, LongValue> context = expected.acquireUsingLocked("one", value)) {
assertEquals(0, context.value().getValue());
assert value == context.value();
LongValue value1 = context.value();
try (MapKeyContext<LongValue> context = expected.acquireContext("one", value)) {
assertEquals(0, context.get().getValue());
assert value == context.get();
LongValue value1 = context.get();
value1.addValue(1);
}
@@ -293,13 +292,14 @@ public class ChronicleMapImportExportTest {
final BondVOInterface value = expected.newValueInstance();
// this will add the entry
try (WriteContext context = expected.acquireUsingLocked("one", value)) {
try (MapKeyContext context = expected.acquireContext("one", value)) {
value.setCoupon(8.98);
BondVOInterface.MarketPx marketPxIntraDayHistoryAt = value.getMarketPxIntraDayHistoryAt(1);
BondVOInterface.MarketPx marketPxIntraDayHistoryAt =
value.getMarketPxIntraDayHistoryAt(1);
marketPxIntraDayHistoryAt.setAskPx(12.0);
assert value == context.value();
assert value == context.get();
}
expected.getAll(file);
@@ -731,7 +731,7 @@ public class ChronicleMapTest {
map.getUsing(key, value);
} else {
try (WriteContext wc = map.acquireUsingLocked(key, value)) {
try (MapKeyContext wc = map.acquireContext(key, value)) {
if (value.length() < value0.length() - 1)
value.append(value0);
else if (value.length() > value0.length())
@@ -823,7 +823,8 @@ public class ChronicleMapTest {
map.getUsing(key, value);
} else {
try (WriteContext wc = map.acquireUsingLocked(key, value)) {
try (MapKeyContext wc = map.acquireContext(key,
value)) {
if (value.length() < value0.length() - 1)
value.append(value0);
else if (value.length() > value0.length())
@@ -862,6 +863,12 @@ public class ChronicleMapTest {
es.awaitTermination(1, TimeUnit.MINUTES);
}
static final LongValue ONE = DataValueClasses.newInstance(LongValue.class);
static {
ONE.setValue(1);
}
@Test
@Ignore("Performance test")
public void testAcquireLockedPerf()
@@ -889,6 +896,8 @@ public class ChronicleMapTest {
int count = runs >= 5 ? 2 : 3;
final int independence = Math.min(procs, runs > 500 ? 8 : 4);
System.out.println("\nKey size: " + runs + " Million entries. " + builder);
for (int j = 0; j < count; j++) {
long start = System.currentTimeMillis();
List<Future> futures = new ArrayList<Future>();
@@ -912,12 +921,23 @@ public class ChronicleMapTest {
long n;
// 75% read
if (rand.nextBoolean() || rand.nextBoolean()) {
try (ReadContext rc = map.getUsingLocked(sb, value)) {
n = rc.present() ? value.getValue() + 1 : 1;
try (MapKeyContext<LongValue> c = map.context(sb)) {
if (c.containsKey()) {
// Attempt to pass abstraction hierarchies
n = c.entry().readVolatileLong(c.valueOffset()) + 1;
} else {
n = 1;
}
}
} else {
try (WriteContext wc = map.acquireUsingLocked(sb, value)) {
n = value.addValue(1);
try (MapKeyContext<LongValue> wc = map.context(sb)) {
wc.updateLock().lock();
if (wc.containsKey()) {
n = wc.entry().addLong(wc.valueOffset(), 1);
} else {
wc.put(ONE);
n = 1;
}
}
}
assert n > 0 && n < 1000 : "Counter corrupted " + n;
@@ -990,7 +1010,7 @@ public class ChronicleMapTest {
j += independence) {
key.setValue(j * factor);
long n;
try (WriteContext wc = map.acquireUsingLocked(key, value)) {
try (MapKeyContext wc = map.acquireContext(key, value)) {
n = value.addValue(1);
}
assert n > 0 && n < 1000 : "Counter corrupted " + n;
@@ -1533,58 +1553,6 @@ public class ChronicleMapTest {
return map;
}
@Test
public void testEntriesSpanningSeveralChunksPutRemoveReplace() throws IOException {
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.entries(10)
.minSegments(1)
.actualChunkSize(10);
testEntriesSpanningSeveralChunksPutRemoveReplace(
(VanillaChronicleMap<CharSequence, ?, ?, CharSequence, ?, ?>)
builder.createPersistedTo(getPersistenceFile())
);
}
public void testEntriesSpanningSeveralChunksPutRemoveReplace(
VanillaChronicleMap<CharSequence, ?, ?, CharSequence, ?, ?> map) {
String key = "k";
String largeKey = "very large key";
String value = "v";
String largeValue = "vary large value";
map.put(key, largeValue);
assertEquals(largeValue, map.get(key));
map.replace(key, value);
map.checkConsistency();
assertEquals(value, map.get(key));
map.replace(key, largeValue);
map.checkConsistency();
assertEquals(largeValue, map.get(key));
map.put(largeKey, value);
map.checkConsistency();
assertEquals(value, map.get(largeKey));
map.replace(largeKey, largeValue);
map.checkConsistency();
assertEquals(largeValue, map.get(largeKey));
map.remove(largeKey);
map.checkConsistency();
assertEquals(null, map.get(largeKey));
map.put(largeKey, largeValue);
map.checkConsistency();
assertEquals(largeValue, map.get(largeKey));
map.replace(largeKey, largeValue, value);
map.checkConsistency();
assertEquals(value, map.get(largeKey));
assertEquals(value, map.remove(largeKey));
map.checkConsistency();
assertEquals(largeValue, map.remove(key));
map.checkConsistency();
map.close();
}
@Test
public void equalsTest() throws IOException {
final ChronicleMap<Integer, String> map1 = ChronicleMapBuilder
@@ -1612,7 +1580,8 @@ public class ChronicleMapTest {
private final CyclicBarrier barrier;
private IncrementRunnable(ChronicleMap<CharSequence, LongValue> map, CharSequence key, int iterations, CyclicBarrier barrier) {
private IncrementRunnable(ChronicleMap<CharSequence, LongValue> map, CharSequence key,
int iterations, CyclicBarrier barrier) {
this.map = map;
this.key = key;
this.iterations = iterations;
@@ -1667,46 +1636,49 @@ public class ChronicleMapTest {
LongValue value = nativeLongValue();
// this will add the entry
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
assertEquals(0, context.value().getValue());
assert value == context.value();
LongValue value1 = context.value();
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
assertEquals(0, context.get().getValue());
assert value == context.get();
LongValue value1 = context.get();
value1.addValue(1);
}
// check that the entry was added
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assert value == context.value();
assertEquals(true, context.present());
assertEquals(1, context.value().getValue());
try (MapKeyContext<LongValue> context = map.context("one")) {
assertEquals(true, context.containsKey());
LongValue v = context.getUsing(value);
assert v == value;
assertEquals(1, v.getValue());
}
// this will remove the entry
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
assert value == context.value();
context.removeEntry();
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
assert value == context.get();
context.remove();
}
// check that the entry was removed
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assertEquals(false, context.present());
assertEquals(null, context.value());
try (MapKeyContext<LongValue> context = map.context("one")) {
context.updateLock().lock();
assertEquals(false, context.containsKey());
assertEquals(null, context.get());
}
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
assert value == context.value();
assertEquals(0, context.value().getValue());
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
assert value == context.get();
assertEquals(0, context.get().getValue());
}
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
assert value == context.value();
context.value().addValue(1);
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
assert value == context.get();
context.get().addValue(1);
}
// check that the entry was removed
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assert value == context.value();
assertEquals(1, context.value().getValue());
try (MapKeyContext<LongValue> context = map.context("one")) {
LongValue v = context.getUsing(value);
assert value == v;
assertEquals(1, context.get().getValue());
}
map.close();
@@ -1757,7 +1729,7 @@ public class ChronicleMapTest {
try (final ChronicleMap<CharSequence, String> map = builder.create()) {
// this will add the entry
try (WriteContext<?, String> context = map.acquireUsingLocked("one", "")) {
try (MapKeyContext<String> context = map.acquireContext("one", "")) {
// do nothing
}
}
@@ -1775,7 +1747,7 @@ public class ChronicleMapTest {
.create()) {
StringBuilder value = new StringBuilder();
try (WriteContext<?, CharSequence> context = map.acquireUsingLocked("one", value)) {
try (MapKeyContext<CharSequence> context = map.acquireContext("one", value)) {
value.append("Hello World");
}
@@ -1795,56 +1767,56 @@ public class ChronicleMapTest {
LongValue value = DataValueClasses.newDirectReference(LongValue.class);
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assertEquals(false, context.present());
assertEquals(null, context.value());
try (MapKeyContext<LongValue> context = map.context("one")) {
assertEquals(false, context.containsKey());
assertEquals(null, context.get());
}
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
value.setValue(10);
}
// this will add the entry
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
LongValue value1 = context.value();
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
LongValue value1 = context.get();
value1.addValue(1);
}
// check that the entry was added
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assertEquals(true, context.present());
assertEquals(11, context.value().getValue());
try (MapKeyContext<LongValue> context = map.context("one")) {
assertEquals(true, context.containsKey());
assertEquals(11, context.get().getValue());
}
// this will remove the entry
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
context.removeEntry();
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
context.remove();
}
// check that the entry was removed
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
assertEquals(false, context.present());
assertEquals(null, context.value());
try (MapKeyContext<LongValue> context = map.context("one")) {
assertEquals(false, context.containsKey());
assertEquals(null, context.get());
}
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
assertEquals(0, context.value().getValue());
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
assertEquals(0, context.get().getValue());
}
value.setValue(1);
try (ReadContext<?, LongValue> lockedEntry = map.getUsingLocked("one", value)) {
assertEquals(1, lockedEntry.value().getValue());
try (MapKeyContext<LongValue> lockedEntry = map.context("one")) {
assertEquals(1, lockedEntry.get().getValue());
}
try (WriteContext<?, LongValue> context = map.acquireUsingLocked("one", value)) {
LongValue value1 = context.value();
try (MapKeyContext<LongValue> context = map.acquireContext("one", value)) {
LongValue value1 = context.get();
value1.addValue(1);
}
// check that the entry was removed
try (ReadContext<?, LongValue> context = map.getUsingLocked("one", value)) {
LongValue value1 = context.value();
try (MapKeyContext<LongValue> context = map.context("one")) {
LongValue value1 = context.get();
assertEquals(2, value1.getValue());
}
@@ -8,6 +8,7 @@ import org.junit.Test;
import java.io.File;
import java.util.Map;
import static net.openhft.lang.model.DataValueClasses.newDirectReference;
import static org.junit.Assert.*;
public class DemoChronicleMapTest {
@@ -25,8 +26,8 @@ public class DemoChronicleMapTest {
.createPersistedTo(file)) {
IntValue key = DataValueClasses.newDirectInstance(IntValue.class);
DemoOrderVOInterface value = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value2 = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value = newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value2 = newDirectReference(DemoOrderVOInterface.class);
// Initially populate the map
for (int i = 0; i < maxEntries; i++) {
@@ -65,23 +66,24 @@ public class DemoChronicleMapTest {
.createPersistedTo(file)) {
IntValue key = DataValueClasses.newDirectInstance(IntValue.class);
DemoOrderVOInterface value = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value2 = DataValueClasses.newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value = newDirectReference(DemoOrderVOInterface.class);
DemoOrderVOInterface value2 = newDirectReference(DemoOrderVOInterface.class);
// Initially populate the map
for (int i = 0; i < maxEntries; i++) {
key.setValue(i);
try (WriteContext wc = map.acquireUsingLocked(key, value)) {
try (MapKeyContext wc = map.acquireContext(key, value)) {
value.setSymbol("IBM-" + i);
value.addAtomicOrderQty(1000);
}
try (ReadContext rc = map.getUsingLocked(key, value2)) {
assertTrue(rc.present());
assertEquals("IBM-" + i, value.getSymbol());
assertEquals(1000, value.getOrderQty(), 0.0);
}
// TODO suspicious -- getUsing `value2`, working with `value` then
// try (ReadContext rc = map.getUsingLocked(key, value2)) {
// assertTrue(rc.present());
// assertEquals("IBM-" + i, value.getSymbol());
// assertEquals(1000, value.getOrderQty(), 0.0);
// }
}
for (Map.Entry<IntValue, DemoOrderVOInterface> entry : map.entrySet()) {
@@ -21,6 +21,7 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.Closeable;
@@ -36,6 +37,7 @@ import java.util.concurrent.atomic.AtomicReference;
*
* @author Rob Austin.
*/
@Ignore("event listening API yet determined")
public class EventListenerWithTCPSocketReplicationTest {
ChronicleMap<Integer, CharSequence> map1;
@@ -50,24 +52,24 @@ public class EventListenerWithTCPSocketReplicationTest {
final AtomicBoolean wasRemoved = new AtomicBoolean(false);
final AtomicReference<CharSequence> valueRemoved = new AtomicReference<CharSequence>();
final MapEventListener<Integer, CharSequence> eventListener = new
MapEventListener<Integer, CharSequence>() {
private static final long serialVersionUID = 1L;
@Override
public void onPut(Integer key, CharSequence value, CharSequence replacedValue) {
putWasCalled.getAndSet(true);
keyRef.set(key);
valueRef.set(value);
replacedValueRef.set(replacedValue);
}
@Override
public void onRemove(Integer key, CharSequence value) {
wasRemoved.set(true);
valueRemoved.set(value);
}
};
// final MapEventListener<Integer, CharSequence> eventListener = new
// MapEventListener<Integer, CharSequence>() {
// private static final long serialVersionUID = 1L;
//
// @Override
// public void onPut(MapPutEvent<Integer, CharSequence> event) {
// putWasCalled.getAndSet(true);
// keyRef.set(key);
// valueRef.set(value);
// replacedValueRef.set(replacedValue);
// }
//
// @Override
// public void onRemove(MapEvent<Integer, CharSequence> event) {
// wasRemoved.set(true);
// valueRemoved.set(value);
// }
// };
@After
public void tearDown() throws InterruptedException {
@@ -222,8 +224,8 @@ public class EventListenerWithTCPSocketReplicationTest {
.autoReconnectedUponDroppedConnection(true);
return ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000L)
.replication((byte) 1, tcpConfig)
.eventListener(eventListener);
.replication((byte) 1, tcpConfig);
// .eventListener(eventListener);
}
private void waitTillReplicated(final int timeOutMs) throws InterruptedException {
@@ -26,6 +26,7 @@ import com.google.common.collect.testing.features.CollectionSize;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Ignore;
import java.util.List;
import java.util.Map;
@@ -22,9 +22,8 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.DirectStore;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import net.openhft.lang.io.NativeBytes;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -33,40 +32,50 @@ import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.*;
import static net.openhft.lang.io.NativeBytes.UNSAFE;
/**
* User: peter Date: 09/12/13
*/
@RunWith(value = Parameterized.class)
public class IntIntMultiMapTest {
public class HashLookupTest {
MultiMap map;
static final long CAPACITY = 64;
static long address;
HashLookup map = new HashLookup();
Multimap<Long, Long> referenceMap = HashMultimap.create();
private Class<? extends MultiMap> c;
public IntIntMultiMapTest(Class<? extends MultiMap> c)
public HashLookupTest(Integer entrySize, Integer keyBits, Integer valueBits)
throws Exception {
this.c = c;
map.reuse(address, CAPACITY, entrySize, keyBits, valueBits);
clean();
}
void clean() {
UNSAFE.setMemory(address, CAPACITY * 8L, (byte) 0);
}
@BeforeClass
public static void setUp() {
address = UNSAFE.allocateMemory(CAPACITY * 8L);
}
@AfterClass
public static void tearDown() {
UNSAFE.freeMemory(address);
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ShortShortMultiMap.class},
{Int24Int24MultiMap.class},
{IntIntMultiMap.class},
{3, 12, 9},
{4, 18, 14},
{8, 33, 29},
});
}
private void initMap(long capacity) {
try {
map = c.getConstructor(long.class).newInstance(capacity);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void multiMapEquals() {
class Action implements MultiMap.EntryConsumer {
class Action implements HashLookup.EntryConsumer {
int mapSize = 0;
@Override
@@ -83,9 +92,10 @@ public class IntIntMultiMapTest {
private void valuesEqualsByKey(long k) {
List<Long> values = new ArrayList<Long>();
SearchState searchState = new SearchState();
map.startSearch(k, searchState);
map.init0(k);
map.initSearch0();
long v;
while ((v = map.nextPos(searchState)) >= 0L)
while ((v = map.nextPos()) >= 0L)
values.add(v);
Set<Long> valueSet = new HashSet<Long>(values);
Assert.assertEquals(values.size(), valueSet.size());
@@ -93,18 +103,41 @@ public class IntIntMultiMapTest {
}
private void put(long k, long v) {
map.put(k, v);
put0(k, v);
referenceMap.put(k, v);
}
private void remove(long k, long v) {
map.remove(k, v);
private void put0(long k, long v) {
map.init0(k);
map.initSearch0();
while (map.nextPos() >= 0L)
;
map.put(v);
map.closeSearch0();
map.close0();
}
private void remove(long k, long value) {
long v = remove0(k, value);
referenceMap.remove(k, v);
}
private long remove0(long k, long value) {
map.init0(k);
map.initSearch0();
long v;
while ((v = map.nextPos()) >= 0L && v != value)
;
map.found();
map.remove();
map.closeSearch0();
map.close0();
return v;
}
@Test
public void testPutRemoveSearch() {
initMap(16);
clean();
multiMapEquals();
put(1, 11);
valuesEqualsByKey(1);
@@ -143,54 +176,12 @@ public class IntIntMultiMapTest {
// Testing a specific case when the remove method on the map
// does (did) not work as expected. The size goes correctly to
// 0 but the value is still present in the map.
initMap(10);
clean();
map.put(15, 1);
map.remove(15, 1);
SearchState searchState = new SearchState();
map.startSearch(15, searchState);
Assert.assertTrue(map.nextPos(searchState) < 0);
}
@Test
@Ignore("Very long running test")
public void testMaxCapacity() throws NoSuchFieldException, IllegalAccessException {
Field maxCapacityField = c.getDeclaredField("MAX_CAPACITY");
long maxCapacity = maxCapacityField.getLong(null);
Field entrySizeField = c.getDeclaredField("ENTRY_SIZE");
entrySizeField.setAccessible(true);
long entrySize = entrySizeField.getLong(null);
if (maxMemory() * 1024L < maxCapacity * entrySize * 3L / 2L) {
System.out.println("Skipped " + c + " maxCapacity test because there is not enough " +
"memory in system");
return;
}
initMap(maxCapacity);
Random r = new Random();
int bound = maxCapacity > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) maxCapacity;
for (long i = 0L; i < maxCapacity; i++) {
map.put(r.nextInt(bound), i);
}
final SingleThreadedDirectBitSet bs =
new SingleThreadedDirectBitSet(new DirectStore(maxCapacity / 8).bytes());
map.forEach(new MultiMap.EntryConsumer() {
@Override
public void accept(long key, long value) {
bs.set(value);
}
});
Assert.assertTrue(bs.allSet(0L, maxCapacity));
}
private long maxMemory() {
File meminfo = new File("/proc/meminfo");
try {
try (Scanner sc = new Scanner(meminfo)) {
sc.next();
return sc.nextLong();
}
} catch (FileNotFoundException e) {
return -1;
}
put0(15, 1);
remove0(15, 1);
map.init0(15);
map.initSearch0();
Assert.assertTrue(map.nextPos() < 0);
}
}
@@ -33,7 +33,7 @@ public class NoTcpReplicationSoakTest {
TimeProvider timeProvider = new TimeProvider() {
@Override
public long currentTimeMillis() {
public long currentTime() {
return time;
}
};
@@ -123,25 +123,49 @@ public class NoTcpReplicationSoakTest {
if (rnd.nextBoolean()) {
final CharSequence value = "test" + j;
if (rnd.nextBoolean()) {
map1.put(key, value, (byte) 1, t);
try (MapKeyContext<CharSequence> c = map1.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 1;
c.put(value);
}
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map2.put0(key, value, true, (byte) 1, t);
try (MapKeyContext<CharSequence> c = map2.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 1;
c.put(value);
}
task.decrementAndGet();
}
});
} else {
map2.put(key, value, (byte) 2, t);
try (MapKeyContext<CharSequence> c = map2.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 2;
c.put(value);
}
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map1.put0(key, value, true, (byte) 2, t);
try (MapKeyContext<CharSequence> c = map1.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 2;
c.put(value);
}
task.decrementAndGet();
}
});
@@ -150,25 +174,49 @@ public class NoTcpReplicationSoakTest {
} else {
if (rnd.nextBoolean()) {
map1.remoteRemove(key, (byte) 1, t);
try (MapKeyContext<CharSequence> c = map1.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 1;
c.remove();
}
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map2.remoteRemove(key, (byte) 1, t);
try (MapKeyContext<CharSequence> c = map2.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 1;
c.remove();
}
task.decrementAndGet();
}
});
} else {
map2.remoteRemove(key, (byte) 2, t);
try (MapKeyContext<CharSequence> c = map2.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 2;
c.remove();
}
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map1.remoteRemove(key, (byte) 2, t);
try (MapKeyContext<CharSequence> c = map1.context(key)) {
ReplicatedChronicleMap.ReplicatedContext rc =
(ReplicatedChronicleMap.ReplicatedContext) c;
rc.newTimestamp = t;
rc.newIdentifier = (byte) 2;
c.remove();
}
task.decrementAndGet();
}
});
@@ -264,13 +264,12 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
@NotNull
@Override
public WriteContext<K, V> acquireUsingLocked(@NotNull K key, @NotNull V usingValue) {
public MapKeyContext<V> acquireContext(@NotNull K key, @NotNull V usingValue) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public ReadContext<K, V> getUsingLocked(@NotNull K key, @NotNull V usingValue) {
public MapKeyContext<V> context(K key) {
throw new UnsupportedOperationException();
}
@@ -92,8 +92,7 @@ public class TCPSocketReplicationNativeTest {
LongValue value = DataValueClasses.newDirectReference(LongValue.class);
try (WriteContext<Integer, LongValue> c = map1
.acquireUsingLocked(1, value)) {
try (MapKeyContext<LongValue> c = map1.acquireContext(1, value)) {
value.setValue(10);
}
@@ -109,13 +108,11 @@ public class TCPSocketReplicationNativeTest {
LongValue value = DataValueClasses.newDirectReference(LongValue.class);
try (WriteContext<Integer, LongValue> c = map1
.acquireUsingLocked(1, value)) {
try (MapKeyContext<LongValue> c = map1.acquireContext(1, value)) {
value.setValue(10);
}
try (WriteContext<Integer, LongValue> c = map1
.acquireUsingLocked(1, value)) {
try (MapKeyContext<LongValue> c = map1.acquireContext(1, value)) {
value.setValue(20);
}
@@ -10,7 +10,7 @@ import java.net.InetSocketAddress;
/**
* @author Rob Austin.
*/
public class TcpReplicationWithAcquireUsingLockedTest {
public class TcpReplicationWithAcquireContextTest {
@Test
@@ -45,8 +45,8 @@ public class TcpReplicationWithAcquireUsingLockedTest {
TestInstrumentVOInterface instrumentVOInterface = map1.newValueInstance();
try (WriteContext<CharSequence, TestInstrumentVOInterface> wc =
map1.acquireUsingLocked("KEY1", instrumentVOInterface)) {
try (MapKeyContext<TestInstrumentVOInterface> wc =
map1.acquireContext("KEY1", instrumentVOInterface)) {
instrumentVOInterface.setSymbol("Flyer");
instrumentVOInterface.setCurrencyCode("USA");
}
@@ -43,7 +43,7 @@ public class TcpTimeBasedReplicationSoakTest {
Random rnd = new Random(4);
@Override
public long currentTimeMillis() {
public long currentTime() {
if (rnd.nextBoolean())
return t++;
@@ -160,7 +160,7 @@ public class TcpTimeBasedReplicationSoakTest {
}
private void late(TimeProvider timeProvider) {
Mockito.when(timeProvider.currentTimeMillis()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
Mockito.when(timeProvider.currentTime()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
}
}
@@ -283,7 +283,12 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
// now test assume that we receive a late update to the map, the following update should be ignored
// now test assume that we receive a late update to the map, the following update should be ignored
final long late = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5);
assertEquals(null, map.put("key-1", "value-2", IDENTIFIER, late));
try (MapKeyContext<CharSequence> c = map.context("key-1")) {
c.writeLock().lock();
((ReplicatedChronicleMap.ReplicatedContext) c).newIdentifier = IDENTIFIER;
((ReplicatedChronicleMap.ReplicatedContext) c).newTimestamp = late;
assertFalse(((ReplicatedChronicleMap.ReplicatedContext) c).put("value-2"));
}
// we'll now flip the time back to the current in order to do the read the result
current(timeProvider);
@@ -314,7 +319,12 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
// test assume that we receive a late update to the map, the following update should be ignored
final long late = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(50);
assertEquals(null, map.put("key-1", "value-2", IDENTIFIER, late));
try (MapKeyContext<CharSequence> c = map.context("key-1")) {
c.writeLock().lock();
((ReplicatedChronicleMap.ReplicatedContext) c).newIdentifier = IDENTIFIER;
((ReplicatedChronicleMap.ReplicatedContext) c).newTimestamp = late;
assertFalse(((ReplicatedChronicleMap.ReplicatedContext) c).put("value-2"));
}
assertEquals(null, map.get("key-1"));
assertEquals(false, map.containsKey("key-1"));
@@ -345,10 +355,10 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
}
private void current(TimeProvider timeProvider) {
Mockito.when(timeProvider.currentTimeMillis()).thenReturn(System.currentTimeMillis());
Mockito.when(timeProvider.currentTime()).thenReturn(System.currentTimeMillis());
}
private void late(TimeProvider timeProvider) {
Mockito.when(timeProvider.currentTimeMillis()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
Mockito.when(timeProvider.currentTime()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
}
}
@@ -90,7 +90,8 @@ public class TwoMapOnDifferentServersTest {
map1.put(5, "EXAMPLE-2");
// allow time for the recompilation to resolve
waitTillEqual(5000);
// waitTillEqual(5000);
Thread.sleep(5000);
assertEquals(map1, map2);
assertTrue(!map1.isEmpty());
@@ -20,8 +20,7 @@ package net.openhft.chronicle.map.fromdocs;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.ReadContext;
import net.openhft.chronicle.map.WriteContext;
import net.openhft.chronicle.map.MapKeyContext;
import org.junit.Test;
import java.io.File;
@@ -61,7 +60,7 @@ public class OpenJDKAndHashMapExamplesTest {
.createPersistedTo(file);
BondVOInterface bondVO = chm.newValueInstance();
try (WriteContext wc = chm.acquireUsingLocked("369604103", bondVO)) {
try (MapKeyContext wc = chm.acquireContext("369604103", bondVO)) {
bondVO.setIssueDate(parseYYYYMMDD("20130915"));
bondVO.setMaturityDate(parseYYYYMMDD("20140915"));
bondVO.setCoupon(5.0 / 100); // 5.0%
@@ -81,11 +80,9 @@ public class OpenJDKAndHashMapExamplesTest {
.entries(1000)
.createPersistedTo(file);
// ZERO Copy but creates a new off heap reference each time
// our reusable, mutable off heap reference, generated from the interface.
BondVOInterface bond = chm.newValueInstance();
try (ReadContext rc = chmB.getUsingLocked("369604103", bond)) {
if (rc.present()) {
try (MapKeyContext<BondVOInterface> c = chmB.context("369604103")) {
BondVOInterface bond = c.get();
if (bond != null) {
assertEquals(5.0 / 100, bond.getCoupon(), 0.0);
BondVOInterface.MarketPx mpx930B = bond.getMarketPxIntraDayHistoryAt(0);
@@ -98,8 +95,9 @@ public class OpenJDKAndHashMapExamplesTest {
}
}
BondVOInterface bond = chm.newValueInstance();
// lookup the key and give me a reference I can update in a thread safe way.
try (WriteContext wc = chm.acquireUsingLocked("369604103", bond)) {
try (MapKeyContext<BondVOInterface> c = chm.acquireContext("369604103", bond)) {
// found a key and bond has been set
// get directly without touching the rest of the record.
long _matDate = bond.getMaturityDate();
@@ -20,6 +20,7 @@ package net.openhft.chronicle.map.jsr166.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.*;
import net.openhft.chronicle.map.MapKeyContext;
import net.openhft.chronicle.map.jsr166.JSR166TestCase;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
@@ -105,6 +106,11 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
return d.containsValue(value);
}
@Override
public MapKeyContext<V> context(K key) {
return d.context(key);
}
@Override
public V get(Object key) {
return d.get(key);
@@ -160,13 +166,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
@NotNull
@Override
public WriteContext<K, V> acquireUsingLocked(@NotNull K key, @NotNull V usingValue) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public ReadContext<K, V> getUsingLocked(@NotNull K key, @NotNull V usingValue) {
public MapKeyContext<V> acquireContext(@NotNull K key, @NotNull V usingValue) {
throw new UnsupportedOperationException();
}
@@ -85,7 +85,7 @@ public class SetForTCPSocketReplication3wayTest {
waitTillEqual(15000);
assertEquals(set1, set2);
assertEquals(set3, set2);
assertEquals(set2, set2);
assertTrue(!set1.isEmpty());
}
@@ -108,7 +108,7 @@ public class SetForTCPSocketReplication3wayTest {
waitTillEqual(5000);
assertEquals(set1, set2);
assertEquals(set3, set3);
assertEquals(set2, set3);
assertTrue(!set1.isEmpty());
}
@@ -132,7 +132,7 @@ public class SetForTCPSocketReplication3wayTest {
waitTillEqual(15000);
assertEquals(set1, set2);
assertEquals(set3, set3);
assertEquals(set2, set3);
assertTrue(!set1.isEmpty());
}