You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/06/23 15:53:15 UTC
[32/33] incubator-ignite git commit: ignite-950: implementing marshal
aware
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStreamExt.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStreamExt.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStreamExt.java
new file mode 100644
index 0000000..fc0daa6
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStreamExt.java
@@ -0,0 +1,152 @@
+/*
+ * 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.marshaller.optimized;
+
+import org.apache.ignite.internal.util.*;
+import org.apache.ignite.internal.util.io.*;
+
+import java.io.*;
+import java.util.*;
+
+import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerExt.*;
+import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerUtils.*;
+
+/**
+ * TODO: IGNITE-950
+ */
+public class OptimizedObjectOutputStreamExt extends OptimizedObjectOutputStream {
+ /**
+ * Constructor.
+ *
+ * @param out Output stream.
+ * @throws IOException In case of error.
+ */
+ protected OptimizedObjectOutputStreamExt(GridDataOutput out) throws IOException {
+ super(out);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void writeFieldType(byte type) throws IOException {
+ out.writeByte(type);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected Footer createFooter(Class<?> cls) {
+ if (!ctx.isSystemType(cls.getName()) && (OptimizedMarshalAware.class.isAssignableFrom(cls) ||
+ (metaHandler != null && metaHandler.metadata(resolveTypeId(cls.getName(), mapper)) != null)))
+ return new FooterImpl();
+ else
+ return null;
+ }
+
+ /**
+ *
+ */
+ private class FooterImpl implements OptimizedObjectOutputStream.Footer {
+ /** */
+ private ArrayList<Integer> data;
+
+ /** */
+ private ArrayList<Integer> fields;
+
+ /** */
+ private HashMap<Integer, GridHandleTable.ObjectInfo> handles;
+
+ /** */
+ private boolean hasHandles;
+
+ /** {@inheritDoc} */
+ @Override public void fields(OptimizedClassDescriptor.Fields fields) {
+ if (fields.fieldsIndexingSupported()) {
+ data = new ArrayList<>();
+ this.fields = new ArrayList<>();
+ }
+ else
+ data = null;
+ }
+
+ /** {@inheritDoc} */
+ public void put(int fieldId, OptimizedFieldType fieldType, int len) {
+ if (data == null)
+ return;
+
+ if (fieldType == OptimizedFieldType.OTHER) {
+ data.add(len);
+ fields.add(fieldId);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override public void putHandle(int fieldId, GridHandleTable.ObjectInfo objInfo) {
+ if (data == null)
+ return;
+
+ if (!hasHandles) {
+ hasHandles = true;
+ handles = new HashMap<>();
+ }
+
+ handles.put(fieldId, objInfo);
+
+ // length of handle fields is 5 bytes.
+ put(fieldId, OptimizedFieldType.OTHER, 5);
+ }
+
+ /** {@inheritDoc} */
+ public void write() throws IOException {
+ if (data == null)
+ writeInt(EMPTY_FOOTER);
+ else {
+ int bodyEnd = out.offset();
+
+ // +4 - 2 bytes for footer len at the beginning, 2 bytes for footer len at the end.
+ short footerLen = (short)(data.size() * 4 + 4);
+
+ if (hasHandles)
+ footerLen += handles.size() * 8;
+
+ writeShort(footerLen);
+
+ if (hasHandles) {
+ for (int i = 0; i < data.size(); i++) {
+ GridHandleTable.ObjectInfo objInfo = handles.get(fields.get(i));
+
+ if (objInfo == null)
+ writeInt(data.get(i) & ~FOOTER_BODY_IS_HANDLE_MASK);
+ else {
+ writeInt(data.get(i) | FOOTER_BODY_IS_HANDLE_MASK);
+ writeInt(objInfo.position());
+
+ if (objInfo.length() == 0)
+ // field refers to its own object that hasn't set total length yet.
+ writeInt((bodyEnd - objInfo.position()) + footerLen);
+ else
+ writeInt(objInfo.length());
+ }
+ }
+ }
+ else
+ for (int fieldLen : data)
+ // writing field len and resetting is handle mask
+ writeInt(fieldLen & ~FOOTER_BODY_IS_HANDLE_MASK);
+
+ writeShort(footerLen);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamExtRegistry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamExtRegistry.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamExtRegistry.java
new file mode 100644
index 0000000..f26bacb
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamExtRegistry.java
@@ -0,0 +1,225 @@
+/*
+ * 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.marshaller.optimized;
+
+import org.apache.ignite.*;
+import org.apache.ignite.internal.*;
+import org.apache.ignite.internal.util.io.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+
+import java.io.*;
+import java.util.concurrent.*;
+
+/**
+ * Storage for object streams.
+ */
+class OptimizedObjectStreamExtRegistry {
+ /** Holders. */
+ private static final ThreadLocal<StreamHolder> holders = new ThreadLocal<>();
+
+ /** Holders pool. */
+ private static BlockingQueue<StreamHolder> pool;
+
+ /**
+ * Ensures singleton.
+ */
+ private OptimizedObjectStreamExtRegistry() {
+ // No-op.
+ }
+
+ /**
+ * Sets streams pool size.
+ *
+ * @param size Streams pool size.
+ */
+ static void poolSize(int size) {
+ if (size > 0) {
+ pool = new LinkedBlockingQueue<>(size);
+
+ for (int i = 0; i < size; i++) {
+ boolean b = pool.offer(new StreamHolder());
+
+ assert b;
+ }
+ }
+ else
+ pool = null;
+ }
+
+ /**
+ * Gets output stream.
+ *
+ * @return Object output stream.
+ * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
+ */
+ static OptimizedObjectOutputStreamExt out() throws IgniteInterruptedCheckedException {
+ return holder().acquireOut();
+ }
+
+ /**
+ * Gets input stream.
+ *
+ * @return Object input stream.
+ * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
+ */
+ static OptimizedObjectInputStreamExt in() throws IgniteInterruptedCheckedException {
+ return holder().acquireIn();
+ }
+
+ /**
+ * Closes and releases output stream.
+ *
+ * @param out Object output stream.
+ */
+ static void closeOut(OptimizedObjectOutputStream out) {
+ U.close(out, null);
+
+ StreamHolder holder = holders.get();
+
+ holder.releaseOut();
+
+ if (pool != null) {
+ holders.remove();
+
+ boolean b = pool.offer(holder);
+
+ assert b;
+ }
+ }
+
+ /**
+ * Closes and releases input stream.
+ *
+ * @param in Object input stream.
+ */
+ @SuppressWarnings("TypeMayBeWeakened")
+ static void closeIn(OptimizedObjectInputStream in) {
+ U.close(in, null);
+
+ StreamHolder holder = holders.get();
+
+ holder.releaseIn();
+
+ if (pool != null) {
+ holders.remove();
+
+ boolean b = pool.offer(holder);
+
+ assert b;
+ }
+ }
+
+ /**
+ * Gets holder from pool or thread local.
+ *
+ * @return Stream holder.
+ * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
+ */
+ private static StreamHolder holder() throws IgniteInterruptedCheckedException {
+ StreamHolder holder = holders.get();
+
+ if (holder == null) {
+ try {
+ holders.set(holder = pool != null ? pool.take() : new StreamHolder());
+ }
+ catch (InterruptedException e) {
+ throw new IgniteInterruptedCheckedException(
+ "Failed to take object stream from pool (thread interrupted).", e);
+ }
+ }
+
+ return holder;
+ }
+
+ /**
+ * Streams holder.
+ */
+ private static class StreamHolder {
+ /** Output stream. */
+ private final OptimizedObjectOutputStreamExt out = createOut();
+
+ /** Input stream. */
+ private final OptimizedObjectInputStreamExt in = createIn();
+
+ /** Output streams counter. */
+ private int outAcquireCnt;
+
+ /** Input streams counter. */
+ private int inAcquireCnt;
+
+ /**
+ * Gets output stream.
+ *
+ * @return Object output stream.
+ */
+ OptimizedObjectOutputStreamExt acquireOut() {
+ return outAcquireCnt++ > 0 ? createOut() : out;
+ }
+
+ /**
+ * Gets input stream.
+ *
+ * @return Object input stream.
+ */
+ OptimizedObjectInputStreamExt acquireIn() {
+ return inAcquireCnt++ > 0 ? createIn() : in;
+ }
+
+ /**
+ * Releases output stream.
+ */
+ void releaseOut() {
+ outAcquireCnt--;
+ }
+
+ /**
+ * Releases input stream.
+ */
+ void releaseIn() {
+ inAcquireCnt--;
+ }
+
+ /**
+ * Creates output stream.
+ *
+ * @return Object output stream.
+ */
+ private OptimizedObjectOutputStreamExt createOut() {
+ try {
+ return new OptimizedObjectOutputStreamExt(new GridUnsafeDataOutput(4 * 1024));
+ }
+ catch (IOException e) {
+ throw new IgniteException("Failed to create object output stream.", e);
+ }
+ }
+
+ /**
+ * Creates input stream.
+ *
+ * @return Object input stream.
+ */
+ private OptimizedObjectInputStreamExt createIn() {
+ try {
+ return new OptimizedObjectInputStreamExt(new GridUnsafeDataInput());
+ }
+ catch (IOException e) {
+ throw new IgniteException("Failed to create object input stream.", e);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExt.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExt.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExt.java
deleted file mode 100644
index 6bfe9c6..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExt.java
+++ /dev/null
@@ -1,353 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.*;
-import org.apache.ignite.internal.processors.cache.*;
-import org.apache.ignite.marshaller.optimized.*;
-import org.jetbrains.annotations.*;
-
-import java.io.*;
-
-import static org.apache.ignite.marshaller.optimized.OptimizedClassDescriptor.*;
-import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerUtils.*;
-
-/**
- * TODO
- */
-public class OptimizedMarshallerExt extends OptimizedMarshaller {
- /** */
- static final byte EMPTY_FOOTER = -1;
-
- /** */
- static final byte FOOTER_LEN_OFF = 2;
-
- /** */
- static final int FOOTER_BODY_LEN_MASK = 0x3FFFFFFF;
-
- /** */
- static final int FOOTER_BODY_IS_HANDLE_MASK = 0x40000000;
-
- /** */
- static final byte FOOTER_BODY_HANDLE_MASK_BIT = 30;
-
- /** */
- static final byte VARIABLE_LEN = -1;
-
- /** */
- private volatile OptimizedMarshallerExtMetaHandler metaHandler;
-
- /**
- * Creates new marshaller will all defaults.
- *
- * @throws IgniteException If this marshaller is not supported on the current JVM.
- */
- public OptimizedMarshallerExt() {
- // No-op
- }
-
- /**
- * Creates new marshaller providing whether it should
- * require {@link Serializable} interface or not.
- *
- * @param requireSer Whether to require {@link Serializable}.
- */
- public OptimizedMarshallerExt(boolean requireSer) {
- super(requireSer);
- }
-
- /**
- * Sets metadata handler.
- *
- * @param metaHandler Metadata handler.
- */
- public void setMetadataHandler(OptimizedMarshallerExtMetaHandler metaHandler) {
- this.metaHandler = metaHandler;
- }
-
- /**
- * Returns currently set ID mapper.
- *
- * @return ID mapper.
- */
- public OptimizedMarshallerIdMapper idMapper() {
- return mapper;
- }
-
- /**
- * Enables fields indexing for the object of the given {@code cls}.
- *
- * If enabled then a footer will be added during marshalling of an object of the given {@code cls} to the end of
- * its serialized form.
- *
- * @param cls Class.
- * @return {@code true} if fields indexing is enabled.
- * @throws IgniteCheckedException In case of error.
- */
- public boolean enableFieldsIndexing(Class<?> cls) throws IgniteCheckedException {
- assert metaHandler != null;
-
- if (ctx.isSystemType(cls.getName()))
- return false;
-
- try {
- OptimizedClassDescriptor desc = OptimizedMarshallerUtils.classDescriptor(clsMap, cls, ctx, mapper);
-
- if (desc.fields() != null && desc.fields().fieldsIndexingSupported()) {
- //The function is called on kernel startup, calling metaHandler.metadata() will hang the grid,
- //because the underlying cache is not ready.
- //if (metaHandler.metadata(desc.typeId()) != null)
- // return true;
-
- OptimizedObjectMetadata meta = new OptimizedObjectMetadata();
-
- for (ClassFields clsFields : desc.fields().fieldsList())
- for (FieldInfo info : clsFields.fieldInfoList())
- meta.addMeta(info.id(), info.type());
-
- metaHandler.addMeta(desc.typeId(), meta);
-
- return true;
- }
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to put meta for class: " + cls.getName(), e);
- }
-
- return false;
- }
-
- /**
- * Checks whether fields indexing is enabled for objects of the given {@code cls}.
- *
- * @param cls Class.
- * @return {@code true} if fields indexing is enabled.
- */
- public boolean fieldsIndexingEnabled(Class<?> cls) {
- assert metaHandler != null;
-
- if (ctx.isSystemType(cls.getName()))
- return false;
-
- try {
- OptimizedClassDescriptor desc = OptimizedMarshallerUtils.classDescriptor(clsMap, cls, ctx, mapper);
-
- return desc.fields() != null && desc.fields().fieldsIndexingSupported() &&
- metaHandler.metadata(desc.typeId()) != null;
- }
- catch (IOException e) {
- throw new IgniteException("Failed to load class description: " + cls);
- }
- }
-
- /** {@inheritDoc} */
- @Override public void setPoolSize(int poolSize) {
- OptimizedObjectStreamExtRegistry.poolSize(poolSize);
- }
-
- /** {@inheritDoc} */
- @Override public void marshal(@Nullable Object obj, OutputStream out) throws IgniteCheckedException {
- assert out != null;
-
- OptimizedObjectOutputStreamExt objOut = null;
-
- try {
- objOut = OptimizedObjectStreamExtRegistry.out();
-
- objOut.context(clsMap, ctx, mapper, requireSer, metaHandler);
-
- objOut.out().outputStream(out);
-
- objOut.writeObject(obj);
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to serialize object: " + obj, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeOut(objOut);
- }
- }
-
- /** {@inheritDoc} */
- @Override public byte[] marshal(@Nullable Object obj) throws IgniteCheckedException {
- OptimizedObjectOutputStreamExt objOut = null;
-
- try {
- objOut = OptimizedObjectStreamExtRegistry.out();
-
- objOut.context(clsMap, ctx, mapper, requireSer, metaHandler);
-
- objOut.writeObject(obj);
-
- return objOut.out().array();
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to serialize object: " + obj, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeOut(objOut);
- }
- }
-
- /** {@inheritDoc} */
- @SuppressWarnings("unchecked")
- @Override public <T> T unmarshal(InputStream in, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
- assert in != null;
-
- OptimizedObjectInputStreamExt objIn = null;
-
- try {
- objIn = OptimizedObjectStreamExtRegistry.in();
-
- objIn.context(clsMap, ctx, mapper, clsLdr != null ? clsLdr : dfltClsLdr, metaHandler);
-
- objIn.in().inputStream(in);
-
- return (T)objIn.readObject();
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to deserialize object with given class loader: " + clsLdr, e);
- }
- catch (ClassNotFoundException e) {
- throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
- "(make sure same versions of all classes are available on all nodes or " +
- "enable peer-class-loading): " + clsLdr, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeIn(objIn);
- }
- }
-
- /** {@inheritDoc} */
- @SuppressWarnings("unchecked")
- @Override public <T> T unmarshal(byte[] arr, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
- return unmarshal(arr, 0, arr.length, clsLdr);
- }
-
- /**
- * Unmarshals object from byte array using given class loader and offset with len.
- *
- * @param <T> Type of unmarshalled object.
- * @param arr Byte array.
- * @param off Object's offset in the array.
- * @param len Object's length in the array.
- * @param clsLdr Class loader to use.
- * @return Unmarshalled object.
- * @throws IgniteCheckedException If unmarshalling failed.
- */
- public <T> T unmarshal(byte[] arr, int off, int len, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
- assert arr != null;
-
- OptimizedObjectInputStreamExt objIn = null;
-
- try {
- objIn = OptimizedObjectStreamExtRegistry.in();
-
- objIn.context(clsMap, ctx, mapper, clsLdr != null ? clsLdr : dfltClsLdr, metaHandler);
-
- objIn.in().bytes(arr, off, len);
-
- return (T)objIn.readObject();
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to deserialize object with given class loader: " + clsLdr, e);
- }
- catch (ClassNotFoundException e) {
- throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
- "(make sure same version of all classes are available on all nodes or" +
- " enable peer-class-loading): " + clsLdr, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeIn(objIn);
- }
- }
-
- /**
- * Checks whether object, serialized to byte array {@code arr}, has a field with name {@code fieldName}.
- *
- * @param fieldName Field name.
- * @param arr Object's serialized form.
- * @param off Object's start off.
- * @param len Object's len.
- * @return {@code true} if field exists.
- */
- public boolean hasField(String fieldName, byte[] arr, int off, int len) throws IgniteCheckedException {
- assert arr != null && fieldName != null;
-
- OptimizedObjectInputStreamExt objIn = null;
-
- try {
- objIn = OptimizedObjectStreamExtRegistry.in();
-
- objIn.context(clsMap, ctx, mapper, dfltClsLdr, metaHandler);
-
- objIn.in().bytes(arr, off, len);
-
- return objIn.hasField(fieldName);
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to find field with name: " + fieldName, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeIn(objIn);
- }
- }
-
- /**
- * Looks up field with the given name and returns it in one of the following representations. If the field is
- * serializable and has a footer then it's not deserialized but rather returned wrapped by {@link CacheObjectImpl}
- * for future processing. In all other cases the field is fully deserialized.
- *
- * @param fieldName Field name.
- * @param arr Object's serialized form.
- * @param off Object's start offset.
- * @param len Object's len.
- * @param clsLdr Class loader.
- * @param <T> Expected field class.
- * @return Field.
- * @throws IgniteCheckedException In case of error.
- */
- public <T> T readField(String fieldName, byte[] arr, int off, int len, @Nullable ClassLoader clsLdr)
- throws IgniteCheckedException {
-
- assert arr != null && fieldName != null;
-
- OptimizedObjectInputStreamExt objIn = null;
-
- try {
- objIn = OptimizedObjectStreamExtRegistry.in();
-
- objIn.context(clsMap, ctx, mapper, clsLdr != null ? clsLdr : dfltClsLdr, metaHandler);
-
- objIn.in().bytes(arr, off, len);
-
- return objIn.readField(fieldName);
- }
- catch (IOException e) {
- throw new IgniteCheckedException("Failed to find field with name: " + fieldName, e);
- }
- catch (ClassNotFoundException e) {
- throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
- "(make sure same version of all classes are available on all nodes or" +
- " enable peer-class-loading): " + clsLdr, e);
- }
- finally {
- OptimizedObjectStreamExtRegistry.closeIn(objIn);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtMetaHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtMetaHandler.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtMetaHandler.java
deleted file mode 100644
index ea3b70f..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtMetaHandler.java
+++ /dev/null
@@ -1,40 +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.marshaller.optimized.ext;
-
-/**
- * Metadata handler for optimized objects.
- */
-public interface OptimizedMarshallerExtMetaHandler {
- /**
- * Adds meta data.
- *
- * @param typeId Type ID.
- * @param meta Meta data.
- */
- void addMeta(int typeId, OptimizedObjectMetadata meta);
-
-
- /**
- * Gets meta data for provided type ID.
- *
- * @param typeId Type ID.
- * @return Meta data.
- */
- OptimizedObjectMetadata metadata(int typeId);
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectInputStreamExt.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectInputStreamExt.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectInputStreamExt.java
deleted file mode 100644
index 66544a0..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectInputStreamExt.java
+++ /dev/null
@@ -1,241 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.internal.processors.cache.*;
-import org.apache.ignite.internal.util.io.*;
-import org.apache.ignite.marshaller.*;
-import org.apache.ignite.marshaller.optimized.*;
-
-import java.io.*;
-import java.util.concurrent.*;
-
-import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerUtils.*;
-import static org.apache.ignite.marshaller.optimized.ext.OptimizedMarshallerExt.*;
-
-
-/**
- * TODO: IGNITE-950
- */
-public class OptimizedObjectInputStreamExt extends OptimizedObjectInputStream {
- /** */
- private OptimizedMarshallerExtMetaHandler metaHandler;
-
- /** {@inheritDoc} */
- public OptimizedObjectInputStreamExt(GridDataInput in) throws IOException {
- super(in);
- }
-
- /**
- * @param clsMap Class descriptors by class map.
- * @param ctx Context.
- * @param mapper ID mapper.
- * @param clsLdr Class loader.
- */
- protected void context(ConcurrentMap<Class, OptimizedClassDescriptor> clsMap, MarshallerContext ctx,
- OptimizedMarshallerIdMapper mapper, ClassLoader clsLdr, OptimizedMarshallerExtMetaHandler metaHandler) {
- context(clsMap, ctx, mapper, clsLdr);
-
- this.metaHandler = metaHandler;
- }
-
- /** {@inheritDoc} */
- @Override protected void skipFooter(Class<?> cls) throws IOException {
- if (!ctx.isSystemType(cls.getName()) && metaHandler != null &&
- metaHandler.metadata(resolveTypeId(cls.getName(), mapper)) != null) {
- short footerLen = in.readShort();
-
- if (footerLen != EMPTY_FOOTER)
- in.skipBytes(footerLen - 2);
- }
- }
-
- /** {@inheritDoc} */
- @Override protected int readFieldType() throws IOException {
- return in.readByte();
- }
-
- /** {@inheritDoc} */
- @Override public void close() throws IOException {
- super.close();
-
- metaHandler = null;
- }
-
- /**
- * Checks whether the object has a field with name {@code fieldName}.
- *
- * @param fieldName Field name.
- * @return {@code true} if field exists, {@code false} otherwise.
- * @throws IOException in case of error.
- */
- boolean hasField(String fieldName) throws IOException {
- int pos = in.position();
-
- if (in.readByte() != SERIALIZABLE) {
- in.position(pos);
- return false;
- }
-
- FieldRange range = fieldRange(fieldName, pos);
-
- in.position(pos);
-
- return range != null && range.start > 0;
- }
-
- /**
- * Looks up field with the given name and returns it in one of the following representations. If the field is
- * serializable and has a footer then it's not deserialized but rather returned wrapped by {@link CacheObjectImpl}
- * for future processing. In all other cases the field is fully deserialized.
- *
- * @param fieldName Field name.
- * @return Field.
- * @throws IOException In case of error.
- * @throws ClassNotFoundException In case of error.
- */
- <F> F readField(String fieldName) throws IOException, ClassNotFoundException {
- int pos = in.position();
-
- if (in.readByte() != SERIALIZABLE) {
- in.position(pos);
- return null;
- }
-
- FieldRange range = fieldRange(fieldName, pos);
-
- F field = null;
-
- if (range != null && range.start >= 0) {
- in.position(range.start);
-
- if (in.readByte() == SERIALIZABLE && metaHandler.metadata(in.readInt()) != null)
- //Do we need to make a copy of array?
- field = (F)new CacheIndexedObjectImpl(in.array(), range.start, range.len);
- else {
- in.position(range.start);
- field = (F)readObject();
- }
- }
-
- in.position(pos);
-
- return field;
- }
-
- /**
- * Returns field offset in the byte stream.
- *
- * @param fieldName Field name.
- * @param start Object's start offset.
- * @return positive range or {@code null} if the object doesn't have such a field.
- * @throws IOException in case of error.
- */
- private FieldRange fieldRange(String fieldName, int start) throws IOException {
- int fieldId = resolveFieldId(fieldName);
-
- int typeId = readInt();
-
- int clsNameLen = 0;
-
- if (typeId == 0) {
- int pos = in.position();
-
- typeId = OptimizedMarshallerUtils.resolveTypeId(readUTF(), mapper);
-
- clsNameLen = in.position() - pos;
- }
-
- OptimizedObjectMetadata meta = metaHandler.metadata(typeId);
-
- if (meta == null)
- // TODO: IGNITE-950 add warning!
- return null;
-
- int end = in.size();
-
- in.position(end - FOOTER_LEN_OFF);
-
- short footerLen = in.readShort();
-
- if (footerLen == EMPTY_FOOTER)
- return null;
-
- // +2 - skipping length at the beginning
- int footerOff = (end - footerLen) + 2;
- in.position(footerOff);
-
- int fieldOff = 0;
-
- for (OptimizedObjectMetadata.FieldInfo info : meta.getMeta()) {
- int len;
- boolean isHandle;
-
- if (info.len == VARIABLE_LEN) {
- int fieldInfo = in.readInt();
-
- len = fieldInfo & FOOTER_BODY_LEN_MASK;
- isHandle = ((fieldInfo & FOOTER_BODY_IS_HANDLE_MASK) >> FOOTER_BODY_HANDLE_MASK_BIT) == 1;
- }
- else {
- len = info.len;
- isHandle = false;
- }
-
- if (info.id == fieldId) {
- if (!isHandle) {
- //object header len: 1 - for type, 4 - for type ID, 2 - for checksum.
- fieldOff += 1 + 4 + clsNameLen + 2;
-
- return new FieldRange(start + fieldOff, len);
- }
- else
- return new FieldRange(in.readInt(), in.readInt());
- }
- else {
- fieldOff += len;
-
- if (isHandle) {
- in.skipBytes(8);
- fieldOff += 8;
- }
- }
- }
-
- return null;
- }
-
- /**
- *
- */
- private static class FieldRange {
- /** */
- private int start;
-
- /** */
- private int len;
-
- /**
- * @param start Start.
- * @param len Length.
- */
- public FieldRange(int start, int len) {
- this.start = start;
- this.len = len;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadata.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadata.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadata.java
deleted file mode 100644
index b3b2ecc..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadata.java
+++ /dev/null
@@ -1,157 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.*;
-import org.apache.ignite.marshaller.optimized.*;
-
-import java.io.*;
-import java.util.*;
-
-/**
- * Metadata that keeps fields information. Used in conjunction with the footer that is added to some objects during
- * marshalling.
- */
-public class OptimizedObjectMetadata implements Externalizable {
- /** */
- private static final long serialVersionUID = 0L;
-
- /** */
- private List<FieldInfo> fieldsInfo;
-
- /** Constructor. */
- public OptimizedObjectMetadata() {
- // No-op
- }
-
- /**
- * Adds meta for a new field.
- *
- * @param fieldId Field ID.
- * @param fieldType Field type.
- */
- public void addMeta(int fieldId, OptimizedFieldType fieldType) {
- if (fieldsInfo == null)
- fieldsInfo = new ArrayList<>();
-
-
-
- fieldsInfo.add(new FieldInfo(fieldId, fieldType));
- }
-
- /**
- * Gets {@link OptimizedObjectMetadata.FieldInfo} at the {@code index}.
- *
- * @param index Position.
- * @return Field meta info.
- */
- public FieldInfo getMeta(int index) {
- return fieldsInfo.get(index);
- }
- /**
- * Returns all the metadata stored for the object.
- *
- * @return Metadata collection.
- */
- public List<FieldInfo> getMeta() {
- return Collections.unmodifiableList(fieldsInfo);
- }
-
- /** {@inheritDoc} */
- @Override public void writeExternal(ObjectOutput out) throws IOException {
- if (fieldsInfo == null) {
- out.writeInt(0);
- return;
- }
-
- out.writeInt(fieldsInfo.size());
-
- for (FieldInfo fieldInfo : fieldsInfo) {
- out.writeInt(fieldInfo.id);
- out.writeByte(fieldInfo.type.ordinal());
- }
- }
-
- /** {@inheritDoc} */
- @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- int size = in.readInt();
-
- fieldsInfo = new ArrayList<>(size);
-
- for (int i = 0; i < size; i++)
- fieldsInfo.add(new FieldInfo(in.readInt(), OptimizedFieldType.values()[in.readByte()]));
- }
-
- /**
- * Field info.
- */
- public static class FieldInfo {
- /** Field ID. */
- int id;
-
- /** Field len. */
- int len;
-
- /** Field type. */
- OptimizedFieldType type;
-
- /**
- * Constructor.
- *
- * @param id Field ID.
- * @param type Field len.
- */
- public FieldInfo(int id, OptimizedFieldType type) {
- this.id = id;
- this.type = type;
-
- len = 1;
-
- switch (type) {
- case BYTE:
- case BOOLEAN:
- len += 1;
- break;
-
- case SHORT:
- case CHAR:
- len += 2;
- break;
-
- case INT:
- case FLOAT:
- len += 4;
- break;
-
- case LONG:
- case DOUBLE:
- len += 8;
- break;
-
- case OTHER:
- len = OptimizedMarshallerExt.VARIABLE_LEN;
- break;
-
- default:
- throw new IgniteException("Unknown field type: " + type);
- }
-
- assert len != 1;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadataKey.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadataKey.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadataKey.java
deleted file mode 100644
index b9fcd58..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectMetadataKey.java
+++ /dev/null
@@ -1,70 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.internal.processors.cache.*;
-
-import java.io.*;
-
-/**
- * Optimized object metadata key.
- */
-public class OptimizedObjectMetadataKey extends GridCacheUtilityKey<OptimizedObjectMetadataKey>
- implements Externalizable {
- /** */
- private static final long serialVersionUID = 0L;
-
- /** */
- private int typeId;
-
- /**
- * For {@link Externalizable}.
- */
- public OptimizedObjectMetadataKey() {
- // No-op
- }
-
- /**
- * Constructor.
- *
- * @param typeId Type id.
- */
- public OptimizedObjectMetadataKey(int typeId) {
- this.typeId = typeId;
- }
-
- /** {@inheritDoc} */
- @Override public void writeExternal(ObjectOutput out) throws IOException {
- out.writeInt(typeId);
- }
-
- /** {@inheritDoc} */
- @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- typeId = in.readInt();
- }
-
- /** {@inheritDoc} */
- @Override protected boolean equalsx(OptimizedObjectMetadataKey key) {
- return typeId == key.typeId;
- }
-
- /** {@inheritDoc} */
- @Override public int hashCode() {
- return typeId;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectOutputStreamExt.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectOutputStreamExt.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectOutputStreamExt.java
deleted file mode 100644
index 8ae8f9e..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectOutputStreamExt.java
+++ /dev/null
@@ -1,179 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.internal.util.*;
-import org.apache.ignite.internal.util.io.*;
-import org.apache.ignite.marshaller.*;
-import org.apache.ignite.marshaller.optimized.*;
-
-import java.io.*;
-import java.util.*;
-import java.util.concurrent.*;
-
-import static org.apache.ignite.marshaller.optimized.ext.OptimizedMarshallerExt.*;
-import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerUtils.*;
-
-/**
- * TODO: IGNITE-950
- */
-public class OptimizedObjectOutputStreamExt extends OptimizedObjectOutputStream {
- /** */
- private OptimizedMarshallerExtMetaHandler metaHandler;
-
- /**
- * Constructor.
- *
- * @param out Output stream.
- * @throws IOException In case of error.
- */
- protected OptimizedObjectOutputStreamExt(GridDataOutput out) throws IOException {
- super(out);
- }
-
- /**
- * @param clsMap Class descriptors by class map.
- * @param ctx Context.
- * @param mapper ID mapper.
- * @param requireSer Require {@link Serializable} flag.
- * @param metaHandler Metadata handler.
- */
- protected void context(ConcurrentMap<Class, OptimizedClassDescriptor> clsMap, MarshallerContext ctx,
- OptimizedMarshallerIdMapper mapper, boolean requireSer, OptimizedMarshallerExtMetaHandler metaHandler) {
- context(clsMap, ctx, mapper, requireSer);
-
- this.metaHandler = metaHandler;
- }
-
- /** {@inheritDoc} */
- @Override protected void writeFieldType(byte type) throws IOException {
- out.writeByte(type);
- }
-
- /** {@inheritDoc} */
- @Override protected Footer createFooter(Class<?> cls) {
- if (!ctx.isSystemType(cls.getName()) && metaHandler != null &&
- metaHandler.metadata(resolveTypeId(cls.getName(), mapper)) != null)
- return new FooterImpl();
- else
- return null;
- }
-
- /** {@inheritDoc} */
- @Override public void close() throws IOException {
- super.close();
-
- metaHandler = null;
- }
-
- /**
- *
- */
- private class FooterImpl implements OptimizedObjectOutputStream.Footer {
- /** */
- private ArrayList<Integer> data;
-
- /** */
- private ArrayList<Integer> fields;
-
- /** */
- private HashMap<Integer, GridHandleTable.ObjectInfo> handles;
-
- /** */
- private boolean hasHandles;
-
- /** {@inheritDoc} */
- @Override public void fields(OptimizedClassDescriptor.Fields fields) {
- if (fields.fieldsIndexingSupported()) {
- data = new ArrayList<>();
- this.fields = new ArrayList<>();
- }
- else
- data = null;
- }
-
- /** {@inheritDoc} */
- public void put(int fieldId, OptimizedFieldType fieldType, int len) {
- if (data == null)
- return;
-
- if (fieldType == OptimizedFieldType.OTHER) {
- data.add(len);
- fields.add(fieldId);
- }
- }
-
- /** {@inheritDoc} */
- @Override public void putHandle(int fieldId, GridHandleTable.ObjectInfo objInfo) {
- if (data == null)
- return;
-
- if (!hasHandles) {
- hasHandles = true;
- handles = new HashMap<>();
- }
-
- handles.put(fieldId, objInfo);
-
- // length of handle fields is 5 bytes.
- put(fieldId, OptimizedFieldType.OTHER, 5);
- }
-
- /** {@inheritDoc} */
- public void write() throws IOException {
- if (data == null)
- writeInt(EMPTY_FOOTER);
- else {
- int bodyEnd = out.offset();
-
- // +4 - 2 bytes for footer len at the beginning, 2 bytes for footer len at the end.
- short footerLen = (short)(data.size() * 4 + 4);
-
- if (hasHandles)
- footerLen += handles.size() * 8;
-
- writeShort(footerLen);
-
- if (hasHandles) {
- for (int i = 0; i < data.size(); i++) {
- GridHandleTable.ObjectInfo objInfo = handles.get(fields.get(i));
-
- if (objInfo == null)
- writeInt(data.get(i) & ~FOOTER_BODY_IS_HANDLE_MASK);
- else {
- writeInt(data.get(i) | FOOTER_BODY_IS_HANDLE_MASK);
- writeInt(objInfo.position());
-
- if (objInfo.length() == 0)
- // field refers to its own object that hasn't set total length yet.
- writeInt((bodyEnd - objInfo.position()) + footerLen);
- else
- writeInt(objInfo.length());
- }
- }
- }
- else
- for (int fieldLen : data)
- // writing field len and resetting is handle mask
- writeInt(fieldLen & ~FOOTER_BODY_IS_HANDLE_MASK);
-
- writeShort(footerLen);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectStreamExtRegistry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectStreamExtRegistry.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectStreamExtRegistry.java
deleted file mode 100644
index e07b4de..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/OptimizedObjectStreamExtRegistry.java
+++ /dev/null
@@ -1,226 +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.marshaller.optimized.ext;
-
-import org.apache.ignite.*;
-import org.apache.ignite.internal.*;
-import org.apache.ignite.internal.util.io.*;
-import org.apache.ignite.internal.util.typedef.internal.*;
-import org.apache.ignite.marshaller.optimized.*;
-
-import java.io.*;
-import java.util.concurrent.*;
-
-/**
- * Storage for object streams.
- */
-class OptimizedObjectStreamExtRegistry {
- /** Holders. */
- private static final ThreadLocal<StreamHolder> holders = new ThreadLocal<>();
-
- /** Holders pool. */
- private static BlockingQueue<StreamHolder> pool;
-
- /**
- * Ensures singleton.
- */
- private OptimizedObjectStreamExtRegistry() {
- // No-op.
- }
-
- /**
- * Sets streams pool size.
- *
- * @param size Streams pool size.
- */
- static void poolSize(int size) {
- if (size > 0) {
- pool = new LinkedBlockingQueue<>(size);
-
- for (int i = 0; i < size; i++) {
- boolean b = pool.offer(new StreamHolder());
-
- assert b;
- }
- }
- else
- pool = null;
- }
-
- /**
- * Gets output stream.
- *
- * @return Object output stream.
- * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
- */
- static OptimizedObjectOutputStreamExt out() throws IgniteInterruptedCheckedException {
- return holder().acquireOut();
- }
-
- /**
- * Gets input stream.
- *
- * @return Object input stream.
- * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
- */
- static OptimizedObjectInputStreamExt in() throws IgniteInterruptedCheckedException {
- return holder().acquireIn();
- }
-
- /**
- * Closes and releases output stream.
- *
- * @param out Object output stream.
- */
- static void closeOut(OptimizedObjectOutputStream out) {
- U.close(out, null);
-
- StreamHolder holder = holders.get();
-
- holder.releaseOut();
-
- if (pool != null) {
- holders.remove();
-
- boolean b = pool.offer(holder);
-
- assert b;
- }
- }
-
- /**
- * Closes and releases input stream.
- *
- * @param in Object input stream.
- */
- @SuppressWarnings("TypeMayBeWeakened")
- static void closeIn(OptimizedObjectInputStream in) {
- U.close(in, null);
-
- StreamHolder holder = holders.get();
-
- holder.releaseIn();
-
- if (pool != null) {
- holders.remove();
-
- boolean b = pool.offer(holder);
-
- assert b;
- }
- }
-
- /**
- * Gets holder from pool or thread local.
- *
- * @return Stream holder.
- * @throws IgniteInterruptedCheckedException If thread is interrupted while trying to take holder from pool.
- */
- private static StreamHolder holder() throws IgniteInterruptedCheckedException {
- StreamHolder holder = holders.get();
-
- if (holder == null) {
- try {
- holders.set(holder = pool != null ? pool.take() : new StreamHolder());
- }
- catch (InterruptedException e) {
- throw new IgniteInterruptedCheckedException(
- "Failed to take object stream from pool (thread interrupted).", e);
- }
- }
-
- return holder;
- }
-
- /**
- * Streams holder.
- */
- private static class StreamHolder {
- /** Output stream. */
- private final OptimizedObjectOutputStreamExt out = createOut();
-
- /** Input stream. */
- private final OptimizedObjectInputStreamExt in = createIn();
-
- /** Output streams counter. */
- private int outAcquireCnt;
-
- /** Input streams counter. */
- private int inAcquireCnt;
-
- /**
- * Gets output stream.
- *
- * @return Object output stream.
- */
- OptimizedObjectOutputStreamExt acquireOut() {
- return outAcquireCnt++ > 0 ? createOut() : out;
- }
-
- /**
- * Gets input stream.
- *
- * @return Object input stream.
- */
- OptimizedObjectInputStreamExt acquireIn() {
- return inAcquireCnt++ > 0 ? createIn() : in;
- }
-
- /**
- * Releases output stream.
- */
- void releaseOut() {
- outAcquireCnt--;
- }
-
- /**
- * Releases input stream.
- */
- void releaseIn() {
- inAcquireCnt--;
- }
-
- /**
- * Creates output stream.
- *
- * @return Object output stream.
- */
- private OptimizedObjectOutputStreamExt createOut() {
- try {
- return new OptimizedObjectOutputStreamExt(new GridUnsafeDataOutput(4 * 1024));
- }
- catch (IOException e) {
- throw new IgniteException("Failed to create object output stream.", e);
- }
- }
-
- /**
- * Creates input stream.
- *
- * @return Object input stream.
- */
- private OptimizedObjectInputStreamExt createIn() {
- try {
- return new OptimizedObjectInputStreamExt(new GridUnsafeDataInput());
- }
- catch (IOException e) {
- throw new IgniteException("Failed to create object input stream.", e);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/package-info.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/package-info.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/package-info.java
deleted file mode 100644
index 84d1ce3..0000000
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/ext/package-info.java
+++ /dev/null
@@ -1,21 +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 description. -->
- * Contains <b>extended</b> version of Optimized marshaller.
- */
-package org.apache.ignite.marshaller.optimized.ext;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtSelfTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtSelfTest.java
index c1f07ce..7866500 100644
--- a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/ext/OptimizedMarshallerExtSelfTest.java
@@ -22,6 +22,7 @@ import org.apache.ignite.marshaller.*;
import org.apache.ignite.marshaller.optimized.*;
import org.apache.ignite.testframework.junits.common.*;
+import java.io.*;
import java.util.concurrent.*;
/**
@@ -33,7 +34,7 @@ public class OptimizedMarshallerExtSelfTest extends OptimizedMarshallerSelfTest
private static ConcurrentHashMap<Integer, OptimizedObjectMetadata> META_BUF = new ConcurrentHashMap<>();
/** */
- private static final OptimizedMarshallerExtMetaHandler META_HANDLER = new OptimizedMarshallerExtMetaHandler() {
+ private static final OptimizedMarshallerMetaHandler META_HANDLER = new OptimizedMarshallerMetaHandler() {
@Override public void addMeta(int typeId, OptimizedObjectMetadata meta) {
META_BUF.putIfAbsent(typeId, meta);
}
@@ -143,6 +144,29 @@ public class OptimizedMarshallerExtSelfTest extends OptimizedMarshallerSelfTest
assertEquals(selfLinkObject, selfLinkObject2);
}
+
+ /**
+ * @throws Exception In case of error.
+ */
+ /*public void testMarshalAware() throws Exception {
+ META_BUF.clear();
+
+ OptimizedMarshallerExt marsh = (OptimizedMarshallerExt)OptimizedMarshallerExtSelfTest.marsh;
+
+ assertTrue(marsh.enableFieldsIndexing(TestMarshalAware.class));
+ assertEquals(0, META_BUF.size());
+
+ TestMarshalAware test = new TestMarshalAware(100, "MarshalAware");
+
+ byte[] arr = marsh.marshal(test);
+
+ assertEquals(1, META_BUF.size());
+
+ TestMarshalAware test2 = marsh.unmarshal(arr, null);
+
+ assertEquals(test, test2);
+ }*/
+
private static class InternalMarshaller extends OptimizedMarshallerExt {
/**
* Constructor.
@@ -161,7 +185,7 @@ public class OptimizedMarshallerExtSelfTest extends OptimizedMarshallerSelfTest
}
/** {@inheritDoc} */
- @Override public void setMetadataHandler(OptimizedMarshallerExtMetaHandler metaHandler) {
+ @Override public void setMetadataHandler(OptimizedMarshallerMetaHandler metaHandler) {
// No-op
}
}
@@ -281,4 +305,51 @@ public class OptimizedMarshallerExtSelfTest extends OptimizedMarshallerSelfTest
return true;
}
}
+
+ /**
+ *
+ */
+ private static class TestMarshalAware implements OptimizedMarshalAware {
+ /** */
+ private int i;
+
+ /** */
+ private String str;
+
+ public TestMarshalAware() {
+ // No-op
+ }
+
+ public TestMarshalAware(int i, String str) {
+ this.i = i;
+ this.str = str;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeFields(OptimizedFieldsWriter writer) throws IOException {
+ writer.writeInt("i", i);
+ writer.writeString("str", str);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void readFields(OptimizedFieldsReader reader) throws IOException {
+ i = reader.readInt("i");
+ str = reader.readString("str");
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ TestMarshalAware that = (TestMarshalAware)o;
+
+ if (i != that.i)
+ return false;
+
+ return !(str != null ? !str.equals(that.str) : that.str != null);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java
index e7609d6..929bdf5 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteTestResources.java
@@ -22,7 +22,6 @@ import org.apache.ignite.internal.processors.resource.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.*;
import org.apache.ignite.marshaller.optimized.*;
-import org.apache.ignite.marshaller.optimized.ext.*;
import org.apache.ignite.resources.*;
import org.apache.ignite.testframework.config.*;
import org.apache.ignite.testframework.junits.logger.*;
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5749b068/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOptimizedMarshallerExtQuerySelfTest.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOptimizedMarshallerExtQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOptimizedMarshallerExtQuerySelfTest.java
index 010f3e6..77e5bb2 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOptimizedMarshallerExtQuerySelfTest.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOptimizedMarshallerExtQuerySelfTest.java
@@ -21,9 +21,7 @@ import org.apache.ignite.*;
import org.apache.ignite.cache.*;
import org.apache.ignite.cache.query.*;
import org.apache.ignite.configuration.*;
-import org.apache.ignite.internal.processors.cache.query.*;
-import org.apache.ignite.internal.processors.query.*;
-import org.apache.ignite.marshaller.optimized.ext.*;
+import org.apache.ignite.marshaller.optimized.*;
import javax.cache.*;
import java.util.*;