IGNITE-1917: Reworked reader context (handles).
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/1a32d399 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/1a32d399 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/1a32d399 Branch: refs/heads/ignite-1917 Commit: 1a32d3991f33b2ea8bfd2c567253989b16ecf85b Parents: 6e773a0 Author: vozerov-gridgain <voze...@gridgain.com> Authored: Tue Nov 17 13:30:20 2015 +0300 Committer: vozerov-gridgain <voze...@gridgain.com> Committed: Tue Nov 17 13:30:20 2015 +0300 ---------------------------------------------------------------------- .../internal/portable/BinaryObjectEx.java | 8 +- .../internal/portable/BinaryObjectImpl.java | 2 +- .../portable/BinaryObjectOffheapImpl.java | 2 +- .../internal/portable/BinaryReaderExImpl.java | 20 ++-- .../internal/portable/BinaryReaderHandles.java | 109 +++++++++++++++++++ .../portable/PortableReaderContext.java | 105 ------------------ 6 files changed, 125 insertions(+), 121 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectEx.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectEx.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectEx.java index 6902675..597fad5 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectEx.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectEx.java @@ -79,7 +79,7 @@ public abstract class BinaryObjectEx implements BinaryObject { * @param fieldName Field name. * @return Field value. */ - @Nullable protected abstract <F> F field(PortableReaderContext ctx, String fieldName); + @Nullable protected abstract <F> F field(BinaryReaderHandles ctx, String fieldName); /** * Get schema ID. @@ -157,7 +157,7 @@ public abstract class BinaryObjectEx implements BinaryObject { * @param handles Handles for already traversed objects. * @return String representation. */ - private String toString(PortableReaderContext ctx, IdentityHashMap<BinaryObject, Integer> handles) { + private String toString(BinaryReaderHandles ctx, IdentityHashMap<BinaryObject, Integer> handles) { int idHash = System.identityHashCode(this); BinaryType meta; @@ -232,9 +232,9 @@ public abstract class BinaryObjectEx implements BinaryObject { /** {@inheritDoc} */ @Override public String toString() { try { - PortableReaderContext ctx = new PortableReaderContext(); + BinaryReaderHandles ctx = new BinaryReaderHandles(); - ctx.setPortableHandler(start(), this); + ctx.put(start(), this); return toString(ctx, new IdentityHashMap<BinaryObject, Integer>()); } http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java index d04dc7d..86dc247 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java @@ -412,7 +412,7 @@ public final class BinaryObjectImpl extends BinaryObjectEx implements Externaliz /** {@inheritDoc} */ @SuppressWarnings("unchecked") - @Nullable @Override protected <F> F field(PortableReaderContext rCtx, String fieldName) { + @Nullable @Override protected <F> F field(BinaryReaderHandles rCtx, String fieldName) { BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new PortableHeapInputStream(arr), start, http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java index 7d8c044..f97ab94 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java @@ -342,7 +342,7 @@ public class BinaryObjectOffheapImpl extends BinaryObjectEx implements Externali /** {@inheritDoc} */ @SuppressWarnings("unchecked") - @Nullable @Override protected <F> F field(PortableReaderContext rCtx, String fieldName) { + @Nullable @Override protected <F> F field(BinaryReaderHandles rCtx, String fieldName) { BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new PortableOffheapInputStream(ptr, size, false), start, http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java index 8b256bc..7cdfced 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java @@ -127,7 +127,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje private final int start; /** */ - private final PortableReaderContext rCtx; + private final BinaryReaderHandles rCtx; /** */ private final ClassLoader ldr; @@ -178,7 +178,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje * @param ldr Class loader. */ public BinaryReaderExImpl(PortableContext ctx, byte[] arr, int start, ClassLoader ldr) { - this(ctx, new PortableHeapInputStream(arr), start, ldr, new PortableReaderContext()); + this(ctx, new PortableHeapInputStream(arr), start, ldr, new BinaryReaderHandles()); } /** @@ -187,7 +187,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje * @param start Start. */ BinaryReaderExImpl(PortableContext ctx, PortableInputStream in, int start, ClassLoader ldr) { - this(ctx, in, start, ldr, new PortableReaderContext()); + this(ctx, in, start, ldr, new BinaryReaderHandles()); } /** @@ -197,7 +197,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje * @param rCtx Context. */ BinaryReaderExImpl(PortableContext ctx, PortableInputStream in, int start, ClassLoader ldr, - PortableReaderContext rCtx) { + BinaryReaderHandles rCtx) { this.ctx = ctx; this.in = in; this.start = start; @@ -1016,7 +1016,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje * @param obj Object. */ void setHandler(Object obj) { - rCtx.setObjectHandler(start, obj); + rCtx.put(start, obj); } /** @@ -1024,7 +1024,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje * @param pos Position. */ void setHandler(Object obj, int pos) { - rCtx.setObjectHandler(pos, obj); + rCtx.put(pos, obj); } /** @@ -1036,7 +1036,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje private <T> T readHandleField() { int handle = (in.position() - 1) - in.readInt(); - Object obj = rCtx.getObjectByHandle(handle); + Object obj = rCtx.get(handle); if (obj == null) { in.position(handle); @@ -1556,7 +1556,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje case HANDLE: int handle = start - in.readInt(); - BinaryObject handledPo = rCtx.getPortableByHandle(handle); + BinaryObject handledPo = rCtx.get(handle); if (handledPo != null) return handledPo; @@ -1585,7 +1585,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje in.remaining() + in.position()) : new BinaryObjectImpl(ctx, in.array(), start); - rCtx.setPortableHandler(start, po); + rCtx.put(start, po); in.position(start + po.length()); @@ -1807,7 +1807,7 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje case HANDLE: int handle = start - in.readInt(); - obj = rCtx.getObjectByHandle(handle); + obj = rCtx.get(handle); if (obj == null) { int retPos = in.position(); http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderHandles.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderHandles.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderHandles.java new file mode 100644 index 0000000..cb4ddaa --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderHandles.java @@ -0,0 +1,109 @@ +/* + * 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.ignite.internal.portable; + +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; + +/** + * Reader handles. + */ +// TODO: IGNITE-1917: This is "add-only" map => can be improved! +public class BinaryReaderHandles { + /** Mode: empty. */ + private static final int MODE_EMPTY = 0; + + /** Mode: single object. */ + private static final int MODE_SINGLE = 1; + + /** Mode: multiple objects. */ + private static final int MODE_MULTIPLE = 2; + + /** Position. */ + private int singlePos; + + /** Data. This is either an object or a map. */ + private Object data; + + /** Mode. */ + private int mode = MODE_EMPTY; + + /** + * Get object by position. + * + * @param pos Position. + * @return Object. + */ + @SuppressWarnings("unchecked") + public @Nullable <T> T get(int pos) { + switch (mode) { + case MODE_EMPTY: + return null; + + case MODE_SINGLE: + return (T)data; + + default: + assert mode == MODE_MULTIPLE; + + return (T)((Map<Integer, Object>)data).get(pos); + } + } + + /** + * Put object to registry and return previous position (if any). + * + * @param pos Position. + * @param obj Object. + */ + @SuppressWarnings("unchecked") + public void put(int pos, Object obj) { + assert pos >= 0; + assert obj != null; + + switch (mode) { + case MODE_EMPTY: + this.singlePos = pos; + this.data = obj; + this.mode = MODE_SINGLE; + + break; + + case MODE_SINGLE: + Map<Integer, Object> newData = new HashMap(3, 1.0f); + + newData.put(singlePos, data); + newData.put(pos, obj); + + this.singlePos = -1; + this.data = newData; + this.mode = MODE_MULTIPLE; + + break; + + default: + assert mode == MODE_MULTIPLE; + + Map<Integer, Object> data0 = (Map<Integer, Object>)data; + + data0.put(pos, obj); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/1a32d399/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderContext.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderContext.java deleted file mode 100644 index 869f81d..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderContext.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.ignite.internal.portable; - -import java.util.HashMap; -import java.util.Map; -import org.apache.ignite.internal.util.typedef.internal.S; -import org.apache.ignite.binary.BinaryObject; -import org.apache.ignite.lang.IgniteBiTuple; -import org.jetbrains.annotations.Nullable; - -/** -* Reader context. -*/ -class PortableReaderContext { - /** */ - private Object oHandles; - - /** */ - private Map<Integer, BinaryObject> poHandles; - - /** - * @param handle Handle. - * @param obj Object. - */ - @SuppressWarnings("unchecked") - void setObjectHandler(int handle, Object obj) { - assert obj != null; - - if (oHandles == null) - oHandles = new IgniteBiTuple(handle, obj); - else if (oHandles instanceof IgniteBiTuple) { - Map map = new HashMap(3, 1.0f); - - IgniteBiTuple t = (IgniteBiTuple)oHandles; - - map.put(t.getKey(), t.getValue()); - map.put(handle, obj); - - oHandles = map; - } - else - ((Map)oHandles).put(handle, obj); - } - - /** - * @param handle Handle. - * @param po Portable object. - */ - void setPortableHandler(int handle, BinaryObject po) { - assert po != null; - - if (poHandles == null) - poHandles = new HashMap<>(3, 1.0f); - - poHandles.put(handle, po); - } - - /** - * @param handle Handle. - * @return Object. - */ - @Nullable Object getObjectByHandle(int handle) { - if (oHandles != null) { - if (oHandles instanceof IgniteBiTuple) { - IgniteBiTuple t = (IgniteBiTuple)oHandles; - - if ((int)t.get1() == handle) - return t.get2(); - } - else - return ((Map)oHandles).get(handle); - } - - return null; - } - - /** - * @param handle Handle. - * @return Object. - */ - @Nullable BinaryObject getPortableByHandle(int handle) { - return poHandles != null ? poHandles.get(handle) : null; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(PortableReaderContext.class, this); - } -}