# ignite-51
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/7719749c Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/7719749c Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/7719749c Branch: refs/heads/ignite-410 Commit: 7719749c1338421d11b065f3c7f1c8d984d6060d Parents: 33a4bee Author: sboikov <sboi...@gridgain.com> Authored: Tue Mar 10 13:05:33 2015 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Tue Mar 10 13:07:54 2015 +0300 ---------------------------------------------------------------------- .../ignite/internal/GridKernalContext.java | 2 +- .../ignite/internal/GridKernalContextImpl.java | 2 +- .../apache/ignite/internal/IgniteKernal.java | 2 +- .../processors/cache/CacheObjectContext.java | 2 +- .../processors/cache/GridCacheContext.java | 2 +- .../cacheobject/IgniteCacheObjectProcessor.java | 160 +++++++++ .../IgniteCacheObjectProcessorImpl.java | 348 +++++++++++++++++++ .../processors/cacheobject/package.html | 24 ++ .../dataload/IgniteDataLoaderImpl.java | 2 +- .../portable/IgniteCacheObjectProcessor.java | 161 --------- .../IgniteCacheObjectProcessorImpl.java | 348 ------------------- 11 files changed, 538 insertions(+), 515 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContext.java b/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContext.java index d0bd35d..fcc7598 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContext.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContext.java @@ -31,6 +31,7 @@ import org.apache.ignite.internal.managers.loadbalancer.*; import org.apache.ignite.internal.managers.swapspace.*; import org.apache.ignite.internal.processors.affinity.*; import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.internal.processors.cacheobject.*; import org.apache.ignite.internal.processors.clock.*; import org.apache.ignite.internal.processors.closure.*; import org.apache.ignite.internal.processors.cluster.*; @@ -44,7 +45,6 @@ import org.apache.ignite.internal.processors.jobmetrics.*; import org.apache.ignite.internal.processors.offheap.*; import org.apache.ignite.internal.processors.plugin.*; import org.apache.ignite.internal.processors.port.*; -import org.apache.ignite.internal.processors.portable.*; import org.apache.ignite.internal.processors.query.*; import org.apache.ignite.internal.processors.resource.*; import org.apache.ignite.internal.processors.rest.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContextImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContextImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContextImpl.java index 2e0204d..ec2be92 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContextImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/GridKernalContextImpl.java @@ -33,6 +33,7 @@ import org.apache.ignite.internal.processors.affinity.*; import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.processors.cache.dr.*; import org.apache.ignite.internal.processors.cache.dr.os.*; +import org.apache.ignite.internal.processors.cacheobject.*; import org.apache.ignite.internal.processors.clock.*; import org.apache.ignite.internal.processors.closure.*; import org.apache.ignite.internal.processors.cluster.*; @@ -46,7 +47,6 @@ import org.apache.ignite.internal.processors.jobmetrics.*; import org.apache.ignite.internal.processors.offheap.*; import org.apache.ignite.internal.processors.plugin.*; import org.apache.ignite.internal.processors.port.*; -import org.apache.ignite.internal.processors.portable.*; import org.apache.ignite.internal.processors.query.*; import org.apache.ignite.internal.processors.resource.*; import org.apache.ignite.internal.processors.rest.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/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 3f50133..eb844a8 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 @@ -37,6 +37,7 @@ import org.apache.ignite.internal.managers.swapspace.*; import org.apache.ignite.internal.processors.*; import org.apache.ignite.internal.processors.affinity.*; import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.internal.processors.cacheobject.*; import org.apache.ignite.internal.processors.clock.*; import org.apache.ignite.internal.processors.closure.*; import org.apache.ignite.internal.processors.cluster.*; @@ -49,7 +50,6 @@ import org.apache.ignite.internal.processors.jobmetrics.*; import org.apache.ignite.internal.processors.offheap.*; import org.apache.ignite.internal.processors.plugin.*; import org.apache.ignite.internal.processors.port.*; -import org.apache.ignite.internal.processors.portable.*; import org.apache.ignite.internal.processors.query.*; import org.apache.ignite.internal.processors.resource.*; import org.apache.ignite.internal.processors.rest.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java index 9351206..9a05b0a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java @@ -19,7 +19,7 @@ package org.apache.ignite.internal.processors.cache; import org.apache.ignite.cache.affinity.*; import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.portable.*; +import org.apache.ignite.internal.processors.cacheobject.*; import java.util.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java index c1f3b9e..51098b2 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java @@ -39,9 +39,9 @@ import org.apache.ignite.internal.processors.cache.query.*; import org.apache.ignite.internal.processors.cache.query.continuous.*; import org.apache.ignite.internal.processors.cache.transactions.*; import org.apache.ignite.internal.processors.cache.version.*; +import org.apache.ignite.internal.processors.cacheobject.*; import org.apache.ignite.internal.processors.closure.*; import org.apache.ignite.internal.processors.offheap.*; -import org.apache.ignite.internal.processors.portable.*; import org.apache.ignite.internal.processors.timeout.*; import org.apache.ignite.internal.util.*; import org.apache.ignite.internal.util.lang.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java new file mode 100644 index 0000000..ce2c579 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java @@ -0,0 +1,160 @@ +/* + * 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.processors.cacheobject; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.internal.processors.*; +import org.apache.ignite.internal.processors.cache.*; +import org.jetbrains.annotations.*; + +/** + * Cache objects processor. + */ +public interface IgniteCacheObjectProcessor extends GridProcessor { + /** {@inheritDoc} */ + public void onCacheProcessorStarted(); + + /** + * @param typeName Type name. + * @return Type ID. + */ + public int typeId(String typeName); + + /** + * @param obj Object to get type ID for. + * @return Type ID. + */ + public int typeId(Object obj); + + /** + * Converts temporary off-heap object to heap-based. + * + * @param ctx Context. + * @param obj Object. + * @return Heap-based object. + * @throws IgniteException In case of error. + */ + @Nullable public Object unwrapTemporary(GridCacheContext ctx, @Nullable Object obj) throws IgniteException; + + /** + * Prepares cache object for cache (e.g. copies user-provided object if needed). + * + * @param obj Cache object. + * @param cctx Cache context. + * @return Object to be store in cache. + */ + @Nullable public CacheObject prepareForCache(@Nullable CacheObject obj, GridCacheContext cctx); + + /** + * Checks whether object is portable object. + * + * @param obj Object to check. + * @return {@code True} if object is already a portable object, {@code false} otherwise. + */ + public boolean isPortableObject(Object obj); + + /** + * @param obj Portable object to get field from. + * @param fieldName Field name. + * @return Field value. + */ + public Object field(Object obj, String fieldName); + + /** + * Checks whether field is set in the object. + * + * @param obj Object. + * @param fieldName Field name. + * @return {@code true} if field is set. + */ + public boolean hasField(Object obj, String fieldName); + + /** + * @param ctx Cache object context. + * @param val Value. + * @return Value bytes. + * @throws IgniteCheckedException If failed. + */ + public byte[] marshal(CacheObjectContext ctx, Object val) throws IgniteCheckedException; + + /** + * @param ctx Context. + * @param bytes Bytes. + * @param clsLdr Class loader. + * @return Unmarshalled object. + * @throws IgniteCheckedException If failed. + */ + public Object unmarshal(CacheObjectContext ctx, byte[] bytes, ClassLoader clsLdr) throws IgniteCheckedException; + + /** + * @param node Node. + * @param cacheName Cache name. + * @return Cache object context. + */ + public CacheObjectContext contextForCache(ClusterNode node, @Nullable String cacheName); + + /** + * @param ctx Cache context. + * @param obj Key value. + * @param userObj If {@code true} then given object is object provided by user and should be copied + * before stored in cache. + * @return Cache key object. + */ + public KeyCacheObject toCacheKeyObject(CacheObjectContext ctx, Object obj, boolean userObj); + + /** + * @param ctx Cache context. + * @param obj Object. + * @param userObj If {@code true} then given object is object provided by user and should be copied + * before stored in cache. + * @return Cache object. + */ + @Nullable public CacheObject toCacheObject(CacheObjectContext ctx, @Nullable Object obj, boolean userObj); + + /** + * @param ctx Cache context. + * @param type Object type. + * @param bytes Object bytes. + * @return Cache object. + */ + public CacheObject toCacheObject(CacheObjectContext ctx, byte type, byte[] bytes); + + /** + * @param ctx Context. + * @param valPtr Value pointer. + * @param tmp If {@code true} can return temporary instance which is valid while entry lock is held. + * @return Cache object. + * @throws IgniteCheckedException If failed. + */ + public CacheObject toCacheObject(GridCacheContext ctx, long valPtr, boolean tmp) throws IgniteCheckedException; + + /** + * @param obj Value. + * @return {@code True} if object is of known immutable type of it is marked + * with {@link IgniteImmutable} annotation. + */ + public boolean immutable(Object obj); + + /** + * @param cacheName Cache name. + * @return {@code True} if portable format should be preserved when passing values to cache store. + */ + public boolean keepPortableInStore(@Nullable String cacheName); +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessorImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessorImpl.java new file mode 100644 index 0000000..8f9ce16 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessorImpl.java @@ -0,0 +1,348 @@ +/* + * 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.processors.cacheobject; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.processors.*; +import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.internal.processors.cacheobject.*; +import org.apache.ignite.internal.util.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; +import org.jetbrains.annotations.*; + +import java.math.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheMemoryMode.*; + +/** + * + */ +public class IgniteCacheObjectProcessorImpl extends GridProcessorAdapter implements IgniteCacheObjectProcessor { + /** */ + private static final sun.misc.Unsafe UNSAFE = GridUnsafe.unsafe(); + + /** Immutable classes. */ + private static final Collection<Class<?>> IMMUTABLE_CLS = new HashSet<>(); + + /** */ + private final GridBoundedConcurrentLinkedHashMap<Class<?>, Boolean> reflectionCache = + new GridBoundedConcurrentLinkedHashMap<>(1024, 1024); + + /** + * + */ + static { + IMMUTABLE_CLS.add(String.class); + IMMUTABLE_CLS.add(Boolean.class); + IMMUTABLE_CLS.add(Byte.class); + IMMUTABLE_CLS.add(Short.class); + IMMUTABLE_CLS.add(Character.class); + IMMUTABLE_CLS.add(Integer.class); + IMMUTABLE_CLS.add(Long.class); + IMMUTABLE_CLS.add(Float.class); + IMMUTABLE_CLS.add(Double.class); + IMMUTABLE_CLS.add(UUID.class); + IMMUTABLE_CLS.add(IgniteUuid.class); + IMMUTABLE_CLS.add(BigDecimal.class); + } + + /** + * @param ctx Context. + */ + public IgniteCacheObjectProcessorImpl(GridKernalContext ctx) { + super(ctx); + } + + /** {@inheritDoc} */ + @Nullable @Override public CacheObject prepareForCache(@Nullable CacheObject obj, GridCacheContext cctx) { + if (obj == null) + return null; + + return obj.prepareForCache(cctx.cacheObjectContext()); + } + + /** {@inheritDoc} */ + @Override public byte[] marshal(CacheObjectContext ctx, Object val) throws IgniteCheckedException { + return CU.marshal(ctx.kernalContext().cache().context(), val); + } + + /** {@inheritDoc} */ + @Override public Object unmarshal(CacheObjectContext ctx, byte[] bytes, ClassLoader clsLdr) + throws IgniteCheckedException + { + return ctx.kernalContext().cache().context().marshaller().unmarshal(bytes, clsLdr); + } + + /** {@inheritDoc} */ + @Nullable public KeyCacheObject toCacheKeyObject(CacheObjectContext ctx, Object obj, boolean userObj) { + if (obj instanceof KeyCacheObject) + return (KeyCacheObject)obj; + + return toCacheKeyObject0(obj, userObj); + } + + /** + * @param obj Object. + * @param userObj If {@code true} then given object is object provided by user and should be copied + * before stored in cache. + * @return Key cache object. + */ + @SuppressWarnings("ExternalizableWithoutPublicNoArgConstructor") + protected KeyCacheObject toCacheKeyObject0(Object obj, boolean userObj) { + if (!userObj) + return new KeyCacheObjectImpl(obj, null); + + return new KeyCacheObjectImpl(obj, null) { + @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { + return super.value(ctx, false); // Do not need copy since user value is not in cache. + } + + @Override public CacheObject prepareForCache(CacheObjectContext ctx) { + try { + if (!ctx.processor().immutable(val)) { + if (valBytes == null) + valBytes = ctx.processor().marshal(ctx, val); + + ClassLoader ldr = ctx.p2pEnabled() ? + IgniteUtils.detectClass(this.val).getClassLoader() : val.getClass().getClassLoader(); + + Object val = ctx.processor().unmarshal(ctx, + valBytes, + ldr); + + return new KeyCacheObjectImpl(val, valBytes); + } + + return new KeyCacheObjectImpl(val, valBytes); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to marshal object: " + val, e); + } + } + }; + } + + /** {@inheritDoc} */ + @Override public CacheObject toCacheObject(GridCacheContext ctx, long valPtr, boolean tmp) + throws IgniteCheckedException + { + assert valPtr != 0; + + int size = UNSAFE.getInt(valPtr); + + byte type = UNSAFE.getByte(valPtr + 4); + + byte[] bytes = U.copyMemory(valPtr + 5, size); + + if (ctx.kernalContext().config().isPeerClassLoadingEnabled() && + ctx.offheapTiered() && + type != CacheObjectAdapter.TYPE_BYTE_ARR) { + IgniteUuid valClsLdrId = U.readGridUuid(valPtr + 5 + size); + + ClassLoader ldr = + valClsLdrId != null ? ctx.deploy().getClassLoader(valClsLdrId) : ctx.deploy().localLoader(); + + return toCacheObject(ctx.cacheObjectContext(), unmarshal(ctx.cacheObjectContext(), bytes, ldr), false); + } + else + return toCacheObject(ctx.cacheObjectContext(), type, bytes); + } + + /** {@inheritDoc} */ + @Override public CacheObject toCacheObject(CacheObjectContext ctx, byte type, byte[] bytes) { + switch (type) { + case CacheObjectAdapter.TYPE_BYTE_ARR: + return new CacheObjectByteArrayImpl(bytes); + + case CacheObjectAdapter.TYPE_REGULAR: + return new CacheObjectImpl(null, bytes); + } + + throw new IllegalArgumentException("Invalid object type: " + type); + } + + /** {@inheritDoc} */ + @Nullable @Override public CacheObject toCacheObject(CacheObjectContext ctx, + @Nullable Object obj, + boolean userObj) + { + if (obj == null || obj instanceof CacheObject) + return (CacheObject)obj; + + return toCacheObject0(obj, userObj); + } + + /** + * @param obj Object. + * @param userObj If {@code true} then given object is object provided by user and should be copied + * before stored in cache. + * @return Cache object. + */ + @SuppressWarnings("ExternalizableWithoutPublicNoArgConstructor") + protected CacheObject toCacheObject0(@Nullable Object obj, boolean userObj) { + assert obj != null; + + if (obj instanceof byte[]) { + if (!userObj) + return new CacheObjectByteArrayImpl((byte[])obj); + + return new CacheObjectByteArrayImpl((byte[]) obj) { + @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { + return super.value(ctx, false); // Do not need copy since user value is not in cache. + } + + @Override public CacheObject prepareForCache(CacheObjectContext ctx) { + byte[] valCpy = Arrays.copyOf(val, val.length); + + return new CacheObjectByteArrayImpl(valCpy); + } + }; + } + + if (!userObj) + new CacheObjectImpl(obj, null); + + return new CacheObjectImpl(obj, null) { + @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { + return super.value(ctx, false); // Do not need copy since user value is not in cache. + } + + @Override public CacheObject prepareForCache(CacheObjectContext ctx) { + if (!ctx.processor().immutable(val)) { + try { + if (valBytes == null) + valBytes = ctx.processor().marshal(ctx, val); + + if (ctx.unmarshalValues()) { + ClassLoader ldr = ctx.p2pEnabled() ? + IgniteUtils.detectClass(this.val).getClassLoader() : val.getClass().getClassLoader(); + + Object val = ctx.processor().unmarshal(ctx, valBytes, ldr); + + return new CacheObjectImpl(val, valBytes); + } + + return new CacheObjectImpl(null, valBytes); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to marshal object: " + val, e); + } + } + else + return new CacheObjectImpl(val, valBytes); + } + }; + } + + /** {@inheritDoc} */ + @Override public CacheObjectContext contextForCache(ClusterNode node, @Nullable String cacheName) { + CacheConfiguration ccfg = null; + + for (CacheConfiguration ccfg0 : ctx.config().getCacheConfiguration()) { + if (F.eq(cacheName, ccfg0.getName())) { + ccfg = ccfg0; + + break; + } + } + + if (ccfg != null) { + CacheMemoryMode memMode = ccfg.getMemoryMode(); + + return new CacheObjectContext(ctx, + new GridCacheDefaultAffinityKeyMapper(), + ccfg.isCopyOnGet() && memMode == ONHEAP_TIERED, + ctx.config().isPeerClassLoadingEnabled() || ccfg.isQueryIndexEnabled()); + } + else + return new CacheObjectContext( + ctx, + new GridCacheDefaultAffinityKeyMapper(), + false, + ctx.config().isPeerClassLoadingEnabled()); + } + + /** {@inheritDoc} */ + @Override public boolean immutable(Object obj) { + assert obj != null; + + Class<?> cls = obj.getClass(); + + if (IMMUTABLE_CLS.contains(cls)) + return true; + + Boolean immutable = reflectionCache.get(cls); + + if (immutable != null) + return immutable; + + immutable = IgniteUtils.hasAnnotation(cls, IgniteImmutable.class); + + reflectionCache.putIfAbsent(cls, immutable); + + return immutable; + } + + /** {@inheritDoc} */ + @Override public boolean keepPortableInStore(@Nullable String cacheName) { + return false; + } + + /** {@inheritDoc} */ + @Override public void onCacheProcessorStarted() { + // No-op. + } + + /** {@inheritDoc} */ + @Override public int typeId(String typeName) { + return 0; + } + + + /** {@inheritDoc} */ + @Override public Object unwrapTemporary(GridCacheContext ctx, Object obj) throws IgniteException { + return obj; + } + + /** {@inheritDoc} */ + @Override public boolean isPortableObject(Object obj) { + return false; + } + + /** {@inheritDoc} */ + @Override public int typeId(Object obj) { + return 0; + } + + /** {@inheritDoc} */ + @Override public Object field(Object obj, String fieldName) { + return null; + } + + /** {@inheritDoc} */ + @Override public boolean hasField(Object obj, String fieldName) { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/package.html ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/package.html b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/package.html new file mode 100644 index 0000000..aebebb8 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/package.html @@ -0,0 +1,24 @@ +<!-- + 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. +--> + +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html> +<body> + <!-- Package description. --> + Cache object processor. +</body> +</html> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/dataload/IgniteDataLoaderImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/dataload/IgniteDataLoaderImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/dataload/IgniteDataLoaderImpl.java index 6fd90a8..12d3e35 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/dataload/IgniteDataLoaderImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/dataload/IgniteDataLoaderImpl.java @@ -29,8 +29,8 @@ import org.apache.ignite.internal.processors.affinity.*; import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.processors.cache.distributed.dht.*; import org.apache.ignite.internal.processors.cache.version.*; +import org.apache.ignite.internal.processors.cacheobject.*; import org.apache.ignite.internal.processors.dr.*; -import org.apache.ignite.internal.processors.portable.*; import org.apache.ignite.internal.util.*; import org.apache.ignite.internal.util.future.*; import org.apache.ignite.internal.util.lang.*; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessor.java deleted file mode 100644 index 7c63df9..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessor.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.processors.portable; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.internal.client.marshaller.*; -import org.apache.ignite.internal.processors.*; -import org.apache.ignite.internal.processors.cache.*; -import org.jetbrains.annotations.*; - -/** - * Cache objects processor. - */ -public interface IgniteCacheObjectProcessor extends GridProcessor { - /** {@inheritDoc} */ - public void onCacheProcessorStarted(); - - /** - * @param typeName Type name. - * @return Type ID. - */ - public int typeId(String typeName); - - /** - * @param obj Object to get type ID for. - * @return Type ID. - */ - public int typeId(Object obj); - - /** - * Converts temporary off-heap object to heap-based. - * - * @param ctx Context. - * @param obj Object. - * @return Heap-based object. - * @throws IgniteException In case of error. - */ - @Nullable public Object unwrapTemporary(GridCacheContext ctx, @Nullable Object obj) throws IgniteException; - - /** - * Prepares cache object for cache (e.g. copies user-provided object if needed). - * - * @param obj Cache object. - * @param cctx Cache context. - * @return Object to be store in cache. - */ - @Nullable public CacheObject prepareForCache(@Nullable CacheObject obj, GridCacheContext cctx); - - /** - * Checks whether object is portable object. - * - * @param obj Object to check. - * @return {@code True} if object is already a portable object, {@code false} otherwise. - */ - public boolean isPortableObject(Object obj); - - /** - * @param obj Portable object to get field from. - * @param fieldName Field name. - * @return Field value. - */ - public Object field(Object obj, String fieldName); - - /** - * Checks whether field is set in the object. - * - * @param obj Object. - * @param fieldName Field name. - * @return {@code true} if field is set. - */ - public boolean hasField(Object obj, String fieldName); - - /** - * @param ctx Cache object context. - * @param val Value. - * @return Value bytes. - * @throws IgniteCheckedException If failed. - */ - public byte[] marshal(CacheObjectContext ctx, Object val) throws IgniteCheckedException; - - /** - * @param ctx Context. - * @param bytes Bytes. - * @param clsLdr Class loader. - * @return Unmarshalled object. - * @throws IgniteCheckedException If failed. - */ - public Object unmarshal(CacheObjectContext ctx, byte[] bytes, ClassLoader clsLdr) throws IgniteCheckedException; - - /** - * @param node Node. - * @param cacheName Cache name. - * @return Cache object context. - */ - public CacheObjectContext contextForCache(ClusterNode node, @Nullable String cacheName); - - /** - * @param ctx Cache context. - * @param obj Key value. - * @param userObj If {@code true} then given object is object provided by user and should be copied - * before stored in cache. - * @return Cache key object. - */ - public KeyCacheObject toCacheKeyObject(CacheObjectContext ctx, Object obj, boolean userObj); - - /** - * @param ctx Cache context. - * @param obj Object. - * @param userObj If {@code true} then given object is object provided by user and should be copied - * before stored in cache. - * @return Cache object. - */ - @Nullable public CacheObject toCacheObject(CacheObjectContext ctx, @Nullable Object obj, boolean userObj); - - /** - * @param ctx Cache context. - * @param type Object type. - * @param bytes Object bytes. - * @return Cache object. - */ - public CacheObject toCacheObject(CacheObjectContext ctx, byte type, byte[] bytes); - - /** - * @param ctx Context. - * @param valPtr Value pointer. - * @param tmp If {@code true} can return temporary instance which is valid while entry lock is held. - * @return Cache object. - * @throws IgniteCheckedException If failed. - */ - public CacheObject toCacheObject(GridCacheContext ctx, long valPtr, boolean tmp) throws IgniteCheckedException; - - /** - * @param obj Value. - * @return {@code True} if object is of known immutable type of it is marked - * with {@link IgniteImmutable} annotation. - */ - public boolean immutable(Object obj); - - /** - * @param cacheName Cache name. - * @return {@code True} if portable format should be preserved when passing values to cache store. - */ - public boolean keepPortableInStore(@Nullable String cacheName); -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7719749c/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessorImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessorImpl.java deleted file mode 100644 index 6fcc507..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/portable/IgniteCacheObjectProcessorImpl.java +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.processors.portable; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.client.marshaller.*; -import org.apache.ignite.internal.processors.*; -import org.apache.ignite.internal.processors.cache.*; -import org.apache.ignite.internal.util.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.apache.ignite.lang.*; -import org.jetbrains.annotations.*; - -import java.math.*; -import java.util.*; - -import static org.apache.ignite.cache.CacheMemoryMode.*; - -/** - * - */ -public class IgniteCacheObjectProcessorImpl extends GridProcessorAdapter implements IgniteCacheObjectProcessor { - /** */ - private static final sun.misc.Unsafe UNSAFE = GridUnsafe.unsafe(); - - /** Immutable classes. */ - private static final Collection<Class<?>> IMMUTABLE_CLS = new HashSet<>(); - - /** */ - private final GridBoundedConcurrentLinkedHashMap<Class<?>, Boolean> reflectionCache = - new GridBoundedConcurrentLinkedHashMap<>(1024, 1024); - - /** - * - */ - static { - IMMUTABLE_CLS.add(String.class); - IMMUTABLE_CLS.add(Boolean.class); - IMMUTABLE_CLS.add(Byte.class); - IMMUTABLE_CLS.add(Short.class); - IMMUTABLE_CLS.add(Character.class); - IMMUTABLE_CLS.add(Integer.class); - IMMUTABLE_CLS.add(Long.class); - IMMUTABLE_CLS.add(Float.class); - IMMUTABLE_CLS.add(Double.class); - IMMUTABLE_CLS.add(UUID.class); - IMMUTABLE_CLS.add(IgniteUuid.class); - IMMUTABLE_CLS.add(BigDecimal.class); - } - - /** - * @param ctx Context. - */ - public IgniteCacheObjectProcessorImpl(GridKernalContext ctx) { - super(ctx); - } - - /** {@inheritDoc} */ - @Nullable @Override public CacheObject prepareForCache(@Nullable CacheObject obj, GridCacheContext cctx) { - if (obj == null) - return null; - - return obj.prepareForCache(cctx.cacheObjectContext()); - } - - /** {@inheritDoc} */ - @Override public byte[] marshal(CacheObjectContext ctx, Object val) throws IgniteCheckedException { - return CU.marshal(ctx.kernalContext().cache().context(), val); - } - - /** {@inheritDoc} */ - @Override public Object unmarshal(CacheObjectContext ctx, byte[] bytes, ClassLoader clsLdr) - throws IgniteCheckedException - { - return ctx.kernalContext().cache().context().marshaller().unmarshal(bytes, clsLdr); - } - - /** {@inheritDoc} */ - @Nullable public KeyCacheObject toCacheKeyObject(CacheObjectContext ctx, Object obj, boolean userObj) { - if (obj instanceof KeyCacheObject) - return (KeyCacheObject)obj; - - return toCacheKeyObject0(obj, userObj); - } - - /** - * @param obj Object. - * @param userObj If {@code true} then given object is object provided by user and should be copied - * before stored in cache. - * @return Key cache object. - */ - @SuppressWarnings("ExternalizableWithoutPublicNoArgConstructor") - protected KeyCacheObject toCacheKeyObject0(Object obj, boolean userObj) { - if (!userObj) - return new KeyCacheObjectImpl(obj, null); - - return new KeyCacheObjectImpl(obj, null) { - @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { - return super.value(ctx, false); // Do not need copy since user value is not in cache. - } - - @Override public CacheObject prepareForCache(CacheObjectContext ctx) { - try { - if (!ctx.processor().immutable(val)) { - if (valBytes == null) - valBytes = ctx.processor().marshal(ctx, val); - - ClassLoader ldr = ctx.p2pEnabled() ? - IgniteUtils.detectClass(this.val).getClassLoader() : val.getClass().getClassLoader(); - - Object val = ctx.processor().unmarshal(ctx, - valBytes, - ldr); - - return new KeyCacheObjectImpl(val, valBytes); - } - - return new KeyCacheObjectImpl(val, valBytes); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to marshal object: " + val, e); - } - } - }; - } - - /** {@inheritDoc} */ - @Override public CacheObject toCacheObject(GridCacheContext ctx, long valPtr, boolean tmp) - throws IgniteCheckedException - { - assert valPtr != 0; - - int size = UNSAFE.getInt(valPtr); - - byte type = UNSAFE.getByte(valPtr + 4); - - byte[] bytes = U.copyMemory(valPtr + 5, size); - - if (ctx.kernalContext().config().isPeerClassLoadingEnabled() && - ctx.offheapTiered() && - type != CacheObjectAdapter.TYPE_BYTE_ARR) { - IgniteUuid valClsLdrId = U.readGridUuid(valPtr + 5 + size); - - ClassLoader ldr = - valClsLdrId != null ? ctx.deploy().getClassLoader(valClsLdrId) : ctx.deploy().localLoader(); - - return toCacheObject(ctx.cacheObjectContext(), unmarshal(ctx.cacheObjectContext(), bytes, ldr), false); - } - else - return toCacheObject(ctx.cacheObjectContext(), type, bytes); - } - - /** {@inheritDoc} */ - @Override public CacheObject toCacheObject(CacheObjectContext ctx, byte type, byte[] bytes) { - switch (type) { - case CacheObjectAdapter.TYPE_BYTE_ARR: - return new CacheObjectByteArrayImpl(bytes); - - case CacheObjectAdapter.TYPE_REGULAR: - return new CacheObjectImpl(null, bytes); - } - - throw new IllegalArgumentException("Invalid object type: " + type); - } - - /** {@inheritDoc} */ - @Nullable @Override public CacheObject toCacheObject(CacheObjectContext ctx, - @Nullable Object obj, - boolean userObj) - { - if (obj == null || obj instanceof CacheObject) - return (CacheObject)obj; - - return toCacheObject0(obj, userObj); - } - - /** - * @param obj Object. - * @param userObj If {@code true} then given object is object provided by user and should be copied - * before stored in cache. - * @return Cache object. - */ - @SuppressWarnings("ExternalizableWithoutPublicNoArgConstructor") - protected CacheObject toCacheObject0(@Nullable Object obj, boolean userObj) { - assert obj != null; - - if (obj instanceof byte[]) { - if (!userObj) - return new CacheObjectByteArrayImpl((byte[])obj); - - return new CacheObjectByteArrayImpl((byte[]) obj) { - @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { - return super.value(ctx, false); // Do not need copy since user value is not in cache. - } - - @Override public CacheObject prepareForCache(CacheObjectContext ctx) { - byte[] valCpy = Arrays.copyOf(val, val.length); - - return new CacheObjectByteArrayImpl(valCpy); - } - }; - } - - if (!userObj) - new CacheObjectImpl(obj, null); - - return new CacheObjectImpl(obj, null) { - @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { - return super.value(ctx, false); // Do not need copy since user value is not in cache. - } - - @Override public CacheObject prepareForCache(CacheObjectContext ctx) { - if (!ctx.processor().immutable(val)) { - try { - if (valBytes == null) - valBytes = ctx.processor().marshal(ctx, val); - - if (ctx.unmarshalValues()) { - ClassLoader ldr = ctx.p2pEnabled() ? - IgniteUtils.detectClass(this.val).getClassLoader() : val.getClass().getClassLoader(); - - Object val = ctx.processor().unmarshal(ctx, valBytes, ldr); - - return new CacheObjectImpl(val, valBytes); - } - - return new CacheObjectImpl(null, valBytes); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to marshal object: " + val, e); - } - } - else - return new CacheObjectImpl(val, valBytes); - } - }; - } - - /** {@inheritDoc} */ - @Override public CacheObjectContext contextForCache(ClusterNode node, @Nullable String cacheName) { - CacheConfiguration ccfg = null; - - for (CacheConfiguration ccfg0 : ctx.config().getCacheConfiguration()) { - if (F.eq(cacheName, ccfg0.getName())) { - ccfg = ccfg0; - - break; - } - } - - if (ccfg != null) { - CacheMemoryMode memMode = ccfg.getMemoryMode(); - - return new CacheObjectContext(ctx, - new GridCacheDefaultAffinityKeyMapper(), - ccfg.isCopyOnGet() && memMode == ONHEAP_TIERED, - ctx.config().isPeerClassLoadingEnabled() || ccfg.isQueryIndexEnabled()); - } - else - return new CacheObjectContext( - ctx, - new GridCacheDefaultAffinityKeyMapper(), - false, - ctx.config().isPeerClassLoadingEnabled()); - } - - /** {@inheritDoc} */ - @Override public boolean immutable(Object obj) { - assert obj != null; - - Class<?> cls = obj.getClass(); - - if (IMMUTABLE_CLS.contains(cls)) - return true; - - Boolean immutable = reflectionCache.get(cls); - - if (immutable != null) - return immutable; - - immutable = IgniteUtils.hasAnnotation(cls, IgniteImmutable.class); - - reflectionCache.putIfAbsent(cls, immutable); - - return immutable; - } - - /** {@inheritDoc} */ - @Override public boolean keepPortableInStore(@Nullable String cacheName) { - return false; - } - - /** {@inheritDoc} */ - @Override public void onCacheProcessorStarted() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public int typeId(String typeName) { - return 0; - } - - - /** {@inheritDoc} */ - @Override public Object unwrapTemporary(GridCacheContext ctx, Object obj) throws IgniteException { - return obj; - } - - /** {@inheritDoc} */ - @Override public boolean isPortableObject(Object obj) { - return false; - } - - /** {@inheritDoc} */ - @Override public int typeId(Object obj) { - return 0; - } - - /** {@inheritDoc} */ - @Override public Object field(Object obj, String fieldName) { - return null; - } - - /** {@inheritDoc} */ - @Override public boolean hasField(Object obj, String fieldName) { - return false; - } -}