nizhikov commented on code in PR #169: URL: https://github.com/apache/ignite-extensions/pull/169#discussion_r934505285
########## modules/cdc-ext/src/main/java/org/apache/ignite/cdc/CdcEventsIgniteApplier.java: ########## @@ -0,0 +1,132 @@ +/* + * 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.cdc; + +import java.util.Map; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.processors.cache.CacheObject; +import org.apache.ignite.internal.processors.cache.CacheObjectImpl; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.apache.ignite.internal.processors.cache.KeyCacheObject; +import org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl; +import org.apache.ignite.internal.processors.cache.dr.GridCacheDrExpirationInfo; +import org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.collection.IntHashMap; +import org.apache.ignite.internal.util.collection.IntMap; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.CU; +import org.apache.ignite.internal.util.typedef.internal.U; + +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.EXPIRE_TIME_CALCULATE; +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.TTL_NOT_CHANGED; + +/** + * Contains logic to process {@link CdcEvent} and apply them to the destination cluster. + * + * @see IgniteInternalCache#putAllConflict(Map) + * @see IgniteInternalCache#removeAllConflict(Map) + */ +public class CdcEventsIgniteApplier extends CdcEventsApplier<KeyCacheObject, GridCacheDrInfo> { + /** Destination cluster. */ + private final IgniteEx ignite; + + /** Caches. */ + private final IntMap<IgniteInternalCache<BinaryObject, BinaryObject>> ignCaches = new IntHashMap<>(); + + /** + * @param ignite Destination cluster. + * @param maxBatchSize Maximum batch size. + * @param log Logger. + */ + public CdcEventsIgniteApplier(Ignite ignite, int maxBatchSize, IgniteLogger log) { + super(maxBatchSize, log); + + this.ignite = (IgniteEx)ignite; + } + + /** {@inheritDoc} */ + @Override protected void putAllConflict(int cacheId, Map<KeyCacheObject, GridCacheDrInfo> drMap) { + try { + IgniteInternalCache<BinaryObject, BinaryObject> cache = cache(cacheId); + + if (cache.configuration().getExpiryPolicyFactory() != null) { + drMap = F.viewReadOnly(drMap, + (info) -> new GridCacheDrExpirationInfo(info.value(), info.version(), + TTL_NOT_CHANGED, EXPIRE_TIME_CALCULATE)); + } + + cache(cacheId).putAllConflict(drMap); + } + catch (IgniteCheckedException e) { + throw U.convertException(e); + } + } + + /** {@inheritDoc} */ + @Override protected void removeAllConflict(int cacheId, Map<KeyCacheObject, GridCacheVersion> drMap) { + try { + cache(cacheId).removeAllConflict(drMap); + } + catch (IgniteCheckedException e) { + throw U.convertException(e); + } + } + + /** {@inheritDoc} */ + @Override protected KeyCacheObject toKey(CdcEvent evt) { + Object key = evt.key(); + + if (key instanceof KeyCacheObject) + return (KeyCacheObject)key; + else + return new KeyCacheObjectImpl(key, null, evt.partition()); + } + + /** {@inheritDoc} */ + @Override protected GridCacheDrInfo toValue(Object val, GridCacheVersion ver) { + CacheObject cacheObj; + + if (val instanceof CacheObject) + cacheObj = (CacheObject)val; + else + cacheObj = new CacheObjectImpl(val, null); + + return new GridCacheDrInfo(cacheObj, ver); Review Comment: Let's check here expiry policy for cache to eliminate object recreation on apply. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
