Mercurial > stress-tester
changeset 733:cc190cb53f4f
ValueCoder - works with byte[]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoder.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoder.java Mon Dec 04 14:18:21 2017 +0100 @@ -4,6 +4,7 @@ import com.passus.data.ByteString; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.mutable.Mutable; /** * @@ -82,6 +83,22 @@ return coder.decode(buff); } + @Override + public int encode(byte[] bytes, int off, Object value) { + byte code = findCode(value); + bytes[off++] = code; + int written = CODE_TO_CODER[code].encode(bytes, off, value); + return written + 1; + } + + @Override + public int decode(byte[] bytes, int off, Mutable value) { + byte code = bytes[off++]; + ValueCoder coder = CODE_TO_CODER[code]; + int read = coder.decode(bytes, off, value); + return read + 1; + } + private static class NullCoder implements ValueCoder<Object> { @Override @@ -92,5 +109,16 @@ public Object decode(ByteBuff buff) { return null; } + + @Override + public int encode(byte[] bytes, int off, Object value) { + return 0; + } + + @Override + public int decode(byte[] bytes, int off, Mutable<Object> value) { + value.setValue(null); + return 0; + } } }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoder.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoder.java Mon Dec 04 14:18:21 2017 +0100 @@ -1,6 +1,7 @@ package com.passus.st.reader.nc.option; import com.passus.data.ByteBuff; +import org.apache.commons.lang3.mutable.Mutable; /** * @@ -9,6 +10,10 @@ */ public interface ValueCoder<T> { + public int encode(byte[] bytes, int off, T value); + + public int decode(byte[] bytes, int off, Mutable<T> value); + public void encode(T value, ByteBuff buff); public T decode(ByteBuff buff);
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java Mon Dec 04 14:18:21 2017 +0100 @@ -4,6 +4,9 @@ import com.passus.data.ByteString; import com.passus.data.DataHelper; import com.passus.st.reader.nc.NcDataHelper; +import org.apache.commons.lang3.mutable.Mutable; +import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.commons.lang3.mutable.MutableLong; /** * @@ -17,6 +20,18 @@ public static class IntCoder implements ValueCoder<Integer> { @Override + public int encode(byte[] bytes, int off, Integer value) { + HELPER.writeInt4(bytes, off, value); + return 4; + } + + @Override + public int decode(byte[] bytes, int off, Mutable<Integer> value) { + value.setValue(HELPER.getInt4(bytes, off)); + return 4; + } + + @Override public void encode(Integer value, ByteBuff buff) { HELPER.writeInt4(buff, value); } @@ -30,6 +45,19 @@ public static class IntVlcCoder implements ValueCoder<Integer> { @Override + public int encode(byte[] bytes, int off, Integer value) { + return NC_HELPER.writeIntVLC(bytes, off, value); + } + + @Override + public int decode(byte[] bytes, int off, Mutable<Integer> value) { + MutableInt mi = new MutableInt(); + int read = NC_HELPER.getIntVLC(bytes, off, bytes.length, mi); + value.setValue(mi.getValue()); + return read; + } + + @Override public void encode(Integer value, ByteBuff buff) { NC_HELPER.writeIntVLC(buff, value); } @@ -43,6 +71,18 @@ public static class LongCoder implements ValueCoder<Long> { @Override + public int encode(byte[] bytes, int off, Long value) { + HELPER.writeLong8(bytes, off, value); + return 8; + } + + @Override + public int decode(byte[] bytes, int off, Mutable<Long> value) { + value.setValue(HELPER.getLong8(bytes, off)); + return 8; + } + + @Override public void encode(Long value, ByteBuff buff) { HELPER.writeLong8(buff, value); } @@ -56,6 +96,19 @@ public static class LongVlcCoder implements ValueCoder<Long> { @Override + public int encode(byte[] bytes, int off, Long value) { + return NC_HELPER.writeLongVLC(bytes, off, value); + } + + @Override + public int decode(byte[] bytes, int off, Mutable<Long> value) { + MutableLong ml = new MutableLong(); + int read = NC_HELPER.getLongVLC(bytes, off, bytes.length, ml); + value.setValue(ml.getValue()); + return read; + } + + @Override public void encode(Long value, ByteBuff buff) { NC_HELPER.writeLongVLC(buff, value); } @@ -69,6 +122,16 @@ public static class ByteStringNTCoder implements ValueCoder<ByteString> { @Override + public int encode(byte[] bytes, int off, ByteString value) { + return NC_HELPER.writeByteStringNullTerminated(bytes, off, bytes.length, value, true); + } + + @Override + public int decode(byte[] bytes, int off, Mutable<ByteString> value) { + return NC_HELPER.readByteStringNullTerminated(bytes, off, value); + } + + @Override public void encode(ByteString value, ByteBuff buff) { NC_HELPER.writeByteStringNullTerminated(buff, value); } @@ -82,6 +145,16 @@ public static class StringNTCoder implements ValueCoder<String> { @Override + public int encode(byte[] bytes, int off, String value) { + return NC_HELPER.writeStringNullTerminated(bytes, off, bytes.length, value); + } + + @Override + public int decode(byte[] bytes, int off, Mutable<String> value) { + return NC_HELPER.readStringNullTerminated(bytes, off, value); + } + + @Override public void encode(String value, ByteBuff buff) { NC_HELPER.writeStringNullTerminated(buff, value); }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOption.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOption.java Mon Dec 04 14:18:21 2017 +0100 @@ -5,11 +5,20 @@ * @author mikolaj.podbielski */ public class VarOption { - + private String name; private long startOffset; private long endOffset; + public VarOption() { + } + + public VarOption(String name, long startOffset, long endOffset) { + this.name = name; + this.startOffset = startOffset; + this.endOffset = endOffset; + } + public String getName() { return name; } @@ -33,5 +42,5 @@ public void setEndOffset(long endOffset) { this.endOffset = endOffset; } - + }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java Mon Dec 04 14:18:21 2017 +0100 @@ -2,6 +2,9 @@ import com.passus.data.ByteBuff; import com.passus.st.reader.nc.NcDataHelper; +import org.apache.commons.lang3.mutable.Mutable; +import org.apache.commons.lang3.mutable.MutableLong; +import org.apache.commons.lang3.mutable.MutableObject; /** * @@ -12,6 +15,35 @@ public static NcDataHelper NC_HELPER = NcDataHelper.getInstance(); @Override + public int encode(byte[] bytes, int off, VarOption value) { + int newOffset = off; + newOffset += NC_HELPER.writeStringNullTerminated(bytes, newOffset, bytes.length, value.getName()); + newOffset += NC_HELPER.writeLongVLC(bytes, newOffset, value.getStartOffset()); + newOffset += NC_HELPER.writeLongVLC(bytes, newOffset, value.getEndOffset()); + return newOffset - off; + } + + @Override + public int decode(byte[] bytes, int off, Mutable<VarOption> value) { + Mutable<String> ms = new MutableObject<>(); + MutableLong ml = new MutableLong(); + VarOption option = new VarOption(); + int newOffset = off; + + newOffset += NC_HELPER.readStringNullTerminated(bytes, off, ms); + option.setName(ms.getValue()); + + newOffset += NC_HELPER.getLongVLC(bytes, newOffset, bytes.length, ml); + option.setStartOffset(ml.getValue()); + + newOffset += NC_HELPER.getLongVLC(bytes, newOffset, bytes.length, ml); + option.setEndOffset(ml.getValue()); + + value.setValue(option); + return newOffset - off; + } + + @Override public void encode(VarOption value, ByteBuff buff) { NC_HELPER.writeStringNullTerminated(buff, value.getName()); NC_HELPER.writeLongVLC(buff, value.getStartOffset());
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java Mon Dec 04 14:18:21 2017 +0100 @@ -1,7 +1,10 @@ package com.passus.st.reader.nc.option; +import com.passus.commons.AsciiUtils; import com.passus.data.ByteString; import com.passus.data.HeapByteBuff; +import org.apache.commons.lang3.mutable.Mutable; +import org.apache.commons.lang3.mutable.MutableObject; import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -12,15 +15,31 @@ public class DefaultValueCoderTest { @Test - public void testConcreteClass() { + public void testConcreteClass_bytes() { + byte[] dst = {-1, -2, -3, -4, -5, -6, -7, -8}; + String s = "abc"; + byte[] encoded = {-1, DefaultValueCoder.STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL, -7, -8}; + Mutable result = new MutableObject(); + + int n1 = DefaultValueCoder.INSTANCE.encode(dst, 1, s); + assertEquals(encoded, dst); + assertEquals(5, n1); + + int n2 = DefaultValueCoder.INSTANCE.decode(encoded, 1, result); + assertEquals(s, result.getValue()); + assertEquals(5, n2); + } + + @Test + public void testConcreteClass_ByteBuff() { HeapByteBuff buff = new HeapByteBuff(8); String s = "abc"; DefaultValueCoder.INSTANCE.encode(s, buff); - byte[] expected = {DefaultValueCoder.STRING_CODE, 'a', 'b', 'c', 0, 0, 0, 0}; + byte[] expected = {DefaultValueCoder.STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; assertEquals(0, buff.startIndex()); assertEquals(5, buff.endIndex()); - assertEquals(expected, buff.buffer()); + assertEquals(expected, buff.toArray()); Object obj = DefaultValueCoder.INSTANCE.decode(buff); assertEquals(5, buff.startIndex()); @@ -29,15 +48,31 @@ } @Test - public void testAssignableClass() { + public void testAssignableClass_bytes() { + byte[] dst = {-1, -2, -3, -4, -5, -6, -7, -8}; + ByteString bs = ByteString.create("abc"); + byte[] encoded = {-1, DefaultValueCoder.BYTE_STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL, -7, -8}; + Mutable result = new MutableObject(); + + int n1 = DefaultValueCoder.INSTANCE.encode(dst, 1, bs); + assertEquals(encoded, dst); + assertEquals(5, n1); + + int n2 = DefaultValueCoder.INSTANCE.decode(encoded, 1, result); + assertEquals("abc", result.getValue().toString()); + assertEquals(5, n2); + } + + @Test + public void testAssignableClass_ByteBuff() { HeapByteBuff buff = new HeapByteBuff(8); ByteString bs = ByteString.create("abc"); DefaultValueCoder.INSTANCE.encode(bs, buff); - byte[] expected = {DefaultValueCoder.BYTE_STRING_CODE, 'a', 'b', 'c', 0, 0, 0, 0}; + byte[] expected = {DefaultValueCoder.BYTE_STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; assertEquals(0, buff.startIndex()); assertEquals(5, buff.endIndex()); - assertEquals(expected, buff.buffer()); + assertEquals(expected, buff.toArray()); Object obj = DefaultValueCoder.INSTANCE.decode(buff); assertEquals(5, buff.startIndex()); @@ -46,7 +81,22 @@ } @Test - public void testNull() { + public void testNull_bytes() { + byte[] dst = {-1, -2, -3, -4}; + byte[] encoded = {-1, DefaultValueCoder.NULL_CODE, -3, -4}; + Mutable result = new MutableObject(); + + int n1 = DefaultValueCoder.INSTANCE.encode(dst, 1, null); + assertEquals(encoded, dst); + assertEquals(1, n1); + + int n2 = DefaultValueCoder.INSTANCE.decode(encoded, 1, result); + assertEquals(null, result.getValue()); + assertEquals(1, n2); + } + + @Test + public void testNull_ByteBuf() { HeapByteBuff buff = new HeapByteBuff(8); DefaultValueCoder.INSTANCE.encode(null, buff); @@ -62,6 +112,11 @@ } @Test(expectedExceptions = IllegalArgumentException.class) + public void testUnknownObjectType_bytes() { + DefaultValueCoder.INSTANCE.encode(null, 0, new Thread()); + } + + @Test(expectedExceptions = IllegalArgumentException.class) public void testUnknownObjectType() { DefaultValueCoder.INSTANCE.encode(new Thread(), null); }
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java Mon Dec 04 14:18:21 2017 +0100 @@ -1,8 +1,11 @@ package com.passus.st.reader.nc.option; +import com.passus.commons.AsciiUtils; import com.passus.data.ByteBuff; import com.passus.data.ByteString; import com.passus.data.HeapByteBuff; +import org.apache.commons.lang3.mutable.Mutable; +import org.apache.commons.lang3.mutable.MutableObject; import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -13,7 +16,22 @@ public class ValueCodersTest { @Test - public void testIntCoder() { + public void testIntCoder_bytes() { + byte[] dst = new byte[8]; + byte[] encoded = {0, 0, 0, 0, 0, 123, 0, 0}; + Mutable<Integer> result = new MutableObject<>(); + + int n1 = ValueCoders.INT_CODER.encode(dst, 2, 123); + assertEquals(encoded, dst); + assertEquals(4, n1); + + int n2 = ValueCoders.INT_CODER.decode(dst, 2, result); + assertEquals(123, result.getValue().intValue()); + assertEquals(4, n2); + } + + @Test + public void testIntCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(8); ValueCoders.INT_CODER.encode(123, buff); @@ -29,7 +47,22 @@ } @Test - public void testIntVlcCoder() { + public void testIntVlcCoder_bytes() { + byte[] dst = new byte[6]; + byte[] encoded = {0, 0, 123, 0, 0, 0}; + Mutable<Integer> result = new MutableObject<>(); + + int n1 = ValueCoders.INT_VLC_CODER.encode(dst, 2, 123); + assertEquals(encoded, dst); + assertEquals(1, n1); + + int n2 = ValueCoders.INT_VLC_CODER.decode(dst, 2, result); + assertEquals(123, result.getValue().intValue()); + assertEquals(1, n2); + } + + @Test + public void testIntVlcCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(8); ValueCoders.INT_VLC_CODER.encode(123, buff); @@ -45,7 +78,22 @@ } @Test - public void testLongCoder() { + public void testLongCoder_bytes() { + byte[] dst = new byte[12]; + byte[] encoded = {0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0}; + Mutable<Long> result = new MutableObject<>(); + + int n1 = ValueCoders.LONG_CODER.encode(dst, 2, 123L); + assertEquals(encoded, dst); + assertEquals(8, n1); + + int n2 = ValueCoders.LONG_CODER.decode(dst, 2, result); + assertEquals(123, result.getValue().intValue()); + assertEquals(8, n2); + } + + @Test + public void testLongCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(12); ValueCoders.LONG_CODER.encode(123L, buff); @@ -61,7 +109,22 @@ } @Test - public void testLongVlcCoder() { + public void testLongVlcCoder_bytes() { + byte[] dst = new byte[6]; + byte[] encoded = {0, 0, 0, 123, 0, 0}; + Mutable<Long> result = new MutableObject<>(); + + int n1 = ValueCoders.LONG_VLC_CODER.encode(dst, 2, 123L); + assertEquals(encoded, dst); + assertEquals(2, n1); + + int n2 = ValueCoders.LONG_VLC_CODER.decode(dst, 2, result); + assertEquals(123, result.getValue().intValue()); + assertEquals(2, n2); + } + + @Test + public void testLongVlcCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(8); ValueCoders.LONG_VLC_CODER.encode(123L, buff); @@ -77,7 +140,23 @@ } @Test - public void testByteStringNTCoder() { + public void testByteStringNTCoder_bytes() { + byte[] dst = new byte[8]; + ByteString bs = ByteString.create("12\n45"); + byte[] encoded = {0, '1', '2', '\n', '4', '5', AsciiUtils.NUL, 0}; + Mutable<ByteString> result = new MutableObject<>(); + + int n1 = ValueCoders.BYTE_STRING_NT_CODER.encode(dst, 1, bs); + assertEquals(encoded, dst); + assertEquals(6, n1); + + int n2 = ValueCoders.BYTE_STRING_NT_CODER.decode(encoded, 1, result); + assertEquals("12\n45", result.getValue().toString()); + assertEquals(6, n2); + } + + @Test + public void testByteStringNTCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(8); ByteString bs = ByteString.create("12\n45"); @@ -94,7 +173,23 @@ } @Test - public void testStringNTCoder() { + public void testStringNTCoder_bytes() { + byte[] dst = new byte[8]; + String bs = "12\n45"; + byte[] encoded = {0, '1', '2', '\n', '4', '5', AsciiUtils.NUL, 0}; + Mutable<String> result = new MutableObject<>(); + + int n1 = ValueCoders.STRING_NT_CODER.encode(dst, 1, bs); + assertEquals(encoded, dst); + assertEquals(6, n1); + + int n2 = ValueCoders.STRING_NT_CODER.decode(encoded, 1, result); + assertEquals("12\n45", result.getValue()); + assertEquals(6, n2); + } + + @Test + public void testStringNTCoder_ByteBuff() { ByteBuff buff = new HeapByteBuff(8); ValueCoders.STRING_NT_CODER.encode("ąć", buff);
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/VarOptionCoderTest.java Mon Dec 04 13:26:28 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/VarOptionCoderTest.java Mon Dec 04 14:18:21 2017 +0100 @@ -1,6 +1,9 @@ package com.passus.st.reader.nc.option; +import com.passus.commons.AsciiUtils; import com.passus.data.HeapByteBuff; +import org.apache.commons.lang3.mutable.Mutable; +import org.apache.commons.lang3.mutable.MutableObject; import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -9,38 +12,53 @@ * @author mikolaj.podbielski */ public class VarOptionCoderTest { - + VarOptionCoder coder = new VarOptionCoder(); - + @Test - public void testEncode() { - VarOption vo = new VarOption(); - vo.setName("user"); // 5B - vo.setStartOffset(120); // 2B - vo.setEndOffset(140); // 2B - + public void testEncodeDecode_bytes() { + VarOption vo = new VarOption("user", 120, 140); // 5B + 2B + 2B + byte[] encoded = {0, 'u', 's', 'e', 'r', AsciiUtils.NUL, 0, 120, 0, (byte) 140, 0, 0}; + byte[] dst = new byte[12]; + Mutable<VarOption> result = new MutableObject<>(); + + int n1 = coder.encode(dst, 1, vo); + assertEquals(encoded, dst); + assertEquals(9, n1); + + int n2 = coder.decode(encoded, 1, result); + VarOption value = result.getValue(); + assertEquals(value.getName(), "user"); + assertEquals(value.getStartOffset(), 120); + assertEquals(value.getEndOffset(), 140); + assertEquals(9, n2); + } + + @Test + public void testEncode_ByteBuff() { + VarOption vo = new VarOption("user", 120, 140); // 5B + 2B + 2B HeapByteBuff dst = new HeapByteBuff(); coder.encode(vo, dst); - - byte[] expected = {'u', 's', 'e', 'r', 0, 0, 120, 0, (byte) 140}; + + byte[] expected = {'u', 's', 'e', 'r', AsciiUtils.NUL, 0, 120, 0, (byte) 140}; assertEquals(0, dst.startIndex()); assertEquals(9, dst.endIndex()); assertEquals(expected, dst.toArray()); } - + @Test - public void testDecode() { + public void testDecode_ByteBuff() { byte[] zeros = new byte[4]; byte[] expected = {'u', 's', 'e', 'r', 0, 0, 120, 0, (byte) 140}; - + HeapByteBuff src = new HeapByteBuff(); src.append(zeros); src.append(expected); src.append(zeros); src.skipBytes(zeros.length); - + VarOption vo = coder.decode(src); - + assertEquals("user", vo.getName()); assertEquals(120, vo.getStartOffset()); assertEquals(140, vo.getEndOffset());