Merge branch 'master' of github.com:OpenHFT/Chronicle-Map

Conflicts:
	pom.xml
This commit is contained in:
leventov
2014-10-19 21:02:01 +04:00
17 changed files with 1191 additions and 421 deletions
@@ -21,8 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.TimeUnit;
/**
* @author Rob Austin.
@@ -69,7 +68,7 @@ public class Builder {
final int serverPort,
final InetSocketAddress... endpoints) throws IOException {
TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(serverPort, endpoints)
.heartBeatInterval(1L, SECONDS);
.heartBeatInterval(1L, TimeUnit.SECONDS);
return ChronicleMapBuilder.of(kClass, vClass)
.entries(20000L)
.replicators(identifier, tcpConfig);
@@ -34,9 +34,8 @@ public class OSResizesMain {
public static void main(String[] args) throws IOException, InterruptedException {
File file = File.createTempFile("over-sized", "deleteme");
ChronicleMap<String, String> map = ChronicleMapBuilder.of(String.class, String.class)
.entrySize(1024 * 1024)
.entries(1024 * 1024)
.entrySize(100 * 1024 * 1024)
.entries(1000000)
.create(file);
for (int i = 0; i < 10000; i++) {
char[] chars = new char[i];
@@ -48,6 +47,10 @@ public class OSResizesMain {
file.length() / 1e9,
run("du", "-h", file.getAbsolutePath()).split("\\s")[0],
run("grep", "over-sized", "/proc/" + Jvm.getProcessId() + "/maps").split("\\s")[0]);
// show up in top.
long time = System.currentTimeMillis();
while (time + 20000 > System.currentTimeMillis())
Thread.yield();
map.close();
file.delete();
}
@@ -0,0 +1,240 @@
package net.openhft.chronicle.map;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import net.openhft.lang.model.constraints.NotNull;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Collections;
public class SerializerTest {
@Test
public void testValueMarshallable() throws Exception {
testReadWriteValue("Test");
testReadWriteValue(1);
testReadWriteValue(1L);
testReadWriteValue(1.0);
testReadWriteValue(1.0f);
testReadWriteValue(Collections.singleton("Test"));
testReadWriteValue(Collections.EMPTY_MAP);
testReadWriteValue(new MyTestClass(3));
testReadWriteValue(new MyTestClassMarshallable(3));
testReadWriteValue(new MyTestClassExternalizable(3));
testReadWriteValue(new MyTestClassObjectGraph(3));
}
@Test
public void testKeyMarshallable() throws Exception {
testReadWriteKey("Test");
testReadWriteKey(1);
testReadWriteKey(1L);
testReadWriteKey(1.0);
testReadWriteKey(1.0f);
testReadWriteKey(Collections.singleton("Test"));
testReadWriteKey(Collections.EMPTY_MAP);
testReadWriteKey(new MyTestClass(3));
testReadWriteKey(new MyTestClassMarshallable(3));
testReadWriteKey(new MyTestClassExternalizable(3));
testReadWriteKey(new MyTestClassObjectGraph(3));
}
public void testReadWriteValue(Object value) throws Exception {
Class valueClass = value.getClass();
final ByteBufferBytes out = new ByteBufferBytes(ByteBuffer.allocateDirect(1024));
ByteBufferBytes in = out.slice();
ChronicleMapBuilder builder = ChronicleMapBuilder.of(Integer.class, valueClass);
builder.preMapConstruction();
Serializer v = new Serializer(builder.valueBuilder);
v.writeMarshallable(value, out);
long position = out.position();
in.limit(position);
Object actual = v.readMarshallable(in);
Assert.assertEquals(actual, value);
}
public void testReadWriteKey(Object key) throws Exception {
Class clazz = key.getClass();
final ByteBufferBytes out = new ByteBufferBytes(ByteBuffer.allocateDirect(1024));
ByteBufferBytes in = out.slice();
ChronicleMapBuilder builder = ChronicleMapBuilder.of(clazz, Integer.class);
builder.preMapConstruction();
{
Serializer v = new Serializer(builder.keyBuilder);
v.writeMarshallable(key, out);
long position = out.position();
in.limit(position);
Object actual = v.readMarshallable(in);
Assert.assertEquals(actual, key);
}
}
public static class MyTestClassExternalizable implements Externalizable {
int a;
public MyTestClassExternalizable() {
}
MyTestClassExternalizable(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassExternalizable that = (MyTestClassExternalizable) o;
if (a != that.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.write(a);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
a = in.readInt();
}
}
public static class MyTestClass implements Serializable {
int a;
MyTestClass(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClass myTestClass = (MyTestClass) o;
if (a != myTestClass.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
}
public static class MyTestClassMarshallable implements BytesMarshallable {
int a;
public MyTestClassMarshallable() {
}
public MyTestClassMarshallable(int a) {
this.a = a;
}
public void setA(int a) {
this.a = a;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassMarshallable that = (MyTestClassMarshallable) o;
if (a != that.a) return false;
return true;
}
@Override
public int hashCode() {
return a;
}
public void readMarshallable(@NotNull Bytes in) throws IllegalStateException {
a = in.readInt();
}
/**
* write an object to bytes
*
* @param out to write to
*/
public void writeMarshallable(@NotNull Bytes out) {
out.writeInt(a);
}
}
public static class MyTestClassObjectGraph implements Serializable {
MyTestClass delegate;
public MyTestClassObjectGraph() {
}
MyTestClassObjectGraph(int a) {
delegate = new MyTestClass(a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyTestClassObjectGraph that = (MyTestClassObjectGraph) o;
if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null)
return false;
return true;
}
@Override
public int hashCode() {
return delegate != null ? delegate.hashCode() : 0;
}
}
}
@@ -0,0 +1,41 @@
package net.openhft.chronicle.map;
import org.junit.Test;
import org.testng.Assert;
import java.io.IOException;
import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.SECONDS;
import static net.openhft.chronicle.map.Builder.getPersistenceFile;
import static net.openhft.chronicle.map.TcpReplicationConfig.of;
/**
* @author Rob Austin.
*/
public class StatelessClientTest {
@Test
public void test() throws IOException, InterruptedException {
ChronicleMapBuilder<Integer, CharSequence> builder =
ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(20000L)
.replicators((byte) 2, of(8076).heartBeatInterval(1L, SECONDS));
{
final ChronicleMap<Integer, CharSequence> map2a =
builder.create(getPersistenceFile());
map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2
}
KeyValueSerializer keyValueSerializer = new KeyValueSerializer(builder.keyBuilder, builder.valueBuilder);
InetSocketAddress remote = new InetSocketAddress("localhost", 8076);
StatelessMapClient<Integer, String> map = new StatelessMapClient<Integer, String>(keyValueSerializer, remote);
Assert.assertEquals("EXAMPLE-10", map.get(10));
Assert.assertEquals(1, map.size());
}
}