IGNITE-5497 WIP Signed-off-by: nikolay_tikhonov <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/49bc7fb4 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/49bc7fb4 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/49bc7fb4 Branch: refs/heads/ignite-5947 Commit: 49bc7fb414d14e78bd913390a9913b0c79d085fc Parents: 020ff36 Author: nikolay_tikhonov <[email protected]> Authored: Tue Aug 15 20:51:44 2017 +0300 Committer: nikolay_tikhonov <[email protected]> Committed: Tue Aug 15 20:59:27 2017 +0300 ---------------------------------------------------------------------- .../processors/cache/CacheObjectUtils.java | 27 +- .../cache/CacheTwoDimensionalArrayTest.java | 291 +++++++++++++++++++ 2 files changed, 316 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/49bc7fb4/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectUtils.java index f9c76df..6a63768 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectUtils.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.processors.cache; +import java.lang.reflect.Array; import org.apache.ignite.internal.binary.BinaryUtils; import org.apache.ignite.internal.util.typedef.F; @@ -123,8 +124,30 @@ public class CacheObjectUtils { Object[] res = new Object[arr.length]; - for (int i = 0; i < arr.length; i++) - res[i] = unwrapBinary(ctx, arr[i], keepBinary, cpy); + boolean canCastArray = true; + Class cls = null; + + for (int i = 0; i < arr.length; i++) { + Object obj = unwrapBinary(ctx, arr[i], keepBinary, cpy); + + res[i] = obj; + + if (canCastArray && obj != null) { + if (cls == null) + cls = obj.getClass(); + else if (cls != obj.getClass()) + canCastArray = false; + } + } + + // If array contains all element the same type then will create typed array. + if (canCastArray && cls != null) { + Object[] res0 = (Object[])Array.newInstance(cls, res.length); + + System.arraycopy(res, 0, res0, 0, res.length); + + res = res0; + } return res; } http://git-wip-us.apache.org/repos/asf/ignite/blob/49bc7fb4/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTwoDimensionalArrayTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTwoDimensionalArrayTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTwoDimensionalArrayTest.java new file mode 100644 index 0000000..69d457c --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTwoDimensionalArrayTest.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.internal.processors.cache; + +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.jetbrains.annotations.NotNull; + +import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC; +import static org.apache.ignite.cache.CacheMode.PARTITIONED; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; +import static org.junit.Assert.assertArrayEquals; + +/** + * + */ +public class CacheTwoDimensionalArrayTest extends GridCommonAbstractTest { + /** */ + private static int NODES = 3; + + /** */ + private static int KEYS = 100; + + /** */ + private static int size = 5; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + startGridsMultiThreaded(NODES); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + super.afterTestsStopped(); + + stopAllGrids(); + } + + /** + * @throws Exception If failed. + */ + public void testSimpleModel() throws Exception { + doTestSimpleModel(ATOMIC, PARTITIONED); + } + + /** + * @param atomicityMode Cache atomicity mode. + * @param cacheMode Cache mode. + * + * @throws Exception If failed. + */ + private void doTestSimpleModel(CacheAtomicityMode atomicityMode, CacheMode cacheMode) throws Exception { + CacheConfiguration ccfg = getConfiguration(atomicityMode, cacheMode); + + ignite(0).getOrCreateCache(ccfg); + + int n = size, m = size -1; + + // Primitive empty array. + { + IgniteCache<Integer, int[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, new int[n][m]); + + for (int key = 0; key < KEYS; key++) { + int[][] exp = new int[n][m]; + + int[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + // Object empty array. + { + IgniteCache<Integer, Object[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, new Object[n][m]); + + for (int key = 0; key < KEYS; key++) { + Object[][] exp = new Object[n][m]; + + Object[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + { + IgniteCache<Integer, int[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, intArray(n, m, key)); + + for (int key = 0; key < KEYS; key++) { + int[][] exp = intArray(n, m, key); + + int[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + { + IgniteCache<Integer, int[][][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, new int[5][6][7]); + + for (int key = 0; key < KEYS; key++) { + int[][][] exp = new int[5][6][7]; + + int[][][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + { + IgniteCache<Integer, Object[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, objectArray(n, m, key)); + + for (int key = 0; key < KEYS; key++) { + Object[][] exp = objectArray(n, m, key); + + Object[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + { + IgniteCache<Integer, TestObject[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, testObjectArray(n, m, key)); + + for (int key = 0; key < KEYS; key++) { + TestObject[][] exp = testObjectArray(n, m, key); + + TestObject[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + + { + IgniteCache<Integer, TestObject[][]> cache = ignite(0).cache(ccfg.getName()); + + for (int key = 0; key < KEYS; key++) + cache.put(key, testObjectArray(n, m, key)); + + for (int key = 0; key < KEYS; key++) { + TestObject[][] exp = testObjectArray(n, m, key); + + TestObject[][] act = cache.get(key); + + assertArrayEquals(exp, act); + } + + cache.removeAll(); + } + } + + /** + * @return Array. + */ + private int[][] intArray(int n, int m,int K) { + int[][] arr = new int[n][m]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) + arr[i][j] = (i + j) * K; + } + + return arr; + } + + /** + * @return Array. + */ + private Object[][] objectArray(int n, int m, int K) { + Object[][] arr = new Object[n][m]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) + arr[i][j] = ((n + m) % 2 == 0) ? (i + j) * K : new TestObject((i + j) * K); + } + + return arr; + } + + /** + * @return Array. + */ + private TestObject[][] testObjectArray(int n, int m, int K) { + TestObject[][] arr = new TestObject[n][m]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) + arr[i][j] = new TestObject((i + j) * K); + } + + return arr; + } + + /** + * @param atomicityMode Atomicity mode. + * @param cacheMode Cache mode. + * + * @return Cache configuration. + */ + @NotNull private CacheConfiguration getConfiguration(CacheAtomicityMode atomicityMode, + CacheMode cacheMode) { + CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME); + + ccfg.setCacheMode(cacheMode); + ccfg.setAtomicityMode(atomicityMode); + ccfg.setWriteSynchronizationMode(FULL_SYNC); + ccfg.setBackups(1); + + return ccfg; + } + + /** + * + */ + private static class TestObject { + /** */ + private int val; + + /** + * @param val Value. + */ + public TestObject(int val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (o == null || getClass() != o.getClass()) + return false; + + TestObject object = (TestObject)o; + + return val == object.val; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val; + } + } +}
