http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/configuration/IgniteConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/configuration/IgniteConfiguration.java b/modules/core/src/main/java/org/apache/ignite/configuration/IgniteConfiguration.java index 3d38ed9..a91aa7e 100644 --- a/modules/core/src/main/java/org/apache/ignite/configuration/IgniteConfiguration.java +++ b/modules/core/src/main/java/org/apache/ignite/configuration/IgniteConfiguration.java @@ -31,6 +31,7 @@ import javax.net.ssl.SSLContext; import org.apache.ignite.IgniteLogger; import org.apache.ignite.IgniteSystemProperties; import org.apache.ignite.Ignition; +import org.apache.ignite.cache.CacheKeyConfiguration; import org.apache.ignite.cache.store.CacheStoreSessionListener; import org.apache.ignite.cluster.ClusterGroup; import org.apache.ignite.cluster.ClusterNode; @@ -427,6 +428,9 @@ public class IgniteConfiguration { /** Platform configuration. */ private PlatformConfiguration platformCfg; + /** Cache key configuration. */ + private CacheKeyConfiguration[] cacheKeyCfg; + /** * Creates valid grid configuration with all default values. */ @@ -463,6 +467,7 @@ public class IgniteConfiguration { atomicCfg = cfg.getAtomicConfiguration(); daemon = cfg.isDaemon(); cacheCfg = cfg.getCacheConfiguration(); + cacheKeyCfg = cfg.getCacheKeyConfiguration(); cacheSanityCheckEnabled = cfg.isCacheSanityCheckEnabled(); connectorCfg = cfg.getConnectorConfiguration(); classLdr = cfg.getClassLoader(); @@ -1950,6 +1955,25 @@ public class IgniteConfiguration { } /** + * Gets cache key configuration. + * + * @return Cache key configuration. + */ + public CacheKeyConfiguration[] getCacheKeyConfiguration() { + return cacheKeyCfg; + } + + /** + * Sets cache key configuration. + * Cache key configuration defines + * + * @param cacheKeyCfg Cache key configuration. + */ + public void setCacheKeyCfg(CacheKeyConfiguration... cacheKeyCfg) { + this.cacheKeyCfg = cacheKeyCfg; + } + + /** * Gets flag indicating whether cache sanity check is enabled. If enabled, then Ignite * will perform the following checks and throw an exception if check fails: * <ul>
http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObject.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObject.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObject.java new file mode 100644 index 0000000..c3ba1c1 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObject.java @@ -0,0 +1,153 @@ +/* + * 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.igniteobject; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.TreeMap; +import org.apache.ignite.marshaller.portable.PortableMarshaller; +import org.jetbrains.annotations.Nullable; + +/** + * Wrapper for portable object in portable binary format. Once an object is defined as portable, + * Ignite will always store it in memory in the portable (i.e. binary) format. + * User can choose to work either with the portable format or with the deserialized form + * (assuming that class definitions are present in the classpath). + * <p> + * <b>NOTE:</b> user does not need to (and should not) implement this interface directly. + * <p> + * To work with the portable format directly, user should create a cache projection + * over {@code PortableObject} class and then retrieve individual fields as needed: + * <pre name=code class=java> + * IgniteCache<PortableObject, PortableObject> prj = cache.withKeepBinary(); + * + * // Convert instance of MyKey to portable format. + * // We could also use GridPortableBuilder to create the key in portable format directly. + * PortableObject key = grid.portables().toPortable(new MyKey()); + * + * PortableObject val = prj.get(key); + * + * String field = val.field("myFieldName"); + * </pre> + * Alternatively, if we have class definitions in the classpath, we may choose to work with deserialized + * typed objects at all times. In this case we do incur the deserialization cost. However, if + * {@link PortableMarshaller#isKeepDeserialized()} is {@code true} then Ignite will only deserialize on the first access + * and will cache the deserialized object, so it does not have to be deserialized again: + * <pre name=code class=java> + * IgniteCache<MyKey.class, MyValue.class> cache = grid.cache(null); + * + * MyValue val = cache.get(new MyKey()); + * + * // Normal java getter. + * String fieldVal = val.getMyFieldName(); + * </pre> + * <h1 class="header">Working With Maps and Collections</h1> + * All maps and collections in the portable objects are serialized automatically. When working + * with different platforms, e.g. C++ or .NET, Ignite will automatically pick the most + * adequate collection or map in either language. For example, {@link ArrayList} in Java will become + * {@code List} in C#, {@link LinkedList} in Java is {@link LinkedList} in C#, {@link HashMap} + * in Java is {@code Dictionary} in C#, and {@link TreeMap} in Java becomes {@code SortedDictionary} + * in C#, etc. + * <h1 class="header">Dynamic Structure Changes</h1> + * Since objects are always cached in the portable binary format, server does not need to + * be aware of the class definitions. Moreover, if class definitions are not present or not + * used on the server, then clients can continuously change the structure of the portable + * objects without having to restart the cluster. For example, if one client stores a + * certain class with fields A and B, and another client stores the same class with + * fields B and C, then the server-side portable object will have the fields A, B, and C. + * As the structure of a portable object changes, the new fields become available for SQL queries + * automatically. + * <h1 class="header">Building Portable Objects</h1> + * Ignite comes with {@link IgniteObjectBuilder} which allows to build portable objects dynamically: + * <pre name=code class=java> + * PortableBuilder builder = Ignition.ignite().portables().builder("org.project.MyObject"); + * + * builder.setField("fieldA", "A"); + * builder.setField("fieldB", "B"); + * + * PortableObject portableObj = builder.build(); + * </pre> + * For the cases when class definition is present + * in the class path, it is also possible to populate a standard POJO and then + * convert it to portable format, like so: + * <pre name=code class=java> + * MyObject obj = new MyObject(); + * + * obj.setFieldA("A"); + * obj.setFieldB(123); + * + * PortableObject portableObj = Ignition.ignite().portables().toPortable(obj); + * </pre> + * <h1 class="header">Portable Metadata</h1> + * Even though Ignite portable protocol only works with hash codes for type and field names + * to achieve better performance, Ignite provides metadata for all portable types which + * can be queried ar runtime via any of the {@link org.apache.ignite.IgniteObjects#metadata(Class)} + * methods. Having metadata also allows for proper formatting of {@code PortableObject.toString()} method, + * even when portable objects are kept in binary format only, which may be necessary for audit reasons. + */ +public interface IgniteObject extends Serializable, Cloneable { + /** + * Gets portable object type ID. + * + * @return Type ID. + */ + public int typeId(); + + /** + * Gets meta data for this portable object. + * + * @return Meta data. + * @throws IgniteObjectException In case of error. + */ + @Nullable public IgniteObjectMetadata metaData() throws IgniteObjectException; + + /** + * Gets field value. + * + * @param fieldName Field name. + * @return Field value. + * @throws IgniteObjectException In case of any other error. + */ + @Nullable public <F> F field(String fieldName) throws IgniteObjectException; + + /** + * Checks whether field is set. + * + * @param fieldName Field name. + * @return {@code true} if field is set. + */ + public boolean hasField(String fieldName); + + /** + * Gets fully deserialized instance of portable object. + * + * @return Fully deserialized instance of portable object. + * @throws IgniteObjectInvalidClassException If class doesn't exist. + * @throws IgniteObjectException In case of any other error. + */ + @Nullable public <T> T deserialize() throws IgniteObjectException; + + /** + * Copies this portable object. + * + * @return Copy of this portable object. + */ + public IgniteObject clone() throws CloneNotSupportedException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectBuilder.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectBuilder.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectBuilder.java new file mode 100644 index 0000000..08e1c2a --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectBuilder.java @@ -0,0 +1,136 @@ +/* + * 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.igniteobject; + +import org.jetbrains.annotations.Nullable; + +/** + * Portable object builder. Provides ability to build portable objects dynamically without having class definitions. + * <p> + * Here is an example of how a portable object can be built dynamically: + * <pre name=code class=java> + * PortableBuilder builder = Ignition.ignite().portables().builder("org.project.MyObject"); + * + * builder.setField("fieldA", "A"); + * builder.setField("fieldB", "B"); + * + * PortableObject portableObj = builder.build(); + * </pre> + * + * <p> + * Also builder can be initialized by existing portable object. This allows changing some fields without affecting + * other fields. + * <pre name=code class=java> + * PortableBuilder builder = Ignition.ignite().portables().builder(person); + * + * builder.setField("name", "John"); + * + * person = builder.build(); + * </pre> + * </p> + * + * If you need to modify nested portable object you can get builder for nested object using + * {@link #getField(String)}, changes made on nested builder will affect parent object, + * for example: + * + * <pre name=code class=java> + * PortableBuilder personBuilder = grid.portables().createBuilder(personPortableObj); + * PortableBuilder addressBuilder = personBuilder.setField("address"); + * + * addressBuilder.setField("city", "New York"); + * + * personPortableObj = personBuilder.build(); + * + * // Should be "New York". + * String city = personPortableObj.getField("address").getField("city"); + * </pre> + * + * @see org.apache.ignite.IgniteObjects#builder(int) + * @see org.apache.ignite.IgniteObjects#builder(String) + * @see org.apache.ignite.IgniteObjects#builder(IgniteObject) + */ +public interface IgniteObjectBuilder { + /** + * Returns value assigned to the specified field. + * If the value is a portable object instance of {@code GridPortableBuilder} will be returned, + * which can be modified. + * <p> + * Collections and maps returned from this method are modifiable. + * + * @param name Field name. + * @return Filed value. + */ + public <T> T getField(String name); + + /** + * Sets field value. + * + * @param name Field name. + * @param val Field value (cannot be {@code null}). + * @see IgniteObject#metaData() + */ + public IgniteObjectBuilder setField(String name, Object val); + + /** + * Sets field value with value type specification. + * <p> + * Field type is needed for proper metadata update. + * + * @param name Field name. + * @param val Field value. + * @param type Field type. + * @see IgniteObject#metaData() + */ + public <T> IgniteObjectBuilder setField(String name, @Nullable T val, Class<? super T> type); + + /** + * Sets field value. + * <p> + * This method should be used if field is portable object. + * + * @param name Field name. + * @param builder Builder for object field. + */ + public IgniteObjectBuilder setField(String name, @Nullable IgniteObjectBuilder builder); + + /** + * Removes field from this builder. + * + * @param fieldName Field name. + * @return {@code this} instance for chaining. + */ + public IgniteObjectBuilder removeField(String fieldName); + + /** + * Sets hash code for resulting portable object returned by {@link #build()} method. + * <p> + * If not set {@code 0} is used. + * + * @param hashCode Hash code. + * @return {@code this} instance for chaining. + */ + public IgniteObjectBuilder hashCode(int hashCode); + + /** + * Builds portable object. + * + * @return Portable object. + * @throws IgniteObjectException In case of error. + */ + public IgniteObject build() throws IgniteObjectException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectConfiguration.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectConfiguration.java new file mode 100644 index 0000000..20a4dec --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectConfiguration.java @@ -0,0 +1,155 @@ +/* + * 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.igniteobject; + +import java.util.Collection; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.marshaller.portable.PortableMarshaller; + +/** + * Defines configuration properties for a specific portable type. Providing per-type + * configuration is optional, as it is generally enough, and also optional, to provide global portable + * configuration using {@link PortableMarshaller#setClassNames(Collection)}. + * However, this class allows you to change configuration properties for a specific + * portable type without affecting configuration for other portable types. + * <p> + * Per-type portable configuration can be specified in {@link PortableMarshaller#getTypeConfigurations()} method. + */ +public class IgniteObjectConfiguration { + /** Class name. */ + private String clsName; + + /** ID mapper. */ + private IgniteObjectIdMapper idMapper; + + /** Serializer. */ + private IgniteObjectSerializer serializer; + + /** Meta data enabled flag. */ + private Boolean metaDataEnabled; + + /** Keep deserialized flag. */ + private Boolean keepDeserialized; + + /** + */ + public IgniteObjectConfiguration() { + // No-op. + } + + /** + * @param clsName Class name. + */ + public IgniteObjectConfiguration(String clsName) { + this.clsName = clsName; + } + + /** + * Gets type name. + * + * @return Type name. + */ + public String getClassName() { + return clsName; + } + + /** + * Sets type name. + * + * @param clsName Type name. + */ + public void setClassName(String clsName) { + this.clsName = clsName; + } + + /** + * Gets ID mapper. + * + * @return ID mapper. + */ + public IgniteObjectIdMapper getIdMapper() { + return idMapper; + } + + /** + * Sets ID mapper. + * + * @param idMapper ID mapper. + */ + public void setIdMapper(IgniteObjectIdMapper idMapper) { + this.idMapper = idMapper; + } + + /** + * Gets serializer. + * + * @return Serializer. + */ + public IgniteObjectSerializer getSerializer() { + return serializer; + } + + /** + * Sets serializer. + * + * @param serializer Serializer. + */ + public void setSerializer(IgniteObjectSerializer serializer) { + this.serializer = serializer; + } + + /** + * Defines whether meta data is collected for this type. If provided, this value will override + * {@link PortableMarshaller#isMetaDataEnabled()} property. + * + * @return Whether meta data is collected. + */ + public Boolean isMetaDataEnabled() { + return metaDataEnabled; + } + + /** + * @param metaDataEnabled Whether meta data is collected. + */ + public void setMetaDataEnabled(Boolean metaDataEnabled) { + this.metaDataEnabled = metaDataEnabled; + } + + /** + * Defines whether {@link IgniteObject} should cache deserialized instance. If provided, + * this value will override {@link PortableMarshaller#isKeepDeserialized()} + * property. + * + * @return Whether deserialized value is kept. + */ + public Boolean isKeepDeserialized() { + return keepDeserialized; + } + + /** + * @param keepDeserialized Whether deserialized value is kept. + */ + public void setKeepDeserialized(Boolean keepDeserialized) { + this.keepDeserialized = keepDeserialized; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IgniteObjectConfiguration.class, this, super.toString()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectException.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectException.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectException.java new file mode 100644 index 0000000..c86c17f --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectException.java @@ -0,0 +1,57 @@ +/* + * 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.igniteobject; + +import org.apache.ignite.IgniteException; +import org.jetbrains.annotations.Nullable; + +/** + * Exception indicating portable object serialization error. + */ +public class IgniteObjectException extends IgniteException { + /** */ + private static final long serialVersionUID = 0L; + + /** + * Creates portable exception with error message. + * + * @param msg Error message. + */ + public IgniteObjectException(String msg) { + super(msg); + } + + /** + * Creates portable exception with {@link Throwable} as a cause. + * + * @param cause Cause. + */ + public IgniteObjectException(Throwable cause) { + super(cause); + } + + /** + * Creates portable exception with error message and {@link Throwable} as a cause. + * + * @param msg Error message. + * @param cause Cause. + */ + public IgniteObjectException(String msg, @Nullable Throwable cause) { + super(msg, cause); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectIdMapper.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectIdMapper.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectIdMapper.java new file mode 100644 index 0000000..ea11824 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectIdMapper.java @@ -0,0 +1,56 @@ +/* + * 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.igniteobject; + +import org.apache.ignite.marshaller.portable.PortableMarshaller; + +/** + * Type and field ID mapper for portable objects. Ignite never writes full + * strings for field or type names. Instead, for performance reasons, Ignite + * writes integer hash codes for type and field names. It has been tested that + * hash code conflicts for the type names or the field names + * within the same type are virtually non-existent and, to gain performance, it is safe + * to work with hash codes. For the cases when hash codes for different types or fields + * actually do collide {@code PortableIdMapper} allows to override the automatically + * generated hash code IDs for the type and field names. + * <p> + * Portable ID mapper can be configured for all portable objects via {@link PortableMarshaller#getIdMapper()} method, + * or for a specific portable type via {@link IgniteObjectConfiguration#getIdMapper()} method. + */ +public interface IgniteObjectIdMapper { + /** + * Gets type ID for provided class name. + * <p> + * If {@code 0} is returned, hash code of class simple name will be used. + * + * @param clsName Class name. + * @return Type ID. + */ + public int typeId(String clsName); + + /** + * Gets ID for provided field. + * <p> + * If {@code 0} is returned, hash code of field name will be used. + * + * @param typeId Type ID. + * @param fieldName Field name. + * @return Field ID. + */ + public int fieldId(int typeId, String fieldName); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectInvalidClassException.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectInvalidClassException.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectInvalidClassException.java new file mode 100644 index 0000000..8b2b223 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectInvalidClassException.java @@ -0,0 +1,58 @@ +/* + * 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.igniteobject; + +import org.jetbrains.annotations.Nullable; + +/** + * Exception indicating that class needed for deserialization of portable object does not exist. + * <p> + * Thrown from {@link IgniteObject#deserialize()} method. + */ +public class IgniteObjectInvalidClassException extends IgniteObjectException { + /** */ + private static final long serialVersionUID = 0L; + + /** + * Creates invalid class exception with error message. + * + * @param msg Error message. + */ + public IgniteObjectInvalidClassException(String msg) { + super(msg); + } + + /** + * Creates invalid class exception with {@link Throwable} as a cause. + * + * @param cause Cause. + */ + public IgniteObjectInvalidClassException(Throwable cause) { + super(cause); + } + + /** + * Creates invalid class exception with error message and {@link Throwable} as a cause. + * + * @param msg Error message. + * @param cause Cause. + */ + public IgniteObjectInvalidClassException(String msg, @Nullable Throwable cause) { + super(msg, cause); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMarshalAware.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMarshalAware.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMarshalAware.java new file mode 100644 index 0000000..6670431 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMarshalAware.java @@ -0,0 +1,48 @@ +/* + * 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.igniteobject; + +/** + * Interface that allows to implement custom serialization + * logic for portable objects. IgniteObject is not required + * to implement this interface, in which case Ignite will automatically + * serialize portable objects using reflection. + * <p> + * This interface, in a way, is analogous to {@link java.io.Externalizable} + * interface, which allows users to override default serialization logic, + * usually for performance reasons. The only difference here is that portable + * serialization is already very fast and implementing custom serialization + * logic for portables does not provide significant performance gains. + */ +public interface IgniteObjectMarshalAware { + /** + * Writes fields to provided writer. + * + * @param writer Portable object writer. + * @throws IgniteObjectException In case of error. + */ + public void writePortable(IgniteObjectWriter writer) throws IgniteObjectException; + + /** + * Reads fields from provided reader. + * + * @param reader Portable object reader. + * @throws IgniteObjectException In case of error. + */ + public void readPortable(IgniteObjectReader reader) throws IgniteObjectException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMetadata.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMetadata.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMetadata.java new file mode 100644 index 0000000..943d774 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectMetadata.java @@ -0,0 +1,60 @@ +/* + * 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.igniteobject; + +import java.util.Collection; +import org.jetbrains.annotations.Nullable; + +/** + * Portable type meta data. Metadata for portable types can be accessed from any of the + * {@link org.apache.ignite.IgniteObjects#metadata(String)} methods. + * Having metadata also allows for proper formatting of {@code PortableObject#toString()} method, + * even when portable objects are kept in binary format only, which may be necessary for audit reasons. + */ +public interface IgniteObjectMetadata { + /** + * Gets portable type name. + * + * @return Portable type name. + */ + public String typeName(); + + /** + * Gets collection of all field names for this portable type. + * + * @return Collection of all field names for this portable type. + */ + public Collection<String> fields(); + + /** + * Gets name of the field type for a given field. + * + * @param fieldName Field name. + * @return Field type name. + */ + @Nullable public String fieldTypeName(String fieldName); + + /** + * Portable objects can optionally specify custom key-affinity mapping in the + * configuration. This method returns the name of the field which should be + * used for the key-affinity mapping. + * + * @return Affinity key field name. + */ + @Nullable public String affinityKeyFieldName(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawReader.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawReader.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawReader.java new file mode 100644 index 0000000..e908900 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawReader.java @@ -0,0 +1,240 @@ +/* + * 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.igniteobject; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.UUID; +import org.jetbrains.annotations.Nullable; + +/** + * Raw reader for portable objects. Raw reader does not use field name hash codes, therefore, + * making the format even more compact. However, if the raw reader is used, + * dynamic structure changes to the portable objects are not supported. + */ +public interface IgniteObjectRawReader { + /** + * @return Byte value. + * @throws IgniteObjectException In case of error. + */ + public byte readByte() throws IgniteObjectException; + + /** + * @return Short value. + * @throws IgniteObjectException In case of error. + */ + public short readShort() throws IgniteObjectException; + + /** + * @return Integer value. + * @throws IgniteObjectException In case of error. + */ + public int readInt() throws IgniteObjectException; + + /** + * @return Long value. + * @throws IgniteObjectException In case of error. + */ + public long readLong() throws IgniteObjectException; + + /** + * @return Float value. + * @throws IgniteObjectException In case of error. + */ + public float readFloat() throws IgniteObjectException; + + /** + * @return Double value. + * @throws IgniteObjectException In case of error. + */ + public double readDouble() throws IgniteObjectException; + + /** + * @return Char value. + * @throws IgniteObjectException In case of error. + */ + public char readChar() throws IgniteObjectException; + + /** + * @return Boolean value. + * @throws IgniteObjectException In case of error. + */ + public boolean readBoolean() throws IgniteObjectException; + + /** + * @return Decimal value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public BigDecimal readDecimal() throws IgniteObjectException; + + /** + * @return String value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public String readString() throws IgniteObjectException; + + /** + * @return UUID. + * @throws IgniteObjectException In case of error. + */ + @Nullable public UUID readUuid() throws IgniteObjectException; + + /** + * @return Date. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Date readDate() throws IgniteObjectException; + + /** + * @return Timestamp. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Timestamp readTimestamp() throws IgniteObjectException; + + /** + * @return Object. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> T readObject() throws IgniteObjectException; + + /** + * @return Byte array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public byte[] readByteArray() throws IgniteObjectException; + + /** + * @return Short array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public short[] readShortArray() throws IgniteObjectException; + + /** + * @return Integer array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public int[] readIntArray() throws IgniteObjectException; + + /** + * @return Long array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public long[] readLongArray() throws IgniteObjectException; + + /** + * @return Float array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public float[] readFloatArray() throws IgniteObjectException; + + /** + * @return Byte array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public double[] readDoubleArray() throws IgniteObjectException; + + /** + * @return Char array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public char[] readCharArray() throws IgniteObjectException; + + /** + * @return Boolean array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public boolean[] readBooleanArray() throws IgniteObjectException; + + /** + * @return Decimal array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public BigDecimal[] readDecimalArray() throws IgniteObjectException; + + /** + * @return String array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public String[] readStringArray() throws IgniteObjectException; + + /** + * @return UUID array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public UUID[] readUuidArray() throws IgniteObjectException; + + /** + * @return Date array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Date[] readDateArray() throws IgniteObjectException; + + /** + * @return Timestamp array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Timestamp[] readTimestampArray() throws IgniteObjectException; + + /** + * @return Object array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Object[] readObjectArray() throws IgniteObjectException; + + /** + * @return Collection. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> Collection<T> readCollection() throws IgniteObjectException; + + /** + * @param colCls Collection class. + * @return Collection. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> Collection<T> readCollection(Class<? extends Collection<T>> colCls) + throws IgniteObjectException; + + /** + * @return Map. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <K, V> Map<K, V> readMap() throws IgniteObjectException; + + /** + * @param mapCls Map class. + * @return Map. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <K, V> Map<K, V> readMap(Class<? extends Map<K, V>> mapCls) throws IgniteObjectException; + + /** + * @return Value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T extends Enum<?>> T readEnum() throws IgniteObjectException; + + /** + * @return Value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T extends Enum<?>> T[] readEnumArray() throws IgniteObjectException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawWriter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawWriter.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawWriter.java new file mode 100644 index 0000000..a1bfd83 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectRawWriter.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.igniteobject; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.UUID; +import org.jetbrains.annotations.Nullable; + +/** + * Raw writer for portable object. Raw writer does not write field name hash codes, therefore, + * making the format even more compact. However, if the raw writer is used, + * dynamic structure changes to the portable objects are not supported. + */ +public interface IgniteObjectRawWriter { + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeByte(byte val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeShort(short val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeInt(int val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeLong(long val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeFloat(float val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDouble(double val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeChar(char val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeBoolean(boolean val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDecimal(@Nullable BigDecimal val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeString(@Nullable String val) throws IgniteObjectException; + + /** + * @param val UUID to write. + * @throws IgniteObjectException In case of error. + */ + public void writeUuid(@Nullable UUID val) throws IgniteObjectException; + + /** + * @param val Date to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDate(@Nullable Date val) throws IgniteObjectException; + + /** + * @param val Timestamp to write. + * @throws IgniteObjectException In case of error. + */ + public void writeTimestamp(@Nullable Timestamp val) throws IgniteObjectException; + + /** + * @param obj Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeObject(@Nullable Object obj) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeByteArray(@Nullable byte[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeShortArray(@Nullable short[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeIntArray(@Nullable int[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeLongArray(@Nullable long[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeFloatArray(@Nullable float[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDoubleArray(@Nullable double[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeCharArray(@Nullable char[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeBooleanArray(@Nullable boolean[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDecimalArray(@Nullable BigDecimal[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeStringArray(@Nullable String[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeUuidArray(@Nullable UUID[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDateArray(@Nullable Date[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeTimestampArray(@Nullable Timestamp[] val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeObjectArray(@Nullable Object[] val) throws IgniteObjectException; + + /** + * @param col Collection to write. + * @throws IgniteObjectException In case of error. + */ + public <T> void writeCollection(@Nullable Collection<T> col) throws IgniteObjectException; + + /** + * @param map Map to write. + * @throws IgniteObjectException In case of error. + */ + public <K, V> void writeMap(@Nullable Map<K, V> map) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public <T extends Enum<?>> void writeEnum(T val) throws IgniteObjectException; + + /** + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public <T extends Enum<?>> void writeEnumArray(T[] val) throws IgniteObjectException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectReader.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectReader.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectReader.java new file mode 100644 index 0000000..bad4473 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectReader.java @@ -0,0 +1,291 @@ +/* + * 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.igniteobject; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.UUID; +import org.jetbrains.annotations.Nullable; + +/** + * Reader for portable objects used in {@link IgniteObjectMarshalAware} implementations. + * Useful for the cases when user wants a fine-grained control over serialization. + * <p> + * Note that Ignite never writes full strings for field or type names. Instead, + * for performance reasons, Ignite writes integer hash codes for type and field names. + * It has been tested that hash code conflicts for the type names or the field names + * within the same type are virtually non-existent and, to gain performance, it is safe + * to work with hash codes. For the cases when hash codes for different types or fields + * actually do collide, Ignite provides {@link IgniteObjectIdMapper} which + * allows to override the automatically generated hash code IDs for the type and field names. + */ +public interface IgniteObjectReader { + /** + * @param fieldName Field name. + * @return Byte value. + * @throws IgniteObjectException In case of error. + */ + public byte readByte(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Short value. + * @throws IgniteObjectException In case of error. + */ + public short readShort(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Integer value. + * @throws IgniteObjectException In case of error. + */ + public int readInt(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Long value. + * @throws IgniteObjectException In case of error. + */ + public long readLong(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @throws IgniteObjectException In case of error. + * @return Float value. + */ + public float readFloat(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Double value. + * @throws IgniteObjectException In case of error. + */ + public double readDouble(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Char value. + * @throws IgniteObjectException In case of error. + */ + public char readChar(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Boolean value. + * @throws IgniteObjectException In case of error. + */ + public boolean readBoolean(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Decimal value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public BigDecimal readDecimal(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return String value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public String readString(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return UUID. + * @throws IgniteObjectException In case of error. + */ + @Nullable public UUID readUuid(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Date. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Date readDate(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Timestamp. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Timestamp readTimestamp(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Object. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> T readObject(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Byte array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public byte[] readByteArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Short array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public short[] readShortArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Integer array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public int[] readIntArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Long array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public long[] readLongArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Float array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public float[] readFloatArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Byte array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public double[] readDoubleArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Char array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public char[] readCharArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Boolean array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public boolean[] readBooleanArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Decimal array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public BigDecimal[] readDecimalArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return String array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public String[] readStringArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return UUID array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public UUID[] readUuidArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Date array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Date[] readDateArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Timestamp array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Timestamp[] readTimestampArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Object array. + * @throws IgniteObjectException In case of error. + */ + @Nullable public Object[] readObjectArray(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Collection. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> Collection<T> readCollection(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param colCls Collection class. + * @return Collection. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T> Collection<T> readCollection(String fieldName, Class<? extends Collection<T>> colCls) + throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Map. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <K, V> Map<K, V> readMap(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param mapCls Map class. + * @return Map. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <K, V> Map<K, V> readMap(String fieldName, Class<? extends Map<K, V>> mapCls) + throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T extends Enum<?>> T readEnum(String fieldName) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @return Value. + * @throws IgniteObjectException In case of error. + */ + @Nullable public <T extends Enum<?>> T[] readEnumArray(String fieldName) throws IgniteObjectException; + + /** + * Gets raw reader. Raw reader does not use field name hash codes, therefore, + * making the format even more compact. However, if the raw reader is used, + * dynamic structure changes to the portable objects are not supported. + * + * @return Raw reader. + */ + public IgniteObjectRawReader rawReader(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectSerializer.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectSerializer.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectSerializer.java new file mode 100644 index 0000000..2ac3d0e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectSerializer.java @@ -0,0 +1,49 @@ +/* + * 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.igniteobject; + +import org.apache.ignite.marshaller.portable.PortableMarshaller; + +/** + * Interface that allows to implement custom serialization logic for portable objects. + * Can be used instead of {@link IgniteObjectMarshalAware} in case if the class + * cannot be changed directly. + * <p> + * Portable serializer can be configured for all portable objects via + * {@link PortableMarshaller#getSerializer()} method, or for a specific + * portable type via {@link IgniteObjectConfiguration#getSerializer()} method. + */ +public interface IgniteObjectSerializer { + /** + * Writes fields to provided writer. + * + * @param obj Empty object. + * @param writer Portable object writer. + * @throws IgniteObjectException In case of error. + */ + public void writePortable(Object obj, IgniteObjectWriter writer) throws IgniteObjectException; + + /** + * Reads fields from provided reader. + * + * @param obj Empty object + * @param reader Portable object reader. + * @throws IgniteObjectException In case of error. + */ + public void readPortable(Object obj, IgniteObjectReader reader) throws IgniteObjectException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectWriter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectWriter.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectWriter.java new file mode 100644 index 0000000..3409f07 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/IgniteObjectWriter.java @@ -0,0 +1,273 @@ +/* + * 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.igniteobject; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.UUID; +import org.jetbrains.annotations.Nullable; + +/** + * Writer for portable object used in {@link IgniteObjectMarshalAware} implementations. + * Useful for the cases when user wants a fine-grained control over serialization. + * <p> + * Note that Ignite never writes full strings for field or type names. Instead, + * for performance reasons, Ignite writes integer hash codes for type and field names. + * It has been tested that hash code conflicts for the type names or the field names + * within the same type are virtually non-existent and, to gain performance, it is safe + * to work with hash codes. For the cases when hash codes for different types or fields + * actually do collide, Ignite provides {@link IgniteObjectIdMapper} which + * allows to override the automatically generated hash code IDs for the type and field names. + */ +public interface IgniteObjectWriter { + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeByte(String fieldName, byte val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeShort(String fieldName, short val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeInt(String fieldName, int val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeLong(String fieldName, long val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeFloat(String fieldName, float val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDouble(String fieldName, double val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeChar(String fieldName, char val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeBoolean(String fieldName, boolean val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDecimal(String fieldName, @Nullable BigDecimal val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeString(String fieldName, @Nullable String val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val UUID to write. + * @throws IgniteObjectException In case of error. + */ + public void writeUuid(String fieldName, @Nullable UUID val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Date to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDate(String fieldName, @Nullable Date val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Timestamp to write. + * @throws IgniteObjectException In case of error. + */ + public void writeTimestamp(String fieldName, @Nullable Timestamp val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param obj Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeObject(String fieldName, @Nullable Object obj) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeByteArray(String fieldName, @Nullable byte[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeShortArray(String fieldName, @Nullable short[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeIntArray(String fieldName, @Nullable int[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeLongArray(String fieldName, @Nullable long[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeFloatArray(String fieldName, @Nullable float[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDoubleArray(String fieldName, @Nullable double[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeCharArray(String fieldName, @Nullable char[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeBooleanArray(String fieldName, @Nullable boolean[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDecimalArray(String fieldName, @Nullable BigDecimal[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeStringArray(String fieldName, @Nullable String[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeUuidArray(String fieldName, @Nullable UUID[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeDateArray(String fieldName, @Nullable Date[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeTimestampArray(String fieldName, @Nullable Timestamp[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public void writeObjectArray(String fieldName, @Nullable Object[] val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param col Collection to write. + * @throws IgniteObjectException In case of error. + */ + public <T> void writeCollection(String fieldName, @Nullable Collection<T> col) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param map Map to write. + * @throws IgniteObjectException In case of error. + */ + public <K, V> void writeMap(String fieldName, @Nullable Map<K, V> map) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public <T extends Enum<?>> void writeEnum(String fieldName, T val) throws IgniteObjectException; + + /** + * @param fieldName Field name. + * @param val Value to write. + * @throws IgniteObjectException In case of error. + */ + public <T extends Enum<?>> void writeEnumArray(String fieldName, T[] val) throws IgniteObjectException; + + /** + * Gets raw writer. Raw writer does not write field name hash codes, therefore, + * making the format even more compact. However, if the raw writer is used, + * dynamic structure changes to the portable objects are not supported. + * + * @return Raw writer. + */ + public IgniteObjectRawWriter rawWriter(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/igniteobject/package-info.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/igniteobject/package-info.java b/modules/core/src/main/java/org/apache/ignite/igniteobject/package-info.java new file mode 100644 index 0000000..abc305e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/igniteobject/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 portable objects API classes. + */ +package org.apache.ignite.igniteobject; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java index 6cddb47..f2e98c3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java @@ -32,7 +32,7 @@ import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteFileSystem; import org.apache.ignite.IgniteLogger; import org.apache.ignite.IgniteMessaging; -import org.apache.ignite.IgnitePortables; +import org.apache.ignite.IgniteObjects; import org.apache.ignite.IgniteQueue; import org.apache.ignite.IgniteScheduler; import org.apache.ignite.IgniteServices; @@ -125,7 +125,6 @@ import org.apache.ignite.lifecycle.LifecycleBean; import org.apache.ignite.lifecycle.LifecycleEventType; import org.apache.ignite.marshaller.MarshallerExclusions; import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; -import org.apache.ignite.marshaller.portable.PortableMarshaller; import org.apache.ignite.mxbean.ClusterLocalNodeMetricsMXBean; import org.apache.ignite.mxbean.IgniteMXBean; import org.apache.ignite.mxbean.ThreadPoolMXBean; @@ -2765,7 +2764,7 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public IgnitePortables portables() { + @Override public IgniteObjects portables() { return ((CacheObjectPortableProcessor)ctx.cacheObjects()).portables(); } http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java index 079015c..61fa93a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java @@ -31,7 +31,7 @@ import org.apache.ignite.internal.managers.deployment.GridDeploymentInfoBean; import org.apache.ignite.internal.managers.deployment.GridDeploymentRequest; import org.apache.ignite.internal.managers.deployment.GridDeploymentResponse; import org.apache.ignite.internal.managers.eventstorage.GridEventStorageMessage; -import org.apache.ignite.internal.portable.PortableObjectImpl; +import org.apache.ignite.internal.portable.IgniteObjectImpl; import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; import org.apache.ignite.internal.processors.cache.CacheEntryInfoCollection; import org.apache.ignite.internal.processors.cache.CacheEntryPredicateContainsValue; @@ -680,7 +680,7 @@ public class GridIoMessageFactory implements MessageFactory { break; case 113: - msg = new PortableObjectImpl(); + msg = new IgniteObjectImpl(); break; http://git-wip-us.apache.org/repos/asf/ignite/blob/35b6d61f/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java index 6f16755..2bd121c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java @@ -19,7 +19,7 @@ package org.apache.ignite.internal.portable; import org.apache.ignite.internal.portable.streams.PortableInputStream; import org.apache.ignite.internal.portable.streams.PortableOutputStream; -import org.apache.ignite.portable.PortableException; +import org.apache.ignite.igniteobject.IgniteObjectException; import org.jetbrains.annotations.Nullable; /** @@ -230,13 +230,13 @@ public class GridPortableMarshaller { * @param obj Object to marshal. * @param off Offset. * @return Byte array. - * @throws PortableException In case of error. + * @throws org.apache.ignite.igniteobject.IgniteObjectException In case of error. */ - public byte[] marshal(@Nullable Object obj, int off) throws PortableException { + public byte[] marshal(@Nullable Object obj, int off) throws IgniteObjectException { if (obj == null) return new byte[] { NULL }; - try (PortableWriterExImpl writer = new PortableWriterExImpl(ctx, off)) { + try (IgniteObjectWriterExImpl writer = new IgniteObjectWriterExImpl(ctx, off)) { writer.marshal(obj, false); return writer.array(); @@ -246,13 +246,13 @@ public class GridPortableMarshaller { /** * @param bytes Bytes array. * @return Portable object. - * @throws PortableException In case of error. + * @throws org.apache.ignite.igniteobject.IgniteObjectException In case of error. */ @SuppressWarnings("unchecked") - @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader clsLdr) throws PortableException { + @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader clsLdr) throws IgniteObjectException { assert bytes != null; - PortableReaderExImpl reader = new PortableReaderExImpl(ctx, bytes, 0, clsLdr); + IgniteObjectReaderExImpl reader = new IgniteObjectReaderExImpl(ctx, bytes, 0, clsLdr); return (T)reader.unmarshal(); } @@ -260,10 +260,10 @@ public class GridPortableMarshaller { /** * @param in Input stream. * @return Portable object. - * @throws PortableException In case of error. + * @throws org.apache.ignite.igniteobject.IgniteObjectException In case of error. */ @SuppressWarnings("unchecked") - @Nullable public <T> T unmarshal(PortableInputStream in) throws PortableException { + @Nullable public <T> T unmarshal(PortableInputStream in) throws IgniteObjectException { return (T)reader(in).unmarshal(); } @@ -271,17 +271,17 @@ public class GridPortableMarshaller { * @param arr Byte array. * @param ldr Class loader. * @return Deserialized object. - * @throws PortableException In case of error. + * @throws org.apache.ignite.igniteobject.IgniteObjectException In case of error. */ @SuppressWarnings("unchecked") - @Nullable public <T> T deserialize(byte[] arr, @Nullable ClassLoader ldr) throws PortableException { + @Nullable public <T> T deserialize(byte[] arr, @Nullable ClassLoader ldr) throws IgniteObjectException { assert arr != null; assert arr.length > 0; if (arr[0] == NULL) return null; - PortableReaderExImpl reader = new PortableReaderExImpl(ctx, arr, 0, ldr); + IgniteObjectReaderExImpl reader = new IgniteObjectReaderExImpl(ctx, arr, 0, ldr); return (T)reader.deserialize(); } @@ -292,8 +292,8 @@ public class GridPortableMarshaller { * @param out Output stream. * @return Writer. */ - public PortableWriterExImpl writer(PortableOutputStream out) { - return new PortableWriterExImpl(ctx, out, 0); + public IgniteObjectWriterExImpl writer(PortableOutputStream out) { + return new IgniteObjectWriterExImpl(ctx, out, 0); } /** @@ -302,9 +302,9 @@ public class GridPortableMarshaller { * @param in Input stream. * @return Reader. */ - public PortableReaderExImpl reader(PortableInputStream in) { + public IgniteObjectReaderExImpl reader(PortableInputStream in) { // TODO: IGNITE-1272 - Is class loader needed here? - return new PortableReaderExImpl(ctx, in, in.position(), null); + return new IgniteObjectReaderExImpl(ctx, in, in.position(), null); } /**
