ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [1/3] ignite git commit: Added base infrastructure classes.
Date Sat, 13 Aug 2016 19:14:23 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-3681 [created] 2376ac1d6


Added base infrastructure classes.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/bc33643e
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/bc33643e
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/bc33643e

Branch: refs/heads/ignite-3681
Commit: bc33643e442192b5ef911e0ef31d7a0e3e2d9f85
Parents: 1139a9f
Author: vozerov-gridgain <vozerov@gridgain.com>
Authored: Sat Aug 13 21:27:44 2016 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Sat Aug 13 21:27:44 2016 +0300

----------------------------------------------------------------------
 .../internal/binary/BinaryClassDescriptor.java  | 36 +++++---
 .../binary/BinaryMethodWriteReplacer.java       | 59 ++++++++++++
 .../ignite/internal/binary/BinaryTreeMap.java   | 96 ++++++++++++++++++++
 .../binary/BinaryTreeMapWriteReplacer.java      | 34 +++++++
 .../ignite/internal/binary/BinaryTreeSet.java   | 93 +++++++++++++++++++
 .../binary/BinaryTreeSetWriteReplacer.java      | 34 +++++++
 .../internal/binary/BinaryWriteReplacer.java    | 33 +++++++
 .../internal/binary/BinaryWriterExImpl.java     | 35 +++----
 8 files changed, 385 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java
index d2d715b..2588f59 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryClassDescriptor.java
@@ -89,8 +89,8 @@ public class BinaryClassDescriptor {
     /** */
     private final BinaryFieldAccessor[] fields;
 
-    /** */
-    private final Method writeReplaceMtd;
+    /** Write replacer. */
+    private final BinaryWriteReplacer writeReplacer;
 
     /** */
     private final Method readResolveMtd;
@@ -298,11 +298,8 @@ public class BinaryClassDescriptor {
 
                             schemaBuilder.addField(fieldId);
 
-                            if (metaDataEnabled) {
-                                assert stableFieldsMeta != null;
-
+                            if (metaDataEnabled)
                                 stableFieldsMeta.put(name, fieldInfo.mode().typeId());
-                            }
                         }
                     }
                 }
@@ -320,14 +317,19 @@ public class BinaryClassDescriptor {
                 throw new BinaryObjectException("Invalid mode: " + mode);
         }
 
+        Method writeReplaceMthd;
+
         if (mode == BinaryWriteMode.BINARY || mode == BinaryWriteMode.OBJECT) {
             readResolveMtd = U.findNonPublicMethod(cls, "readResolve");
-            writeReplaceMtd = U.findNonPublicMethod(cls, "writeReplace");
+
+            writeReplaceMthd = U.findNonPublicMethod(cls, "writeReplace");
         }
         else {
             readResolveMtd = null;
-            writeReplaceMtd = null;
+            writeReplaceMthd = null;
         }
+
+        writeReplacer = writeReplaceMthd == null ? null : new BinaryMethodWriteReplacer(writeReplaceMthd);
     }
 
     /**
@@ -469,10 +471,22 @@ public class BinaryClassDescriptor {
     }
 
     /**
-     * @return binaryWriteReplace() method
+     * @return {@code True} if write-replace should be performed for class.
      */
-    @Nullable Method getWriteReplaceMethod() {
-        return writeReplaceMtd;
+    public boolean isWriteReplace() {
+        return writeReplacer != null;
+    }
+
+    /**
+     * Perform write replace.
+     *
+     * @param obj Original object.
+     * @return Replaced object.
+     */
+    public Object writeReplace(Object obj) {
+        assert isWriteReplace();
+
+        return writeReplacer.replace(obj);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryMethodWriteReplacer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryMethodWriteReplacer.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryMethodWriteReplacer.java
new file mode 100644
index 0000000..783048e
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryMethodWriteReplacer.java
@@ -0,0 +1,59 @@
+/*
+ * 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.binary;
+
+import org.apache.ignite.binary.BinaryObjectException;
+import org.jetbrains.annotations.Nullable;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Write replacer based on method invocation.
+ */
+public class BinaryMethodWriteReplacer implements BinaryWriteReplacer {
+    /** Method. */
+    private final Method mthd;
+
+    /**
+     * Constructor.
+     *
+     * @param mthd Method.
+     */
+    public BinaryMethodWriteReplacer(Method mthd) {
+        assert mthd != null;
+
+        this.mthd = mthd;
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override public Object replace(Object target) {
+        try {
+            return mthd.invoke(target);
+        }
+        catch (IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
+        catch (InvocationTargetException e) {
+            if (e.getTargetException() instanceof BinaryObjectException)
+                throw (BinaryObjectException)e.getTargetException();
+
+            throw new BinaryObjectException("Failed to execute writeReplace() method on "
+ target, e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMap.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMap.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMap.java
new file mode 100644
index 0000000..6a7cf9b
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMap.java
@@ -0,0 +1,96 @@
+/*
+ * 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.binary;
+
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryRawReader;
+import org.apache.ignite.binary.BinaryRawWriter;
+import org.apache.ignite.binary.BinaryReader;
+import org.apache.ignite.binary.BinaryWriter;
+import org.apache.ignite.binary.Binarylizable;
+
+import java.io.ObjectStreamException;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * Binary {@link TreeMap} wrapper.
+ */
+public class BinaryTreeMap implements Binarylizable {
+    /** Original map. */
+    private TreeMap map;
+
+    /**
+     * Default constructor.
+     */
+    public BinaryTreeMap() {
+        // No-op.
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param map Original map.
+     */
+    public BinaryTreeMap(TreeMap map) {
+        this.map = map;
+    }
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException {
+        BinaryRawWriter rawWriter = writer.rawWriter();
+
+        rawWriter.writeObject(map.comparator());
+
+        int size = map.size();
+
+        rawWriter.writeInt(size);
+
+        for (Map.Entry<Object, Object> entry : ((TreeMap<Object, Object>)map).entrySet())
{
+            rawWriter.writeObject(entry.getKey());
+            rawWriter.writeObject(entry.getValue());
+        }
+    }
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override public void readBinary(BinaryReader reader) throws BinaryObjectException {
+        BinaryRawReader rawReader = reader.rawReader();
+
+        Comparator comp =  rawReader.readObject();
+
+        map = comp == null ? new TreeMap() : new TreeMap(comp);
+
+        int size = rawReader.readInt();
+
+        for (int i = 0; i < size; i++)
+            map.put(rawReader.readObject(), rawReader.readObject());
+    }
+
+    /**
+     * Reconstructs object on unmarshalling.
+     *
+     * @return Reconstructed object.
+     * @throws ObjectStreamException Thrown in case of unmarshalling error.
+     */
+    protected Object readResolve() throws ObjectStreamException {
+        return map;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMapWriteReplacer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMapWriteReplacer.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMapWriteReplacer.java
new file mode 100644
index 0000000..049db8e
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeMapWriteReplacer.java
@@ -0,0 +1,34 @@
+/*
+ * 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.binary;
+
+import org.jetbrains.annotations.Nullable;
+
+import java.util.TreeMap;
+
+/**
+ * Binary {@link TreeMap} write replacer.
+ */
+public class BinaryTreeMapWriteReplacer implements BinaryWriteReplacer {
+    /** {@inheritDoc} */
+    @Nullable @Override public Object replace(Object target) {
+        assert target instanceof TreeMap;
+
+        return new BinaryTreeMap((TreeMap)target);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSet.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSet.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSet.java
new file mode 100644
index 0000000..2b01528
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSet.java
@@ -0,0 +1,93 @@
+/*
+ * 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.binary;
+
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryRawReader;
+import org.apache.ignite.binary.BinaryRawWriter;
+import org.apache.ignite.binary.BinaryReader;
+import org.apache.ignite.binary.BinaryWriter;
+import org.apache.ignite.binary.Binarylizable;
+
+import java.io.ObjectStreamException;
+import java.util.Comparator;
+import java.util.TreeSet;
+
+/**
+ * Binary {@link TreeSet} wrapper.
+ */
+public class BinaryTreeSet implements Binarylizable {
+    /** Original set. */
+    private TreeSet set;
+
+    /**
+     * Default constructor.
+     */
+    public BinaryTreeSet() {
+        // No-op.
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param set Original set.
+     */
+    public BinaryTreeSet(TreeSet set) {
+        this.set = set;
+    }
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException {
+        BinaryRawWriter rawWriter = writer.rawWriter();
+
+        rawWriter.writeObject(set.comparator());
+
+        int size = set.size();
+
+        rawWriter.writeInt(size);
+
+        for (Object val : set)
+            rawWriter.writeObject(val);
+    }
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override public void readBinary(BinaryReader reader) throws BinaryObjectException {
+        BinaryRawReader rawReader = reader.rawReader();
+
+        Comparator comp =  rawReader.readObject();
+
+        set = comp == null ? new TreeSet() : new TreeSet(comp);
+
+        int size = rawReader.readInt();
+
+        for (int i = 0; i < size; i++)
+            set.add(rawReader.readObject());
+    }
+
+    /**
+     * Reconstructs object on unmarshalling.
+     *
+     * @return Reconstructed object.
+     * @throws ObjectStreamException Thrown in case of unmarshalling error.
+     */
+    protected Object readResolve() throws ObjectStreamException {
+        return set;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSetWriteReplacer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSetWriteReplacer.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSetWriteReplacer.java
new file mode 100644
index 0000000..4350777
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryTreeSetWriteReplacer.java
@@ -0,0 +1,34 @@
+/*
+ * 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.binary;
+
+import org.jetbrains.annotations.Nullable;
+
+import java.util.TreeSet;
+
+/**
+ * Binary {@link TreeSet} write replacer.
+ */
+public class BinaryTreeSetWriteReplacer implements BinaryWriteReplacer {
+    /** {@inheritDoc} */
+    @Nullable @Override public Object replace(Object target) {
+        assert target instanceof TreeSet;
+
+        return new BinaryTreeSet((TreeSet)target);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteReplacer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteReplacer.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteReplacer.java
new file mode 100644
index 0000000..9376c37
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteReplacer.java
@@ -0,0 +1,33 @@
+/*
+ * 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.binary;
+
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Interface to perform write replace.
+ */
+public interface BinaryWriteReplacer {
+    /**
+     * Perform replace.
+     *
+     * @param target Original object.
+     * @return Replaced object.
+     */
+    @Nullable public Object replace(Object target);
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/bc33643e/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java
index 30710f4..9450482 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java
@@ -17,10 +17,18 @@
 
 package org.apache.ignite.internal.binary;
 
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryRawWriter;
+import org.apache.ignite.binary.BinaryWriter;
+import org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream;
+import org.apache.ignite.internal.binary.streams.BinaryOutputStream;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.jetbrains.annotations.Nullable;
+
 import java.io.IOException;
 import java.io.ObjectOutput;
 import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Proxy;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -29,14 +37,6 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.Map;
 import java.util.UUID;
-import org.apache.ignite.IgniteCheckedException;
-import org.apache.ignite.binary.BinaryObjectException;
-import org.apache.ignite.binary.BinaryRawWriter;
-import org.apache.ignite.binary.BinaryWriter;
-import org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream;
-import org.apache.ignite.internal.binary.streams.BinaryOutputStream;
-import org.apache.ignite.internal.util.typedef.internal.A;
-import org.jetbrains.annotations.Nullable;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 
@@ -170,21 +170,8 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx,
Obje
             return;
         }
 
-        if (enableReplace && desc.getWriteReplaceMethod() != null) {
-            Object replacedObj;
-
-            try {
-                replacedObj = desc.getWriteReplaceMethod().invoke(obj);
-            }
-            catch (IllegalAccessException e) {
-                throw new RuntimeException(e);
-            }
-            catch (InvocationTargetException e) {
-                if (e.getTargetException() instanceof BinaryObjectException)
-                    throw (BinaryObjectException)e.getTargetException();
-
-                throw new BinaryObjectException("Failed to execute writeReplace() method
on " + obj, e);
-            }
+        if (enableReplace && desc.isWriteReplace()) {
+            Object replacedObj = desc.writeReplace(obj);
 
             if (replacedObj == null) {
                 out.writeByte(GridBinaryMarshaller.NULL);


Mime
View raw message