Index: modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/DynamicIndexRecreateAfterClearTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/DynamicIndexRecreateAfterClearTest.java (revision ) +++ modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/DynamicIndexRecreateAfterClearTest.java (revision ) @@ -0,0 +1,162 @@ +/* +* 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.query; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import javax.cache.Cache; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.QueryEntity; +import org.apache.ignite.cache.QueryIndex; +import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cache.query.SqlQuery; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * + */ +public class DynamicIndexRecreateAfterClearTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + DataStorageConfiguration dsCfg = new DataStorageConfiguration().setDefaultDataRegionConfiguration( + new DataRegionConfiguration() + .setMaxSize(200L * 1024 * 1024) + .setPersistenceEnabled(true)); + + cfg.setDataStorageConfiguration(dsCfg); + + cfg.setConsistentId(igniteInstanceName); + + cfg.setCacheConfiguration(new CacheConfiguration() + .setName("person-cache") + .setAffinity(new RendezvousAffinityFunction(false, 32)) + .setAtomicityMode(CacheAtomicityMode.ATOMIC) + .setQueryEntities(F.asList(personEntity(false, true)))); // Index by name will be configured dynamically. + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + cleanPersistenceDir(); + } + + /** + * + */ + public void test() throws Exception { + IgniteEx ig = (IgniteEx)startGrid(); + + ig.cluster().active(true); + + IgniteCache cache = ig.cache("person-cache"); + + for (int i = 0; i < 10; i++) + cache.put(i, new Person(i, "name" + i, 10)); + + cache.query(new SqlFieldsQuery("CREATE INDEX idx_person_first_name ON Person (name)")).getAll(); + + cache.query(new SqlFieldsQuery("DROP INDEX IF EXISTS idx_person_first_name")).getAll(); + + cache.clear(); + + for (int i = 0; i < 10; i++) + cache.put(i, new Person(i, "name" + i, 5000)); + + cache.query(new SqlFieldsQuery("CREATE INDEX idx_person_first_name ON Person (name)")).getAll(); + + List> res = cache.query(new SqlQuery<>(Person.class, "orgId < ?").setArgs(5)).getAll(); + + assertEquals(5, res.size()); + } + + /** + * @param idxName Index name. + * @param idxOrgId Index org id. + */ + private QueryEntity personEntity(boolean idxName, boolean idxOrgId) { + QueryEntity entity = new QueryEntity(); + + entity.setKeyType(Integer.class.getName()); + entity.setValueType(Person.class.getName()); + + entity.addQueryField("orgId", Integer.class.getName(), null); + entity.addQueryField("name", String.class.getName(), null); + + List idxs = new ArrayList<>(); + + if (idxName) { + QueryIndex idx = new QueryIndex("name"); + + idxs.add(idx); + } + + if (idxOrgId) { + QueryIndex idx = new QueryIndex("orgId"); + + idxs.add(idx); + } + + entity.setIndexes(idxs); + + return entity; + } + + /** + * + */ + private static class Person implements Serializable { + /** */ + int orgId; + + /** */ + String name; + + /** */ + byte[] payload; + + /** + * @param orgId Organization ID. + * @param name Name. + * @param payloadSize Size of random payload. + */ + public Person(int orgId, String name, int payloadSize) { + this.orgId = orgId; + this.name = name; + payload = new byte[payloadSize]; + + ThreadLocalRandom.current().nextBytes(payload); + } + } +}