QPID-7531: [Java Broker, AMQP 1.0] Refactor LazyConstructors in various DescribedTypeConstructors
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/56cf7b8a Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/56cf7b8a Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/56cf7b8a Branch: refs/heads/master Commit: 56cf7b8a90cd6f6c6a13b27c52c721abf5578419 Parents: d8613a6 Author: Lorenz Quack <lqu...@apache.org> Authored: Fri Sep 22 16:25:29 2017 +0100 Committer: Lorenz Quack <lqu...@apache.org> Committed: Thu Sep 28 14:30:17 2017 +0100 ---------------------------------------------------------------------- .../codec/AbstractLazyConstructor.java | 76 +++++++++++ .../codec/AmqpValueSectionConstructor.java | 125 +++++++------------ .../messaging/codec/DataSectionConstructor.java | 48 ++----- .../codec/DescribedListSectionConstructor.java | 49 ++------ .../codec/DescribedMapSectionConstructor.java | 52 ++------ 5 files changed, 155 insertions(+), 195 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/56cf7b8a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AbstractLazyConstructor.java ---------------------------------------------------------------------- diff --git a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AbstractLazyConstructor.java b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AbstractLazyConstructor.java new file mode 100644 index 0000000..3b80153 --- /dev/null +++ b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AbstractLazyConstructor.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.server.protocol.v1_0.type.messaging.codec; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.qpid.server.bytebuffer.QpidByteBuffer; +import org.apache.qpid.server.protocol.v1_0.codec.TypeConstructor; +import org.apache.qpid.server.protocol.v1_0.codec.ValueHandler; +import org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException; + +public abstract class AbstractLazyConstructor<T> implements TypeConstructor<T> +{ + private final int[] _originalPositions; + + AbstractLazyConstructor(final int[] originalPositions) + { + _originalPositions = originalPositions; + } + + @Override + public final T construct(final List<QpidByteBuffer> in, final ValueHandler handler) throws AmqpErrorException + { + skipValue(in); + + List<QpidByteBuffer> encoding = new ArrayList<>(); + int offset = in.size() - _originalPositions.length; + for (int i = offset; i < in.size(); i++) + { + QpidByteBuffer buf = in.get(i); + if (buf.position() == _originalPositions[i - offset]) + { + if (buf.hasRemaining()) + { + break; + } + } + else + { + QpidByteBuffer dup = buf.duplicate(); + dup.position(_originalPositions[i - offset]); + dup.limit(buf.position()); + encoding.add(dup); + } + } + T object = createObject(encoding, handler); + for (QpidByteBuffer buffer : encoding) + { + buffer.dispose(); + } + return object; + } + + protected abstract T createObject(final List<QpidByteBuffer> encoding, final ValueHandler handler); + + protected abstract void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException; +} http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/56cf7b8a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AmqpValueSectionConstructor.java ---------------------------------------------------------------------- diff --git a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AmqpValueSectionConstructor.java b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AmqpValueSectionConstructor.java index daa76a9..274a2aa 100644 --- a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AmqpValueSectionConstructor.java +++ b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/AmqpValueSectionConstructor.java @@ -23,7 +23,6 @@ package org.apache.qpid.server.protocol.v1_0.type.messaging.codec; -import java.util.ArrayList; import java.util.List; import org.apache.qpid.server.bytebuffer.QpidByteBuffer; @@ -68,98 +67,68 @@ public class AmqpValueSectionConstructor implements DescribedTypeConstructor<Amq } - private class LazyConstructor implements TypeConstructor<AmqpValueSection> + private class LazyConstructor extends AbstractLazyConstructor<AmqpValueSection> { - - private final int[] _originalPositions; - - public LazyConstructor(final int[] originalPositions) + LazyConstructor(final int[] originalPositions) { + super(originalPositions); + } - _originalPositions = originalPositions; + @Override + protected AmqpValueSection createObject(final List<QpidByteBuffer> encoding, final ValueHandler handler) + { + return new AmqpValueSection(encoding); } @Override - public AmqpValueSection construct(final List<QpidByteBuffer> in, final ValueHandler handler) - throws AmqpErrorException + protected void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException { - skipValue(in); + byte formatCode = QpidByteBufferUtils.get(in); - List<QpidByteBuffer> encoding = new ArrayList<>(); - int offset = in.size() - _originalPositions.length; - for (int i = offset; i < in.size(); i++) + if (formatCode == 0) + { + // This is only valid if the described value is not an array + skipValue(in); + skipValue(in); + } + else { - QpidByteBuffer buf = in.get(i); - if (buf.position() == _originalPositions[i - offset]) + int category = (formatCode >> 4) & 0x0F; + switch (category) { - if (buf.hasRemaining()) - { + case 0x04: break; - } - } - else - { - QpidByteBuffer dup = buf.duplicate(); - dup.position(_originalPositions[i - offset]); - dup.limit(buf.position()); - encoding.add(dup); + case 0x05: + QpidByteBufferUtils.skip(in, 1); + break; + case 0x06: + QpidByteBufferUtils.skip(in, 2); + break; + case 0x07: + QpidByteBufferUtils.skip(in, 4); + break; + case 0x08: + QpidByteBufferUtils.skip(in, 8); + break; + case 0x09: + QpidByteBufferUtils.skip(in, 16); + break; + case 0x0a: + case 0x0c: + case 0x0e: + QpidByteBufferUtils.skip(in, ((int) QpidByteBufferUtils.get(in)) & 0xFF); + break; + case 0x0b: + case 0x0d: + case 0x0f: + QpidByteBufferUtils.skip(in, QpidByteBufferUtils.getInt(in)); + break; + default: + throw new AmqpErrorException(ConnectionError.FRAMING_ERROR, "Unknown type"); } } - AmqpValueSection object = new AmqpValueSection(encoding); - for (QpidByteBuffer buffer: encoding) - { - buffer.dispose(); - } - return object; } - } - - private void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException - { - byte formatCode = QpidByteBufferUtils.get(in); - if (formatCode == 0) - { - // This is only valid if the described value is not an array - skipValue(in); - skipValue(in); - } - else - { - int category = (formatCode >> 4) & 0x0F; - switch (category) - { - case 0x04: - break; - case 0x05: - QpidByteBufferUtils.skip(in, 1); - break; - case 0x06: - QpidByteBufferUtils.skip(in, 2); - break; - case 0x07: - QpidByteBufferUtils.skip(in, 4); - break; - case 0x08: - QpidByteBufferUtils.skip(in, 8); - break; - case 0x09: - QpidByteBufferUtils.skip(in, 16); - break; - case 0x0a: - case 0x0c: - case 0x0e: - QpidByteBufferUtils.skip(in, ((int) QpidByteBufferUtils.get(in)) & 0xFF); - break; - case 0x0b: - case 0x0d: - case 0x0f: - QpidByteBufferUtils.skip(in, QpidByteBufferUtils.getInt(in)); - break; - default: - throw new AmqpErrorException(ConnectionError.FRAMING_ERROR, "Unknown type"); - } - } } } http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/56cf7b8a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DataSectionConstructor.java ---------------------------------------------------------------------- diff --git a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DataSectionConstructor.java b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DataSectionConstructor.java index a5d2469..d9b331a 100644 --- a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DataSectionConstructor.java +++ b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DataSectionConstructor.java @@ -23,7 +23,6 @@ package org.apache.qpid.server.protocol.v1_0.type.messaging.codec; -import java.util.ArrayList; import java.util.List; import org.apache.qpid.server.bytebuffer.QpidByteBuffer; @@ -84,23 +83,24 @@ public class DataSectionConstructor implements DescribedTypeConstructor<DataSect } - private class LazyConstructor implements TypeConstructor<DataSection> + private class LazyConstructor extends AbstractLazyConstructor<DataSection> { - private final int _sizeBytes; - private final int[] _originalPositions; - public LazyConstructor(final int sizeBytes, - final int[] originalPositions) + public LazyConstructor(final int sizeBytes, final int[] originalPositions) { - + super(originalPositions); _sizeBytes = sizeBytes; - _originalPositions = originalPositions; } @Override - public DataSection construct(final List<QpidByteBuffer> in, final ValueHandler handler) - throws AmqpErrorException + protected DataSection createObject(final List<QpidByteBuffer> encoding, final ValueHandler handler) + { + return new DataSection(encoding); + } + + @Override + protected void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException { int size; switch(_sizeBytes) @@ -115,34 +115,6 @@ public class DataSectionConstructor implements DescribedTypeConstructor<DataSect throw new AmqpErrorException(AmqpError.INVALID_FIELD, "Unexpected constructor type, can only be 1 or 4"); } QpidByteBufferUtils.skip(in, size); - - List<QpidByteBuffer> encoding = new ArrayList<>(); - int offset = in.size() - _originalPositions.length; - for(int i = offset; i < in.size(); i++) - { - QpidByteBuffer buf = in.get(i); - if(buf.position() == _originalPositions[i-offset]) - { - if(buf.hasRemaining()) - { - break; - } - } - else - { - QpidByteBuffer dup = buf.duplicate(); - dup.position(_originalPositions[i-offset]); - dup.limit(buf.position()); - encoding.add(dup); - } - } - DataSection object = new DataSection(encoding); - for (QpidByteBuffer buffer: encoding) - { - buffer.dispose(); - } - return object; - } } } http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/56cf7b8a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedListSectionConstructor.java ---------------------------------------------------------------------- diff --git a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedListSectionConstructor.java b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedListSectionConstructor.java index c01b560..d038f33 100644 --- a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedListSectionConstructor.java +++ b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedListSectionConstructor.java @@ -23,7 +23,6 @@ package org.apache.qpid.server.protocol.v1_0.type.messaging.codec; -import java.util.ArrayList; import java.util.List; import org.apache.qpid.server.bytebuffer.QpidByteBuffer; @@ -68,24 +67,24 @@ public abstract class DescribedListSectionConstructor<S extends AbstractSection> } - private class LazyConstructor implements TypeConstructor<S> + private class LazyConstructor extends AbstractLazyConstructor<S> { - private final int _sizeBytes; - private final int[] _originalPositions; - private DescribedListSectionConstructor _describedTypeConstructor; - public LazyConstructor(final int sizeBytes, - final int[] originalPositions) + LazyConstructor(final int sizeBytes, final int[] originalPositions) { - + super(originalPositions); _sizeBytes = sizeBytes; - _originalPositions = originalPositions; } @Override - public S construct(final List<QpidByteBuffer> in, final ValueHandler handler) - throws AmqpErrorException + protected S createObject(final List<QpidByteBuffer> encoding, final ValueHandler handler) + { + return DescribedListSectionConstructor.this.createObject(encoding); + } + + @Override + protected void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException { int size; switch(_sizeBytes) @@ -103,34 +102,6 @@ public abstract class DescribedListSectionConstructor<S extends AbstractSection> throw new AmqpErrorException(AmqpError.INVALID_FIELD, "Unexpected constructor type, can only be 0,1 or 4"); } QpidByteBufferUtils.skip(in, size); - - List<QpidByteBuffer> encoding = new ArrayList<>(); - int offset = in.size() - _originalPositions.length; - for(int i = offset; i < in.size(); i++) - { - QpidByteBuffer buf = in.get(i); - if(buf.position() == _originalPositions[i-offset]) - { - if(buf.hasRemaining()) - { - break; - } - } - else - { - QpidByteBuffer dup = buf.duplicate(); - dup.position(_originalPositions[i-offset]); - dup.limit(buf.position()); - encoding.add(dup); - } - } - S object = createObject(encoding); - for (QpidByteBuffer buffer: encoding) - { - buffer.dispose(); - } - return object; - } } http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/56cf7b8a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedMapSectionConstructor.java ---------------------------------------------------------------------- diff --git a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedMapSectionConstructor.java b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedMapSectionConstructor.java index 718c6da..fb1f545 100644 --- a/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedMapSectionConstructor.java +++ b/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/type/messaging/codec/DescribedMapSectionConstructor.java @@ -27,9 +27,9 @@ import java.util.ArrayList; import java.util.List; import org.apache.qpid.server.bytebuffer.QpidByteBuffer; +import org.apache.qpid.server.bytebuffer.QpidByteBufferUtils; import org.apache.qpid.server.protocol.v1_0.codec.DescribedTypeConstructor; import org.apache.qpid.server.protocol.v1_0.codec.DescribedTypeConstructorRegistry; -import org.apache.qpid.server.bytebuffer.QpidByteBufferUtils; import org.apache.qpid.server.protocol.v1_0.codec.SectionDecoderRegistry; import org.apache.qpid.server.protocol.v1_0.codec.TypeConstructor; import org.apache.qpid.server.protocol.v1_0.codec.ValueHandler; @@ -67,24 +67,25 @@ public abstract class DescribedMapSectionConstructor<S extends AbstractSection> } - private class LazyConstructor implements TypeConstructor<S> + private class LazyConstructor extends AbstractLazyConstructor<S> { - private final int _sizeBytes; - private final int[] _originalPositions; - private DescribedMapSectionConstructor _describedTypeConstructor; - public LazyConstructor(final int sizeBytes, - final int[] originalPositions) + LazyConstructor(final int sizeBytes, final int[] originalPositions) { - + super(originalPositions); _sizeBytes = sizeBytes; - _originalPositions = originalPositions; } @Override - public S construct(final List<QpidByteBuffer> in, final ValueHandler handler) - throws AmqpErrorException + protected S createObject(final List<QpidByteBuffer> encoding, final ValueHandler handler) + { + return DescribedMapSectionConstructor.this.createObject(((SectionDecoderRegistry) handler.getDescribedTypeRegistry()) + .getUnderlyingRegistry(), encoding); + } + + @Override + protected void skipValue(final List<QpidByteBuffer> in) throws AmqpErrorException { int size; switch(_sizeBytes) @@ -99,35 +100,6 @@ public abstract class DescribedMapSectionConstructor<S extends AbstractSection> throw new AmqpErrorException(AmqpError.INVALID_FIELD, "Unexpected constructor type, can only be 1 or 4"); } QpidByteBufferUtils.skip(in, size); - - List<QpidByteBuffer> encoding = new ArrayList<>(); - int offset = in.size() - _originalPositions.length; - for(int i = offset; i < in.size(); i++) - { - QpidByteBuffer buf = in.get(i); - if(buf.position() == _originalPositions[i-offset]) - { - if(buf.hasRemaining()) - { - break; - } - } - else - { - QpidByteBuffer dup = buf.duplicate(); - dup.position(_originalPositions[i-offset]); - dup.limit(buf.position()); - encoding.add(dup); - } - } - S object = createObject(((SectionDecoderRegistry)handler.getDescribedTypeRegistry()).getUnderlyingRegistry(), - encoding); - for (QpidByteBuffer buffer: encoding) - { - buffer.dispose(); - } - return object; - } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org