com.gemstone.gemfire.cache.query.internal.index.IndexMaintenanceTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
/**
* Created on May 31, 2007
*
*/
package com.gemstone.gemfire.cache.query.internal.index;
import com.gemstone.gemfire.cache.AttributesMutator;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheLoaderException;
import com.gemstone.gemfire.cache.LoaderHelper;
import com.gemstone.gemfire.cache.Region;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.gemstone.gemfire.cache.query.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import com.gemstone.gemfire.cache.query.data.Portfolio;
import com.gemstone.gemfire.cache.query.internal.DefaultQueryService;
import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
import com.gemstone.gemfire.cache.query.internal.QueryObserver;
import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
import com.gemstone.gemfire.cache.query.internal.index.IndexStore.IndexStoreEntry;
import com.gemstone.gemfire.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry;
import com.gemstone.gemfire.cache.query.internal.index.RangeIndex;
import com.gemstone.gemfire.cache.query.internal.index.CompactRangeIndex;
import com.gemstone.gemfire.cache.query.internal.index.AbstractIndex.RegionEntryToValuesMap;
import com.gemstone.gemfire.internal.cache.LocalRegion;
import com.gemstone.gemfire.internal.cache.RegionEntry;
import com.gemstone.gemfire.internal.cache.persistence.query.CloseableIterator;
/**
*
* @author Asif
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class IndexMaintenanceTest extends TestCase
{
private QueryService qs;
private Region region;
protected boolean indexUsed = false;
private Set idSet;
private void init() {
idSet = new HashSet();
try {
CacheUtils.startCache();
Cache cache = CacheUtils.getCache();
region = CacheUtils.createRegion("portfolio", Portfolio.class);
region.put("0", new Portfolio(0));
region.put("1", new Portfolio(1));
region.put("2", new Portfolio(2));
region.put("3", new Portfolio(3));
for (int j = 0; j < 6; ++j) {
idSet.add(j + "");
}
qs = cache.getQueryService();
}
catch (Exception e) {
e.printStackTrace();
}
}
public IndexMaintenanceTest(String testName) {
super(testName);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
protected void setUp() throws Exception {
init();
}
protected void tearDown() throws Exception {
CacheUtils.closeCache();
}
public static Test suite() {
TestSuite suite = new TestSuite(IndexMaintenanceTest.class);
return suite;
}
/**
* Tests Index maintenance on heterogenous objects
* @author Asif
*/
public void test000IndexMaintenanceWithHeterogenousObjects() {
try {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = true;
IndexManager.TEST_RANGEINDEX_ONLY = true;
Cache cache = CacheUtils.getCache();
qs = cache.getQueryService();
region = CacheUtils.createRegion("portfolio1", null);
idSet.clear();
Portfolio p = new Portfolio(4);
region.put("4", p);
idSet.add(""+p.getID());
p = new Portfolio(5);
region.put("5", p);
idSet.add(""+p.getID());
region.put("6",new Integer(6));
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"pf.getID()", "/portfolio1 pf");
RangeIndex ri = (RangeIndex)i1;
assertEquals(2, ri.valueToEntriesMap.size());
Iterator itr = ri.valueToEntriesMap.values().iterator();
while (itr.hasNext()) {
RangeIndex.RegionEntryToValuesMap re2ValMap = (RangeIndex.RegionEntryToValuesMap)itr.next();
assertEquals(1, re2ValMap.map.size());
Object obj = re2ValMap.map.values().iterator().next();
assertFalse(obj instanceof Collection);
assertTrue(obj instanceof Portfolio);
Portfolio pf = (Portfolio)obj;
assertTrue(idSet.contains(String.valueOf(pf.getID())));
}
assertEquals(1,ri.undefinedMappedEntries.map.size());
Map.Entry entry = (Map.Entry)ri.undefinedMappedEntries.map.entrySet().iterator().next();
assertFalse(entry.getValue() instanceof Collection);
assertTrue(entry.getValue() instanceof Integer);
assertTrue(entry.getValue().equals(new Integer(6)));
region.put("7",new Integer(7));
idSet.add(new Integer(7));
assertEquals(2,ri.undefinedMappedEntries.map.size());
itr = ri.undefinedMappedEntries.map.entrySet().iterator();
while (itr.hasNext()) {
entry = (Map.Entry)itr.next();
assertFalse(entry.getValue() instanceof Collection);
assertTrue(entry.getValue() instanceof Integer);
idSet.contains(entry.getValue());
}
region.remove("7");
idSet.remove(new Integer(7));
Index i2 = qs.createIndex("indx2", IndexType.FUNCTIONAL,
"pf.pkid", "/portfolio1 pf");
ri = (RangeIndex)i2;
assertEquals(2, ri.valueToEntriesMap.size());
itr = ri.valueToEntriesMap.values().iterator();
while (itr.hasNext()) {
RangeIndex.RegionEntryToValuesMap re2ValMap = (RangeIndex.RegionEntryToValuesMap)itr.next();
assertEquals(1, re2ValMap.map.size());
Object obj = re2ValMap.map.values().iterator().next();
assertFalse(obj instanceof Collection);
assertTrue(obj instanceof Portfolio);
Portfolio pf = (Portfolio)obj;
assertTrue(idSet.contains(String.valueOf(pf.getID())));
}
assertEquals(1,ri.undefinedMappedEntries.map.size());
entry = (Map.Entry)ri.undefinedMappedEntries.map.entrySet().iterator().next();
assertFalse(entry.getValue() instanceof Collection);
assertTrue(entry.getValue() instanceof Integer);
assertTrue(entry.getValue().equals(new Integer(6)));
region.put("7",new Integer(7));
idSet.add(new Integer(7));
assertEquals(2,ri.undefinedMappedEntries.map.size());
itr = ri.undefinedMappedEntries.map.entrySet().iterator();
while (itr.hasNext()) {
entry = (Map.Entry)itr.next();
assertFalse(entry.getValue() instanceof Collection);
assertTrue(entry.getValue() instanceof Integer);
idSet.contains(entry.getValue());
}
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = false;
IndexManager.TEST_RANGEINDEX_ONLY = false;
}
}
/**
* Tests query on region containing heterogenous objects
* @author Asif
*/
public void test001QueryOnHeterogenousObjects() {
try {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = true;
Cache cache = CacheUtils.getCache();
region = CacheUtils.createRegion("portfolio1", null);
for(int i=0; i < 5 ; ++i) {
Portfolio p = new Portfolio(i+1);
region.put(new Integer(i+1), p);
}
for(int i=5; i < 10 ; ++i) {
region.put(new Integer(i+1), new Integer(i+1));
}
String queryStr = "Select distinct * from /portfolio1 pf1 where pf1.getID() > 3";
Query q = qs.newQuery(queryStr);
SelectResults rs = (SelectResults)q.execute();
assertEquals(2,rs.size());
Iterator itr = rs.iterator();
while( itr.hasNext()) {
Portfolio p = (Portfolio)itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)) );
}
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"pf.getID()", "/portfolio1 pf");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexUsed = false;
public void beforeIndexLookup(Index index, int oper, Object key) {
indexUsed = true;
}
public void endQuery() {
assertTrue(indexUsed);
}
});
rs = (SelectResults)q.execute();
assertEquals(2,rs.size());
itr = rs.iterator();
while( itr.hasNext()) {
Portfolio p = (Portfolio)itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)) );
}
qs.removeIndex(i1);
queryStr = "Select distinct * from /portfolio1 pf1 where pf1.pkid > '3'";
q = qs.newQuery(queryStr);
rs = (SelectResults)q.execute();
assertEquals(2,rs.size());
itr = rs.iterator();
while( itr.hasNext()) {
Portfolio p = (Portfolio)itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)) );
}
i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"pf.pkid", "/portfolio1 pf");
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexUsed = false;
public void beforeIndexLookup(Index index, int oper, Object key) {
indexUsed = true;
}
public void endQuery() {
assertTrue(indexUsed);
}
});
rs = (SelectResults)q.execute();
assertEquals(2,rs.size());
itr = rs.iterator();
while( itr.hasNext()) {
Portfolio p = (Portfolio)itr.next();
assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)) );
}
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = false;
}
}
/**
* Tests Index maintenance on method Keys() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test002IndexMaintenanceWithIndexOnMethodKeys() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "ks.toString",
"/portfolio.keys() ks");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForKeys(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method asList() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test003IndexMaintenanceWithIndexOnMethodAsList() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID",
"/portfolio.asList() pf");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForValues(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method values() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test004IndexMaintenanceWithIndexOnMethodValues() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID",
"/portfolio.values() pf");
assertTrue(i1 instanceof CompactRangeIndex);
Cache cache = CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForValues(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method getValues() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test005IndexMaintenanceWithIndexOnMethodGetValues() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID",
"/portfolio.getValues() pf");
assertTrue(i1 instanceof CompactRangeIndex);
Cache cache = CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForValues(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method toArray() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test006IndexMaintenanceWithIndexOnMethodtoArray() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID",
"/portfolio.toArray() pf");
assertTrue(i1 instanceof CompactRangeIndex);
Cache cache = CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForValues(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method asSet() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test007IndexMaintenanceWithIndexOnMethodAsSet() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID",
"/portfolio.asSet() pf");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForValues(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method keySet() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test008IndexMaintenanceWithIndexOnMethodKeySet() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "ks.toString",
"/portfolio.keySet() ks");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForKeys(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method getKeys() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test009IndexMaintenanceWithIndexOnMethodGetKeys() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "ks.toString",
"/portfolio.getKeys() ks");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForKeys(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method entrySet() as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test010IndexMaintenanceWithIndexOnMethodEntrySet() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"entries.value.getID", "/portfolio.entrySet() entries");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForEntries(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method entries(boolean ) as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test011IndexMaintenanceWithIndexOnMethodEntries() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"entries.value.getID", "/portfolio.entries(false) entries");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForEntries(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
/**
* Tests Index maintenance on method getEntries( ) as iterator ( with focus on
* behaviour if not implemented in DummyQRegion
* @author Asif
*/
public void test012IndexMaintenanceWithIndexOnMethodGetEntries() {
try {
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL,
"entries.value.getID", "/portfolio.getEntries() entries");
CacheUtils.getCache();
region = CacheUtils.getRegion("/portfolio");
region.put("4", new Portfolio(4));
region.put("5", new Portfolio(5));
CompactRangeIndex ri = (CompactRangeIndex)i1;
validateIndexForEntries(ri);
}
catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
public void test013MapKeyIndexMaintenanceForNonCompactTypeAllKeysIndex()
throws Exception
{
QueryService qs;
qs = CacheUtils.getQueryService();
LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
int ID = 1;
// Add some test data now
// Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
// and so on
for (; ID <= 5; ++ID) {
MapKeyIndexData mkid = new MapKeyIndexData(ID);
for (int j = 1; j <= ID; ++j) {
mkid.maap.put("key" + j, "val" + j);
}
testRgn.put(ID, mkid);
}
--ID;
Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap[*]",
"/testRgn objs");
assertEquals(i1.getCanonicalizedIndexedExpression(), "index_iter1.maap[*]");
assertTrue(i1 instanceof MapRangeIndex);
MapRangeIndex mri = (MapRangeIndex)i1;
// Test index maintenance
// addition of new Portfolio object
Map